Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions src/daemon/connection-host.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,28 +171,32 @@ describe("DaemonEndpointHost", () => {
await expect(ipc.connect(endpoint)).rejects.toMatchObject({ code: "endpoint-not-found" });
});

it("creates the directory its endpoint lives in owner-only", async () => {
// why: under the umask every real host starts from, a directory made without a mode comes
// out 0755, so only a mode the host asks for ends at 0700.
const previousUmask = process.umask(0o022);
const base = await mkdtemp(join(tmpdir(), "simlock-endpoint-"));
const ipc = new MemoryIpcTransport();
const host = new DaemonEndpointHost({
connector: ipc,
endpoint: join(base, "home", "daemon.sock"),
filesystem: new NodeFilesystem(),
listenerFactory: ipc,
});
try {
await host.start(() => undefined);
// why: a POSIX mode and umask have no Windows counterpart, and the Windows endpoint is a pipe that lives in no directory.
it.skipIf(process.platform === "win32")(
"creates the directory its endpoint lives in owner-only",
async () => {
// why: under the umask every real host starts from, a directory made without a mode comes
// out 0755, so only a mode the host asks for ends at 0700.
const previousUmask = process.umask(0o022);
const base = await mkdtemp(join(tmpdir(), "simlock-endpoint-"));
const ipc = new MemoryIpcTransport();
const host = new DaemonEndpointHost({
connector: ipc,
endpoint: join(base, "home", "daemon.sock"),
filesystem: new NodeFilesystem(),
listenerFactory: ipc,
});
try {
await host.start(() => undefined);

expect((await stat(join(base, "home"))).mode & 0o777).toBe(0o700);
} finally {
process.umask(previousUmask);
await host.stop();
await rm(base, { force: true, recursive: true });
}
});
expect((await stat(join(base, "home"))).mode & 0o777).toBe(0o700);
} finally {
process.umask(previousUmask);
await host.stop();
await rm(base, { force: true, recursive: true });
}
},
);

