Skip to content

Commit

Permalink
fixup! test: add C3 integration test to Wrangler e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Jun 19, 2023
1 parent 5bfd020 commit c6f35fb
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
run: npm ci

- name: Run builds
run: npm run -w wrangler build
run: npm run build
env:
NODE_ENV: "production"

Expand Down
36 changes: 25 additions & 11 deletions packages/wrangler/e2e/c3-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ describe("c3 integration", () => {
let workerName: string;
let workerPath: string;
let workersDev: string | null = null;
let run: typeof shellac;
let runInRoot: typeof shellac;
let runInWorker: typeof shellac;
let normalize: (str: string) => string;

beforeAll(async () => {
const root = await makeRoot();
run = shellac.in(root).env(process.env);
runInRoot = shellac.in(root).env(process.env);
workerName = `smoke-test-worker-${crypto.randomBytes(4).toString("hex")}`;
workerPath = path.join(root, workerName);
runInWorker = shellac.in(workerPath).env(process.env);
normalize = (str) =>
normalizeOutput(str, { [workerName]: "smoke-test-worker" });
});
Expand All @@ -36,15 +38,19 @@ describe("c3 integration", () => {
const env = {
...process.env,
WRANGLER_C3_COMMAND: `exec ${pathToC3}`,
GIT_AUTHOR_NAME: "test-user",
GIT_AUTHOR_EMAIL: "test-user@cloudflare.com",
GIT_COMMITTER_NAME: "test-user",
GIT_COMMITTER_EMAIL: "test-user@cloudflare.com",
};

await run.env(env)`$$ ${WRANGLER} init ${workerName} --yes`;
await runInRoot.env(env)`$ ${WRANGLER} init ${workerName} --yes`;

expect(existsSync(workerPath)).toBe(true);
});

it("deploy the worker", async () => {
const { stdout, stderr } = await run.in(workerPath)`$ ${WRANGLER} deploy`;
const { stdout, stderr } = await runInWorker`$ ${WRANGLER} deploy`;
expect(normalize(stdout)).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Uploaded smoke-test-worker (TIMINGS)
Expand All @@ -54,22 +60,30 @@ describe("c3 integration", () => {
`);
expect(stderr).toMatchInlineSnapshot('""');
workersDev = matchWorkersDev(stdout);
const responseText = await retry("", () =>
fetch(`https://${workerName}.${workersDev}`).then((r) => r.text())
const { text } = await retry(
(s) => s.status !== 200,
async () => {
const r = await fetch(`https://${workerName}.${workersDev}`);
return { text: await r.text(), status: r.status };
}
);
expect(responseText).toMatchInlineSnapshot('"Hello World!"');
expect(text).toMatchInlineSnapshot('"Hello World!"');
});

it("deletes the worker", async () => {
const { stdout, stderr } = await run.in(workerPath)`$ ${WRANGLER} delete`;
it("delete the worker", async () => {
const { stdout, stderr } = await runInWorker`$$ ${WRANGLER} delete`;
expect(normalize(stdout)).toMatchInlineSnapshot(`
"? Are you sure you want to delete smoke-test-worker? This action cannot be undone.
🤖 Using default value in non-interactive context: yes
Successfully deleted smoke-test-worker"
`);
expect(stderr).toMatchInlineSnapshot('""');
const status = await retry(200, () =>
fetch(`https://${workerName}.${workersDev}`).then((r) => r.status)
const { status } = await retry(
(s) => s.status === 200 || s.status === 500,
async () => {
const r = await fetch(`https://${workerName}.${workersDev}`);
return { text: await r.text(), status: r.status };
}
);
expect(status).toBe(404);
});
Expand Down
28 changes: 20 additions & 8 deletions packages/wrangler/e2e/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ describe("deploy", () => {
expect(stderr).toMatchInlineSnapshot('""');
workersDev = matchWorkersDev(stdout);

const responseText = await retry("", () =>
fetch(`https://${workerName}.${workersDev}`).then((r) => r.text())
const { text } = await retry(
(s) => s.status !== 200,
async () => {
const r = await fetch(`https://${workerName}.${workersDev}`);
return { text: await r.text(), status: r.status };
}
);
expect(responseText).toMatchInlineSnapshot('"Hello World!"');
expect(text).toMatchInlineSnapshot('"Hello World!"');
});

it("modify & deploy worker", async () => {
Expand All @@ -94,10 +98,14 @@ describe("deploy", () => {
expect(stderr).toMatchInlineSnapshot('""');
workersDev = matchWorkersDev(stdout);

const responseText = await retry("Hello World!", () =>
fetch(`https://${workerName}.${workersDev}`).then((r) => r.text())
const { text } = await retry(
(s) => s.status !== 200 || s.text === "Hello World!",
async () => {
const r = await fetch(`https://${workerName}.${workersDev}`);
return { text: await r.text(), status: r.status };
}
);
expect(responseText).toMatchInlineSnapshot('"Updated Worker!"');
expect(text).toMatchInlineSnapshot('"Updated Worker!"');
});

it("delete worker", async () => {
Expand All @@ -108,8 +116,12 @@ describe("deploy", () => {
Successfully deleted smoke-test-worker"
`);
expect(stderr).toMatchInlineSnapshot('""');
const status = await retry(200, () =>
fetch(`https://${workerName}.${workersDev}`).then((r) => r.status)
const { status } = await retry(
(s) => s.status === 200 || s.status === 500,
async () => {
const r = await fetch(`https://${workerName}.${workersDev}`);
return { text: await r.text(), status: r.status };
}
);
expect(status).toBe(404);
});
Expand Down
27 changes: 18 additions & 9 deletions packages/wrangler/e2e/deployments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("deployments", () => {
[workerName]: "smoke-test-worker",
[email]: "person@example.com",
});
});
}, 50_000);

it("init worker", async () => {
const { stdout } =
Expand Down Expand Up @@ -78,10 +78,14 @@ describe("deployments", () => {
expect(stderr).toMatchInlineSnapshot('""');
workersDev = matchWorkersDev(stdout);

const responseText = await retry("", () =>
fetch(`https://${workerName}.${workersDev}`).then((r) => r.text())
const { text } = await retry(
(s) => s.status !== 200,
async () => {
const r = await fetch(`https://${workerName}.${workersDev}`);
return { text: await r.text(), status: r.status };
}
);
expect(responseText).toMatchInlineSnapshot('"Hello World!"');
expect(text).toMatchInlineSnapshot('"Hello World!"');
});

it("list 1 deployment", async () => {
Expand Down Expand Up @@ -119,10 +123,14 @@ describe("deployments", () => {
expect(stderr).toMatchInlineSnapshot('""');
workersDev = matchWorkersDev(stdout);

const responseText = await retry("Hello World!", () =>
fetch(`https://${workerName}.${workersDev}`).then((r) => r.text())
const { text } = await retry(
(s) => s.status !== 200 || s.text === "Hello World!",
async () => {
const r = await fetch(`https://${workerName}.${workersDev}`);
return { text: await r.text(), status: r.status };
}
);
expect(responseText).toMatchInlineSnapshot('"Updated Worker!"');
expect(text).toMatchInlineSnapshot('"Updated Worker!"');
});

it("list 2 deployments", async () => {
Expand Down Expand Up @@ -186,8 +194,9 @@ describe("deployments", () => {
Successfully deleted smoke-test-worker"
`);
expect(stderr).toMatchInlineSnapshot('""');
const status = await retry(200, () =>
fetch(`https://${workerName}.${workersDev}`).then((r) => r.status)
const status = await retry(
(s) => s === 200 || s === 500,
() => fetch(`https://${workerName}.${workersDev}`).then((r) => r.status)
);
expect(status).toBe(404);
});
Expand Down
43 changes: 30 additions & 13 deletions packages/wrangler/e2e/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function runDevSession(
const bg = await shellac.env(process.env).bg`
in ${workerPath} {
exits {
$$ ${WRANGLER} dev ${flags} --port ${port}
$ ${WRANGLER} dev ${flags} --port ${port}
}
}
`;
Expand Down Expand Up @@ -66,10 +66,14 @@ describe("basic dev tests", () => {

it("can modify worker during dev session (local)", async () => {
await runDevSession(workerPath, "", async (port) => {
const responseText = await retry("", () =>
fetch(`http://127.0.0.1:${port}`).then((r) => r.text())
const { text } = await retry(
(s) => s.status !== 200,
async () => {
const r = await fetch(`http://127.0.0.1:${port}`);
return { text: await r.text(), status: r.status };
}
);
expect(responseText).toMatchInlineSnapshot('"Hello World!"');
expect(text).toMatchInlineSnapshot('"Hello World!"');

await seed(workerPath, {
"src/index.ts": dedent`
Expand All @@ -80,19 +84,27 @@ describe("basic dev tests", () => {
}`,
});

const response2Text = await retry("Hello World!", () =>
fetch(`http://127.0.0.1:${port}`).then((r) => r.text())
const { text: text2 } = await retry(
(s) => s.status !== 200 || s.text === "Hello World!",
async () => {
const r = await fetch(`http://127.0.0.1:${port}`);
return { text: await r.text(), status: r.status };
}
);
expect(response2Text).toMatchInlineSnapshot('"Updated Worker!"');
expect(text2).toMatchInlineSnapshot('"Updated Worker!"');
});
});

it("can modify worker during dev session (remote)", async () => {
await runDevSession(workerPath, "--remote --ip 127.0.0.1", async (port) => {
const responseText = await retry("", () =>
fetch(`http://127.0.0.1:${port}`).then((r) => r.text())
const { text } = await retry(
(s) => s.status !== 200 || s.text === "",
async () => {
const r = await fetch(`http://127.0.0.1:${port}`);
return { text: await r.text(), status: r.status };
}
);
expect(responseText).toMatchInlineSnapshot('"Hello World!"');
expect(text).toMatchInlineSnapshot('"Hello World!"');

await seed(workerPath, {
"src/index.ts": dedent`
Expand All @@ -104,12 +116,17 @@ describe("basic dev tests", () => {
});

// Give a bit of time for the change to propagate.
// Otherwise the process has a tendency to hang.
await setTimeout(5000);

const response2Text = await retry("Hello World!", () =>
fetch(`http://127.0.0.1:${port}`).then((r) => r.text())
const { text: text2 } = await retry(
(s) => s.status !== 200 || s.text === "Hello World!",
async () => {
const r = await fetch(`http://127.0.0.1:${port}`);
return { text: await r.text(), status: r.status };
}
);
expect(response2Text).toMatchInlineSnapshot('"Updated Worker!"');
expect(text2).toMatchInlineSnapshot('"Updated Worker!"');
});
});
});
2 changes: 1 addition & 1 deletion packages/wrangler/e2e/helpers/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function stripTimings(stdout: string): string {
export function npmStripTimings(stdout: string): string {
return stdout
.replace(
/added \d+ packages, and audited \d+ packages in \d+s/,
/added \d+ packages, and audited \d+ packages in [\dms]+/,
"added (N) packages, and audited (N) packages in (TIMINGS)"
)
.replace(
Expand Down
16 changes: 9 additions & 7 deletions packages/wrangler/e2e/helpers/retry.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { setTimeout } from "node:timers/promises";

export async function retry<T>(
originalState: T,
retryIf: (currentState: T) => boolean,
action: () => Promise<T>,
n = 20
n = 30
): Promise<T> {
const states: T[] = [];
while (n >= 0) {
try {
const currentState = await action();
if (currentState !== originalState) {
if (!retryIf(currentState)) {
return currentState;
}
} catch (e) {
await setTimeout(2_000);
n--;
}
states.push(currentState);
} catch {}
await setTimeout(2_000);
n--;
}
console.error(states);
throw new Error("Timed out waiting for state to change");
}

0 comments on commit c6f35fb

Please sign in to comment.