devrock-gametests 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +172 -3
- package/container/Containerfile +12 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +115 -0
- package/dist/commands.d.ts +17 -0
- package/dist/commands.js +304 -0
- package/dist/deploy.d.ts +53 -0
- package/dist/deploy.js +336 -0
- package/dist/game.d.ts +47 -0
- package/dist/game.js +110 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/list-worker.d.ts +1 -0
- package/dist/list-worker.js +6 -0
- package/dist/nbt.d.ts +32 -0
- package/dist/nbt.js +112 -0
- package/dist/outcome.d.ts +31 -0
- package/dist/outcome.js +21 -0
- package/dist/parallel.d.ts +96 -0
- package/dist/parallel.js +190 -0
- package/dist/podman.d.ts +40 -0
- package/dist/podman.js +201 -0
- package/dist/preflight.d.ts +30 -0
- package/dist/preflight.js +71 -0
- package/dist/project.d.ts +43 -0
- package/dist/project.js +204 -0
- package/dist/registrations.d.ts +38 -0
- package/dist/registrations.js +194 -0
- package/dist/report.d.ts +87 -0
- package/dist/report.js +243 -0
- package/dist/server.d.ts +45 -0
- package/dist/server.js +157 -0
- package/dist/shared.d.ts +23 -0
- package/dist/shared.js +19 -0
- package/dist/world.d.ts +10 -0
- package/dist/world.js +30 -0
- package/package.json +47 -6
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** What kind of run it was, and what it expected to run. */
|
|
2
|
+
export type RunKind = {
|
|
3
|
+
readonly kind: 'full';
|
|
4
|
+
/** The tests the listing found, tagged TEST_TAG. */
|
|
5
|
+
readonly listed: readonly string[];
|
|
6
|
+
/** Whether the listing failed, so `listed` is empty whatever the entry registers. */
|
|
7
|
+
readonly listingFailed: boolean;
|
|
8
|
+
/** How many tests the listing found, with the run tag or without. */
|
|
9
|
+
readonly registered: number;
|
|
10
|
+
} | {
|
|
11
|
+
readonly kind: 'named';
|
|
12
|
+
/** The ids the named run asked for. */
|
|
13
|
+
readonly ids: readonly string[];
|
|
14
|
+
};
|
|
15
|
+
export interface Outcome {
|
|
16
|
+
readonly exitCode: 0 | 1 | 2;
|
|
17
|
+
/** The ids a named run asked for that no container ran. */
|
|
18
|
+
readonly missing: string[];
|
|
19
|
+
/**
|
|
20
|
+
* Why a full run in which no test ran fails: the listing found tests the game never kept, the listing or the game
|
|
21
|
+
* found tests but none has the run tag, or the listing failed and the game kept none. Undefined when tests ran,
|
|
22
|
+
* or when a run with no tests at all is expected.
|
|
23
|
+
*/
|
|
24
|
+
readonly noneRan: 'listed' | 'untagged' | 'listing-failed' | undefined;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The run's exit code and what it fails on beyond the merge. `ran` is every test a container's test pack kept,
|
|
28
|
+
* `registered` every test one registered, tagged or not, `allReported` whether every container gave a report, and
|
|
29
|
+
* `mergedExitCode` the merge's own exit code, which stays 2 when a container gave no result.
|
|
30
|
+
*/
|
|
31
|
+
export declare function runOutcome(run: RunKind, ran: readonly string[], registered: readonly string[], allReported: boolean, mergedExitCode: 0 | 1 | 2): Outcome;
|
package/dist/outcome.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// What a run's own checks add to the merged result of its containers.
|
|
2
|
+
/**
|
|
3
|
+
* The run's exit code and what it fails on beyond the merge. `ran` is every test a container's test pack kept,
|
|
4
|
+
* `registered` every test one registered, tagged or not, `allReported` whether every container gave a report, and
|
|
5
|
+
* `mergedExitCode` the merge's own exit code, which stays 2 when a container gave no result.
|
|
6
|
+
*/
|
|
7
|
+
export function runOutcome(run, ran, registered, allReported, mergedExitCode) {
|
|
8
|
+
const found = new Set(ran.map((id) => id.toLowerCase()));
|
|
9
|
+
const missing = run.kind === 'named' ? run.ids.filter((id) => !found.has(id.toLowerCase())) : [];
|
|
10
|
+
let noneRan;
|
|
11
|
+
if (run.kind === 'full' && ran.length === 0 && allReported) {
|
|
12
|
+
if (run.listed.length > 0)
|
|
13
|
+
noneRan = 'listed';
|
|
14
|
+
else if (run.registered > 0 || registered.length > 0)
|
|
15
|
+
noneRan = 'untagged';
|
|
16
|
+
else if (run.listingFailed)
|
|
17
|
+
noneRan = 'listing-failed';
|
|
18
|
+
}
|
|
19
|
+
const failed = missing.length > 0 || noneRan !== undefined;
|
|
20
|
+
return { exitCode: failed ? Math.max(mergedExitCode, 1) : mergedExitCode, missing, noneRan };
|
|
21
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { Registration } from './registrations.js';
|
|
2
|
+
import { type ContentLogLine, type RequiredPasses, type TestReport } from './report.js';
|
|
3
|
+
/**
|
|
4
|
+
* Tests the engine runs together, at the same time: one named batch, or every test with no batch name. A batch is
|
|
5
|
+
* never split, because a test may rely on sharing the world with only its own batch.
|
|
6
|
+
*/
|
|
7
|
+
export interface Batch {
|
|
8
|
+
/** The batch name, or '' for the tests with none. */
|
|
9
|
+
readonly name: string;
|
|
10
|
+
/** `class:test` ids, in the order they registered. */
|
|
11
|
+
readonly tests: readonly string[];
|
|
12
|
+
/** How long the batch is expected to take. */
|
|
13
|
+
readonly seconds: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The batches of the tests tagged TEST_TAG, in the order their first test registered. A batch takes the seconds
|
|
17
|
+
* `measured` holds for it, or else its longest tick limit, since its tests run at the same time. The batches only
|
|
18
|
+
* plan the run: the test packs run what the game registers, which the listing outside the game can miss or misname.
|
|
19
|
+
*/
|
|
20
|
+
export declare function batchesOf(registrations: readonly Registration[], measured: ReadonlyMap<string, number>): Batch[];
|
|
21
|
+
/**
|
|
22
|
+
* Deals the batches to at most `containers` containers, longest first, each to the container with the least
|
|
23
|
+
* expected time so far (LPT), or of those the one with the fewest batches. Every container it returns has at least one
|
|
24
|
+
* batch.
|
|
25
|
+
*/
|
|
26
|
+
export declare function assignBatches(batches: readonly Batch[], containers: number): Batch[][];
|
|
27
|
+
/**
|
|
28
|
+
* The most containers `run` starts unless told otherwise. On the machine it was measured on, a run with this many
|
|
29
|
+
* finished in the time of its longest batch, so more would only add servers.
|
|
30
|
+
*/
|
|
31
|
+
export declare const MAX_DEFAULT_CONTAINERS = 8;
|
|
32
|
+
/**
|
|
33
|
+
* How many containers `run` starts when not told: MAX_DEFAULT_CONTAINERS, or fewer when the memory Podman reports
|
|
34
|
+
* can't hold that many servers, and always at least one.
|
|
35
|
+
*/
|
|
36
|
+
export declare function defaultContainers(podmanMemoryBytes: number): number;
|
|
37
|
+
export declare const expectedSeconds: (batches: readonly Batch[]) => number;
|
|
38
|
+
export interface TimedLine {
|
|
39
|
+
/** When the line arrived, in milliseconds since the epoch. */
|
|
40
|
+
readonly at: number;
|
|
41
|
+
readonly line: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The length of the transcript, one line and a line break per entry, that had arrived by `time`. The run cuts its
|
|
45
|
+
* verdicts there at its deadline, so a verdict that arrives after it can't pass a test.
|
|
46
|
+
*/
|
|
47
|
+
export declare function transcriptLengthUntil(timeline: readonly TimedLine[], time: number): number;
|
|
48
|
+
/**
|
|
49
|
+
* The seconds each batch took, from when its lines arrived. The engine runs batches one after another, so a batch
|
|
50
|
+
* lasts until the next one starts, and the last until the last verdict. A batch the engine splits into parts sums
|
|
51
|
+
* them.
|
|
52
|
+
*/
|
|
53
|
+
export declare function batchSeconds(timeline: readonly TimedLine[]): Map<string, number>;
|
|
54
|
+
/** What one container did, for the merge. */
|
|
55
|
+
export interface ContainerRun {
|
|
56
|
+
/** Counts from 1. */
|
|
57
|
+
readonly index: number;
|
|
58
|
+
/** The tests the container's test pack kept the run tag on, so its `runset` runs them. */
|
|
59
|
+
readonly tests: readonly string[];
|
|
60
|
+
/** The tests the container's test pack registered but left to another container. */
|
|
61
|
+
readonly skipped?: readonly string[];
|
|
62
|
+
/** Every test the container's test pack registered, tagged or not. */
|
|
63
|
+
readonly registered?: readonly string[];
|
|
64
|
+
/** How many passes each test that set them needs. A test it doesn't name needs one. */
|
|
65
|
+
readonly required?: RequiredPasses;
|
|
66
|
+
/** Parsed from everything the server printed, or undefined when it printed nothing to parse. */
|
|
67
|
+
readonly report?: TestReport;
|
|
68
|
+
readonly stoppedCleanly: boolean;
|
|
69
|
+
/** Why the container gave no result, such as a server that never started. */
|
|
70
|
+
readonly error?: string;
|
|
71
|
+
}
|
|
72
|
+
export interface ContainerVerdict {
|
|
73
|
+
readonly id: string;
|
|
74
|
+
readonly passed: boolean;
|
|
75
|
+
readonly error?: string;
|
|
76
|
+
readonly container: number;
|
|
77
|
+
}
|
|
78
|
+
export interface Merged {
|
|
79
|
+
/** One per test given to a container, then one per test a container ran without being given it. */
|
|
80
|
+
readonly verdicts: ContainerVerdict[];
|
|
81
|
+
readonly contentErrors: (ContentLogLine & {
|
|
82
|
+
readonly container: number;
|
|
83
|
+
})[];
|
|
84
|
+
/** Container failures that are not a test's verdict or a content-log line. */
|
|
85
|
+
readonly problems: {
|
|
86
|
+
readonly container: number;
|
|
87
|
+
readonly message: string;
|
|
88
|
+
}[];
|
|
89
|
+
/** 0 when everything passed, 1 when a test, the content log or a container failed, 2 when one gave no result. */
|
|
90
|
+
readonly exitCode: 0 | 1 | 2;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Merges the containers' reports once all of them have stopped. Any container's failed test, content-log line,
|
|
94
|
+
* unclean stop, or missing result fails the run.
|
|
95
|
+
*/
|
|
96
|
+
export declare function mergeRuns(runs: readonly ContainerRun[]): Merged;
|
package/dist/parallel.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { BATCH_RE, testsSeen, verdicts } from './report.js';
|
|
2
|
+
import { TEST_TAG } from './shared.js';
|
|
3
|
+
/** GameTest's tick limit for a test that sets none. */
|
|
4
|
+
const DEFAULT_MAX_TICKS = 100;
|
|
5
|
+
const TICKS_PER_SECOND = 20;
|
|
6
|
+
/**
|
|
7
|
+
* The batches of the tests tagged TEST_TAG, in the order their first test registered. A batch takes the seconds
|
|
8
|
+
* `measured` holds for it, or else its longest tick limit, since its tests run at the same time. The batches only
|
|
9
|
+
* plan the run: the test packs run what the game registers, which the listing outside the game can miss or misname.
|
|
10
|
+
*/
|
|
11
|
+
export function batchesOf(registrations, measured) {
|
|
12
|
+
const batches = new Map();
|
|
13
|
+
for (const { className, name, batch = '', maxTicks = DEFAULT_MAX_TICKS, tags } of registrations) {
|
|
14
|
+
if (!tags.includes(TEST_TAG))
|
|
15
|
+
continue;
|
|
16
|
+
const entry = batches.get(batch) ?? { tests: [], maxTicks: 0 };
|
|
17
|
+
entry.tests.push(`${className}:${name}`);
|
|
18
|
+
entry.maxTicks = Math.max(entry.maxTicks, maxTicks);
|
|
19
|
+
batches.set(batch, entry);
|
|
20
|
+
}
|
|
21
|
+
return [...batches].map(([name, { tests, maxTicks }]) => ({
|
|
22
|
+
name,
|
|
23
|
+
tests,
|
|
24
|
+
seconds: measured.get(name) ?? maxTicks / TICKS_PER_SECOND,
|
|
25
|
+
}));
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Deals the batches to at most `containers` containers, longest first, each to the container with the least
|
|
29
|
+
* expected time so far (LPT), or of those the one with the fewest batches. Every container it returns has at least one
|
|
30
|
+
* batch.
|
|
31
|
+
*/
|
|
32
|
+
export function assignBatches(batches, containers) {
|
|
33
|
+
const longestFirst = [...batches].sort((a, b) => b.seconds - a.seconds || a.name.localeCompare(b.name));
|
|
34
|
+
const groups = Array.from({ length: Math.min(containers, batches.length) }, () => []);
|
|
35
|
+
const load = groups.map(() => 0);
|
|
36
|
+
for (const batch of longestFirst) {
|
|
37
|
+
let least = 0;
|
|
38
|
+
for (let i = 1; i < groups.length; i++) {
|
|
39
|
+
const fewer = load[i] === load[least] && groups[i].length < groups[least].length;
|
|
40
|
+
if (load[i] < load[least] || fewer)
|
|
41
|
+
least = i;
|
|
42
|
+
}
|
|
43
|
+
groups[least].push(batch);
|
|
44
|
+
load[least] += batch.seconds;
|
|
45
|
+
}
|
|
46
|
+
return groups;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The most containers `run` starts unless told otherwise. On the machine it was measured on, a run with this many
|
|
50
|
+
* finished in the time of its longest batch, so more would only add servers.
|
|
51
|
+
*/
|
|
52
|
+
export const MAX_DEFAULT_CONTAINERS = 8;
|
|
53
|
+
/** One server peaked at about 350 MiB in a full run. This leaves room above that. */
|
|
54
|
+
const CONTAINER_MEMORY = 512 * 1024 ** 2;
|
|
55
|
+
/** Kept free for the Podman machine itself. */
|
|
56
|
+
const MACHINE_MEMORY = 1024 ** 3;
|
|
57
|
+
/**
|
|
58
|
+
* How many containers `run` starts when not told: MAX_DEFAULT_CONTAINERS, or fewer when the memory Podman reports
|
|
59
|
+
* can't hold that many servers, and always at least one.
|
|
60
|
+
*/
|
|
61
|
+
export function defaultContainers(podmanMemoryBytes) {
|
|
62
|
+
const fit = Math.floor((podmanMemoryBytes - MACHINE_MEMORY) / CONTAINER_MEMORY);
|
|
63
|
+
return Math.max(1, Math.min(MAX_DEFAULT_CONTAINERS, fit));
|
|
64
|
+
}
|
|
65
|
+
export const expectedSeconds = (batches) => batches.reduce((sum, batch) => sum + batch.seconds, 0);
|
|
66
|
+
/**
|
|
67
|
+
* The length of the transcript, one line and a line break per entry, that had arrived by `time`. The run cuts its
|
|
68
|
+
* verdicts there at its deadline, so a verdict that arrives after it can't pass a test.
|
|
69
|
+
*/
|
|
70
|
+
export function transcriptLengthUntil(timeline, time) {
|
|
71
|
+
let length = 0;
|
|
72
|
+
for (const { at, line } of timeline) {
|
|
73
|
+
if (at > time)
|
|
74
|
+
break;
|
|
75
|
+
length += line.length + 1;
|
|
76
|
+
}
|
|
77
|
+
return length;
|
|
78
|
+
}
|
|
79
|
+
const VERDICT_RE = /^onTest(?:Passed|Failed): /;
|
|
80
|
+
/**
|
|
81
|
+
* The seconds each batch took, from when its lines arrived. The engine runs batches one after another, so a batch
|
|
82
|
+
* lasts until the next one starts, and the last until the last verdict. A batch the engine splits into parts sums
|
|
83
|
+
* them.
|
|
84
|
+
*/
|
|
85
|
+
export function batchSeconds(timeline) {
|
|
86
|
+
const seconds = new Map();
|
|
87
|
+
let current;
|
|
88
|
+
let lastVerdict = 0;
|
|
89
|
+
const close = (end) => {
|
|
90
|
+
if (current)
|
|
91
|
+
seconds.set(current.name, (seconds.get(current.name) ?? 0) + (end - current.start) / 1000);
|
|
92
|
+
};
|
|
93
|
+
for (const { at, line } of timeline) {
|
|
94
|
+
const batch = BATCH_RE.exec(line);
|
|
95
|
+
if (batch) {
|
|
96
|
+
close(at);
|
|
97
|
+
current = { name: batch[1], start: at };
|
|
98
|
+
}
|
|
99
|
+
else if (VERDICT_RE.test(line)) {
|
|
100
|
+
lastVerdict = at;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (current && lastVerdict >= current.start)
|
|
104
|
+
close(lastVerdict);
|
|
105
|
+
return seconds;
|
|
106
|
+
}
|
|
107
|
+
/** `containers` as words, such as `containers 1 and 2`. */
|
|
108
|
+
const containerList = (containers) => `containers ${containers.slice(0, -1).join(', ')} and ${containers.at(-1)}`;
|
|
109
|
+
/**
|
|
110
|
+
* What the containers disagree on, which each one alone can't see: a test more than one ran, a test some test pack
|
|
111
|
+
* registered but none ran, and a batch the engine started in more than one. Each container's test pack decides on
|
|
112
|
+
* its own, so this happens only when the test packs registered tests differently.
|
|
113
|
+
*/
|
|
114
|
+
function crossCheck(runs) {
|
|
115
|
+
const reported = runs.filter((run) => run.report !== undefined);
|
|
116
|
+
const problems = [];
|
|
117
|
+
// The engine matches ids in any letter case, so two spellings of one id are one test.
|
|
118
|
+
const same = (a, b) => a.toLowerCase() === b.toLowerCase();
|
|
119
|
+
const ranIn = (id) => reported.filter((run) => run.tests.some((ran) => same(ran, id))).map((run) => run.index);
|
|
120
|
+
const registered = new Map();
|
|
121
|
+
for (const id of reported.flatMap((run) => [...run.tests, ...(run.skipped ?? [])])) {
|
|
122
|
+
if (!registered.has(id.toLowerCase()))
|
|
123
|
+
registered.set(id.toLowerCase(), id);
|
|
124
|
+
}
|
|
125
|
+
for (const id of registered.values()) {
|
|
126
|
+
const where = ranIn(id);
|
|
127
|
+
const why = 'because their test packs gave it different batches.';
|
|
128
|
+
if (where.length > 1)
|
|
129
|
+
problems.push({ container: where[1], message: `${id} ran in ${containerList(where)}, ${why}` });
|
|
130
|
+
if (where.length === 0) {
|
|
131
|
+
problems.push({ container: reported[0].index, message: `${id} was registered, but no container ran it, ${why}` });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const batches = new Set(reported.flatMap((run) => run.report?.batches ?? []));
|
|
135
|
+
for (const batch of batches) {
|
|
136
|
+
const where = reported.filter((run) => run.report?.batches.includes(batch)).map((run) => run.index);
|
|
137
|
+
if (where.length > 1) {
|
|
138
|
+
problems.push({
|
|
139
|
+
container: where[1],
|
|
140
|
+
message: `The batch ${JSON.stringify(batch)} ran in ${containerList(where)}, so its tests did not run together.`,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return problems;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Merges the containers' reports once all of them have stopped. Any container's failed test, content-log line,
|
|
148
|
+
* unclean stop, or missing result fails the run.
|
|
149
|
+
*/
|
|
150
|
+
export function mergeRuns(runs) {
|
|
151
|
+
const merged = { verdicts: [], contentErrors: [], problems: [], exitCode: 0 };
|
|
152
|
+
let noResult = false;
|
|
153
|
+
for (const { index: container, tests, report, stoppedCleanly, error, required } of runs) {
|
|
154
|
+
const problem = (message) => merged.problems.push({ container, message });
|
|
155
|
+
if (error !== undefined) {
|
|
156
|
+
problem(error);
|
|
157
|
+
noResult = true;
|
|
158
|
+
}
|
|
159
|
+
if (!stoppedCleanly) {
|
|
160
|
+
problem("The server didn't stop cleanly, so warnings it printed while stopping may be missing.");
|
|
161
|
+
noResult = true;
|
|
162
|
+
}
|
|
163
|
+
if (report === undefined) {
|
|
164
|
+
for (const id of tests)
|
|
165
|
+
merged.verdicts.push({ id, passed: false, error: 'Never ran.', container });
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
for (const line of report.contentErrors)
|
|
169
|
+
merged.contentErrors.push({ ...line, container });
|
|
170
|
+
if (report.noTestsForTag && tests.length > 0) {
|
|
171
|
+
problem(`The engine found none of the ${tests.length} tests given to this container.`);
|
|
172
|
+
}
|
|
173
|
+
else if (report.expected === null && !report.noTestsForTag) {
|
|
174
|
+
problem('The server never started the tests.');
|
|
175
|
+
noResult = true;
|
|
176
|
+
}
|
|
177
|
+
else if (report.expected !== null && report.finished.length < report.expected) {
|
|
178
|
+
problem(`The engine announced ${report.expected} tests, but only ${report.finished.length} reported a verdict.`);
|
|
179
|
+
}
|
|
180
|
+
for (const verdict of verdicts(report, tests, required))
|
|
181
|
+
merged.verdicts.push({ ...verdict, container });
|
|
182
|
+
for (const id of testsSeen(report).filter((seen) => !tests.includes(seen))) {
|
|
183
|
+
merged.verdicts.push({ id, passed: false, error: 'Ran in a container it was not given to.', container });
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
for (const { container, message } of crossCheck(runs))
|
|
187
|
+
merged.problems.push({ container, message });
|
|
188
|
+
const failed = merged.problems.length > 0 || merged.contentErrors.length > 0 || merged.verdicts.some((verdict) => !verdict.passed);
|
|
189
|
+
return { ...merged, exitCode: noResult ? 2 : failed ? 1 : 0 };
|
|
190
|
+
}
|
package/dist/podman.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Project } from './project.js';
|
|
2
|
+
export type Log = (message: string) => void;
|
|
3
|
+
export declare const imageName: (serverVersion: string) => string;
|
|
4
|
+
/** Unique per server, so containers from separate runs never collide. */
|
|
5
|
+
export declare const containerName: () => string;
|
|
6
|
+
/**
|
|
7
|
+
* `podman run` for one server: attached to stdin, stopped when stdin closes, removed when it stops, and with no
|
|
8
|
+
* network. `serverDir` holds the server.properties, config and worlds the container mounts.
|
|
9
|
+
*/
|
|
10
|
+
export declare function runArgs(name: string, image: string, serverDir: string): string[];
|
|
11
|
+
/** The memory Podman's host has for containers, the Podman machine's on Windows and macOS, in bytes. */
|
|
12
|
+
export declare function podmanMemory(): number;
|
|
13
|
+
/**
|
|
14
|
+
* Where this runner's process ids mean something: the machine, the platform, and on Linux the process-id namespace.
|
|
15
|
+
* Two runners that share one Podman service from different machines or namespaces get different values.
|
|
16
|
+
*/
|
|
17
|
+
export declare const RUNNER: string;
|
|
18
|
+
/**
|
|
19
|
+
* The runner containers whose runner process is gone: started by a runner where this one's process ids mean the same
|
|
20
|
+
* thing, by the process id in each name. A runner killed outright can leave one, for example a container Podman was
|
|
21
|
+
* still starting, or a server stuck in a script that never reads its `stop`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function leftoverContainers(containers: readonly {
|
|
24
|
+
readonly name: string;
|
|
25
|
+
readonly runner: string;
|
|
26
|
+
}[], alive: (pid: number) => boolean): string[];
|
|
27
|
+
/** Removes the containers that runners killed outright left behind, in any project this machine runs. */
|
|
28
|
+
export declare function removeLeftoverContainers(): void;
|
|
29
|
+
/**
|
|
30
|
+
* Stops and removes a container at once, and returns whether it is gone within 30 seconds. Removing one that is
|
|
31
|
+
* already gone succeeds. Killing the podman client leaves its container running.
|
|
32
|
+
*/
|
|
33
|
+
export declare function removeContainer(name: string): boolean;
|
|
34
|
+
/** Builds the server image for the project's version unless Podman already has it. Building may use the network. */
|
|
35
|
+
export declare function ensureImage(project: Project, log: Log): Promise<string>;
|
|
36
|
+
/**
|
|
37
|
+
* True if `file` exists, starts with a zip's local file header, and ends with its end-of-central-directory record.
|
|
38
|
+
* A download cut short has the first but not the second.
|
|
39
|
+
*/
|
|
40
|
+
export declare function isZip(file: string): Promise<boolean>;
|
package/dist/podman.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { randomBytes } from 'node:crypto';
|
|
3
|
+
import { createWriteStream, existsSync, readlinkSync } from 'node:fs';
|
|
4
|
+
import { copyFile, mkdir, open, rename, rm } from 'node:fs/promises';
|
|
5
|
+
import { hostname } from 'node:os';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { Readable } from 'node:stream';
|
|
8
|
+
import { pipeline } from 'node:stream/promises';
|
|
9
|
+
import { fileURLToPath } from 'node:url';
|
|
10
|
+
const CONTAINERFILE = fileURLToPath(new URL('../container/Containerfile', import.meta.url));
|
|
11
|
+
/** minecraft.net refuses some non-browser user agents. */
|
|
12
|
+
const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)';
|
|
13
|
+
export const imageName = (serverVersion) => `localhost/devrock-gametests-bds:${serverVersion}`;
|
|
14
|
+
/** Unique per server, so containers from separate runs never collide. */
|
|
15
|
+
export const containerName = () => `devrock-gametests-${process.pid}-${randomBytes(4).toString('hex')}`;
|
|
16
|
+
/**
|
|
17
|
+
* Runs the server with its console on a pipe that gets `stop` once the container's stdin closes. A runner that dies
|
|
18
|
+
* any way at all, even killed outright, takes its podman client with it, which closes that stdin, so the server
|
|
19
|
+
* stops and `--rm` removes its container. A background job's stdin is /dev/null, so it reads the real one from fd 3.
|
|
20
|
+
*/
|
|
21
|
+
const CONSOLE_SCRIPT = [
|
|
22
|
+
'exec 3<&0',
|
|
23
|
+
'mkfifo /tmp/console',
|
|
24
|
+
'{ cat <&3; echo stop; } >/tmp/console &',
|
|
25
|
+
'exec ./bedrock_server </tmp/console 3<&-',
|
|
26
|
+
].join('\n');
|
|
27
|
+
/**
|
|
28
|
+
* `podman run` for one server: attached to stdin, stopped when stdin closes, removed when it stops, and with no
|
|
29
|
+
* network. `serverDir` holds the server.properties, config and worlds the container mounts.
|
|
30
|
+
*/
|
|
31
|
+
export function runArgs(name, image, serverDir) {
|
|
32
|
+
const mount = (host, container, mode) => ['-v', `${host}:${container}:${mode}`];
|
|
33
|
+
return [
|
|
34
|
+
'run',
|
|
35
|
+
'-i',
|
|
36
|
+
'--rm',
|
|
37
|
+
'--name',
|
|
38
|
+
name,
|
|
39
|
+
'--network',
|
|
40
|
+
'none',
|
|
41
|
+
'--entrypoint',
|
|
42
|
+
'/bin/sh',
|
|
43
|
+
// On a host with SELinux on, files the runner writes carry a label the container can't read. This turns the
|
|
44
|
+
// label check off for this container only, and changes nothing on a host without SELinux.
|
|
45
|
+
'--security-opt',
|
|
46
|
+
'label=disable',
|
|
47
|
+
'--label',
|
|
48
|
+
`${RUNNER_LABEL}=${RUNNER}`,
|
|
49
|
+
...mount(path.join(serverDir, 'worlds'), '/bds/worlds', 'rw'),
|
|
50
|
+
...mount(path.join(serverDir, 'server.properties'), '/bds/server.properties', 'ro'),
|
|
51
|
+
...mount(path.join(serverDir, 'config'), '/bds/config', 'ro'),
|
|
52
|
+
image,
|
|
53
|
+
'-c',
|
|
54
|
+
CONSOLE_SCRIPT,
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
/** The memory Podman's host has for containers, the Podman machine's on Windows and macOS, in bytes. */
|
|
58
|
+
export function podmanMemory() {
|
|
59
|
+
const info = spawnSync('podman', ['info', '--format', '{{.Host.MemTotal}}'], { encoding: 'utf8', windowsHide: true });
|
|
60
|
+
const bytes = Number(info.stdout?.trim());
|
|
61
|
+
if (info.status !== 0 || !Number.isFinite(bytes) || bytes <= 0) {
|
|
62
|
+
throw new Error(`\`podman info\` didn't report the memory Podman has. ${info.stderr ?? ''}`.trim());
|
|
63
|
+
}
|
|
64
|
+
return bytes;
|
|
65
|
+
}
|
|
66
|
+
const CONTAINER_RE = /^devrock-gametests-(\d+)-[0-9a-f]+$/;
|
|
67
|
+
/** The container label that says which runner started a container. */
|
|
68
|
+
const RUNNER_LABEL = 'devrock-gametests.runner';
|
|
69
|
+
/**
|
|
70
|
+
* Where this runner's process ids mean something: the machine, the platform, and on Linux the process-id namespace.
|
|
71
|
+
* Two runners that share one Podman service from different machines or namespaces get different values.
|
|
72
|
+
*/
|
|
73
|
+
export const RUNNER = [
|
|
74
|
+
hostname(),
|
|
75
|
+
process.platform,
|
|
76
|
+
process.platform === 'linux' && existsSync('/proc/self/ns/pid') ? readlinkSync('/proc/self/ns/pid') : '',
|
|
77
|
+
].join('|');
|
|
78
|
+
/**
|
|
79
|
+
* The runner containers whose runner process is gone: started by a runner where this one's process ids mean the same
|
|
80
|
+
* thing, by the process id in each name. A runner killed outright can leave one, for example a container Podman was
|
|
81
|
+
* still starting, or a server stuck in a script that never reads its `stop`.
|
|
82
|
+
*/
|
|
83
|
+
export function leftoverContainers(containers, alive) {
|
|
84
|
+
return containers
|
|
85
|
+
.filter(({ name, runner }) => {
|
|
86
|
+
const match = CONTAINER_RE.exec(name);
|
|
87
|
+
return match !== null && runner === RUNNER && !alive(Number(match[1]));
|
|
88
|
+
})
|
|
89
|
+
.map(({ name }) => name);
|
|
90
|
+
}
|
|
91
|
+
const processAlive = (pid) => {
|
|
92
|
+
try {
|
|
93
|
+
process.kill(pid, 0);
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
// EPERM means the process exists but belongs to someone else.
|
|
98
|
+
return error.code === 'EPERM';
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
/** Removes the containers that runners killed outright left behind, in any project this machine runs. */
|
|
102
|
+
export function removeLeftoverContainers() {
|
|
103
|
+
const format = `{{.Names}}\t{{index .Labels "${RUNNER_LABEL}"}}`;
|
|
104
|
+
const list = spawnSync('podman', ['ps', '-a', '--format', format], { encoding: 'utf8', windowsHide: true });
|
|
105
|
+
if (list.status !== 0)
|
|
106
|
+
return;
|
|
107
|
+
const containers = list.stdout.split(/\r?\n/).map((line) => {
|
|
108
|
+
const [name = '', runner = ''] = line.split('\t');
|
|
109
|
+
return { name, runner };
|
|
110
|
+
});
|
|
111
|
+
for (const name of leftoverContainers(containers, processAlive))
|
|
112
|
+
removeContainer(name);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Stops and removes a container at once, and returns whether it is gone within 30 seconds. Removing one that is
|
|
116
|
+
* already gone succeeds. Killing the podman client leaves its container running.
|
|
117
|
+
*/
|
|
118
|
+
export function removeContainer(name) {
|
|
119
|
+
// A Podman that hangs would otherwise hang the runner, even as it exits.
|
|
120
|
+
const result = spawnSync('podman', ['rm', '--force', '--ignore', '--time', '0', name], {
|
|
121
|
+
stdio: 'ignore',
|
|
122
|
+
timeout: 30_000,
|
|
123
|
+
killSignal: 'SIGKILL',
|
|
124
|
+
windowsHide: true,
|
|
125
|
+
});
|
|
126
|
+
return result.status === 0;
|
|
127
|
+
}
|
|
128
|
+
/** Builds the server image for the project's version unless Podman already has it. Building may use the network. */
|
|
129
|
+
export async function ensureImage(project, log) {
|
|
130
|
+
const image = imageName(project.serverVersion);
|
|
131
|
+
const exists = spawnSync('podman', ['image', 'exists', image], { stdio: 'ignore', windowsHide: true });
|
|
132
|
+
if (exists.status === 0)
|
|
133
|
+
return image;
|
|
134
|
+
const zip = process.env.BDS_ZIP ? path.resolve(process.env.BDS_ZIP) : await download(project, log);
|
|
135
|
+
const context = path.join(project.workDir, 'image', project.serverVersion);
|
|
136
|
+
await rm(context, { recursive: true, force: true });
|
|
137
|
+
await mkdir(context, { recursive: true });
|
|
138
|
+
await copyFile(CONTAINERFILE, path.join(context, 'Containerfile'));
|
|
139
|
+
await copyFile(zip, path.join(context, 'bedrock-server.zip'));
|
|
140
|
+
log(`Building the image ${image}. This happens once per server version.`);
|
|
141
|
+
const build = spawnSync('podman', ['build', '-t', image, context], { stdio: 'inherit', windowsHide: true });
|
|
142
|
+
if (build.status !== 0)
|
|
143
|
+
throw new Error(`\`podman build\` failed for ${image}. See its output above.`);
|
|
144
|
+
await rm(context, { recursive: true, force: true });
|
|
145
|
+
return image;
|
|
146
|
+
}
|
|
147
|
+
async function download(project, log) {
|
|
148
|
+
const version = project.serverVersion;
|
|
149
|
+
const zip = path.join(project.workDir, 'downloads', `bedrock-server-${version}-bin-linux.zip`);
|
|
150
|
+
if (await isZip(zip))
|
|
151
|
+
return zip;
|
|
152
|
+
const url = `https://www.minecraft.net/bedrockdedicatedserver/bin-linux/bedrock-server-${version}.zip`;
|
|
153
|
+
log(`Downloading ${url}`);
|
|
154
|
+
log('By using Bedrock Dedicated Server you agree to the Minecraft EULA and the Microsoft Privacy Policy.');
|
|
155
|
+
log('EULA: https://www.minecraft.net/eula');
|
|
156
|
+
log('Privacy Policy: https://go.microsoft.com/fwlink/?LinkId=521839');
|
|
157
|
+
const response = await fetch(url, { headers: { 'User-Agent': USER_AGENT } });
|
|
158
|
+
if (!response.ok || !response.body) {
|
|
159
|
+
throw new Error(`Download failed: ${response.status} ${response.statusText}. ` +
|
|
160
|
+
'Download the Linux zip from https://www.minecraft.net/download/server/bedrock and rerun with BDS_ZIP=<path to zip>.');
|
|
161
|
+
}
|
|
162
|
+
await mkdir(path.dirname(zip), { recursive: true });
|
|
163
|
+
const partial = `${zip}.partial`;
|
|
164
|
+
await pipeline(Readable.fromWeb(response.body), createWriteStream(partial));
|
|
165
|
+
// A page served with status 200, such as a sign-in or error page, would otherwise be kept and reused.
|
|
166
|
+
if (!(await isZip(partial))) {
|
|
167
|
+
await rm(partial, { force: true });
|
|
168
|
+
throw new Error(`${url} didn't send a zip. Download the Linux zip from https://www.minecraft.net/download/server/bedrock ` +
|
|
169
|
+
'and rerun with BDS_ZIP=<path to zip>.');
|
|
170
|
+
}
|
|
171
|
+
await rename(partial, zip);
|
|
172
|
+
return zip;
|
|
173
|
+
}
|
|
174
|
+
/** A zip's end-of-central-directory record is 22 bytes plus a comment of up to 65535. */
|
|
175
|
+
const ZIP_END_MAX = 22 + 0xffff;
|
|
176
|
+
/**
|
|
177
|
+
* True if `file` exists, starts with a zip's local file header, and ends with its end-of-central-directory record.
|
|
178
|
+
* A download cut short has the first but not the second.
|
|
179
|
+
*/
|
|
180
|
+
export async function isZip(file) {
|
|
181
|
+
try {
|
|
182
|
+
const handle = await open(file, 'r');
|
|
183
|
+
try {
|
|
184
|
+
const { size } = await handle.stat();
|
|
185
|
+
const head = (await handle.read(Buffer.alloc(4), 0, 4, 0)).buffer;
|
|
186
|
+
if (!head.equals(Buffer.from([0x50, 0x4b, 0x03, 0x04])))
|
|
187
|
+
return false;
|
|
188
|
+
const tailLength = Math.min(size, ZIP_END_MAX);
|
|
189
|
+
const tail = (await handle.read(Buffer.alloc(tailLength), 0, tailLength, size - tailLength)).buffer;
|
|
190
|
+
// The record is 22 bytes, so a signature with fewer bytes after it is a download cut off inside it.
|
|
191
|
+
const end = tail.lastIndexOf(Buffer.from([0x50, 0x4b, 0x05, 0x06]));
|
|
192
|
+
return end !== -1 && tailLength - end >= 22;
|
|
193
|
+
}
|
|
194
|
+
finally {
|
|
195
|
+
await handle.close();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
catch {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export type BlockedReason = 'unsupported-arch' | 'podman-missing' | 'wsl-missing' | 'no-machine' | 'machine-start-failed' | 'podman-unreachable';
|
|
2
|
+
export type Preflight = {
|
|
3
|
+
kind: 'ready';
|
|
4
|
+
} | {
|
|
5
|
+
kind: 'blocked';
|
|
6
|
+
reason: BlockedReason;
|
|
7
|
+
message: string;
|
|
8
|
+
};
|
|
9
|
+
export type RunResult = {
|
|
10
|
+
kind: 'exited';
|
|
11
|
+
status: number | null;
|
|
12
|
+
stdout: string;
|
|
13
|
+
stderr: string;
|
|
14
|
+
} | {
|
|
15
|
+
kind: 'spawn-failed';
|
|
16
|
+
error: Error;
|
|
17
|
+
};
|
|
18
|
+
export interface Host {
|
|
19
|
+
readonly platform: NodeJS.Platform;
|
|
20
|
+
readonly arch: string;
|
|
21
|
+
run(command: string, args: readonly string[]): RunResult;
|
|
22
|
+
/** Tells the person running the check what it is doing while they wait. */
|
|
23
|
+
say(message: string): void;
|
|
24
|
+
}
|
|
25
|
+
export declare const nodeHost: Host;
|
|
26
|
+
/**
|
|
27
|
+
* Checks that Podman can run a server container, and starts a stopped Podman machine. It never creates
|
|
28
|
+
* a machine or installs anything.
|
|
29
|
+
*/
|
|
30
|
+
export declare function preflight(host: Host): Preflight;
|