|
| 1 | +import { AbstractStartedContainer, GenericContainer, type StartedTestContainer, Wait } from "testcontainers"; |
| 2 | + |
| 3 | +const S3_HTTP_PORT = 9090; |
| 4 | +const S3_HTTPS_PORT = 3903; |
| 5 | +// https://github.com/adobe/S3Mock/issues/1250#issuecomment-1653387576 |
| 6 | +const ACCESS_KEY_ID = "any-access-key-id"; |
| 7 | +const SECRET_ACCESS_KEY = "any-secret-access-key"; |
| 8 | +const REGION = "auto"; |
| 9 | + |
| 10 | +export class S3MockContainer extends GenericContainer { |
| 11 | + #accessKeyId: string = ACCESS_KEY_ID; |
| 12 | + #secretAccessKey: string = SECRET_ACCESS_KEY; |
| 13 | + |
| 14 | + constructor(image: string) { |
| 15 | + super(image); |
| 16 | + this.withExposedPorts(S3_HTTP_PORT, S3_HTTPS_PORT); |
| 17 | + this.withEnvironment({ |
| 18 | + COM_ADOBE_TESTING_S3MOCK_STORE_REGION: REGION, |
| 19 | + }); |
| 20 | + |
| 21 | + this.withWaitStrategy(Wait.forHttp("/favicon.ico", S3_HTTP_PORT)); |
| 22 | + } |
| 23 | + |
| 24 | + withAccessKeyId(accessKeyId: string): this { |
| 25 | + this.#accessKeyId = accessKeyId; |
| 26 | + return this; |
| 27 | + } |
| 28 | + withSecretAccessKey(secretAccessKey: string): this { |
| 29 | + this.#secretAccessKey = secretAccessKey; |
| 30 | + return this; |
| 31 | + } |
| 32 | + |
| 33 | + override async start(): Promise<StartedS3MockContainer> { |
| 34 | + return new StartedS3MockContainer( |
| 35 | + await super.start(), |
| 36 | + this.#accessKeyId, |
| 37 | + this.#secretAccessKey, |
| 38 | + S3_HTTP_PORT, |
| 39 | + S3_HTTPS_PORT |
| 40 | + ); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export class StartedS3MockContainer extends AbstractStartedContainer { |
| 45 | + readonly #accessKeyId: string; |
| 46 | + readonly #secretAccessKey: string; |
| 47 | + readonly #s3HttpPort: number; |
| 48 | + readonly #s3HttpsPort: number; |
| 49 | + |
| 50 | + constructor( |
| 51 | + startedContainer: StartedTestContainer, |
| 52 | + accessKeyId: string, |
| 53 | + secretAccessKey: string, |
| 54 | + s3HttpPort: number, |
| 55 | + s3HttpsPort: number |
| 56 | + ) { |
| 57 | + super(startedContainer); |
| 58 | + this.#accessKeyId = accessKeyId; |
| 59 | + this.#secretAccessKey = secretAccessKey; |
| 60 | + this.#s3HttpPort = s3HttpPort; |
| 61 | + this.#s3HttpsPort = s3HttpsPort; |
| 62 | + } |
| 63 | + |
| 64 | + getHttpPort() { |
| 65 | + return this.startedTestContainer.getMappedPort(this.#s3HttpPort); |
| 66 | + } |
| 67 | + getHttpsPort() { |
| 68 | + return this.startedTestContainer.getMappedPort(this.#s3HttpsPort); |
| 69 | + } |
| 70 | + getAccessKeyId(): string { |
| 71 | + return this.#accessKeyId; |
| 72 | + } |
| 73 | + getSecretAccessKey(): string { |
| 74 | + return this.#secretAccessKey; |
| 75 | + } |
| 76 | + |
| 77 | + getHttpConnectionUrl() { |
| 78 | + return `http://${this.getHost()}:${this.getHttpPort()}`; |
| 79 | + } |
| 80 | + getHttpsConnectionUrl() { |
| 81 | + return `https://${this.getHost()}:${this.getHttpsPort()}`; |
| 82 | + } |
| 83 | +} |
0 commit comments