File size: 3,016 Bytes
e81f4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { outputLine } from "./io.mjs";
import { commandAvailable, outputCommand, runCommand } from "./process.mjs";

if (!(await commandAvailable("docker"))) {
	outputLine("Docker smoke skipped: docker executable not found");
	process.exit(0);
}

const image = `web2gem:smoke-${process.pid}`;
let container = "";

try {
	await runCommand("docker", ["build", "-t", image, "."]);
	container = (
		await outputCommand("docker", [
			"run",
			"-d",
			"--rm",
			"-p",
			"127.0.0.1::52389",
			"-e",
			"API_KEYS=smoke-key",
			"-e",
			"CURRENT_INPUT_FILE_ENABLED=false",
			image,
		])
	).trim();

	const port = await mappedPort(container);
	const base = `http://127.0.0.1:${port}`;
	await waitForHealth(base);

	const health = await fetch(`${base}/`);
	assert(health.status === 200, `health status ${health.status}`);
	const healthBody = await health.json();
	assert(healthBody.status === "ok", "health payload did not report ok");

	const authFailure = await fetch(`${base}/v1/models`);
	assert(
		authFailure.status === 401,
		`auth failure status ${authFailure.status}`,
	);

	const models = await fetch(`${base}/v1/models`, {
		headers: { Authorization: "Bearer smoke-key" },
	});
	assert(models.status === 200, `authenticated models status ${models.status}`);

	const invalidModel = await fetch(`${base}/v1/chat/completions`, {
		method: "POST",
		headers: {
			Authorization: "Bearer smoke-key",
			"Content-Type": "application/json",
		},
		body: JSON.stringify({
			model: "not-a-model",
			messages: [{ role: "user", content: "hello" }],
		}),
	});
	assert(
		invalidModel.status === 400,
		`invalid model status ${invalidModel.status}`,
	);
	const invalidBody = await invalidModel.json();
	assert(
		invalidBody.error && invalidBody.error.code === "model_not_found",
		"invalid model did not return model_not_found",
	);

	outputLine("Docker smoke check passed");
} finally {
	if (container) {
		await outputCommand("docker", ["stop", container], { allowFailure: true });
	}
	await outputCommand("docker", ["rmi", image], { allowFailure: true });
}

async function mappedPort(containerId) {
	for (let i = 0; i < 30; i++) {
		const raw = (
			await outputCommand("docker", ["port", containerId, "52389/tcp"], {
				allowFailure: true,
			})
		).trim();
		const match = /:(\d+)\s*$/.exec(raw);
		if (match) return Number(match[1]);
		await delay(250);
	}
	throw new Error("Docker smoke failed: container port was not mapped");
}

async function waitForHealth(base) {
	let lastError = null;
	for (let i = 0; i < 60; i++) {
		try {
			const resp = await fetch(`${base}/`);
			if (resp.status === 200) return;
			lastError = new Error(`health status ${resp.status}`);
		} catch (err) {
			lastError = err;
		}
		await delay(250);
	}
	throw new Error(
		`Docker smoke failed: health route did not become ready: ${lastError}`,
	);
}

function delay(ms) {
	return new Promise((resolve) => setTimeout(resolve, ms));
}

function assert(ok, message) {
	if (!ok) throw new Error(`Docker smoke failed: ${message}`);
}