import { AbstractStartedContainer, BoundPorts, type ContainerRuntimeClient, GenericContainer, getContainerRuntimeClient, type InspectResult, IntervalRetry, log, type StartedTestContainer, Wait, type WaitStrategy, } from "testcontainers"; import type { BucketDefinition } from "./bucket-definition"; import { CouchbaseService } from "./couchbase-service"; import PORTS from "./ports"; export class CouchbaseContainer extends GenericContainer { private username = "Administrator"; private password = "password"; private enabledServices: Set = new Set([ CouchbaseService.KV, CouchbaseService.QUERY, CouchbaseService.SEARCH, CouchbaseService.INDEX, ]); private readonly customServiceQuotas: Map = new Map(); private readonly buckets: Set = new Set([]); private isEnterprise = false; constructor(image: string) { super(image); this.withWaitStrategy(Wait.forLogMessage("Starting Couchbase Server -- Web UI available at http://:8091")); } withCredentials(username: string, password: string) { this.username = username; this.password = password; return this; } withBucket(bucket: BucketDefinition) { this.buckets.add(bucket); return this; } withEnabledServices(...services: CouchbaseService[]) { this.enabledServices = new Set([...services]); return this; } withServiceQuota(service: CouchbaseService, quotaMb: number) { if (!service.hasQuota()) { throw new Error(`The provided service (${service.getIdentifier()}) has no quota to configure`); } if (quotaMb < service.getMinimumQuotaMb()) { throw new Error( `The custom quota (${quotaMb}) must not be smaller than the minimum quota for the service (${service.getMinimumQuotaMb()})` ); } this.customServiceQuotas.set(service, quotaMb); return this; } withAnalyticService() { this.enabledServices.add(CouchbaseService.ANALYTICS); return this; } withEventingService() { this.enabledServices.add(CouchbaseService.EVENTING); return this; } private async doHttpRequest( host: string, port: number, path: string, method: string, body: RequestInit["body"], auth = false ) { try { return await fetch(`http://${host}:${port}${path}`, { method, body, headers: { "Content-Type": "application/x-www-form-urlencoded", ...(auth ? { Authorization: `Basic ${btoa(`${this.username}:${this.password}`)}` } : {}), }, }); } catch (cause) { throw new Error("Could not perform request against couchbase HTTP endpoint", { cause }); } } private async checkResponse(response: Response, errorMessage: string) { if (!response.status.toString().startsWith("2")) { throw new Error(`${errorMessage}:${await response.text()}`); } } private getPortsToExpose() { const exposedPorts = [PORTS.MGMT_PORT, PORTS.MGMT_SSL_PORT]; if (this.enabledServices.has(CouchbaseService.KV)) { exposedPorts.push(PORTS.KV_PORT, PORTS.KV_SSL_PORT, PORTS.VIEW_PORT, PORTS.VIEW_SSL_PORT); } if (this.enabledServices.has(CouchbaseService.ANALYTICS)) { exposedPorts.push(PORTS.ANALYTICS_PORT, PORTS.ANALYTICS_SSL_PORT); } if (this.enabledServices.has(CouchbaseService.QUERY)) { exposedPorts.push(PORTS.QUERY_PORT, PORTS.QUERY_SSL_PORT); } if (this.enabledServices.has(CouchbaseService.SEARCH)) { exposedPorts.push(PORTS.SEARCH_PORT, PORTS.SEARCH_SSL_PORT); } if (this.enabledServices.has(CouchbaseService.EVENTING)) { exposedPorts.push(PORTS.EVENTING_PORT, PORTS.EVENTING_SSL_PORT); } return exposedPorts.filter((port) => !this.hasExposedPort(port)); } private constructWaitStrategies() { const waitStrategies: WaitStrategy[] = []; waitStrategies.push( Wait.forHttp("/pools/default", PORTS.MGMT_PORT) .withBasicCredentials(this.username, this.password) .forStatusCode(200) .forResponsePredicate((response) => { try { const jsonResponse = JSON.parse(response); return jsonResponse.nodes[0].status === "healthy"; } catch (e) { log.error(`Unable to parse response: ${response}, error: ${e}`); return false; } }) ); if (this.enabledServices.has(CouchbaseService.QUERY)) { waitStrategies.push( Wait.forHttp("/admin/ping", PORTS.QUERY_PORT) .withBasicCredentials(this.username, this.password) .forStatusCode(200) ); } if (this.enabledServices.has(CouchbaseService.ANALYTICS)) { waitStrategies.push( Wait.forHttp("/admin/ping", PORTS.ANALYTICS_PORT) .withBasicCredentials(this.username, this.password) .forStatusCode(200) ); } if (this.enabledServices.has(CouchbaseService.EVENTING)) { waitStrategies.push( Wait.forHttp("/api/v1/config", PORTS.EVENTING_PORT) .withBasicCredentials(this.username, this.password) .forStatusCode(200) ); } return waitStrategies; } private async waitUntilNodeIsOnline( client: ContainerRuntimeClient, inspectResult: InspectResult, startedTestContainer: StartedTestContainer, timeoutMs: number ) { return Wait.forHttp("/pools", PORTS.MGMT_PORT) .forStatusCode(200) .withStartupTimeout(timeoutMs) .waitUntilReady( client.container.getById(startedTestContainer.getId()), BoundPorts.fromInspectResult(client.info.containerRuntime.hostIps, inspectResult) ); } private async initializeIsEnterprise(container: StartedTestContainer) { const response = await this.doHttpRequest( container.getHost(), container.getMappedPort(PORTS.MGMT_PORT), "/pools", "GET", undefined, true ); let jsonResponse: { isEnterprise: boolean }; try { jsonResponse = (await response.json()) as { isEnterprise: boolean }; } catch (cause) { throw new Error("Couchbase /pools did not return valid JSON", { cause }); } this.isEnterprise = jsonResponse.isEnterprise; if (!jsonResponse.isEnterprise) { if (this.enabledServices.has(CouchbaseService.ANALYTICS)) { throw new Error("The Analytics Service is only supported with the Enterprise version"); } if (this.enabledServices.has(CouchbaseService.EVENTING)) { throw new Error("The Eventing Service is only supported with the Enterprise version"); } } } private async renameNode(container: StartedTestContainer, inspectResult: InspectResult) { log.debug(`Renaming Couchbase Node from localhost to ${container.getHost()}`); const networkName = container.getNetworkNames()[0]; const internalIpAddress = inspectResult.networkSettings[networkName].ipAddress; const body = new URLSearchParams(); body.append("hostname", internalIpAddress); const response = await this.doHttpRequest( container.getHost(), container.getMappedPort(PORTS.MGMT_PORT), "/node/controller/rename", "POST", body.toString() ); await this.checkResponse(response, "Could not rename couchbase node"); } private async initializeServices(container: StartedTestContainer) { const services = Array.from(this.enabledServices) .map((service) => service.getIdentifier()) .join(","); log.debug(`Initializing couchbase services on host: ${services}`); const body = new URLSearchParams(); body.append("services", services); const response = await this.doHttpRequest( container.getHost(), container.getMappedPort(PORTS.MGMT_PORT), "/node/controller/setupServices", "POST", body ); await this.checkResponse(response, "Could not enable couchbase services"); } private async setMemoryQuotas(container: StartedTestContainer) { for (const service of this.enabledServices) { if (!service.hasQuota()) { continue; } const body = new URLSearchParams(); const quota = this.customServiceQuotas.get(service) ?? service.getMinimumQuotaMb(); log.debug(`Custom service memory quotas: ${service.getIdentifier()} - ${quota} Mb.`); if (service.getIdentifier() === CouchbaseService.KV.getIdentifier()) { body.set("memoryQuota", quota.toString()); } else { body.set(`${service.getIdentifier()}MemoryQuota`, quota.toString()); } const response = await this.doHttpRequest( container.getHost(), container.getMappedPort(PORTS.MGMT_PORT), "/pools/default", "POST", body ); await this.checkResponse(response, "Could not configure service memory quotas"); } } private async configureAdminUser(container: StartedTestContainer) { log.debug(`Configuring couchbase admin user with username: "${this.username}"`); const body = new URLSearchParams(); body.set("username", this.username); body.set("password", this.password); body.set("port", PORTS.MGMT_PORT.toString()); const response = await this.doHttpRequest( container.getHost(), container.getMappedPort(PORTS.MGMT_PORT), "/settings/web", "POST", body ); await this.checkResponse(response, "Could not configure couchbase admin user"); } private async configureExternalPorts(container: StartedTestContainer) { log.debug("Mapping external ports to the alternate address configuration"); const body = new URLSearchParams(); body.set("hostname", container.getHost()); body.set("mgmt", container.getMappedPort(PORTS.MGMT_PORT).toString()); if (this.isEnterprise) { body.set("mgmtSSL", container.getMappedPort(PORTS.MGMT_SSL_PORT).toString()); } this.setServicePorts(body, container); const response = await this.doHttpRequest( container.getHost(), container.getMappedPort(PORTS.MGMT_PORT), "/node/controller/setupAlternateAddresses/external", "PUT", body, true ); await this.checkResponse(response, "Could not configure external ports"); } private setServicePorts(body: URLSearchParams, container: StartedTestContainer) { if (this.enabledServices.has(CouchbaseService.KV)) { body.set("kv", container.getMappedPort(PORTS.KV_PORT).toString()); body.set("capi", container.getMappedPort(PORTS.VIEW_PORT).toString()); if (this.isEnterprise) { body.set("kvSSL", container.getMappedPort(PORTS.KV_SSL_PORT).toString()); body.set("capiSSL", container.getMappedPort(PORTS.VIEW_SSL_PORT).toString()); } } if (this.enabledServices.has(CouchbaseService.QUERY)) { body.set("n1ql", container.getMappedPort(PORTS.QUERY_PORT).toString()); if (this.isEnterprise) { body.set("n1qlSSL", container.getMappedPort(PORTS.QUERY_SSL_PORT).toString()); } } if (this.enabledServices.has(CouchbaseService.SEARCH)) { body.set("fts", container.getMappedPort(PORTS.SEARCH_PORT).toString()); if (this.isEnterprise) { body.set("ftsSSL", container.getMappedPort(PORTS.SEARCH_SSL_PORT).toString()); } } if (this.enabledServices.has(CouchbaseService.ANALYTICS)) { body.set("cbas", container.getMappedPort(PORTS.ANALYTICS_PORT).toString()); if (this.isEnterprise) { body.set("cbasSSL", container.getMappedPort(PORTS.ANALYTICS_SSL_PORT).toString()); } } if (this.enabledServices.has(CouchbaseService.EVENTING)) { body.set("eventingAdminPort", container.getMappedPort(PORTS.EVENTING_PORT).toString()); if (this.isEnterprise) { body.set("eventingSSL", container.getMappedPort(PORTS.EVENTING_SSL_PORT).toString()); } } } private async configureIndexer(container: StartedTestContainer) { log.debug("Configuring the indexer service"); const body = new URLSearchParams(); body.set("storageMode", this.isEnterprise ? "memory_optimized" : "forestdb"); const response = await this.doHttpRequest( container.getHost(), container.getMappedPort(PORTS.MGMT_PORT), "/settings/indexes", "POST", body, true ); await this.checkResponse(response, "Could not configure the indexing service"); } private async createBuckets( client: ContainerRuntimeClient, inspectResult: InspectResult, startedTestContainer: StartedTestContainer, remainingStartupMs: () => number ) { log.debug(`Creating ${this.buckets.size} buckets (and corresponding indexes).`); for (const bucket of this.buckets) { log.debug(`Creating bucket ${bucket.getName()}`); await this.createBucket(startedTestContainer, bucket); await this.waitUntilBucketHasEnabledServices( client, inspectResult, startedTestContainer, bucket, remainingStartupMs() ); await this.waitUntilBucketIsHealthy(client, inspectResult, startedTestContainer, bucket, remainingStartupMs()); if (this.enabledServices.has(CouchbaseService.QUERY)) { await this.waitUntilKeyspaceIsPresent(client, startedTestContainer, bucket, remainingStartupMs()); } if (bucket.hasPrimaryIndex()) { if (this.enabledServices.has(CouchbaseService.QUERY)) { await this.createPrimaryIndex(startedTestContainer, bucket); await this.waitUntilPrimaryIndexIsOnline(client, startedTestContainer, bucket, remainingStartupMs()); } else { log.info( `Primary index creation for bucket ${bucket.getName()} ignored, since QUERY service is not present.` ); } } } } private async createBucket(startedTestContainer: StartedTestContainer, bucket: BucketDefinition) { const body = new URLSearchParams(); body.set("name", bucket.getName()); body.set("ramQuotaMB", bucket.getQuota().toString()); body.set("flushEnabled", bucket.hasFlushEnabled() ? "1" : "0"); body.set("replicaNumber", bucket.getNumOfReplicas().toString()); const response = await this.doHttpRequest( startedTestContainer.getHost(), startedTestContainer.getMappedPort(PORTS.MGMT_PORT), "/pools/default/buckets", "POST", body, true ); await this.checkResponse(response, `Could not create bucket ${bucket.getName()}`); } private async waitUntilBucketHasEnabledServices( client: ContainerRuntimeClient, inspectResult: InspectResult, startedTestContainer: StartedTestContainer, bucket: BucketDefinition, timeoutMs: number ) { await Wait.forHttp(`/pools/default/b/${bucket.getName()}`, PORTS.MGMT_PORT) .withBasicCredentials(this.username, this.password) .forStatusCode(200) .withStartupTimeout(timeoutMs) .forResponsePredicate((response) => { try { const jsonResponse = JSON.parse(response); const services = jsonResponse.nodesExt[0].services; const serviceNames = Object.keys(services); let found = false; for (const enabledService of this.enabledServices) { if (serviceNames.some((s) => s.startsWith(enabledService.getIdentifier()))) { found = true; } else { found = false; log.trace(`Service ${enabledService.getIdentifier()} not yet part of config, retrying.`); break; } } return found; } catch (e) { log.error(`Unable to parse response: ${response} ${e}`); return false; } }) .waitUntilReady( client.container.getById(startedTestContainer.getId()), BoundPorts.fromInspectResult(client.info.containerRuntime.hostIps, inspectResult) ); } private async waitUntilBucketIsHealthy( client: ContainerRuntimeClient, inspectResult: InspectResult, startedTestContainer: StartedTestContainer, bucket: BucketDefinition, timeoutMs: number ) { await Wait.forHttp(`/pools/default/buckets/${bucket.getName()}`, PORTS.MGMT_PORT) .withBasicCredentials(this.username, this.password) .forStatusCode(200) .withStartupTimeout(timeoutMs) .forResponsePredicate((response) => { try { const jsonResponse = JSON.parse(response); return jsonResponse.nodes[0]?.status === "healthy"; } catch (e) { log.error(`Unable to parse response: ${response}, error: ${e}`); return false; } }) .waitUntilReady( client.container.getById(startedTestContainer.getId()), BoundPorts.fromInspectResult(client.info.containerRuntime.hostIps, inspectResult) ); } private async waitUntilKeyspaceIsPresent( client: ContainerRuntimeClient, startedTestContainer: StartedTestContainer, bucket: BucketDefinition, timeoutMs: number ) { await new IntervalRetry(1000).retryUntil( async () => { try { const body = new URLSearchParams(); body.set( "statement", `SELECT COUNT(*) > 0 as present FROM system:keyspaces WHERE name = "${bucket.getName()}"` ); const response = await this.doHttpRequest( startedTestContainer.getHost(), startedTestContainer.getMappedPort(PORTS.QUERY_PORT), "/query/service", "POST", body, true ); await this.checkResponse(response, `Could not poll query service state for bucket: ${bucket.getName()}`); return response; } catch (_e) { return undefined; } }, async (response) => { if (response === undefined) { return false; } const jsonResponse = (await response.json()) as { results: Array<{ present: boolean }> }; return jsonResponse.results[0].present; }, () => { const message = `URL /query/service not accessible after ${timeoutMs}ms`; log.error(message, { containerId: client.container.getById(startedTestContainer.getId()).id }); throw new Error(message); }, timeoutMs ); } private async createPrimaryIndex(startedTestContainer: StartedTestContainer, bucket: BucketDefinition) { const body = new URLSearchParams(); body.set("statement", `CREATE PRIMARY INDEX on \`${bucket.getName()}\``); //TODO: check here! const response = await this.doHttpRequest( startedTestContainer.getHost(), startedTestContainer.getMappedPort(PORTS.QUERY_PORT), "/query/service", "POST", body, true ); await this.checkResponse(response, `Could not create primary index for bucket ${bucket.getName()}`); } private async waitUntilPrimaryIndexIsOnline( client: ContainerRuntimeClient, startedTestContainer: StartedTestContainer, bucket: BucketDefinition, timeoutMs: number ) { await new IntervalRetry(1000).retryUntil( async () => { try { const body = new URLSearchParams(); body.set( "statement", `SELECT COUNT(*) > 0 as online FROM system:indexes WHERE keyspace_id = "${bucket.getName()}" and is_primary = true and state = "online"` ); const response = await this.doHttpRequest( startedTestContainer.getHost(), startedTestContainer.getMappedPort(PORTS.QUERY_PORT), "/query/service", "POST", body, true ); await this.checkResponse(response, `Could not poll primary index state for bucket: ${bucket.getName()}`); return response; } catch (_e) { return undefined; } }, async (response) => { if (response === undefined) { return false; } const jsonResponse = (await response.json()) as { results: Array<{ online: boolean }> }; return jsonResponse.results[0].online; }, () => { const message = `URL /query/service not accessible after ${timeoutMs}ms`; log.error(message, { containerId: client.container.getById(startedTestContainer.getId()).id }); throw new Error(message); }, timeoutMs ); } protected override async containerStarted( startedTestContainer: StartedTestContainer, inspectResult: InspectResult ): Promise { const deadline = this.startupTimeoutMs === undefined ? undefined : Date.now() + this.startupTimeoutMs; const remainingStartupMs = () => (deadline === undefined ? 60_000 : Math.max(deadline - Date.now(), 0)); const client = await getContainerRuntimeClient(); await this.waitUntilNodeIsOnline(client, inspectResult, startedTestContainer, remainingStartupMs()); await this.initializeIsEnterprise(startedTestContainer); await this.renameNode(startedTestContainer, inspectResult); await this.initializeServices(startedTestContainer); await this.setMemoryQuotas(startedTestContainer); await this.configureAdminUser(startedTestContainer); await this.configureExternalPorts(startedTestContainer); if (this.enabledServices.has(CouchbaseService.INDEX)) { await this.configureIndexer(startedTestContainer); } await Promise.all( this.constructWaitStrategies().map((s) => s .withStartupTimeout(remainingStartupMs()) .waitUntilReady( client.container.getById(startedTestContainer.getId()), BoundPorts.fromInspectResult(client.info.containerRuntime.hostIps, inspectResult) ) ) ); if (this.enabledServices.has(CouchbaseService.KV)) { await this.waitUntilKvAcceptsCredentials(startedTestContainer, remainingStartupMs()); } await this.createBuckets(client, inspectResult, startedTestContainer, remainingStartupMs); log.info( `Couchbase container is ready! UI available at http://${startedTestContainer.getHost()}:${startedTestContainer.getMappedPort( PORTS.MGMT_PORT )}` ); } private async waitUntilKvAcceptsCredentials(container: StartedTestContainer, timeoutMs: number) { await new IntervalRetry(100).retryUntil( async () => { const { exitCode } = await container.exec( ["/opt/couchbase/bin/mcstat", "--host", `127.0.0.1:${PORTS.KV_PORT}`, "--user", this.username, "connections"], { env: { CB_PASSWORD: this.password } } ); return exitCode; }, (exitCode) => exitCode === 0, () => { const message = `KV service did not accept credentials after ${timeoutMs}ms`; log.error(message, { containerId: container.getId() }); throw new Error(message); }, timeoutMs ); } public override async start(): Promise { this.withExposedPorts(...this.getPortsToExpose()); return new StartedCouchbaseContainer(await super.start(), this.username, this.password); } } export class StartedCouchbaseContainer extends AbstractStartedContainer { private readonly host: string; private readonly username: string; private readonly password: string; constructor(startedTestContainer: StartedTestContainer, username: string, password: string) { super(startedTestContainer); this.host = startedTestContainer.getHost(); this.username = username; this.password = password; } private getBootstrapCarrierDirectPort() { return this.getMappedPort(PORTS.KV_PORT); } getUsername() { return this.username; } getPassword() { return this.password; } getConnectionString() { return `couchbase://${this.host}:${this.getBootstrapCarrierDirectPort()}`; } }