-
-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathazurite-container.ts
More file actions
executable file
·225 lines (193 loc) · 7.14 KB
/
azurite-container.ts
File metadata and controls
executable file
·225 lines (193 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import {
AbstractStartedContainer,
GenericContainer,
getContainerPort,
hasHostBinding,
PortWithOptionalBinding,
StartedTestContainer,
Wait,
} from "testcontainers";
const BLOB_PORT = 10000;
const QUEUE_PORT = 10001;
const TABLE_PORT = 10002;
const DEFAULT_ACCOUNT_NAME = "devstoreaccount1";
const DEFAULT_ACCOUNT_KEY = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
export class AzuriteContainer extends GenericContainer {
constructor(image: string) {
super(image);
this.withEntrypoint(["azurite"])
.withWaitStrategy(
Wait.forAll([
Wait.forLogMessage(/.*Blob service is successfully listening.*/),
Wait.forLogMessage(/.*Queue service is successfully listening.*/),
Wait.forLogMessage(/.*Table service is successfully listening.*/),
])
)
.withStartupTimeout(120_000);
}
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;
private skipApiVersionCheck = false;
private inMemoryPersistence = false;
private extentMemoryLimitInMegaBytes?: number = undefined;
/**
* Sets a custom storage account name (default account will be disabled).
* @param accountName Storage account names must be between 3 and 24 characters in length and may contain numbers and lowercase letters only.
*/
public withAccountName(accountName: string): this {
this.accountName = accountName;
return this;
}
/**
* Sets a custom storage account key (default account will be disabled). Note: MUST be a base64-encoded string.
* @param accountKey The account keys must be base64 encoded string.
*/
public withAccountKey(accountKey: string): this {
this.accountKey = accountKey;
return this;
}
/**
* Sets the port to expose the Blob service on.
* @param port The port to expose the Blob service on. Default is 10000.
*/
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(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(tablePort: PortWithOptionalBinding): this {
this.tablePort = tablePort;
return this;
}
/**
* Disable persisting any data to disk and only store data in-memory. If the Azurite process is terminated, all data is lost.
*/
public withInMemoryPersistence(): this {
this.inMemoryPersistence = true;
return this;
}
/**
* By default, the in-memory extent store (for blob and queue content) is limited to 50% of the total memory on the host machine. This is evaluated to using os.totalmem(). This limit can be overridden using the extentMemoryLimit <megabytes> option. This can only be used if in-memory persistence is enabled first.
* @param megabyte The extent memory limit in megabytes.
*/
public withExtentMemoryLimitInMegaBytes(megabyte: number): this {
if (!this.inMemoryPersistence) {
throw new Error("Extent memory limit can only be set when using in-memory persistence");
}
this.extentMemoryLimitInMegaBytes = megabyte;
return this;
}
/**
* By default Azurite will check the request API version is valid API version. This can be disabled by with this option.
*/
public withSkipApiVersionCheck(): this {
this.skipApiVersionCheck = true;
return this;
}
public override async start(): Promise<StartedAzuriteContainer> {
const command = ["--blobHost", "0.0.0.0", "--queueHost", "0.0.0.0", "--tableHost", "0.0.0.0"];
if (this.inMemoryPersistence) {
command.push("--inMemoryPersistence");
if (this.extentMemoryLimitInMegaBytes) {
command.push("--extentMemoryLimit", this.extentMemoryLimitInMegaBytes.toString());
}
}
if (this.skipApiVersionCheck) {
command.push("--skipApiVersionCheck");
}
this.withCommand(command).withExposedPorts(this.blobPort, this.queuePort, this.tablePort);
if (this.accountName !== DEFAULT_ACCOUNT_NAME || this.accountKey !== DEFAULT_ACCOUNT_KEY) {
this.withEnvironment({
AZURITE_ACCOUNTS: `${this.accountName}:${this.accountKey}`,
});
}
const startedContainer = await super.start();
return new StartedAzuriteContainer(
startedContainer,
this.accountName,
this.accountKey,
this.blobPort,
this.queuePort,
this.tablePort
);
}
}
export class StartedAzuriteContainer extends AbstractStartedContainer {
constructor(
startedTestContainer: StartedTestContainer,
private readonly accountName: string,
private readonly accountKey: string,
private readonly blobPort: PortWithOptionalBinding,
private readonly queuePort: PortWithOptionalBinding,
private readonly tablePort: PortWithOptionalBinding
) {
super(startedTestContainer);
}
public getAccountName(): string {
return this.accountName;
}
public getAccountKey(): string {
return this.accountKey;
}
public getBlobPort(): number {
if (hasHostBinding(this.blobPort)) {
return this.blobPort.host;
} else {
const containerPort = getContainerPort(this.blobPort);
return this.getMappedPort(containerPort);
}
}
public getQueuePort(): number {
if (hasHostBinding(this.queuePort)) {
return this.queuePort.host;
} else {
const containerPort = getContainerPort(this.queuePort);
return this.getMappedPort(containerPort);
}
}
public getTablePort(): number {
if (hasHostBinding(this.tablePort)) {
return this.tablePort.host;
} else {
const containerPort = getContainerPort(this.tablePort);
return this.getMappedPort(containerPort);
}
}
public getBlobEndpoint(): string {
return this.getEndpoint(this.getBlobPort(), this.accountName);
}
public getQueueEndpoint(): string {
return this.getEndpoint(this.getQueuePort(), this.accountName);
}
public getTableEndpoint(): string {
return this.getEndpoint(this.getTablePort(), this.accountName);
}
/**
* @returns A connection string in the form of `DefaultEndpointsProtocol=[protocol];AccountName=[accountName];AccountKey=[accountKey];BlobEndpoint=[blobEndpoint];QueueEndpoint=[queueEndpoint];TableEndpoint=[tableEndpoint];`
*/
public getConnectionString(): string {
return `DefaultEndpointsProtocol=http;AccountName=${this.accountName};AccountKey=${
this.accountKey
};BlobEndpoint=${this.getBlobEndpoint()};QueueEndpoint=${this.getQueueEndpoint()};TableEndpoint=${this.getTableEndpoint()};`;
}
private getEndpoint(port: number, containerName: string): string {
const url = new URL(`http://${this.getHost()}`);
url.port = port.toString();
url.pathname = containerName;
return url.toString();
}
}