|
| 1 | +import { Spanner } from "@google-cloud/spanner"; |
| 2 | +import type { IInstance } from "@google-cloud/spanner/build/src/instance"; |
| 3 | +import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers"; |
| 4 | + |
| 5 | +const GRPC_PORT = 9010; |
| 6 | + |
| 7 | +/** |
| 8 | + * SpannerEmulatorContainer runs the Cloud Spanner emulator via the GCloud CLI image. |
| 9 | + */ |
| 10 | +export class SpannerEmulatorContainer extends GenericContainer { |
| 11 | + private projectId?: string; |
| 12 | + |
| 13 | + constructor(image: string) { |
| 14 | + super(image); |
| 15 | + |
| 16 | + // only gRPC port is supported |
| 17 | + this.withExposedPorts(GRPC_PORT).withWaitStrategy(Wait.forLogMessage(/.*Cloud Spanner emulator running\..*/, 1)); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Sets the GCP project ID to use with the emulator. |
| 22 | + */ |
| 23 | + public withProjectId(projectId: string): this { |
| 24 | + this.projectId = projectId; |
| 25 | + return this; |
| 26 | + } |
| 27 | + |
| 28 | + public override async start(): Promise<StartedSpannerEmulatorContainer> { |
| 29 | + const selectedProject = this.projectId ?? "test-project"; |
| 30 | + |
| 31 | + const started = await super.start(); |
| 32 | + return new StartedSpannerEmulatorContainer(started, selectedProject); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * A running Spanner emulator instance with endpoint getters and helper access. |
| 38 | + */ |
| 39 | +export class StartedSpannerEmulatorContainer extends AbstractStartedContainer { |
| 40 | + constructor( |
| 41 | + startedTestContainer: StartedTestContainer, |
| 42 | + private readonly projectId: string |
| 43 | + ) { |
| 44 | + super(startedTestContainer); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @returns host:port for gRPC. |
| 49 | + */ |
| 50 | + public getEmulatorGrpcEndpoint(): string { |
| 51 | + return `${this.getHost()}:${this.getMappedPort(GRPC_PORT)}`; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @returns the GCP project ID used by the emulator. |
| 56 | + */ |
| 57 | + public getProjectId(): string { |
| 58 | + return this.projectId; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @returns a helper for Spanner resource operations against the emulator. |
| 63 | + */ |
| 64 | + public get helper(): SpannerEmulatorHelper { |
| 65 | + return new SpannerEmulatorHelper(this); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Helper class that encapsulates all Spanner client interactions against the emulator. |
| 71 | + * Clients and configs are lazily instantiated; user must call setAsEmulatorHost(). |
| 72 | + */ |
| 73 | +export class SpannerEmulatorHelper { |
| 74 | + private clientInstance?: Spanner; |
| 75 | + private instanceAdminClientInstance?: ReturnType<Spanner["getInstanceAdminClient"]>; |
| 76 | + private databaseAdminClientInstance?: ReturnType<Spanner["getDatabaseAdminClient"]>; |
| 77 | + private instanceConfigValue?: string; |
| 78 | + |
| 79 | + constructor(private readonly emulator: StartedSpannerEmulatorContainer) {} |
| 80 | + |
| 81 | + /** |
| 82 | + * Must be called by the user to configure the SPANNER_EMULATOR_HOST env var. |
| 83 | + */ |
| 84 | + public setAsEmulatorHost(): void { |
| 85 | + process.env.SPANNER_EMULATOR_HOST = this.emulator.getEmulatorGrpcEndpoint(); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Lazily get or create the Spanner client. |
| 90 | + */ |
| 91 | + public get client(): Spanner { |
| 92 | + if (!this.clientInstance) { |
| 93 | + if (!process.env.SPANNER_EMULATOR_HOST) { |
| 94 | + throw new Error("SPANNER_EMULATOR_HOST is not set. Call setAsEmulatorHost() before using the client."); |
| 95 | + } |
| 96 | + // Provide fake credentials so the auth library never tries metadata |
| 97 | + this.clientInstance = new Spanner({ |
| 98 | + projectId: this.emulator.getProjectId(), |
| 99 | + credentials: { |
| 100 | + client_email: "[email protected]", |
| 101 | + private_key: "not-a-real-key", |
| 102 | + }, |
| 103 | + }); |
| 104 | + } |
| 105 | + return this.clientInstance; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Lazily get or create the InstanceAdminClient. |
| 110 | + */ |
| 111 | + private get instanceAdminClient(): ReturnType<Spanner["getInstanceAdminClient"]> { |
| 112 | + if (!this.instanceAdminClientInstance) { |
| 113 | + this.instanceAdminClientInstance = this.client.getInstanceAdminClient(); |
| 114 | + } |
| 115 | + return this.instanceAdminClientInstance; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Lazily get or create the DatabaseAdminClient. |
| 120 | + */ |
| 121 | + private get databaseAdminClient(): ReturnType<Spanner["getDatabaseAdminClient"]> { |
| 122 | + if (!this.databaseAdminClientInstance) { |
| 123 | + this.databaseAdminClientInstance = this.client.getDatabaseAdminClient(); |
| 124 | + } |
| 125 | + return this.databaseAdminClientInstance; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Lazily compute the instanceConfig path. |
| 130 | + */ |
| 131 | + public get instanceConfig(): string { |
| 132 | + if (!this.instanceConfigValue) { |
| 133 | + this.instanceConfigValue = this.instanceAdminClient.instanceConfigPath( |
| 134 | + this.emulator.getProjectId(), |
| 135 | + "emulator-config" |
| 136 | + ); |
| 137 | + } |
| 138 | + return this.instanceConfigValue; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Creates a new Spanner instance in the emulator. |
| 143 | + */ |
| 144 | + public async createInstance(instanceId: string, options?: IInstance): Promise<unknown> { |
| 145 | + const [operation] = await this.instanceAdminClient.createInstance({ |
| 146 | + instanceId, |
| 147 | + parent: this.instanceAdminClient.projectPath(this.emulator.getProjectId()), |
| 148 | + instance: options, |
| 149 | + }); |
| 150 | + const [result] = await operation.promise(); |
| 151 | + return result; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Deletes an existing Spanner instance in the emulator. |
| 156 | + */ |
| 157 | + public async deleteInstance(instanceId: string): Promise<void> { |
| 158 | + await this.client.instance(instanceId).delete(); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Creates a new database under the specified instance in the emulator. |
| 163 | + */ |
| 164 | + public async createDatabase(instanceId: string, databaseId: string): Promise<unknown> { |
| 165 | + const [operation] = await this.databaseAdminClient.createDatabase({ |
| 166 | + parent: this.databaseAdminClient.instancePath(this.emulator.getProjectId(), instanceId), |
| 167 | + createStatement: `CREATE DATABASE \`${databaseId}\``, |
| 168 | + }); |
| 169 | + const [result] = await operation.promise(); |
| 170 | + return result; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Deletes a database under the specified instance in the emulator. |
| 175 | + */ |
| 176 | + public async deleteDatabase(instanceId: string, databaseId: string): Promise<void> { |
| 177 | + await this.client.instance(instanceId).database(databaseId).delete(); |
| 178 | + } |
| 179 | +} |
0 commit comments