Skip to content

Commit cbc5601

Browse files
Gcloud WIP
1 parent 78f01cf commit cbc5601

File tree

5 files changed

+53
-82
lines changed

5 files changed

+53
-82
lines changed

docs/modules/gcloud.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,8 @@ These examples use the following libraries:
5050

5151
Choose an image from the [container registry](https://gcr.io/google.com/cloudsdktool/google-cloud-cli) and substitute `IMAGE`.
5252

53-
#### With default image
54-
5553
<!--codeinclude-->
56-
[](../../packages/modules/gcloud/src/pubsub-emulator-container.test.ts)
54+
[](../../packages/modules/gcloud/src/pubsub-emulator-container.test.ts) inside_block:pubsubExample
5755
<!--/codeinclude-->
5856

5957
---
@@ -68,10 +66,8 @@ These examples use the following libraries:
6866

6967
Choose an image from the [container registry](https://hub.docker.com/r/fsouza/fake-gcs-server) and substitute `IMAGE`.
7068

71-
#### With default image
72-
7369
<!--codeinclude-->
74-
[](../../packages/modules/gcloud/src/cloudstorage-emulator-container.test.ts) inside_block:cloud-storage
70+
[](../../packages/modules/gcloud/src/cloudstorage-emulator-container.test.ts) inside_block:cloudstorageExample
7571
<!--/codeinclude-->
7672

7773
---
@@ -116,8 +112,6 @@ These examples use the following libraries:
116112

117113
Choose an image from the [container registry](https://ghcr.io/goccy/bigquery-emulator) and substitute `IMAGE`.
118114

119-
#### With default image
120-
121115
<!--codeinclude-->
122-
[](../../packages/modules/gcloud/src/bigquery-emulator-container.test.ts)
116+
[](../../packages/modules/gcloud/src/bigquery-emulator-container.test.ts) inside_block:bigqueryExample
123117
<!--/codeinclude-->
Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,33 @@
11
import { BigQuery, TableSchema } from "@google-cloud/bigquery";
22
import { getImage } from "../../../testcontainers/src/utils/test-helper";
3-
import { BigQueryEmulatorContainer, StartedBigQueryEmulatorContainer } from "./bigquery-emulator-container";
3+
import { BigQueryEmulatorContainer } from "./bigquery-emulator-container";
44

55
const IMAGE = getImage(__dirname, 2);
66

77
describe("BigQueryEmulatorContainer", { timeout: 240_000 }, () => {
88
it("should work using default version", async () => {
9-
await using bigQueryEmulatorContainer = await new BigQueryEmulatorContainer(IMAGE).start();
9+
// bigqueryExample {
10+
await using container = await new BigQueryEmulatorContainer(IMAGE).start();
1011

11-
await checkBigQuery(bigQueryEmulatorContainer);
12-
});
12+
const bigQuery = new BigQuery({
13+
projectId: container.getProjectId(),
14+
apiEndpoint: container.getEmulatorEndpoint(),
15+
});
1316

14-
async function checkBigQuery(bigQueryEmulatorContainer: StartedBigQueryEmulatorContainer) {
15-
expect(bigQueryEmulatorContainer).toBeDefined();
16-
const testDataset = "test-dataset";
17-
const testTable = "test-table";
18-
const testSchema: TableSchema = {
19-
fields: [{ name: "message", type: "STRING" }],
20-
};
21-
const config = {
22-
projectId: bigQueryEmulatorContainer.getProjectId(),
23-
apiEndpoint: bigQueryEmulatorContainer.getEmulatorEndpoint(),
24-
};
25-
const bigQuery = new BigQuery(config);
17+
const dataset = "test-dataset";
18+
const table = "test-table";
19+
const schema: TableSchema = { fields: [{ name: "message", type: "STRING" }] };
2620

27-
await bigQuery.dataset(testDataset).create();
28-
await bigQuery.dataset(testDataset).table(testTable).create({ schema: testSchema });
21+
await bigQuery.dataset(dataset).create();
22+
await bigQuery.dataset(dataset).table(table).create({ schema: schema });
2923
await bigQuery
30-
.dataset(testDataset)
31-
.table(testTable)
24+
.dataset(dataset)
25+
.table(table)
3226
.insert([{ message: "Hello, BigQuery!" }]);
3327

34-
const [rows] = await bigQuery.dataset(testDataset).table(testTable).getRows();
28+
const [rows] = await bigQuery.dataset(dataset).table(table).getRows();
3529

3630
expect(rows).toEqual([{ message: "Hello, BigQuery!" }]);
37-
}
31+
// }
32+
});
3833
});

packages/modules/gcloud/src/cloudstorage-emulator-container.test.ts

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Storage } from "@google-cloud/storage";
22
import { setupServer } from "msw/node";
33
import { ReadableStream } from "node:stream/web";
44
import { getImage } from "../../../testcontainers/src/utils/test-helper";
5-
import { CloudStorageEmulatorContainer, StartedCloudStorageEmulatorContainer } from "./cloudstorage-emulator-container";
5+
import { CloudStorageEmulatorContainer } from "./cloudstorage-emulator-container";
66

