Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove explicit port binding in Azurite container #876

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions packages/modules/azurite/src/azurite-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
});
// }
});
64 changes: 40 additions & 24 deletions packages/modules/azurite/src/azurite-container.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -53,26 +61,26 @@ 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;
}

/**
* 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;
}

/**
* 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;
}

Expand Down Expand Up @@ -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({
Expand All @@ -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);
}
Expand All @@ -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);
}

/**
Expand Down
Loading