From b93fdfba5142f9d9091a31e1498d4f97117c4078 Mon Sep 17 00:00:00 2001 From: Alec Mev Date: Sat, 31 Aug 2024 10:28:50 +0100 Subject: [PATCH] Add `autoUpdateExternalUrl` to GCS (#825) Co-authored-by: Cristian Greco --- packages/modules/gcloud/package.json | 1 + .../cloudstorage-emulator-container.test.ts | 34 ++++++++++++++++++- .../src/cloudstorage-emulator-container.ts | 27 +++++++++------ 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/packages/modules/gcloud/package.json b/packages/modules/gcloud/package.json index 3a532945f..f49031816 100644 --- a/packages/modules/gcloud/package.json +++ b/packages/modules/gcloud/package.json @@ -8,6 +8,7 @@ "firestore", "pubsub", "cloudstorage", + "gcs", "testing", "docker", "testcontainers" diff --git a/packages/modules/gcloud/src/cloudstorage-emulator-container.test.ts b/packages/modules/gcloud/src/cloudstorage-emulator-container.test.ts index 8fd12008b..6c8aba11f 100644 --- a/packages/modules/gcloud/src/cloudstorage-emulator-container.test.ts +++ b/packages/modules/gcloud/src/cloudstorage-emulator-container.test.ts @@ -55,7 +55,7 @@ describe("CloudStorageEmulatorContainer", () => { await cloudstorageEmulatorContainer.stop(); }); - it("should update the external URL", async () => { + it("should use the provided external URL", async () => { const cloudstorageEmulatorContainer = await new CloudStorageEmulatorContainer() .withExternalURL("http://cdn.company.local") .start(); @@ -104,6 +104,38 @@ describe("CloudStorageEmulatorContainer", () => { await cloudstorageEmulatorContainer.stop(); }); + it("should use emulator endpoint as default external URL", async () => { + let configUpdated = false; + + server.events.on("request:start", ({ request }) => { + if (request.url.includes("/_internal/config")) configUpdated = true; + }); + + const container = await new CloudStorageEmulatorContainer().start(); + + expect(configUpdated).toBe(true); + expect(container.getExternalUrl()).toBe(container.getEmulatorEndpoint()); + expect((await fetch(`${container.getExternalUrl()}/_internal/healthcheck`)).status).toBe(200); + + await container.stop(); + }); + + it("should allow skipping updating the external URL automatically", async () => { + let configUpdated = false; + + server.events.on("request:start", ({ request }) => { + if (request.url.includes("/_internal/config")) configUpdated = true; + }); + + const container = await new CloudStorageEmulatorContainer().withAutoUpdateExternalUrl(false).start(); + + expect(configUpdated).toBe(false); + expect(container.getExternalUrl()).toBe(undefined); + expect((await fetch(`${container.getEmulatorEndpoint()}/_internal/healthcheck`)).status).toBe(200); + + await container.stop(); + }); + async function checkCloudStorage(cloudstorageEmulatorContainer: StartedCloudStorageEmulatorContainer) { expect(cloudstorageEmulatorContainer).toBeDefined(); diff --git a/packages/modules/gcloud/src/cloudstorage-emulator-container.ts b/packages/modules/gcloud/src/cloudstorage-emulator-container.ts index 0f6961111..7650b6f98 100644 --- a/packages/modules/gcloud/src/cloudstorage-emulator-container.ts +++ b/packages/modules/gcloud/src/cloudstorage-emulator-container.ts @@ -7,13 +7,12 @@ const DEFAULT_IMAGE = "fsouza/fake-gcs-server"; export class CloudStorageEmulatorContainer extends GenericContainer { private _externalURL?: string; private _publicHost?: string; + private autoUpdateExternalUrl = true; constructor(image = DEFAULT_IMAGE) { super(image); - this.withExposedPorts(PORT) - .withWaitStrategy(Wait.forLogMessage(/server started/g, 1)) - .withStartupTimeout(120_000); + this.withExposedPorts(PORT).withWaitStrategy(Wait.forLogMessage("server started")).withStartupTimeout(120_000); } public withExternalURL(url: string): CloudStorageEmulatorContainer { @@ -26,6 +25,11 @@ export class CloudStorageEmulatorContainer extends GenericContainer { return this; } + public withAutoUpdateExternalUrl(autoUpdateExternalUrl: boolean): this { + this.autoUpdateExternalUrl = autoUpdateExternalUrl; + return this; + } + public override async start(): Promise { // Determine the valid entrypoint command when starting the Cloud Storage server @@ -42,7 +46,14 @@ export class CloudStorageEmulatorContainer extends GenericContainer { ]; this.withEntrypoint(entrypoint); - return new StartedCloudStorageEmulatorContainer(await super.start(), this._externalURL); + const container = new StartedCloudStorageEmulatorContainer(await super.start(), this._externalURL); + + if (this.autoUpdateExternalUrl && this._externalURL === undefined) { + // Done after starting because we don't know the port ahead of time + await container.updateExternalUrl(container.getEmulatorEndpoint()); + } + + return container; } } @@ -100,11 +111,7 @@ export class StartedCloudStorageEmulatorContainer extends AbstractStartedContain * @return a host:port pair corresponding to the address on which the emulator is * reachable from the test host machine. */ - public getExternalUrl(): string { - if (this._externalURL) { - return this._externalURL; - } else { - return this.getEmulatorEndpoint(); - } + public getExternalUrl(): string | undefined { + return this._externalURL; } }