77
const IMAGE = getImage(__dirname, 1);
88

@@ -47,30 +47,38 @@ describe.sequential("CloudStorageEmulatorContainer", { timeout: 240_000 }, () =>
4747
server.close();
4848
});
4949

50-
// cloud-storage {
5150
it("should work using default version", async () => {
52-
await using cloudstorageEmulatorContainer = await new CloudStorageEmulatorContainer(IMAGE).start();
51+
// cloudstorageExample {
52+
await using container = await new CloudStorageEmulatorContainer(IMAGE).start();
53+
54+
const storage = new Storage({
55+
projectId: "test-project",
56+
apiEndpoint: container.getExternalUrl(),
57+
});
58+
59+
await storage.createBucket("test-bucket");
60+
const [buckets] = await storage.getBuckets();
5361

54-
await checkCloudStorage(cloudstorageEmulatorContainer);
62+
expect(buckets[0].name).toBe("test-bucket");
63+
// }
5564
});
56-
// }
5765

5866
it("should use the provided external URL", async () => {
59-
await using cloudstorageEmulatorContainer = await new CloudStorageEmulatorContainer(IMAGE)
67+
await using container = await new CloudStorageEmulatorContainer(IMAGE)
6068
.withExternalURL("http://cdn.company.local")
6169
.start();
6270

63-
expect(cloudstorageEmulatorContainer).toBeDefined();
64-
expect(cloudstorageEmulatorContainer.getExternalUrl()).toBe("http://cdn.company.local");
71+
expect(container).toBeDefined();
72+
expect(container.getExternalUrl()).toBe("http://cdn.company.local");
6573
});
6674

6775
it("should be able update the external URL of running instance", async () => {
68-
await using cloudstorageEmulatorContainer = await new CloudStorageEmulatorContainer(IMAGE)
76+
await using container = await new CloudStorageEmulatorContainer(IMAGE)
6977
.withExternalURL("http://cdn.company.local")
7078
.start();
7179

72-
expect(cloudstorageEmulatorContainer).toBeDefined();
73-
expect(cloudstorageEmulatorContainer.getExternalUrl()).toBe("http://cdn.company.local");
80+
expect(container).toBeDefined();
81+
expect(container.getExternalUrl()).toBe("http://cdn.company.local");
7482

7583
const executedRequests: Request[] = [];
7684

@@ -82,13 +90,13 @@ describe.sequential("CloudStorageEmulatorContainer", { timeout: 240_000 }, () =>
8290
}
8391
});
8492

85-
await cloudstorageEmulatorContainer.updateExternalUrl("http://files.company.local");
93+
await container.updateExternalUrl("http://files.company.local");
8694

8795
expect(executedRequests).toHaveLength(1);
8896

8997
const [requestInfo] = executedRequests;
9098

91-
const expectedRequestUrl = cloudstorageEmulatorContainer.getEmulatorEndpoint() + "/_internal/config";
99+
const expectedRequestUrl = container.getEmulatorEndpoint() + "/_internal/config";
92100
expect(requestInfo.url).toContain(expectedRequestUrl);
93101
expect(requestInfo.method).toBe("PUT");
94102

@@ -97,7 +105,7 @@ describe.sequential("CloudStorageEmulatorContainer", { timeout: 240_000 }, () =>
97105
const requestBodyAsJson = JSON.parse(requestBody);
98106
expect(requestBodyAsJson).toEqual(expect.objectContaining({ externalUrl: "http://files.company.local" }));
99107

100-
expect(cloudstorageEmulatorContainer.getExternalUrl()).toBe("http://files.company.local");
108+
expect(container.getExternalUrl()).toBe("http://files.company.local");
101109
});
102110

