-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathContainer.ts
More file actions
91 lines (73 loc) · 2 KB
/
Container.ts
File metadata and controls
91 lines (73 loc) · 2 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
import type { StartedTestContainer } from 'testcontainers';
import { GenericContainer, Wait } from 'testcontainers';
import { Client } from './Client.js';
class Container {
#imageName = 'thenativeweb/eventsourcingdb';
#imageTag = 'latest';
#internalPort = 3000;
#apiToken = 'secret';
#container: StartedTestContainer | undefined;
public withImageTag(tag: string): this {
this.#imageTag = tag;
return this;
}
public withApiToken(token: string): this {
this.#apiToken = token;
return this;
}
public withPort(port: number): this {
this.#internalPort = port;
return this;
}
public async start(): Promise<void> {
this.#container = await new GenericContainer(`${this.#imageName}:${this.#imageTag}`)
.withExposedPorts(this.#internalPort)
.withCommand([
'run',
'--api-token',
this.#apiToken,
'--data-directory-temporary',
'--http-enabled',
'--https-enabled=false',
])
.withWaitStrategy(Wait.forHttp('/api/v1/ping', this.#internalPort).withStartupTimeout(10_000))
.start();
}
public getHost(): string {
if (this.#container === undefined) {
throw new Error('Container must be running.');
}
return this.#container.getHost();
}
public getMappedPort(): number {
if (this.#container === undefined) {
throw new Error('Container must be running.');
}
return this.#container.getMappedPort(this.#internalPort);
}
public getBaseUrl(): URL {
if (this.#container === undefined) {
throw new Error('Container must be running.');
}
const host = this.getHost();
const port = this.getMappedPort();
return new URL(`http://${host}:${port}`);
}
public getApiToken(): string {
return this.#apiToken;
}
public isRunning(): boolean {
return this.#container !== undefined;
}
public async stop(): Promise<void> {
if (this.#container === undefined) {
return;
}
await this.#container.stop();
this.#container = undefined;
}
public getClient(): Client {
return new Client(this.getBaseUrl(), this.getApiToken());
}
}
export { Container };