Skip to content

Commit

Permalink
WIP: hyperdrive resource e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmenPopoviciu committed Jan 10, 2025
1 parent 998d230 commit 96ad393
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.TEST_CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.TEST_CLOUDFLARE_ACCOUNT_ID }}
HYPERDRIVE_DATABASE_URL: ${{ secrets.TEST_HYPERDRIVE_DATABASE_URL}}
WRANGLER: node --no-warnings ${{ github.workspace}}/packages/wrangler/bin/wrangler.js
WRANGLER_IMPORT: ${{ github.workspace}}/packages/wrangler/wrangler-dist/cli.js
NODE_OPTIONS: "--max_old_space_size=8192"
Expand Down
38 changes: 37 additions & 1 deletion packages/wrangler/e2e/dev-with-resources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Agent, fetch } from "undici";
import { beforeEach, describe, expect, it } from "vitest";
import WebSocket from "ws";
import { WranglerE2ETestHelper } from "./helpers/e2e-wrangler-test";
import { fetchText } from "./helpers/fetch-text";
import { generateResourceName } from "./helpers/generate-resource-name";

const port = await getPort();
Expand Down Expand Up @@ -580,6 +581,42 @@ describe.sequential.each(RUNTIMES)("Bindings: $flags", ({ runtime, flags }) => {
);
});

it("exposes Hyperdrive bindings", async () => {
const { id } = await helper.hyperdrive(false);

await helper.seed({
"wrangler.toml": dedent`
name = "${workerName}"
main = "src/index.ts"
compatibility_date = "2023-10-25"
[[hyperdrive]]
binding = "HYPERDRIVE"
id = "${id}"
`,
"src/index.ts": dedent`
export default {
async fetch(request, env) {
if (request.url.includes("connect")) {
const conn = env.HYPERDRIVE.connect();
await conn.writable.getWriter().write(new TextEncoder().encode("test string"));
}
return new Response(env.HYPERDRIVE?.connectionString ?? "no")
}
}`,
});

const worker = helper.runLongLived(`wrangler dev ${flags}`);
const { url } = await worker.waitForReady();
const text = await fetchText(url);

const hyperdrive = new URL(text);
expect(hyperdrive.pathname).toBe("/some_db");
expect(hyperdrive.username).toBe("user");
expect(hyperdrive.password).toBe("!pass");
expect(hyperdrive.host).not.toBe("localhost");
});

it.skipIf(!isLocal).fails("exposes Pipelines bindings", async () => {
await helper.seed({
"wrangler.toml": dedent`
Expand Down Expand Up @@ -692,7 +729,6 @@ describe.sequential.each(RUNTIMES)("Bindings: $flags", ({ runtime, flags }) => {
});

// TODO(soon): implement E2E tests for other bindings
it.todo("exposes hyperdrive bindings");
it.skipIf(isLocal).todo("exposes send email bindings");
it.skipIf(isLocal).todo("exposes browser bindings");
it.skipIf(isLocal).todo("exposes Workers AI bindings");
Expand Down
23 changes: 23 additions & 0 deletions packages/wrangler/e2e/helpers/e2e-wrangler-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,27 @@ export class WranglerE2ETestHelper {

return name;
}

async hyperdrive(isLocal: boolean): Promise<{ id: string; name: string }> {
const name = generateResourceName("hyperdrive");

if (isLocal) {
return { id: crypto.randomUUID(), name };
}

const result = await this.run(
`wrangler hyperdrive create ${name} --connection-string="<TODO>"`
);
const tomlMatch = /id = "([0-9a-f]{32})"/.exec(result.stdout);
const jsonMatch = /"id": "([0-9a-f]{32})"/.exec(result.stdout);
const match = jsonMatch ?? tomlMatch;
assert(match !== null, `Cannot find ID in ${JSON.stringify(result)}`);
const id = match[1];

onTestFinished(async () => {
await this.run(`wrangler hyperdrive delete ${name}`);
});

return { id, name };
}
}

0 comments on commit 96ad393

Please sign in to comment.