Repository navigation
Expand file tree
/
Copy pathglobal-setup.ts
More file actions
83 lines (71 loc) · 2.52 KB
/
Copy pathglobal-setup.ts
File metadata and controls
83 lines (71 loc) · 2.52 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
/* eslint-disable no-console */
import { writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { FullConfig } from "@playwright/test";
import { GenericContainer } from "testcontainers";
import "./src/utils/configure-testcontainers.js";
import { detectRuntime, execFileAsync } from "./src/utils/container-runtime.js";
export interface HalOpInstance {
readonly containerId: string;
readonly port: number;
}
export interface DaveState {
readonly halop: HalOpInstance;
}
const STATE_FILE = join(tmpdir(), "dave-state.json");
const DEFAULT_HALOP_IMAGE = "quay.io/halconsole/hal-op:test-suite";
const DEFAULT_HALOP_PORT = 9090;
const CONTAINER_INTERNAL_PORT = 9090;
const CONTAINER_NAME = "dave_halop";
const CONTAINER_PREFIX = "dave_";
export { STATE_FILE };
async function startHalOp(image: string, port: number): Promise<HalOpInstance> {
const container = await new GenericContainer(image)
.withName(CONTAINER_NAME)
.withExposedPorts({ container: CONTAINER_INTERNAL_PORT, host: port })
.start();
return {
containerId: container.getId(),
port: container.getMappedPort(CONTAINER_INTERNAL_PORT),
};
}
async function removeStaleContainers(): Promise<void> {
const runtime = await detectRuntime();
try {
const { stdout } = await execFileAsync(runtime, [
"ps",
"-a",
"--filter",
`name=^${CONTAINER_PREFIX}`,
"--format",
"{{.Names}}",
]);
const names = stdout
.trim()
.split("\n")
.filter((n) => n.length > 0);
for (const name of names) {
console.log(`Removing stale container: ${name}`);
await execFileAsync(runtime, ["rm", "-f", name]);
}
} catch {
// No stale containers or runtime not ready yet
}
}
async function globalSetup(_config: FullConfig): Promise<void> {
await removeStaleContainers();
const halopImage = process.env.HALOP_IMAGE ?? DEFAULT_HALOP_IMAGE;
const halopPortRaw = process.env.HALOP_PORT ?? DEFAULT_HALOP_PORT;
const halopPort = Number(halopPortRaw);
if (Number.isNaN(halopPort)) {
throw new Error(`Invalid HALOP_PORT: "${halopPortRaw}" is not a number`);
}
console.log(`Starting halOP from "${halopImage}" on port ${halopPort}...`);
const halop = await startHalOp(halopImage, halopPort);
console.log(`halOP started (container: ${halop.containerId.slice(0, 12)})`);
const state: DaveState = { halop };
writeFileSync(STATE_FILE, JSON.stringify(state));
process.env.HALOP_URL = `http://localhost:${halop.port}`;
}
export default globalSetup;