-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
generic-container.ts
367 lines (313 loc) · 11.7 KB
/
generic-container.ts
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import archiver from "archiver";
import { BoundPorts } from "../bound-ports";
import { containerLog, log } from "../logger";
import { PortWithOptionalBinding } from "../port";
import { HostPortCheck, InternalPortCheck } from "../port-check";
import { DefaultPullPolicy, PullPolicy } from "../pull-policy";
import { ReaperInstance } from "../reaper";
import { DockerImageName } from "../docker-image-name";
import { StartedTestContainer, TestContainer } from "../test-container";
import { HostPortWaitStrategy, WaitStrategy } from "../wait-strategy";
import { Readable } from "stream";
import { PortForwarderInstance } from "../port-forwarder";
import { getAuthConfig } from "../registry-auth-locator";
import {
BindMode,
BindMount,
BuildContext,
Command,
ContainerName,
Dir,
Env,
EnvKey,
EnvValue,
ExtraHost,
HealthCheck,
Host,
NetworkMode,
TmpFs,
Labels,
} from "../docker/types";
import { pullImage } from "../docker/functions/image/pull-image";
import { createContainer, CreateContainerOptions } from "../docker/functions/container/create-container";
import { connectNetwork } from "../docker/functions/network/connect-network";
import { dockerClient } from "../docker/docker-client";
import { inspectContainer, InspectResult } from "../docker/functions/container/inspect-container";
import Dockerode from "dockerode";
import { startContainer } from "../docker/functions/container/start-container";
import { containerLogs } from "../docker/functions/container/container-logs";
import { stopContainer } from "../docker/functions/container/stop-container";
import { removeContainer } from "../docker/functions/container/remove-container";
import { putContainerArchive } from "../docker/functions/container/put-container-archive";
import { GenericContainerBuilder } from "./generic-container-builder";
import { StartedGenericContainer } from "./started-generic-container";
import { hash } from "../hash";
import { getContainerByHash } from "../docker/functions/container/get-container";
import { LABEL_CONTAINER_HASH } from "../labels";
export class GenericContainer implements TestContainer {
public static fromDockerfile(context: BuildContext, dockerfileName = "Dockerfile"): GenericContainerBuilder {
return new GenericContainerBuilder(context, dockerfileName);
}
private readonly imageName: DockerImageName;
protected env: Env = {};
protected networkMode?: NetworkMode;
protected networkAliases: string[] = [];
protected ports: PortWithOptionalBinding[] = [];
protected cmd: Command[] = [];
protected bindMounts: BindMount[] = [];
protected name?: ContainerName;
protected labels: Labels = {};
protected tmpFs: TmpFs = {};
protected healthCheck?: HealthCheck;
protected waitStrategy?: WaitStrategy;
protected startupTimeout = 60_000;
protected useDefaultLogDriver = false;
protected privilegedMode = false;
protected ipcMode?: string;
protected user?: string;
protected pullPolicy: PullPolicy = new DefaultPullPolicy();
protected reuse = false;
protected tarToCopy?: archiver.Archiver;
private extraHosts: ExtraHost[] = [];
constructor(readonly image: string) {
this.imageName = DockerImageName.fromString(image);
}
protected preStart?(): Promise<void>;
public async start(): Promise<StartedTestContainer> {
await pullImage((await dockerClient()).dockerode, {
imageName: this.imageName,
force: this.pullPolicy.shouldPull(),
authConfig: await getAuthConfig(this.imageName.registry),
});
if (!this.imageName.isReaper()) {
await ReaperInstance.getInstance();
}
if (this.preStart) {
await this.preStart();
}
if (!this.imageName.isHelperContainer() && PortForwarderInstance.isRunning()) {
const portForwarder = await PortForwarderInstance.getInstance();
this.extraHosts.push({ host: "host.testcontainers.internal", ipAddress: portForwarder.getIpAddress() });
}
const createContainerOptions: CreateContainerOptions = {
imageName: this.imageName,
env: this.env,
cmd: this.cmd,
bindMounts: this.bindMounts,
tmpFs: this.tmpFs,
exposedPorts: this.ports,
name: this.name,
labels: this.labels,
reusable: this.reuse,
networkMode: this.networkAliases.length > 0 ? undefined : this.networkMode,
healthCheck: this.healthCheck,
useDefaultLogDriver: this.useDefaultLogDriver,
privilegedMode: this.privilegedMode,
autoRemove: this.imageName.isReaper(),
extraHosts: this.extraHosts,
ipcMode: this.ipcMode,
user: this.user,
};
if (this.reuse) {
const containerHash = hash(JSON.stringify(createContainerOptions));
createContainerOptions.labels = { [LABEL_CONTAINER_HASH]: containerHash };
log.debug(`Container reuse has been enabled, hash: ${containerHash}`);
const container = await getContainerByHash(containerHash);
if (container !== undefined) {
log.debug(`Found container to reuse with hash: ${containerHash}`);
return this.reuseContainer(container);
} else {
log.debug("No container found to reuse");
return this.startContainer(createContainerOptions);
}
} else {
return this.startContainer(createContainerOptions);
}
}
private async reuseContainer(startedContainer: Dockerode.Container) {
const inspectResult = await inspectContainer(startedContainer);
const boundPorts = BoundPorts.fromInspectResult(inspectResult).filter(this.ports);
await this.waitForContainer(startedContainer, boundPorts);
return new StartedGenericContainer(
startedContainer,
(await dockerClient()).host,
inspectResult,
boundPorts,
inspectResult.name,
this.getWaitStrategy((await dockerClient()).host, startedContainer).withStartupTimeout(this.startupTimeout)
);
}
private async startContainer(createContainerOptions: CreateContainerOptions): Promise<StartedTestContainer> {
const container = await createContainer(createContainerOptions);
if (!this.imageName.isHelperContainer() && PortForwarderInstance.isRunning()) {
const portForwarder = await PortForwarderInstance.getInstance();
const portForwarderNetworkId = portForwarder.getNetworkId();
const excludedNetworks = [portForwarderNetworkId, "none", "host"];
if (!this.networkMode || !excludedNetworks.includes(this.networkMode)) {
await connectNetwork({
containerId: container.id,
networkId: portForwarderNetworkId,
networkAliases: [],
});
}
}
if (this.networkMode && this.networkAliases.length > 0) {
await connectNetwork({
containerId: container.id,
networkId: this.networkMode,
networkAliases: this.networkAliases,
});
}
if (this.tarToCopy) {
this.tarToCopy.finalize();
await putContainerArchive({ container, stream: this.tarToCopy, containerPath: "/" });
}
log.info(`Starting container ${this.imageName} with ID: ${container.id}`);
await startContainer(container);
(await containerLogs(container))
.on("data", (data) => containerLog.trace(`${container.id}: ${data.trim()}`))
.on("err", (data) => containerLog.error(`${container.id}: ${data.trim()}`));
const inspectResult = await inspectContainer(container);
const boundPorts = BoundPorts.fromInspectResult(inspectResult).filter(this.ports);
await this.waitForContainer(container, boundPorts);
const startedContainer = new StartedGenericContainer(
container,
(await dockerClient()).host,
inspectResult,
boundPorts,
inspectResult.name,
this.getWaitStrategy((await dockerClient()).host, container).withStartupTimeout(this.startupTimeout)
);
if (this.postStart) {
await this.postStart(startedContainer, inspectResult, boundPorts);
}
return startedContainer;
}
protected postStart?(
container: StartedTestContainer,
inspectResult: InspectResult,
boundPorts: BoundPorts
): Promise<void>;
protected get hasExposedPorts(): boolean {
return this.ports.length !== 0;
}
public withCmd(cmd: Command[]): this {
this.cmd = cmd;
return this;
}
public withName(name: ContainerName): this {
this.name = name;
return this;
}
public withLabels(labels: Labels): this {
this.labels = { ...labels };
return this;
}
public withEnv(key: EnvKey, value: EnvValue): this {
this.env[key] = value;
return this;
}
public withTmpFs(tmpFs: TmpFs): this {
this.tmpFs = tmpFs;
return this;
}
public withNetworkMode(networkMode: NetworkMode): this {
this.networkMode = networkMode;
return this;
}
public withNetworkAliases(...networkAliases: string[]): this {
this.networkAliases = networkAliases;
return this;
}
public withExtraHosts(...extraHosts: ExtraHost[]): this {
this.extraHosts.push(...extraHosts);
return this;
}
public withExposedPorts(...ports: PortWithOptionalBinding[]): this {
this.ports = ports;
return this;
}
protected addExposedPorts(...ports: PortWithOptionalBinding[]): this {
this.ports.push(...ports);
return this;
}
public withBindMount(source: Dir, target: Dir, bindMode: BindMode = "rw"): this {
this.bindMounts.push({ source, target, bindMode });
return this;
}
public withHealthCheck(healthCheck: HealthCheck): this {
this.healthCheck = healthCheck;
return this;
}
public withStartupTimeout(startupTimeout: number): this {
this.startupTimeout = startupTimeout;
return this;
}
public withWaitStrategy(waitStrategy: WaitStrategy): this {
this.waitStrategy = waitStrategy;
return this;
}
public withDefaultLogDriver(): this {
this.useDefaultLogDriver = true;
return this;
}
public withPrivilegedMode(): this {
this.privilegedMode = true;
return this;
}
public withUser(user: string): this {
this.user = user;
return this;
}
public withReuse(): this {
this.reuse = true;
return this;
}
public withPullPolicy(pullPolicy: PullPolicy): this {
this.pullPolicy = pullPolicy;
return this;
}
public withIpcMode(ipcMode: string): this {
this.ipcMode = ipcMode;
return this;
}
public withCopyFileToContainer(sourcePath: string, containerPath: string): this {
this.getTarToCopy().file(sourcePath, { name: containerPath });
return this;
}
public withCopyContentToContainer(content: string | Buffer | Readable, containerPath: string): this {
this.getTarToCopy().append(content, { name: containerPath });
return this;
}
protected getTarToCopy(): archiver.Archiver {
if (!this.tarToCopy) {
this.tarToCopy = archiver("tar");
}
return this.tarToCopy;
}
private async waitForContainer(container: Dockerode.Container, boundPorts: BoundPorts): Promise<void> {
log.debug(`Waiting for container to be ready: ${container.id}`);
const waitStrategy = this.getWaitStrategy((await dockerClient()).host, container);
try {
await waitStrategy.withStartupTimeout(this.startupTimeout).waitUntilReady(container, boundPorts);
log.info("Container is ready");
} catch (err) {
log.error(`Container failed to be ready: ${err}`);
try {
await stopContainer(container, { timeout: 0 });
await removeContainer(container, { removeVolumes: true });
} catch (stopErr) {
log.error(`Failed to stop container after it failed to be ready: ${stopErr}`);
}
throw err;
}
}
private getWaitStrategy(host: Host, container: Dockerode.Container): WaitStrategy {
if (this.waitStrategy) {
return this.waitStrategy;
}
const hostPortCheck = new HostPortCheck(host);
const internalPortCheck = new InternalPortCheck(container);
return new HostPortWaitStrategy(hostPortCheck, internalPortCheck);
}
}