it("rejects a live endpoint before binding", async () => {
const filesystem = new MemoryFilesystem();
Expand Down
33 changes: 20 additions & 13 deletions src/daemon/device-data-tables.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdtemp, rm } from "node:fs/promises";
import { mkdtemp } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";

Expand All @@ -11,10 +11,12 @@ import { iosDeviceDataTable } from "../drivers/ios/index.js";
import { FakeClock, NodeFilesystem, NodeSqliteOpener, NoopLogger } from "../ports/index.js";
import { openStateDatabase } from "../state/index.js";
import { productionDeviceDataTables } from "./driver-discovery.js";
import { removeDirectory } from "../test-support/remove-directory.js";

describe("the production device-data tables", () => {
it("a registry restarted on one state.db returns an iOS and an Android device's driverData unchanged", async () => {
const directory = await mkdtemp(join(tmpdir(), "simlock-device-data-"));
const states: Awaited<ReturnType<typeof openStateDatabase>>[] = [];
try {
const clock = new FakeClock(1_000);
const iosData = {
Expand All @@ -32,22 +34,25 @@ describe("the production device-data tables", () => {
serial: "emulator-5554",
};
let next = 0;
const open = async () =>
Registry.load({
const open = async () => {
const state = await openStateDatabase({
dataDirectory: directory,
filesystem: new NodeFilesystem(),
logger: new NoopLogger(),
oldFiles: [],
owners: [registryOwner, iosDeviceDataTable, androidDeviceDataTable],
sqlite: new NodeSqliteOpener(),
upgradeStep: "remove it.",
});
states.push(state);
return Registry.load({
clock,
deviceData: productionDeviceDataTables(),
eventBus: new EventBus(clock),
idGenerator: { generate: () => String((next += 1)) },
state: await openStateDatabase({
dataDirectory: directory,
filesystem: new NodeFilesystem(),
logger: new NoopLogger(),
oldFiles: [],
owners: [registryOwner, iosDeviceDataTable, androidDeviceDataTable],
sqlite: new NodeSqliteOpener(),
upgradeStep: "remove it.",
}),
state,
});
};
const registry = await open();
await registry.registerDevice({
driverData: iosData,
Expand All @@ -72,7 +77,9 @@ describe("the production device-data tables", () => {
expect(byDriverId.get("drv-ios")).toStrictEqual(iosData);
expect(byDriverId.get("drv-android")).toStrictEqual(androidData);
} finally {
await rm(directory, { force: true, recursive: true });
// why: Windows refuses to delete a database file that is still open.
for (const state of states) state.database.close();
await removeDirectory(directory);
}
});
});
30 changes: 13 additions & 17 deletions src/daemon/driver-discovery.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { tmpdir } from "node:os";
import { join } from "node:path";
import { join, resolve } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { type Config, OWNED_ROOT_MARKER_FILE, type OwnedRootError } from "../core/index.js";
import { androidDriverConfig } from "../drivers/android/index.js";
import { binaries } from "../drivers/android/testing.js";
import { IosSimctlDriver, iosDriverConfig } from "../drivers/ios/index.js";
import { EventBus } from "../bus/index.js";
import {
Expand Down Expand Up @@ -169,9 +170,7 @@ describe("discoverDrivers on a host with an Android SDK", () => {
const { drivers, rejections } = await discoverAndroid(filesystem, probe, [4242]);

expect(rejections).toEqual([]);
expect(drivers.find((driver) => driver.platform === "android")?.deviceRoot).toBe(
join(SIMLOCK_HOME, "devices", "android"),
);
expect(drivers.find((driver) => driver.platform === "android")?.deviceRoot).toBe(ANDROID_ROOT);
});

it.each([
Expand All @@ -184,7 +183,7 @@ describe("discoverDrivers on a host with an Android SDK", () => {
},
{
platform: "android",
root: join(SIMLOCK_HOME, "devices", "android"),
root: ANDROID_ROOT,
discover: async (filesystem: MemoryFilesystem) => {
await filesystem.mkdirp(SIMLOCK_HOME);
await filesystem.writeFileAtomic(
Expand All @@ -204,7 +203,7 @@ describe("discoverDrivers on a host with an Android SDK", () => {

expect(rename).toHaveBeenCalledWith(
expect.stringMatching(
new RegExp(`/devices/\\.${platform}\\.${process.pid}\\.[^/]+\\.staging$`),
new RegExp(`[\\\\/]devices[\\\\/]\\.${platform}\\.${process.pid}\\.[^\\\\/]+\\.staging$`),
),
root,
);
Expand All @@ -220,11 +219,11 @@ describe("discoverDrivers on a host with an Android SDK", () => {
);
const processRunner = new ScriptedProcessRunner([
{
match: { args: ["devices"], command: "/android-sdk/platform-tools/adb" },
match: { args: ["devices"], command: binaries.adb },
result: { code: 0, stderr: "", stdout: "List of devices attached\n" },
},
{
match: { args: ["-help"], command: "/android-sdk/emulator/emulator" },
match: { args: ["-help"], command: binaries.emulator },
result: { code: 0, stderr: "", stdout: " -crash-report-mode <mode> crash reporting\n" },
},
]);
Expand Down Expand Up @@ -351,7 +350,7 @@ describe("discoverDrivers on a host with an Android SDK", () => {
{
match: {
args: ["list", "device"],
command: "/android-sdk/cmdline-tools/latest/bin/avdmanager",
command: binaries.avdmanager,
},
result: { code: 0, stderr: "", stdout: "Available devices:\n" },
},
Expand Down Expand Up @@ -460,7 +459,7 @@ describe("discoverDrivers on a host with an Android SDK", () => {
processRunner: runner,
});
const driver = drivers.find((candidate) => candidate.platform === "android");
const avdHome = join(SIMLOCK_HOME, "devices", "android", "device-1");
const avdHome = join(ANDROID_ROOT, "device-1");
await filesystem.mkdirp(join(avdHome, "simlock.avd"));
const ready = driver!.makeReady(
{
Expand Down Expand Up @@ -513,12 +512,7 @@ describe("discoverDrivers on a host with an Android SDK", () => {
async function androidSdk(): Promise<MemoryFilesystem> {
const filesystem = new MemoryFilesystem();
process.env.ANDROID_HOME = "/android-sdk";
for (const binary of [
"/android-sdk/platform-tools/adb",
"/android-sdk/emulator/emulator",
"/android-sdk/cmdline-tools/latest/bin/avdmanager",
"/android-sdk/cmdline-tools/latest/bin/sdkmanager",
]) {
for (const binary of Object.values(binaries)) {
await filesystem.mkdirp(binary.slice(0, binary.lastIndexOf("/")));
await filesystem.writeFileAtomic(binary, "binary");
}
Expand Down Expand Up @@ -567,7 +561,9 @@ describe("discoverDrivers on a host with an Android SDK", () => {
}
});

const IOS_ROOT = join(SIMLOCK_HOME, "devices", "ios");
// why: `ensureOwnedRoot` resolves the configured root, so on Windows the driver reads `D:\home\...`.
const IOS_ROOT = resolve(SIMLOCK_HOME, "devices", "ios");
const ANDROID_ROOT = resolve(SIMLOCK_HOME, "devices", "android");

const INSTANCE_ID = "instance-1";

Expand Down
26 changes: 22 additions & 4 deletions src/daemon/gateway-fleet.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
import { createHmac } from "node:crypto";
import { createServer as createHttpServer, type Server } from "node:http";
import { mkdtemp, rm } from "node:fs/promises";
import { mkdtemp, readdir } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";

Expand All @@ -35,6 +35,7 @@ import type { AlertDestination, PassthroughCommand } from "../core/index.js";
import { FakeDriver, fakeDeviceDataTables } from "../core/testing.js";
import type { Filesystem, IpcConnector, IpcListenerFactory, NpmCli } from "../ports/index.js";
import {
FakeDirectoryAccessReader,
FakeHostInfo,
FakeNpmCli,
FakeUpdaterLauncher,
Expand All @@ -51,6 +52,7 @@ import { connectSimlockAdmin } from "../admin/index.js";
import type { DispatchSession } from "./dispatch.js";
import { startDaemon, type StartDaemonOptions } from "./main.js";
import type { DaemonServer } from "./server.js";
import { removeDirectory } from "../test-support/remove-directory.js";

function passthroughOptions(command: PassthroughCommand): object {
return { passthrough: () => command, passthroughTools: ["adb"] };
Expand Down Expand Up @@ -123,10 +125,15 @@ describe("gateway fleet smoke (ADR 0005 §35)", () => {

afterEach(async () => {
await Promise.all(daemons.splice(0).map((daemon) => daemon.stop("test")));
// why: worker-b's directory stays busy on Windows for a few seconds after its daemon stopped (a leftover handle nobody could name), longer than `removeDirectory` waits, so the removal is tried again until it goes through, for less than the hook's own 30 s so a handle that stays held fails with the removal's own error.
await Promise.all(
directories.splice(0).map((directory) => rm(directory, { force: true, recursive: true })),
directories
.splice(0)
.map((directory) =>
vi.waitFor(() => removeDirectory(directory), { interval: 250, timeout: 25_000 }),
),
);
});
}, 30_000);

async function startGateway(
alerts: {
Expand Down Expand Up @@ -157,6 +164,11 @@ describe("gateway fleet smoke (ADR 0005 §35)", () => {
},
role: "gateway",
dataDirectory: directory,
// why: on Windows the daemon reads the data directory's ACL through the process runner, which a scripted one refuses.
directoryAccess: new FakeDirectoryAccessReader({
entries: [],
user: "S-1-5-21-1-2-3-1001",
}),
filesystem: new MemoryFilesystem(),
hostInfo: new FakeHostInfo(GATEWAY_HOST),
logger: new NoopLogger(),
Expand Down Expand Up @@ -416,7 +428,7 @@ describe("gateway fleet smoke (ADR 0005 §35)", () => {
token: tokenA,
stdout: "hello-from-a",
});
await startWorker({
const workerB = await startWorker({
label: "worker-b",
model: "Pixel-B",
token: tokenB,
Expand Down Expand Up @@ -465,6 +477,12 @@ describe("gateway fleet smoke (ADR 0005 §35)", () => {
expect(chunks.join("")).toBe("hello-from-b");

await gateway.dispatch("lease.release", { leaseId: grant.lease.id }, agentSession());

// why: the lease's folder is removed after `lease.release` answers, and Windows refuses to remove the
// worker's directory while that removal is still running, so the test waits for it.
await vi.waitFor(async () => {
expect(await readdir(join(workerB.directory, "lease-files"))).toEqual([]);
});
}, 30_000);

/**
Expand Down
6 changes: 3 additions & 3 deletions src/daemon/lease-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ describe("a lease's folder", () => {
const filesystem = new GatedFilesystem();
const files = leaseFilesOver(filesystem);

expect(await files.folderOf(grant("CI", 1))).toBe("/data/lease-files/4349-1");
expect(await files.folderOf(grant("ci", 1))).toBe("/data/lease-files/6369-1");
expect(await files.folderOf(grant("ci", 2))).toBe("/data/lease-files/6369-2");
expect(await files.folderOf(grant("CI", 1))).toBe(join("/data", "lease-files", "4349-1"));
expect(await files.folderOf(grant("ci", 1))).toBe(join("/data", "lease-files", "6369-1"));
expect(await files.folderOf(grant("ci", 2))).toBe(join("/data", "lease-files", "6369-2"));

await files.leaseEnd.leaseEnded(ended("ci", 1), new AbortController().signal);

Expand Down
12 changes: 9 additions & 3 deletions src/daemon/lease-requires.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { mkdtemp, rm } from "node:fs/promises";
import { mkdtemp } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";

import { afterEach, describe, expect, it, vi } from "vitest";

import { startOnFreePort } from "../test-support/loopback-port.js";
import { removeDirectory } from "../test-support/remove-directory.js";
import { agentDeviceModuleWith, FakeAgentDevice } from "../agent-device/testing.js";
import { FakeDriver, fakeDeviceDataTables } from "../core/testing.js";
import { MemoryFilesystem, NoopLogger, SystemClock } from "../ports/index.js";
Expand Down Expand Up @@ -40,10 +41,15 @@ describe("lease.request requires, through a gateway", () => {

afterEach(async () => {
await Promise.all(daemons.splice(0).map((daemon) => daemon.stop("test")));
// why: Windows keeps a stopped daemon's directory busy for a few seconds, longer than `removeDirectory` waits, so the removal is tried again until it goes through, for less than the hook's own 30 s so a handle that stays held fails with the removal's own error.
await Promise.all(
directories.splice(0).map((directory) => rm(directory, { force: true, recursive: true })),
directories
.splice(0)
.map((directory) =>
vi.waitFor(() => removeDirectory(directory), { interval: 250, timeout: 25_000 }),
),
);
});
}, 30_000);

async function startGateway() {
const directory = await mkdtemp(join(tmpdir(), "simlock-e2e-requires-gateway-"));
Expand Down
Loading
Loading