diff --git a/packages/modules/azurite/src/azurite-container.test.ts b/packages/modules/azurite/src/azurite-container.test.ts index a25bae083..1d118a536 100644 --- a/packages/modules/azurite/src/azurite-container.test.ts +++ b/packages/modules/azurite/src/azurite-container.test.ts @@ -119,9 +119,9 @@ describe("Azurite", () => { const queuePort = 14000; const tablePort = 15000; const container = await new AzuriteContainer() - .withBlobPort(blobPort) - .withQueuePort(queuePort) - .withTablePort(tablePort) + .withBlobPort({ container: 10001, host: blobPort }) + .withQueuePort({ container: 10002, host: queuePort }) + .withTablePort({ container: 10003, host: tablePort }) .start(); expect(container.getBlobPort()).toBe(blobPort); @@ -144,25 +144,34 @@ describe("Azurite", () => { // inMemoryPersistence { it("should be able to use in-memory persistence", async () => { const container = await new AzuriteContainer().withInMemoryPersistence().start(); - - const connectionString = container.getConnectionString(); - expect(connectionString).toBeTruthy(); - - const serviceClient = BlobServiceClient.fromConnectionString(connectionString); - const containerClient = serviceClient.getContainerClient("test"); - await containerClient.createIfNotExists(); const blobName = "hello.txt"; - const content = "Hello world!"; - await containerClient.uploadBlockBlob(blobName, content, Buffer.byteLength(content)); - const blobClient = containerClient.getBlockBlobClient(blobName); - const blobExists = await blobClient.exists(); - expect(blobExists).toBeTruthy(); + { + const connectionString = container.getConnectionString(); + expect(connectionString).toBeTruthy(); + + const serviceClient = BlobServiceClient.fromConnectionString(connectionString); + const containerClient = serviceClient.getContainerClient("test"); + await containerClient.createIfNotExists(); + const content = "Hello world!"; + await containerClient.uploadBlockBlob(blobName, content, Buffer.byteLength(content)); + const blobClient = containerClient.getBlockBlobClient(blobName); + const blobExists = await blobClient.exists(); + expect(blobExists).toBeTruthy(); + } await container.restart(); - const blobExistsAfterRestart = await blobClient.exists(); - expect(blobExistsAfterRestart).toBeFalsy(); + { + const connectionString = container.getConnectionString(); + expect(connectionString).toBeTruthy(); + + const serviceClient = BlobServiceClient.fromConnectionString(connectionString); + const containerClient = serviceClient.getContainerClient("test"); + const blobClient = containerClient.getBlockBlobClient(blobName); + const blobExistsAfterRestart = await blobClient.exists(); + expect(blobExistsAfterRestart).toBeFalsy(); + } }); // } }); diff --git a/packages/modules/azurite/src/azurite-container.ts b/packages/modules/azurite/src/azurite-container.ts index 1af1b289c..aae6a0338 100755 --- a/packages/modules/azurite/src/azurite-container.ts +++ b/packages/modules/azurite/src/azurite-container.ts @@ -1,4 +1,11 @@ -import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers"; +import { + AbstractStartedContainer, + GenericContainer, + hasHostBinding, + PortWithOptionalBinding, + StartedTestContainer, + Wait, +} from "testcontainers"; const AZURITE_IMAGE = "mcr.microsoft.com/azure-storage/azurite:3.33.0"; const BLOB_PORT = 10000; @@ -21,9 +28,10 @@ export class AzuriteContainer extends GenericContainer { .withStartupTimeout(120_000); } - private blobPort: number = BLOB_PORT; - private queuePort: number = QUEUE_PORT; - private tablePort: number = TABLE_PORT; + private blobPort: PortWithOptionalBinding = BLOB_PORT; + private queuePort: PortWithOptionalBinding = QUEUE_PORT; + private tablePort: PortWithOptionalBinding = TABLE_PORT; + private accountName: string = DEFAULT_ACCOUNT_NAME; private accountKey: string = DEFAULT_ACCOUNT_KEY; @@ -53,8 +61,8 @@ export class AzuriteContainer extends GenericContainer { * Sets the port to expose the Blob service on. * @param port The port to expose the Blob service on. Default is 10000. */ - public withBlobPort(port: number): this { - this.blobPort = port; + public withBlobPort(blobPort: PortWithOptionalBinding): this { + this.blobPort = blobPort; return this; } @@ -62,8 +70,8 @@ export class AzuriteContainer extends GenericContainer { * Sets the port to expose the Queue service on. * @param port The port to expose the Queue service on. Default is 10001. */ - public withQueuePort(port: number): this { - this.queuePort = port; + public withQueuePort(queuePort: PortWithOptionalBinding): this { + this.queuePort = queuePort; return this; } @@ -71,8 +79,8 @@ export class AzuriteContainer extends GenericContainer { * Sets the port to expose the Table service on. * @param port The port to expose the Table service on. Default is 10002. */ - public withTablePort(port: number): this { - this.tablePort = port; + public withTablePort(tablePort: PortWithOptionalBinding): this { + this.tablePort = tablePort; return this; } @@ -119,11 +127,7 @@ export class AzuriteContainer extends GenericContainer { command.push("--skipApiVersionCheck"); } - this.withCommand(command).withExposedPorts( - { container: BLOB_PORT, host: this.blobPort }, - { container: QUEUE_PORT, host: this.queuePort }, - { container: TABLE_PORT, host: this.tablePort } - ); + this.withCommand(command).withExposedPorts(this.blobPort, this.queuePort, this.tablePort); if (this.accountName !== DEFAULT_ACCOUNT_NAME || this.accountKey !== DEFAULT_ACCOUNT_KEY) { this.withEnvironment({ @@ -149,9 +153,9 @@ export class StartedAzuriteContainer extends AbstractStartedContainer { startedTestContainer: StartedTestContainer, private readonly accountName: string, private readonly accountKey: string, - private readonly blobPort: number, - private readonly queuePort: number, - private readonly tablePort: number + private readonly blobPort: PortWithOptionalBinding, + private readonly queuePort: PortWithOptionalBinding, + private readonly tablePort: PortWithOptionalBinding ) { super(startedTestContainer); } @@ -165,27 +169,39 @@ export class StartedAzuriteContainer extends AbstractStartedContainer { } public getBlobPort(): number { - return this.blobPort; + if (hasHostBinding(this.blobPort)) { + return this.blobPort.host; + } else { + return this.getMappedPort(this.blobPort); + } } public getQueuePort(): number { - return this.queuePort; + if (hasHostBinding(this.queuePort)) { + return this.queuePort.host; + } else { + return this.getMappedPort(this.queuePort); + } } public getTablePort(): number { - return this.tablePort; + if (hasHostBinding(this.tablePort)) { + return this.tablePort.host; + } else { + return this.getMappedPort(this.tablePort); + } } public getBlobEndpoint(): string { - return this.getEndpoint(this.blobPort, this.accountName); + return this.getEndpoint(this.getBlobPort(), this.accountName); } public getQueueEndpoint(): string { - return this.getEndpoint(this.queuePort, this.accountName); + return this.getEndpoint(this.getQueuePort(), this.accountName); } public getTableEndpoint(): string { - return this.getEndpoint(this.tablePort, this.accountName); + return this.getEndpoint(this.getTablePort(), this.accountName); } /**