-
-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
DatastoreEmulatorContainer
to gcloud module (#782)
- Loading branch information
1 parent
96250a8
commit 693ea8c
Showing
6 changed files
with
147 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/modules/gcloud/src/datastore-emulator-container.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { DatastoreEmulatorContainer, StartedDatastoreEmulatorContainer } from "./datastore-emulator-container"; | ||
import { Datastore } from "@google-cloud/datastore"; | ||
|
||
describe("DatastoreEmulatorContainer", () => { | ||
jest.setTimeout(240_000); | ||
|
||
// datastore4 { | ||
it("should work using default version", async () => { | ||
const datastoreEmulatorContainer = await new DatastoreEmulatorContainer().start(); | ||
|
||
await checkDatastore(datastoreEmulatorContainer); | ||
|
||
await datastoreEmulatorContainer.stop(); | ||
}); | ||
// } | ||
|
||
// datastore5 { | ||
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(); | ||
}); | ||
|
||
// } | ||
|
||
async function checkDatastore(datastoreEmulatorContainer: StartedDatastoreEmulatorContainer) { | ||
expect(datastoreEmulatorContainer).toBeDefined(); | ||
const testProjectId = "test-project"; | ||
const testKind = "test-kind"; | ||
const testId = "123"; | ||
const databaseConfig = { projectId: testProjectId, apiEndpoint: datastoreEmulatorContainer.getEmulatorEndpoint() }; | ||
const datastore = new Datastore(databaseConfig); | ||
|
||
const key = datastore.key([testKind, testId]); | ||
const data = { message: "Hello, Datastore!" }; | ||
await datastore.save({ key, data }); | ||
const [entity] = await datastore.get(key); | ||
|
||
expect(entity).toEqual({ message: "Hello, Datastore!", [Datastore.KEY]: key }); | ||
} | ||
}); |
33 changes: 33 additions & 0 deletions
33
packages/modules/gcloud/src/datastore-emulator-container.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers"; | ||
|
||
const EMULATOR_PORT = 8080; | ||
const CMD = `gcloud beta emulators firestore start --host-port 0.0.0.0:${EMULATOR_PORT} --database-mode=datastore-mode`; | ||
const DEFAULT_IMAGE = "gcr.io/google.com/cloudsdktool/cloud-sdk"; | ||
|
||
export class DatastoreEmulatorContainer extends GenericContainer { | ||
constructor(image = DEFAULT_IMAGE) { | ||
super(image); | ||
this.withExposedPorts(EMULATOR_PORT) | ||
.withCommand(["/bin/sh", "-c", CMD]) | ||
.withWaitStrategy(Wait.forLogMessage(RegExp(".*running.*"), 1)) | ||
.withStartupTimeout(120_000); | ||
} | ||
|
||
public override async start(): Promise<StartedDatastoreEmulatorContainer> { | ||
return new StartedDatastoreEmulatorContainer(await super.start()); | ||
} | ||
} | ||
|
||
export class StartedDatastoreEmulatorContainer extends AbstractStartedContainer { | ||
constructor(startedTestContainer: StartedTestContainer) { | ||
super(startedTestContainer); | ||
} | ||
|
||
/** | ||
* @return a <code>host:port</code> pair corresponding to the address on which the emulator is | ||
* reachable from the test host machine. | ||
*/ | ||
public getEmulatorEndpoint(): string { | ||
return `${this.getHost()}:${this.getMappedPort(EMULATOR_PORT)}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { FirestoreEmulatorContainer, StartedFirestoreEmulatorContainer } from "./firestore-emulator-container"; | ||
export { DatastoreEmulatorContainer, StartedDatastoreEmulatorContainer } from "./datastore-emulator-container"; |