103111
it("should use emulator endpoint as default external URL", async () => {
@@ -127,23 +135,4 @@ describe.sequential("CloudStorageEmulatorContainer", { timeout: 240_000 }, () =>
127135
expect(container.getExternalUrl()).toBe(undefined);
128136
expect((await fetch(`${container.getEmulatorEndpoint()}/_internal/healthcheck`)).status).toBe(200);
129137
});
130-
131-
async function checkCloudStorage(cloudstorageEmulatorContainer: StartedCloudStorageEmulatorContainer) {
132-
expect(cloudstorageEmulatorContainer).toBeDefined();
133-
134-
const cloudStorageClient = new Storage({
135-
projectId: "test-project",
136-
apiEndpoint: cloudstorageEmulatorContainer.getExternalUrl(),
137-
});
138-
expect(cloudStorageClient).toBeDefined();
139-
140-
const createdBucket = await cloudStorageClient.createBucket("test-bucket");
141-
expect(createdBucket).toBeDefined();
142-
143-
const [buckets] = await cloudStorageClient.getBuckets();
144-
expect(buckets).toBeDefined();
145-
expect(buckets).toHaveLength(1);
146-
const [firstBucket] = buckets;
147-
expect(firstBucket.name).toBe("test-bucket");
148-
}
149138
});

packages/modules/gcloud/src/firestore-emulator-container.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ describe("FirestoreEmulatorContainer", { timeout: 240_000 }, () => {
1010
"should work with %s",
1111
async (image) => {
1212
// firestoreExample {
13-
const collection = "test-collection";
14-
const document = "test-doc";
15-
1613
await using container = await new FirestoreEmulatorContainer(image).start();
1714

1815
const firestore = admin.initializeApp({ projectId: "test-project" }, `test-app-${randomUuid()}`).firestore();
@@ -21,6 +18,8 @@ describe("FirestoreEmulatorContainer", { timeout: 240_000 }, () => {
2118
ssl: false,
2219
});
2320

21+
const collection = "test-collection";
22+
const document = "test-doc";
2423
const docRef = firestore.collection(collection).doc(document);
2524
await docRef.set({ message: "Hello, Firestore!" });
2625
const snapshot = await docRef.get();
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
import { PubSub } from "@google-cloud/pubsub";
22
import { getImage } from "../../../testcontainers/src/utils/test-helper";
3-
import { PubSubEmulatorContainer, StartedPubSubEmulatorContainer } from "./pubsub-emulator-container";
3+
import { PubSubEmulatorContainer } from "./pubsub-emulator-container";
44

55
const IMAGE = getImage(__dirname);
66

77
describe("PubSubEmulatorContainer", { timeout: 240_000 }, () => {
88
it("should work using default version", async () => {
9-
await using pubsubEmulatorContainer = await new PubSubEmulatorContainer(IMAGE).start();
9+
// pubsubExample {
10+
await using container = await new PubSubEmulatorContainer(IMAGE).start();
1011

11-
await checkPubSub(pubsubEmulatorContainer);
12-
});
13-
14-
async function checkPubSub(pubsubEmulatorContainer: StartedPubSubEmulatorContainer) {
15-
expect(pubsubEmulatorContainer).toBeDefined();
16-
17-
const pubSubClient = new PubSub({
12+
const pubSub = new PubSub({
1813
projectId: "test-project",
19-
apiEndpoint: pubsubEmulatorContainer.getEmulatorEndpoint(),
14+
apiEndpoint: container.getEmulatorEndpoint(),
2015
});
21-
expect(pubSubClient).toBeDefined();
2216

23-
const [createdTopic] = await pubSubClient.createTopic("test-topic");
24-
expect(createdTopic).toBeDefined();
25-
// Note: topic name format is projects/<projectId>/topics/<topicName>
17+
const [createdTopic] = await pubSub.createTopic("test-topic");
18+
2619
expect(createdTopic.name).toContain("test-topic");
27-
}
20+
// }
21+
});
2822
});

0 commit comments

Comments
 (0)