Repository navigation
Expand file tree
/
Copy pathserverRuntimeState.test.ts
More file actions
491 lines (474 loc) · 19.7 KB
/
Copy pathserverRuntimeState.test.ts
File metadata and controls
491 lines (474 loc) · 19.7 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// @effect-diagnostics nodeBuiltinImport:off - Injects a real file replacement during the native SQLite transaction.
import * as NodeFS from "node:fs";
import * as NodeSqlite from "node:sqlite";
import { assert, describe, it } from "@effect/vitest";
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Logger from "effect/Logger";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Runtime from "effect/Runtime";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
import * as TestClock from "effect/testing/TestClock";
import { vi } from "vite-plus/test";
import { renderBootServicePlist, renderBootServiceUnit } from "./cloud/bootService.ts";
import { ServiceLauncherHostProcess } from "./cloud/serviceLauncherClient.ts";
import { SERVICE_LAUNCHER_CONTEXT_ENV } from "./cloud/serviceProtocol.ts";
import * as ProcessRunner from "./processRunner.ts";
import * as ServerOwnership from "./serverOwnership.ts";
import * as ServerRuntimeState from "./serverRuntimeState.ts";
const startBesideBootService = Effect.fn("test.startBesideBootService")(function* (input: {
readonly platform: "darwin" | "linux";
readonly outputs: Readonly<Record<string, { readonly code: number; readonly stdout?: string }>>;
readonly unitServesOtherHome?: boolean;
readonly restartPending?: boolean;
readonly activeVersion?: string;
readonly npmRuntime?: boolean;
readonly missingServiceState?: boolean;
readonly environment?: NodeJS.ProcessEnv;
readonly connected?: boolean;
}) {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-owner-boot-service-" });
const t3Home = path.join(root, ".t3");
const serviceHome = input.unitServesOtherHome ? path.join(root, "other") : t3Home;
for (const home of [t3Home, serviceHome]) {
yield* fs.makeDirectory(path.join(home, "userdata"), { recursive: true });
}
yield* fs.makeDirectory(path.join(serviceHome, "runtime"));
const activeVersion = input.activeVersion ?? "0.0.42";
if (!input.missingServiceState) {
yield* fs.writeFileString(
path.join(serviceHome, "runtime", "service-state.json"),
JSON.stringify({ protocol: input.npmRuntime ? 2 : 3, activeVersion }),
);
}
if (input.npmRuntime) {
const versionDir = path.join(serviceHome, "runtime", "versions", activeVersion);
const entryPath = path.join(versionDir, "node_modules", "t3", "dist", "bin.mjs");
yield* fs.makeDirectory(path.dirname(entryPath), { recursive: true });
yield* fs.writeFileString(entryPath, "");
}
if (input.restartPending) {
yield* fs.writeFileString(
path.join(serviceHome, "runtime", ".restart-pending"),
`${activeVersion}\n`,
);
}
const plan = {
program: input.npmRuntime
? ["/usr/bin/node", path.join(serviceHome, "runtime", "service-launcher.mjs")]
: [],
baseDir: serviceHome,
logPath: "",
unitPath: "",
};
const [unitPath, unit] =
input.platform === "darwin"
? [
path.join(root, "Library", "LaunchAgents", "com.t3tools.t3code.service.plist"),
renderBootServicePlist(plan, { homeDir: root, environmentPath: "" }),
]
: [
path.join(root, ".config", "systemd", "user", "t3code.service"),
renderBootServiceUnit(plan),
];
yield* fs.makeDirectory(path.dirname(unitPath), { recursive: true });
yield* fs.writeFileString(unitPath, unit);
const probes: string[] = [];
const runner = ProcessRunner.ProcessRunner.of({
run: (run) =>
Effect.gen(function* () {
const probe = `${path.basename(run.command)} ${run.args.find((arg) => !arg.startsWith("--"))}`;
probes.push(probe);
const output = input.outputs[probe];
return yield* output === undefined
? Effect.fail(
new ProcessRunner.ProcessSpawnError({
command: run.command,
argumentCount: run.args.length,
cause: new Error("not found"),
}),
)
: Effect.succeed({
stdout: output.stdout ?? "",
stderr: "",
code: ChildProcessSpawner.ExitCode(output.code),
timedOut: false,
stdoutTruncated: false,
stderrTruncated: false,
stdoutInvalidUtf8: false,
stderrInvalidUtf8: false,
});
}),
});
const result = yield* Effect.scoped(
ServerOwnership.acquireServerOwnership(path.join(t3Home, "userdata", "server-runtime.json")),
).pipe(
Effect.provideService(ProcessRunner.ProcessRunner, runner),
Effect.provideService(HostProcess.Platform, input.platform),
Effect.provideService(HostProcess.UserId, 501),
Effect.provideService(HostProcess.HomeDirectory, root),
Effect.provideService(HostProcess.Environment, input.environment ?? {}),
Effect.provideService(ServiceLauncherHostProcess, {
connected: input.connected ?? false,
send: () => false,
on: () => undefined,
off: () => undefined,
}),
Effect.result,
);
assert.includeMembers(
["launchctl print", "launchctl print-disabled", "systemctl is-active", "systemctl is-enabled"],
probes,
);
return { result, t3Home };
});
describe("server ownership", () => {
// An empty home, so no boot service unit installed on this machine is read.
const layerOwnership = Layer.mergeAll(
ProcessRunner.layer,
Layer.effect(
HostProcess.HomeDirectory,
Effect.gen(function* () {
return yield* (yield* FileSystem.FileSystem).makeTempDirectoryScoped();
}),
),
Layer.succeed(HostProcess.Environment, {}),
).pipe(Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)));
const makeState = ServerRuntimeState.makePersistedServerRuntimeState({
config: { host: undefined, devUrl: undefined },
port: 3773,
});
it.effect("lets only one of two racing starts own a state directory", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-owner-race-" });
const statePath = path.join(root, "server-runtime.json");
const attempts = yield* Effect.all(
[
ServerOwnership.acquireServerOwnership(statePath).pipe(Effect.result),
ServerOwnership.acquireServerOwnership(statePath).pipe(Effect.result),
],
{ concurrency: "unbounded" },
);
assert.equal(attempts.filter((result) => result._tag === "Success").length, 1);
const refused = attempts.find((result) => result._tag === "Failure");
assert.equal(refused?.failure._tag, "ServerAlreadyRunningError");
}).pipe(Effect.provide(layerOwnership)),
);
it.effect("refuses a second owner without touching its record, then allows a restart", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-owner-" });
const statePath = path.join(root, "server-runtime.json");
const state = yield* makeState;
yield* Effect.scoped(
Effect.gen(function* () {
const owner = yield* ServerOwnership.acquireServerOwnership(statePath);
yield* owner.publish(state);
const before = yield* fs.readFileString(statePath);
const refused = yield* ServerOwnership.acquireServerOwnership(statePath).pipe(
Effect.flip,
);
assert.equal(refused._tag, "ServerAlreadyRunningError");
assert.include(refused.message, "do not delete it while a server is running");
assert.equal(yield* fs.readFileString(statePath), before);
}),
);
assert.isFalse(yield* fs.exists(statePath));
yield* Effect.scoped(ServerOwnership.acquireServerOwnership(statePath));
}).pipe(Effect.provide(layerOwnership)),
);
describe.skipIf(HostProcess.Platform.defaultValue() === "win32")("lock file checks", () => {
it.effect.each([
"unchanged",
"unlink",
"replace",
"during acquisition",
"first acquisition",
// Root bypasses directory permissions, so chmod cannot force EACCES.
...(process.getuid?.() === 0 ? [] : (["stat error"] as const)),
] as const)("monitors ownership after %s", (operation) => {
const messages: unknown[] = [];
const warnings: unknown[] = [];
const logger = Logger.make(({ message, logLevel }) =>
(logLevel === "Warn" ? warnings : messages).push(message),
);
return Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-owner-lost-" });
const statePath = path.join(root, "server-runtime.json");
const lockPath = path.join(root, "server-owner.sqlite");
const replacement = path.join(root, "replacement.sqlite");
if (operation === "during acquisition" || operation === "first acquisition") {
if (operation === "during acquisition") yield* fs.writeFileString(lockPath, "");
yield* fs.writeFileString(replacement, "");
const exec = NodeSqlite.DatabaseSync.prototype.exec;
yield* Effect.acquireRelease(
Effect.sync(() =>
vi.spyOn(NodeSqlite.DatabaseSync.prototype, "exec").mockImplementationOnce(function (
this: NodeSqlite.DatabaseSync,
sql: string,
) {
exec.call(this, sql);
NodeFS.renameSync(replacement, lockPath);
}),
),
(spy) => Effect.sync(() => spy.mockRestore()),
);
}
yield* ServerOwnership.acquireServerOwnership(statePath);
if (operation === "unlink") {
yield* fs.remove(lockPath);
} else if (operation === "replace") {
yield* fs.writeFileString(replacement, "");
yield* fs.rename(replacement, lockPath);
}
if (operation === "stat error") {
yield* fs.chmod(root, 0);
yield* TestClock.adjust("15 seconds").pipe(
Effect.ensuring(fs.chmod(root, 0o700).pipe(Effect.orDie)),
);
assert.deepEqual(warnings, [
[`Cannot check server ownership lock at ${lockPath} (EACCES).`],
]);
yield* TestClock.adjust("5 seconds");
assert.deepEqual(messages, []);
yield* fs.remove(lockPath);
}
yield* TestClock.adjust("5 seconds");
const expected =
operation === "unchanged"
? []
: [
[
`Server ownership lock at ${lockPath} was deleted or replaced. Another server may now share this T3 home. Stop any other server using this home, then restart this server. Do not delete server-owner.sqlite while a server is running.`,
],
];
assert.deepEqual(messages, expected);
if (operation !== "unchanged") {
yield* fs.remove(`${lockPath}-journal`, { force: true });
yield* Effect.scoped(ServerOwnership.acquireServerOwnership(statePath));
}
yield* TestClock.adjust("15 seconds");
assert.deepEqual(messages, expected);
assert.lengthOf(warnings, operation === "stat error" ? 1 : 0);
}).pipe(
Effect.provide(
Layer.merge(layerOwnership, Logger.layer([logger], { mergeWithExisting: false })),
),
);
});
});
it.effect("refuses beside a live pre-lock server and ignores a stale record", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-owner-legacy-" });
const statePath = path.join(root, "server-runtime.json");
const state = yield* makeState;
yield* ServerRuntimeState.persistServerRuntimeState({
path: statePath,
// Any record time after this process started proves the PID was not reused.
state: { ...state, startedAt: "2100-01-01T00:00:00.000Z" },
});
const refused = yield* Effect.scoped(ServerOwnership.acquireServerOwnership(statePath)).pipe(
Effect.flip,
);
assert.equal(refused._tag, "ServerAlreadyRunningError");
yield* ServerRuntimeState.persistServerRuntimeState({
path: statePath,
state: { ...state, pid: 0 },
});
yield* Effect.scoped(ServerOwnership.acquireServerOwnership(statePath));
}).pipe(Effect.provide(layerOwnership)),
);
it.effect("never removes a runtime record another server published", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-owner-cleanup-" });
const statePath = path.join(root, "server-runtime.json");
const state = yield* makeState;
yield* Effect.scoped(
Effect.gen(function* () {
const owner = yield* ServerOwnership.acquireServerOwnership(statePath);
yield* owner.publish(state);
yield* ServerRuntimeState.persistServerRuntimeState({
path: statePath,
state: { ...state, ownerId: "other-owner" },
});
}),
);
const remaining = yield* ServerRuntimeState.readPersistedServerRuntimeState(statePath);
assert.equal(Option.getOrThrow(remaining).ownerId, "other-owner");
}).pipe(Effect.provide(layerOwnership)),
);
describe("beside an installed boot service", () => {
it.effect("refuses an old active or scheduled service even without its runtime record", () =>
Effect.gen(function* () {
for (const fixture of [
{
platform: "darwin",
outputs: { "launchctl print": { code: 0 } },
},
{
platform: "darwin",
outputs: {
"launchctl print": { code: 113 },
"launchctl print-disabled": {
code: 0,
stdout: '"com.t3tools.t3code.service" => false',
},
},
},
{
platform: "linux",
outputs: {
// Restart= keeps an otherwise disabled service alive during its restart delay.
"systemctl is-active": { code: 3, stdout: "activating\n" },
"systemctl is-enabled": { code: 1, stdout: "disabled\n" },
},
},
{
platform: "linux",
outputs: {
"systemctl is-active": { code: 3, stdout: "inactive\n" },
"systemctl is-enabled": { code: 0, stdout: "enabled\n" },
},
},
{
platform: "darwin",
outputs: { "launchctl print": { code: 0 } },
environment: { [SERVICE_LAUNCHER_CONTEXT_ENV]: "{}" },
},
] as const) {
const { result, t3Home } = yield* startBesideBootService(fixture);
assert.equal(result._tag, "Failure");
if (result._tag !== "Failure") return;
assert.equal(result.failure._tag, "OlderBootServiceError");
assert.equal(Runtime.getErrorExitCode(result.failure), 78);
assert.include(result.failure.message, t3Home);
assert.include(result.failure.message, "t3 update");
assert.include(result.failure.message, "t3 service restart");
assert.include(result.failure.message, "No server was stopped");
}
}).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
);
it.effect(
"refuses an active npm-installed service with no standalone executable or runtime record",
() =>
Effect.gen(function* () {
const { result } = yield* startBesideBootService({
platform: "linux",
activeVersion: "0.0.40",
npmRuntime: true,
outputs: {
"systemctl is-active": { code: 0, stdout: "active\n" },
},
});
assert.equal(result._tag, "Failure");
if (result._tag !== "Failure") return;
assert.equal(result.failure._tag, "OlderBootServiceError");
assert.equal(Runtime.getErrorExitCode(result.failure), 78);
}).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
);
it.effect("refuses while the loaded service has not restarted into its lock-aware update", () =>
Effect.gen(function* () {
const { result } = yield* startBesideBootService({
platform: "darwin",
restartPending: true,
activeVersion: "0.1.0",
outputs: {
"launchctl print": { code: 0 },
},
});
assert.equal(result._tag === "Failure" && result.failure._tag, "OlderBootServiceError");
}).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
);
it.effect("allows lock-aware stable services that are loaded or stopped and enabled", () =>
Effect.gen(function* () {
for (const fixture of [
{
platform: "darwin",
outputs: {
"launchctl print": { code: 0 },
},
},
{
platform: "linux",
outputs: {
"systemctl is-active": { code: 3, stdout: "inactive\n" },
"systemctl is-enabled": { code: 0, stdout: "enabled\n" },
},
},
] as const) {
const { result } = yield* startBesideBootService({ ...fixture, activeVersion: "0.1.0" });
assert.equal(result._tag, "Success");
}
}).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
);
it.effect("allows services that cannot compete for this home", () =>
Effect.gen(function* () {
const outputs = {
"launchctl print": { code: 0 },
};
for (const fixture of [
{ platform: "darwin", outputs, unitServesOtherHome: true },
{
platform: "darwin",
outputs,
environment: { [SERVICE_LAUNCHER_CONTEXT_ENV]: "{}" },
connected: true,
},
{
platform: "darwin",
outputs: {
"launchctl print": { code: 113 },
"launchctl print-disabled": {
code: 0,
stdout: '"com.t3tools.t3code.service" => disabled',
},
},
},
{
platform: "linux",
outputs: {
"systemctl is-active": { code: 3, stdout: "inactive\n" },
"systemctl is-enabled": { code: 1, stdout: "disabled\n" },
},
},
] as const) {
const { result } = yield* startBesideBootService(fixture);
assert.equal(result._tag, "Success");
}
}).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
);
it.effect("allows startup when the service configuration or manager is unavailable", () =>
Effect.gen(function* () {
for (const fixture of [
{ outputs: {} },
{
outputs: {
"launchctl print": { code: 113 },
"launchctl print-disabled": { code: 1 },
},
},
{ outputs: { "launchctl print": { code: 0 } }, missingServiceState: true },
]) {
const { result } = yield* startBesideBootService({ platform: "darwin", ...fixture });
assert.equal(result._tag, "Success");
}
}).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
);
});
});