Skip to content

Commit b93fdfb

Browse files
Add autoUpdateExternalUrl to GCS (#825)
Co-authored-by: Cristian Greco <[email protected]>
1 parent 055c02f commit b93fdfb

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

packages/modules/gcloud/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"firestore",
99
"pubsub",
1010
"cloudstorage",
11+
"gcs",
1112
"testing",
1213
"docker",
1314
"testcontainers"

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe("CloudStorageEmulatorContainer", () => {
5555
await cloudstorageEmulatorContainer.stop();
5656
});
5757

58-
it("should update the external URL", async () => {
58+
it("should use the provided external URL", async () => {
5959
const cloudstorageEmulatorContainer = await new CloudStorageEmulatorContainer()
6060
.withExternalURL("http://cdn.company.local")
6161
.start();
@@ -104,6 +104,38 @@ describe("CloudStorageEmulatorContainer", () => {
104104
await cloudstorageEmulatorContainer.stop();
105105
});
106106

107+
it("should use emulator endpoint as default external URL", async () => {
108+
let configUpdated = false;
109+
110+
server.events.on("request:start", ({ request }) => {
111+
if (request.url.includes("/_internal/config")) configUpdated = true;
112+
});
113+
114+
const container = await new CloudStorageEmulatorContainer().start();
115+
116+
expect(configUpdated).toBe(true);
117+
expect(container.getExternalUrl()).toBe(container.getEmulatorEndpoint());
118+
expect((await fetch(`${container.getExternalUrl()}/_internal/healthcheck`)).status).toBe(200);
119+
120+
await container.stop();
121+
});
122+
123+
it("should allow skipping updating the external URL automatically", async () => {
124+
let configUpdated = false;
125+
126+
server.events.on("request:start", ({ request }) => {
127+
if (request.url.includes("/_internal/config")) configUpdated = true;
128+
});
129+
130+
const container = await new CloudStorageEmulatorContainer().withAutoUpdateExternalUrl(false).start();
131+
132+
expect(configUpdated).toBe(false);
133+
expect(container.getExternalUrl()).toBe(undefined);
134+
expect((await fetch(`${container.getEmulatorEndpoint()}/_internal/healthcheck`)).status).toBe(200);
135+
136+
await container.stop();
137+
});
138+
107139
async function checkCloudStorage(cloudstorageEmulatorContainer: StartedCloudStorageEmulatorContainer) {
108140
expect(cloudstorageEmulatorContainer).toBeDefined();
109141

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ const DEFAULT_IMAGE = "fsouza/fake-gcs-server";
77
export class CloudStorageEmulatorContainer extends GenericContainer {
88
private _externalURL?: string;
99
private _publicHost?: string;
10+
private autoUpdateExternalUrl = true;
1011

1112
constructor(image = DEFAULT_IMAGE) {
1213
super(image);
1314

14-
this.withExposedPorts(PORT)
15-
.withWaitStrategy(Wait.forLogMessage(/server started/g, 1))
16-
.withStartupTimeout(120_000);
15+
this.withExposedPorts(PORT).withWaitStrategy(Wait.forLogMessage("server started")).withStartupTimeout(120_000);
1716
}
1817

1918
public withExternalURL(url: string): CloudStorageEmulatorContainer {
@@ -26,6 +25,11 @@ export class CloudStorageEmulatorContainer extends GenericContainer {
2625
return this;
2726
}
2827

28+
public withAutoUpdateExternalUrl(autoUpdateExternalUrl: boolean): this {
29+
this.autoUpdateExternalUrl = autoUpdateExternalUrl;
30+
return this;
31+
}
32+
2933
public override async start(): Promise<StartedCloudStorageEmulatorContainer> {
3034
// Determine the valid entrypoint command when starting the Cloud Storage server
3135

@@ -42,7 +46,14 @@ export class CloudStorageEmulatorContainer extends GenericContainer {
4246
];
4347
this.withEntrypoint(entrypoint);
4448

45-
return new StartedCloudStorageEmulatorContainer(await super.start(), this._externalURL);
49+
const container = new StartedCloudStorageEmulatorContainer(await super.start(), this._externalURL);
50+
51+
if (this.autoUpdateExternalUrl && this._externalURL === undefined) {
52+
// Done after starting because we don't know the port ahead of time
53+
await container.updateExternalUrl(container.getEmulatorEndpoint());
54+
}
55+
56+
return container;
4657
}
4758
}
4859

@@ -100,11 +111,7 @@ export class StartedCloudStorageEmulatorContainer extends AbstractStartedContain
100111
* @return a <code>host:port</code> pair corresponding to the address on which the emulator is
101112
* reachable from the test host machine.
102113
*/
103-
public getExternalUrl(): string {
104-
if (this._externalURL) {
105-
return this._externalURL;
106-
} else {
107-
return this.getEmulatorEndpoint();
108-
}
114+
public getExternalUrl(): string | undefined {
115+
return this._externalURL;
109116
}
110117
}

0 commit comments

Comments
 (0)