File size: 5,582 Bytes
76289e7 | 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 | // Shared fixtures for the fleet service suites (service.runtime.test.ts and
// service-upgrade.runtime.test.ts), which exercise the same createFleetService
// seam from opposite ends: cell lifecycle and upgrade/restore.
import path from "node:path";
import { vi } from "vitest";
import { cellOwnerId } from "./cell-profile.js";
import type { FleetContainerInspectResult, FleetContainerRuntime } from "./containers.runtime.js";
import { createFleetService as createFleetServiceRuntime } from "./service.runtime.js";
type FleetServiceOptions = NonNullable<Parameters<typeof createFleetServiceRuntime>[0]>;
export const TEST_ATTEMPT_ID = "22222222222222222222222222222222";
export const NEXT_ATTEMPT_ID = "44444444444444444444444444444444";
// Each suite sets its per-test root in beforeEach before deriving ownership labels.
let suiteRoot = "";
export function setFleetSuiteRoot(root: string): void {
suiteRoot = root;
}
export function createFleetService(options: FleetServiceOptions = {}) {
return createFleetServiceRuntime({ probePort: async () => true, ...options });
}
export function fleetLabels(tenant = "acme", attemptId = TEST_ATTEMPT_ID): Record<string, string> {
return {
"openclaw.fleet.tenant": tenant,
"openclaw.fleet.owner": cellOwnerId(path.join(suiteRoot, "fleet", "cells", tenant)),
"openclaw.fleet.attempt": attemptId,
"openclaw.fleet.env-keys": "FEATURE",
};
}
export function runningInspection(
overrides: Partial<Extract<FleetContainerInspectResult, { kind: "ok" }>> = {},
): Extract<FleetContainerInspectResult, { kind: "ok" }> {
return {
kind: "ok",
containerId: "container-id",
state: "running",
running: true,
labels: fleetLabels(),
environment: {
HOME: "/home/node",
OPENCLAW_GATEWAY_TOKEN: "old-token",
FEATURE: "enabled",
NODE_VERSION: "old-image-default",
},
imageId: "sha256:old-image-id",
memory: "2147483648",
cpus: "2",
pidsLimit: 512,
storageOpt: {},
capDrop: ["ALL"],
effectiveCaps: undefined,
securityOpt: ["no-new-privileges"],
init: true,
restartPolicy: "unless-stopped",
portBindings: [{ containerPort: "18789/tcp", hostIp: "127.0.0.1", hostPort: "19100" }],
...overrides,
};
}
export function createContainerMock(
initialInspection: FleetContainerInspectResult = {
kind: "missing",
state: "missing",
},
) {
const assertLocal = vi.fn<FleetContainerRuntime["assertLocal"]>(async () => undefined);
const inspections = new Map<string, FleetContainerInspectResult>();
const removedContainers = new Set<string>();
const inspect = vi.fn<FleetContainerRuntime["inspect"]>(async (_runtime, name) =>
removedContainers.has(name)
? { kind: "missing", state: "missing" }
: (inspections.get(name) ?? initialInspection),
);
const networks = new Map<
string,
Extract<Awaited<ReturnType<FleetContainerRuntime["inspectNetwork"]>>, { kind: "ok" }>
>();
const inspectNetwork = vi.fn<FleetContainerRuntime["inspectNetwork"]>(
async (_runtime, name) => networks.get(name) ?? { kind: "missing" },
);
const isDockerRootless = vi.fn<FleetContainerRuntime["isDockerRootless"]>(async () => false);
const run = vi.fn<FleetContainerRuntime["run"]>(async (profile, start) => {
removedContainers.delete(profile.containerName);
inspections.set(
profile.containerName,
runningInspection({
state: start ? "running" : "created",
running: start,
labels: {
...fleetLabels(profile.tenantId, profile.attemptId),
"openclaw.fleet.env-keys": profile.userEnvironmentKeys.toSorted().join(","),
},
environment: { ...profile.environment },
containerId: `container-${profile.attemptId}`,
imageId: `sha256:${profile.attemptId}`,
memory: profile.memory,
cpus: profile.cpus,
pidsLimit: profile.pidsLimit,
}),
);
});
const pull = vi.fn<FleetContainerRuntime["pull"]>(async () => undefined);
const createNetwork = vi.fn<FleetContainerRuntime["createNetwork"]>(
async (_runtime, name, labels, options) => {
networks.set(name, {
kind: "ok",
labels: { ...labels },
attachedContainers: [],
internal: options.internal,
});
},
);
const removeNetwork = vi.fn<FleetContainerRuntime["removeNetwork"]>(async (_runtime, name) => {
networks.delete(name);
});
const start = vi.fn<FleetContainerRuntime["start"]>(async (_runtime, name) => {
const current = await inspect("docker", name);
if (current.kind === "ok") {
inspections.set(name, { ...current, state: "running", running: true });
}
});
const stop = vi.fn<FleetContainerRuntime["stop"]>(async () => undefined);
const restart = vi.fn<FleetContainerRuntime["restart"]>(async () => undefined);
const logs = vi.fn<FleetContainerRuntime["logs"]>(async () => undefined);
const remove = vi.fn<FleetContainerRuntime["remove"]>(async (_runtime, name) => {
inspections.delete(name);
removedContainers.add(name);
inspect.mockResolvedValue({ kind: "missing", state: "missing" });
});
return {
runtime: {
assertLocal,
inspect,
inspectNetwork,
isDockerRootless,
run,
pull,
createNetwork,
removeNetwork,
start,
stop,
restart,
logs,
remove,
},
assertLocal,
inspect,
inspectNetwork,
isDockerRootless,
run,
pull,
createNetwork,
removeNetwork,
start,
stop,
restart,
logs,
remove,
};
}
|