-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathgateway-guard-legacy-keepalive-fixture.test.ts
More file actions
209 lines (189 loc) · 6.57 KB
/
Copy pathgateway-guard-legacy-keepalive-fixture.test.ts
File metadata and controls
209 lines (189 loc) · 6.57 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
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import { describe, expect, it, vi } from "vitest";
import {
buildDockerGpuCloneRunArgs,
buildDockerGpuMode,
} from "../../../src/lib/onboard/docker-gpu-patch.ts";
import {
createLegacyKeepaliveFixture,
type LegacyKeepaliveFixtureDeps,
rewriteManagedInspectForLegacyKeepalive,
} from "../live/gateway-guard-legacy-keepalive-fixture.ts";
const OLD_CONTAINER_ID = "a".repeat(64);
const NEW_CONTAINER_ID = "b".repeat(64);
const FIXTURE_PATH = fileURLToPath(
new URL("../live/gateway-guard-legacy-keepalive-fixture.ts", import.meta.url),
);
function successfulResult() {
return {
applied: true as const,
oldContainerId: OLD_CONTAINER_ID,
newContainerId: NEW_CONTAINER_ID,
originalName: "openshell-e2e-2701",
backupContainerName: "openshell-e2e-2701-nemoclaw-gpu-backup-1",
mode: {
kind: "startup-command" as const,
label: "startup command",
device: "",
args: [],
},
backupRemoved: true,
};
}
function managedImageInspect(
entrypoint: string[] = ["/usr/local/bin/nemoclaw-start"],
containerId = OLD_CONTAINER_ID,
command: string[] = ["/bin/bash"],
): string {
return JSON.stringify([
{
Id: containerId,
Image: `sha256:${"c".repeat(64)}`,
Name: "/openshell-e2e-2701",
Config: {
Image: "nemoclaw-managed:test",
Entrypoint: entrypoint,
Cmd: command,
Env: ["OPENSHELL_SANDBOX_COMMAND=env /usr/local/bin/nemoclaw-start"],
},
HostConfig: {},
},
]);
}
describe("gateway guard legacy keepalive fixture", () => {
it("recreates only the pinned sandbox container with the reviewed legacy supervisor contract (#9364)", () => {
const dockerCapture = vi.fn(() => managedImageInspect());
const recreate = vi.fn((_, deps: Parameters<LegacyKeepaliveFixtureDeps["recreate"]>[1]) => {
const rewritten = JSON.parse(
deps?.dockerCapture?.(["inspect", "--type", "container", OLD_CONTAINER_ID], {
ignoreError: true,
}) ?? "null",
);
expect(rewritten[0].Config).toMatchObject({
Entrypoint: ["/opt/openshell/bin/openshell-sandbox"],
Cmd: [],
});
return successfulResult();
});
const result = createLegacyKeepaliveFixture(
{
sandboxName: "e2e-2701",
expectedContainerId: OLD_CONTAINER_ID,
},
{ recreate, dockerCapture },
);
expect(result.newContainerId).toBe(NEW_CONTAINER_ID);
expect(recreate).toHaveBeenCalledOnce();
expect(recreate).toHaveBeenCalledWith(
{
sandboxName: "e2e-2701",
expectedOldContainerId: OLD_CONTAINER_ID,
openshellSandboxCommand: ["sleep", "infinity"],
timeoutSecs: 180,
},
{ dockerCapture: expect.any(Function) },
);
});
it("rejects an unreviewed managed-image entrypoint before legacy recreation (#9364)", () => {
expect(() =>
rewriteManagedInspectForLegacyKeepalive(
managedImageInspect(["/unreviewed/supervisor"]),
OLD_CONTAINER_ID,
),
).toThrow("requires the reviewed managed-image process contract");
});
it("rejects an unreviewed managed-image command before legacy recreation (#9364)", () => {
expect(() =>
rewriteManagedInspectForLegacyKeepalive(
managedImageInspect(["/usr/local/bin/nemoclaw-start"], OLD_CONTAINER_ID, ["/bin/sh"]),
OLD_CONTAINER_ID,
),
).toThrow("requires the reviewed managed-image process contract");
});
it("rejects Docker inspect output for a different container before legacy recreation (#9364)", () => {
expect(() =>
rewriteManagedInspectForLegacyKeepalive(
managedImageInspect(["/usr/local/bin/nemoclaw-start"], NEW_CONTAINER_ID),
OLD_CONTAINER_ID,
),
).toThrow("Docker inspect identity changed");
});
it("produces a clone contract accepted by production startup-command validation (#9364)", () => {
const rewritten = JSON.parse(
rewriteManagedInspectForLegacyKeepalive(managedImageInspect(), OLD_CONTAINER_ID),
);
const immutableImage = `sha256:${"c".repeat(64)}`;
const args = buildDockerGpuCloneRunArgs(rewritten[0], buildDockerGpuMode("startup-command"), {
image: immutableImage,
openshellSandboxCommand: ["sleep", "infinity"],
});
expect(args).toEqual(
expect.arrayContaining([
"--entrypoint",
"/opt/openshell/bin/openshell-sandbox",
"--env",
"OPENSHELL_SANDBOX_COMMAND=sleep infinity",
]),
);
expect(args.slice(args.indexOf(immutableImage))).toEqual([immutableImage]);
});
it.each([
{
name: "an unremoved backup",
result: { ...successfulResult(), backupRemoved: false },
error: "left the original container backup in place",
},
{
name: "a replacement with the wrong mode",
result: {
...successfulResult(),
mode: { ...successfulResult().mode, kind: "cdi" as const },
},
error: "did not use startup-command mode",
},
{
name: "an unchanged container identity",
result: { ...successfulResult(), newContainerId: OLD_CONTAINER_ID },
error: "did not replace the container",
},
])("fails closed for $name", ({ result, error }) => {
const recreate = vi.fn(() => result) as LegacyKeepaliveFixtureDeps["recreate"];
expect(() =>
createLegacyKeepaliveFixture(
{
sandboxName: "e2e-2701",
expectedContainerId: OLD_CONTAINER_ID,
},
{ recreate },
),
).toThrow(error);
});
it("rejects an abbreviated container ID before recreation", () => {
const recreate = vi.fn(() => successfulResult());
expect(() =>
createLegacyKeepaliveFixture(
{
sandboxName: "e2e-2701",
expectedContainerId: "abc123",
},
{ recreate },
),
).toThrow("expected container ID must be a full Docker container ID");
expect(recreate).not.toHaveBeenCalled();
});
it("loads the real recreation dependency through the standalone tsx entrypoint", () => {
const result = spawnSync(
process.execPath,
["--import", "tsx", FIXTURE_PATH, "fixture-import-probe", "f".repeat(64)],
{ encoding: "utf8" },
);
expect(result.status).toBe(1);
expect(result.stderr).toContain(
"Could not find OpenShell Docker container for sandbox 'fixture-import-probe'.",
);
expect(result.stderr).not.toContain("deps.recreate is not a function");
});
});