GCloud Module
Testcontainers module for the Google Cloud Platform's Cloud SDK.
Install
npm install @testcontainers/gcloud --save-dev
The module now supports multiple emulators, including firestore
, which offers both native
and datastore
modes.
To utilize these emulators, you should employ the following classes:
Emulator | Class | Container Image |
---|---|---|
Firestore (Native mode) | FirestoreEmulatorContainer | gcr.io/google.com/cloudsdktool/google-cloud-cli:emulators |
Firestore (Datastore mode) | DatastoreEmulatorContainer | gcr.io/google.com/cloudsdktool/google-cloud-cli:emulators |
Cloud PubSub | PubSubEmulatorContainer | gcr.io/google.com/cloudsdktool/google-cloud-cli:emulators |
Cloud Storage | CloudStorageEmulatorContainer | https://hub.docker.com/r/fsouza/fake-gcs-server |
BigQuery | BigQueryEmulatorContainer | ghcr.io/goccy/bigquery-emulator |
Examples
Firestore (Native mode)
it("should work using default version", async () => {
const firestoreEmulatorContainer = await new FirestoreEmulatorContainer().start();
await checkFirestore(firestoreEmulatorContainer);
await firestoreEmulatorContainer.stop();
});
it("should work using version 468.0.0", async () => {
const firestoreEmulatorContainer = await new FirestoreEmulatorContainer(
"gcr.io/google.com/cloudsdktool/google-cloud-cli:468.0.0-emulators"
).start();
await checkFirestore(firestoreEmulatorContainer);
await firestoreEmulatorContainer.stop();
});
Firestore (Datastore mode)
it("should work using default version", async () => {
const datastoreEmulatorContainer = await new DatastoreEmulatorContainer().start();
await checkDatastore(datastoreEmulatorContainer);
await datastoreEmulatorContainer.stop();
});
it("should work using version 468.0.0", async () => {
const datastoreEmulatorContainer = await new DatastoreEmulatorContainer(
"gcr.io/google.com/cloudsdktool/google-cloud-cli:468.0.0-emulators"
).start();
await checkDatastore(datastoreEmulatorContainer);
await datastoreEmulatorContainer.stop();
});
Cloud PubSub
import { PubSubEmulatorContainer, StartedPubSubEmulatorContainer } from "./pubsub-emulator-container";
import { PubSub } from "@google-cloud/pubsub";
describe("PubSubEmulatorContainer", () => {
jest.setTimeout(240_000);
it("should work using default version", async () => {
const pubsubEmulatorContainer = await new PubSubEmulatorContainer().start();
await checkPubSub(pubsubEmulatorContainer);
await pubsubEmulatorContainer.stop();
});
async function checkPubSub(pubsubEmulatorContainer: StartedPubSubEmulatorContainer) {
expect(pubsubEmulatorContainer).toBeDefined();
const pubSubClient = new PubSub({
projectId: "test-project",
apiEndpoint: pubsubEmulatorContainer.getEmulatorEndpoint(),
});
expect(pubSubClient).toBeDefined();
const [createdTopic] = await pubSubClient.createTopic("test-topic");
expect(createdTopic).toBeDefined();
// Note: topic name format is projects/<projectId>/topics/<topicName>
expect(createdTopic.name).toContain("test-topic");
}
});
Cloud Storage
The Cloud Storage container doesn't rely on a built-in emulator created by Google but instead depends on a fake Cloud Storage server implemented by Francisco Souza. The project is open-source, and the repository can be found at fsouza/fake-gcs-server.
it("should work using default version", async () => {
const cloudstorageEmulatorContainer = await new CloudStorageEmulatorContainer().start();
await checkCloudStorage(cloudstorageEmulatorContainer);
await cloudstorageEmulatorContainer.stop();
});
BigQuery
The BigQuery container doesn't rely on a built-in emulator created by Google, but instead depends on an implementation written in Go by Masaaki Goshima. The project is open-source, and the repository can be found at goccy/bigquery-emulator.
BigQuery emulator uses go-zetasqlite to interpret ZetaSQL (the language used in BigQuery) and runs it in SQLite. The README lists BigQuery features currently supported.
import { BigQuery, TableSchema } from "@google-cloud/bigquery";
import { BigQueryEmulatorContainer, StartedBigQueryEmulatorContainer } from "./bigquery-emulator-container";
describe("BigQueryEmulatorContainer", () => {
jest.setTimeout(240_000);
it("should work using default version", async () => {
const bigQueryEmulatorContainer = await new BigQueryEmulatorContainer().start();
await checkBigQuery(bigQueryEmulatorContainer);
await bigQueryEmulatorContainer.stop();
});
async function checkBigQuery(bigQueryEmulatorContainer: StartedBigQueryEmulatorContainer) {
expect(bigQueryEmulatorContainer).toBeDefined();
const testDataset = "test-dataset";
const testTable = "test-table";
const testSchema: TableSchema = { fields: [{ name: "message", type: "STRING" }] };
const config = {
projectId: bigQueryEmulatorContainer.getProjectId(),
apiEndpoint: bigQueryEmulatorContainer.getEmulatorEndpoint(),
};
const bigQuery = new BigQuery(config);
await bigQuery.dataset(testDataset).create();
await bigQuery.dataset(testDataset).table(testTable).create({ schema: testSchema });
await bigQuery
.dataset(testDataset)
.table(testTable)
.insert([{ message: "Hello, BigQuery!" }]);
const [rows] = await bigQuery.dataset(testDataset).table(testTable).getRows();
expect(rows).toEqual([{ message: "Hello, BigQuery!" }]);
}
});