Skip to content

Commit 1be6052

Browse files
authored
Remove explicit port binding in Azurite container (#876)
1 parent 3cf1da3 commit 1be6052

File tree

2 files changed

+66
-41
lines changed

2 files changed

+66
-41
lines changed

packages/modules/azurite/src/azurite-container.test.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ describe("Azurite", () => {
119119
const queuePort = 14000;
120120
const tablePort = 15000;
121121
const container = await new AzuriteContainer()
122-
.withBlobPort(blobPort)
123-
.withQueuePort(queuePort)
124-
.withTablePort(tablePort)
122+
.withBlobPort({ container: 10001, host: blobPort })
123+
.withQueuePort({ container: 10002, host: queuePort })
124+
.withTablePort({ container: 10003, host: tablePort })
125125
.start();
126126

127127
expect(container.getBlobPort()).toBe(blobPort);
@@ -144,25 +144,34 @@ describe("Azurite", () => {
144144
// inMemoryPersistence {
145145
it("should be able to use in-memory persistence", async () => {
146146
const container = await new AzuriteContainer().withInMemoryPersistence().start();
147-
148-
const connectionString = container.getConnectionString();
149-
expect(connectionString).toBeTruthy();
150-
151-
const serviceClient = BlobServiceClient.fromConnectionString(connectionString);
152-
const containerClient = serviceClient.getContainerClient("test");
153-
await containerClient.createIfNotExists();
154147
const blobName = "hello.txt";
155-
const content = "Hello world!";
156-
await containerClient.uploadBlockBlob(blobName, content, Buffer.byteLength(content));
157148

158-
const blobClient = containerClient.getBlockBlobClient(blobName);
159-
const blobExists = await blobClient.exists();
160-
expect(blobExists).toBeTruthy();
149+
{
150+
const connectionString = container.getConnectionString();
151+
expect(connectionString).toBeTruthy();
152+
153+
const serviceClient = BlobServiceClient.fromConnectionString(connectionString);
154+
const containerClient = serviceClient.getContainerClient("test");
155+
await containerClient.createIfNotExists();
156+
const content = "Hello world!";
157+
await containerClient.uploadBlockBlob(blobName, content, Buffer.byteLength(content));
158+
const blobClient = containerClient.getBlockBlobClient(blobName);
159+
const blobExists = await blobClient.exists();
160+
expect(blobExists).toBeTruthy();
161+
}
161162

162163
await container.restart();
163164

164-
const blobExistsAfterRestart = await blobClient.exists();
165-
expect(blobExistsAfterRestart).toBeFalsy();
165+
{
166+
const connectionString = container.getConnectionString();
167+
expect(connectionString).toBeTruthy();
168+
169+
const serviceClient = BlobServiceClient.fromConnectionString(connectionString);
170+
const containerClient = serviceClient.getContainerClient("test");
171+
const blobClient = containerClient.getBlockBlobClient(blobName);
172+
const blobExistsAfterRestart = await blobClient.exists();
173+
expect(blobExistsAfterRestart).toBeFalsy();
174+
}
166175
});
167176
// }
168177
});

packages/modules/azurite/src/azurite-container.ts

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers";
1+
import {
2+
AbstractStartedContainer,
3+
GenericContainer,
4+
hasHostBinding,
5+
PortWithOptionalBinding,
6+
StartedTestContainer,
7+
Wait,
8+
} from "testcontainers";
29

310
const AZURITE_IMAGE = "mcr.microsoft.com/azure-storage/azurite:3.33.0";
411
const BLOB_PORT = 10000;
@@ -21,9 +28,10 @@ export class AzuriteContainer extends GenericContainer {
2128
.withStartupTimeout(120_000);
2229
}
2330

24-
private blobPort: number = BLOB_PORT;
25-
private queuePort: number = QUEUE_PORT;
26-
private tablePort: number = TABLE_PORT;
31+
private blobPort: PortWithOptionalBinding = BLOB_PORT;
32+
private queuePort: PortWithOptionalBinding = QUEUE_PORT;
33+
private tablePort: PortWithOptionalBinding = TABLE_PORT;
34+
2735
private accountName: string = DEFAULT_ACCOUNT_NAME;
2836
private accountKey: string = DEFAULT_ACCOUNT_KEY;
2937

@@ -53,26 +61,26 @@ export class AzuriteContainer extends GenericContainer {
5361
* Sets the port to expose the Blob service on.
5462
* @param port The port to expose the Blob service on. Default is 10000.
5563
*/
56-
public withBlobPort(port: number): this {
57-
this.blobPort = port;
64+
public withBlobPort(blobPort: PortWithOptionalBinding): this {
65+
this.blobPort = blobPort;
5866
return this;
5967
}
6068

6169
/**
6270
* Sets the port to expose the Queue service on.
6371
* @param port The port to expose the Queue service on. Default is 10001.
6472
*/
65-
public withQueuePort(port: number): this {
66-
this.queuePort = port;
73+
public withQueuePort(queuePort: PortWithOptionalBinding): this {
74+
this.queuePort = queuePort;
6775
return this;
6876
}
6977

7078
/**
7179
* Sets the port to expose the Table service on.
7280
* @param port The port to expose the Table service on. Default is 10002.
7381
*/
74-
public withTablePort(port: number): this {
75-
this.tablePort = port;
82+
public withTablePort(tablePort: PortWithOptionalBinding): this {
83+
this.tablePort = tablePort;
7684
return this;
7785
}
7886

@@ -119,11 +127,7 @@ export class AzuriteContainer extends GenericContainer {
119127
command.push("--skipApiVersionCheck");
120128
}
121129

122-
this.withCommand(command).withExposedPorts(
123-
{ container: BLOB_PORT, host: this.blobPort },
124-
{ container: QUEUE_PORT, host: this.queuePort },
125-
{ container: TABLE_PORT, host: this.tablePort }
126-
);
130+
this.withCommand(command).withExposedPorts(this.blobPort, this.queuePort, this.tablePort);
127131

128132
if (this.accountName !== DEFAULT_ACCOUNT_NAME || this.accountKey !== DEFAULT_ACCOUNT_KEY) {
129133
this.withEnvironment({
@@ -149,9 +153,9 @@ export class StartedAzuriteContainer extends AbstractStartedContainer {
149153
startedTestContainer: StartedTestContainer,
150154
private readonly accountName: string,
151155
private readonly accountKey: string,
152-
private readonly blobPort: number,
153-
private readonly queuePort: number,
154-
private readonly tablePort: number
156+
private readonly blobPort: PortWithOptionalBinding,
157+
private readonly queuePort: PortWithOptionalBinding,
158+
private readonly tablePort: PortWithOptionalBinding
155159
) {
156160
super(startedTestContainer);
157161
}
@@ -165,27 +169,39 @@ export class StartedAzuriteContainer extends AbstractStartedContainer {
165169
}
166170

167171
public getBlobPort(): number {
168-
return this.blobPort;
172+
if (hasHostBinding(this.blobPort)) {
173+
return this.blobPort.host;
174+
} else {
175+
return this.getMappedPort(this.blobPort);
176+
}
169177
}
170178

171179
public getQueuePort(): number {
172-
return this.queuePort;
180+
if (hasHostBinding(this.queuePort)) {
181+
return this.queuePort.host;
182+
} else {
183+
return this.getMappedPort(this.queuePort);
184+
}
173185
}
174186

175187
public getTablePort(): number {
176-
return this.tablePort;
188+
if (hasHostBinding(this.tablePort)) {
189+
return this.tablePort.host;
190+
} else {
191+
return this.getMappedPort(this.tablePort);
192+
}
177193
}
178194

179195
public getBlobEndpoint(): string {
180-
return this.getEndpoint(this.blobPort, this.accountName);
196+
return this.getEndpoint(this.getBlobPort(), this.accountName);
181197
}
182198

183199
public getQueueEndpoint(): string {
184-
return this.getEndpoint(this.queuePort, this.accountName);
200+
return this.getEndpoint(this.getQueuePort(), this.accountName);
185201
}
186202

187203
public getTableEndpoint(): string {
188-
return this.getEndpoint(this.tablePort, this.accountName);
204+
return this.getEndpoint(this.getTablePort(), this.accountName);
189205
}
190206

191207
/**

0 commit comments

Comments
 (0)