|
| 1 | +import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers"; |
| 2 | + |
| 3 | +const COUCHDB_PORT = 5984; |
| 4 | + |
| 5 | +export class CouchDBContainer extends GenericContainer { |
| 6 | + private username: string = "root"; |
| 7 | + private password: string = "root"; |
| 8 | + |
| 9 | + constructor(image: string) { |
| 10 | + super(image); |
| 11 | + this.withExposedPorts(COUCHDB_PORT) |
| 12 | + .withStartupTimeout(120_000) |
| 13 | + .withWaitStrategy(Wait.forHttp("/_up", COUCHDB_PORT).forStatusCode(200).withStartupTimeout(60_000)); |
| 14 | + } |
| 15 | + |
| 16 | + public withUsername(username: string): this { |
| 17 | + this.username = username; |
| 18 | + return this; |
| 19 | + } |
| 20 | + |
| 21 | + public withPassword(password: string): this { |
| 22 | + this.password = password; |
| 23 | + return this; |
| 24 | + } |
| 25 | + |
| 26 | + public override async start(): Promise<StartedCouchDBContainer> { |
| 27 | + this.withEnvironment({ |
| 28 | + COUCHDB_USER: this.username, |
| 29 | + COUCHDB_PASSWORD: this.password, |
| 30 | + }); |
| 31 | + return new StartedCouchDBContainer(await super.start(), this.username, this.password); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +export class StartedCouchDBContainer extends AbstractStartedContainer { |
| 36 | + constructor( |
| 37 | + startedTestContainer: StartedTestContainer, |
| 38 | + private readonly username: string, |
| 39 | + private readonly password: string |
| 40 | + ) { |
| 41 | + super(startedTestContainer); |
| 42 | + } |
| 43 | + |
| 44 | + public getPort(): number { |
| 45 | + return this.getMappedPort(COUCHDB_PORT); |
| 46 | + } |
| 47 | + |
| 48 | + public getUsername(): string { |
| 49 | + return this.username; |
| 50 | + } |
| 51 | + |
| 52 | + public getPassword(): string { |
| 53 | + return this.password; |
| 54 | + } |
| 55 | + |
| 56 | + public getUrl(): string { |
| 57 | + return `http://${this.username}:${this.password}@${this.getHost()}:${this.getPort()}`; |
| 58 | + } |
| 59 | +} |
0 commit comments