UNPKG

@testcontainers/gcloud

Version:
96 lines 3.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StartedCloudStorageEmulatorContainer = exports.CloudStorageEmulatorContainer = void 0; const testcontainers_1 = require("testcontainers"); const PORT = 4443; class CloudStorageEmulatorContainer extends testcontainers_1.GenericContainer { _externalURL; _publicHost; autoUpdateExternalUrl = true; constructor(image) { super(image); this.withExposedPorts(PORT).withWaitStrategy(testcontainers_1.Wait.forLogMessage("server started")).withStartupTimeout(120_000); } withExternalURL(url) { this._externalURL = url; return this; } withPublicHost(host) { this._publicHost = host; return this; } withAutoUpdateExternalUrl(autoUpdateExternalUrl) { this.autoUpdateExternalUrl = autoUpdateExternalUrl; return this; } async start() { // Determine the valid entrypoint command when starting the Cloud Storage server const entrypoint = [ "fake-gcs-server", // Bind to all interfaces "-host", "0.0.0.0", // Using thre http scheme for the server "-scheme", "http", ...(this._externalURL ? ["-external-url", this._externalURL] : []), ...(this._publicHost ? ["-public-host", this._publicHost] : []), ]; this.withEntrypoint(entrypoint); 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; } } exports.CloudStorageEmulatorContainer = CloudStorageEmulatorContainer; class StartedCloudStorageEmulatorContainer extends testcontainers_1.AbstractStartedContainer { _externalURL; _publicHost; constructor(startedTestContainer, externalURL, publicHost) { super(startedTestContainer); this._externalURL = externalURL; this._publicHost = publicHost ?? "storage.googleapis.com"; } async updateExternalUrl(url) { this._externalURL = url; await this.processServerConfigChange(); } async updatePublicHost(host) { this._publicHost = host; await this.processServerConfigChange(); } getEmulatorEndpoint() { return `http://${this.getHost()}:${this.getMappedPort(PORT)}`; } /** * Sends a PUT request to the fake-gcs-server to update the server configuration for externalUrl and publicHost. */ async processServerConfigChange() { const requestUrl = `${this.getEmulatorEndpoint()}/_internal/config`; const response = await fetch(requestUrl, { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ externalUrl: this._externalURL, publicHost: this._publicHost, }, null, 2), }); if (!response.ok) { console.warn(`error updating fake-gcs-server with external url, response status code: ${response.status}`); } } /** * @return a <code>host:port</code> pair corresponding to the address on which the emulator is * reachable from the test host machine. */ getExternalUrl() { return this._externalURL; } } exports.StartedCloudStorageEmulatorContainer = StartedCloudStorageEmulatorContainer; //# sourceMappingURL=cloudstorage-emulator-container.js.map