Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
66 changes: 66 additions & 0 deletions test/e2e/fixtures/phases/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,52 @@ export function buildOpenShellGatewayUserServiceRemovalScript(): string {
].join("\n");
}

export function buildOpenShellGatewayUserServiceStopScript(): string {
return [
"set -eu",
'if [ "$(uname -s)" = Darwin ] && command -v brew >/dev/null 2>&1 && brew list --formula openshell >/dev/null 2>&1; then',
' brew info --json=v2 openshell | grep -Eq \'"tap"[[:space:]]*:[[:space:]]*"nvidia/openshell"\' || exit 1',
" brew services stop openshell",
" exit 0",
"fi",
`if ! command -v systemctl >/dev/null 2>&1; then exit ${USER_SERVICE_UNAVAILABLE_EXIT}; fi`,
"service=",
'if upstream_metadata="$(LC_ALL=C systemctl --user show openshell-gateway.service --property=ActiveState --property=FragmentPath --property=ExecStart 2>/dev/null)"; then',
" active_state=\"$(printf '%s\\n' \"$upstream_metadata\" | sed -n 's/^ActiveState=//p')\"",
" fragment_path=\"$(printf '%s\\n' \"$upstream_metadata\" | sed -n 's/^FragmentPath=//p')\"",
" exec_start=\"$(printf '%s\\n' \"$upstream_metadata\" | sed -n 's/^ExecStart=//p')\"",
" gateway_bin=\"$(printf '%s\\n' \"$exec_start\" | grep -oE 'path=[^ ;}]+' | sed 's/^path=//' || true)\"",
" upstream_trusted=1",
' case "$fragment_path" in',
" /usr/local/lib/systemd/user/openshell-gateway.service|/usr/lib/systemd/user/openshell-gateway.service|/lib/systemd/user/openshell-gateway.service) ;;",
" *) upstream_trusted=0 ;;",
" esac",
' case "$gateway_bin" in',
" /usr/local/bin/openshell-gateway|/usr/bin/openshell-gateway) ;;",
" *) upstream_trusted=0 ;;",
" esac",
' if [ "$active_state" = active ] && [ "$upstream_trusted" -eq 1 ]; then service=openshell-gateway; fi',
"else",
" systemctl --user show-environment >/dev/null",
"fi",
'if [ -z "$service" ]; then',
' case "${XDG_CONFIG_HOME:-}" in',
' /*) config_home="$XDG_CONFIG_HOME" ;;',
' *) config_home="$HOME/.config" ;;',
" esac",
' unit="$config_home/systemd/user/nemoclaw-openshell-gateway.service"',
` if [ -L "$unit" ] || [ ! -f "$unit" ]; then exit ${USER_SERVICE_UNAVAILABLE_EXIT}; fi`,
` grep -Fxq '${NEMOCLAW_OPENSHELL_GATEWAY_USER_SERVICE_MARKER_LINE}' "$unit" || exit ${USER_SERVICE_UNAVAILABLE_EXIT}`,
" service=nemoclaw-openshell-gateway",
' if ! systemctl --user is-active --quiet "$service"; then',
" systemctl --user show-environment >/dev/null",
` exit ${USER_SERVICE_UNAVAILABLE_EXIT}`,
" fi",
"fi",
'systemctl --user stop "$service"',
].join("\n");
}

export function buildOpenShellGatewayUserServiceRestartScript(): string {
return [
"set -eu",
Expand Down Expand Up @@ -596,6 +642,8 @@ export class LifecyclePhaseFixture {
timeoutMs: 30_000,
},
);
if (await this.stopOpenShellGatewayUserService()) return runtime;

await this.host.command(
"sh",
["-lc", "command -v openshell >/dev/null 2>&1 && openshell gateway stop -g nemoclaw || true"],
Expand Down Expand Up @@ -664,6 +712,24 @@ export class LifecyclePhaseFixture {
return runtime;
}

private async stopOpenShellGatewayUserService(): Promise<boolean> {
const result = await this.host.command(
"sh",
["-lc", buildOpenShellGatewayUserServiceStopScript()],
{
artifactName: "lifecycle-gateway-user-service-stop",
env: buildAvailabilityProbeEnv(),
timeoutMs: 120_000,
},
);
if (result.exitCode === 0) return true;
if (result.exitCode === USER_SERVICE_UNAVAILABLE_EXIT) return false;
throw new Error(
`OpenShell gateway user service stop failed during lifecycle qualification: ` +
`${result.stderr || result.stdout || `exit ${String(result.exitCode)}`}`,
);
}

async startGatewayRuntime(
previousRuntime: HostGatewayRuntime | null,
options: { requireUserService?: boolean; sandboxName?: string } = {},
Expand Down
55 changes: 34 additions & 21 deletions test/e2e/support/e2e-phase-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,7 @@ describe("LifecyclePhaseFixture.simulate post-reboot-recovery (stop-original)",
runner.enqueue(shellResult(0, "openshell-cluster-e2e-cloud-oc\n")); // discover
runner.enqueue(shellResult(0)); // docker stop
runner.enqueue(shellResult(0)); // forward stop
runner.enqueue(shellResult(0)); // gateway stop
runner.enqueue(shellResult(0)); // pid stop
runner.enqueue(shellResult(0, "gateway-id\topenshell-cluster-nemoclaw\n")); // discover
runner.enqueue(shellResult(0)); // container stop
runner.enqueue(shellResult(0)); // user service stop
runner.enqueue(shellResult(0)); // user service restart
runner.enqueue(shellResult(0, "Connected to nemoclaw\n")); // openshell status
runner.enqueue(shellResult(0)); // boot-owned docker start
Expand All @@ -278,10 +275,7 @@ describe("LifecyclePhaseFixture.simulate post-reboot-recovery (stop-original)",
"docker container ps --all --filter label=openshell.ai/sandbox-name=e2e-cloud-oc --format {{.Names}}",
"docker container stop openshell-cluster-e2e-cloud-oc",
"sh -lc command -v openshell >/dev/null 2>&1 && openshell forward stop 18789 || true",
"sh -lc command -v openshell >/dev/null 2>&1 && openshell gateway stop -g nemoclaw || true",
expect.stringContaining("sh -lc pid_file="),
"docker container ps --format {{.ID}}\t{{.Names}}",
"docker container stop gateway-id",
expect.stringContaining('systemctl --user stop "$service"'),
expect.stringContaining('systemctl --user cat "$service"'),
"openshell status",
"docker container start openshell-cluster-e2e-cloud-oc",
Expand All @@ -301,9 +295,7 @@ describe("LifecyclePhaseFixture.simulate post-reboot-recovery (stop-original)",
runner.enqueue(shellResult(0, "container-1\n")); // discover
runner.enqueue(shellResult(0)); // docker stop
runner.enqueue(shellResult(0)); // forward stop
runner.enqueue(shellResult(0)); // gateway stop
runner.enqueue(shellResult(0)); // pid stop
runner.enqueue(shellResult(0)); // container stop
runner.enqueue(shellResult(0)); // user service stop
runner.enqueue(shellResult(0)); // user service restart
runner.enqueue(shellResult(0, "Connected to nemoclaw\n")); // openshell status
runner.enqueue(shellResult(0)); // boot-owned docker start
Expand All @@ -322,9 +314,7 @@ describe("LifecyclePhaseFixture.simulate post-reboot-recovery (stop-original)",
runner.enqueue(shellResult(0, "container-1\n")); // discover
runner.enqueue(shellResult(0)); // docker stop
runner.enqueue(shellResult(0)); // forward stop
runner.enqueue(shellResult(0)); // gateway stop
runner.enqueue(shellResult(0)); // pid stop
runner.enqueue(shellResult(0)); // container stop
runner.enqueue(shellResult(0)); // user service stop
runner.enqueue(shellResult(0)); // user service restart
runner.enqueue(shellResult(0, "Connected to nemoclaw\n")); // openshell status
runner.enqueue(shellResult(0)); // boot-owned docker start
Expand Down Expand Up @@ -384,9 +374,7 @@ describe("LifecyclePhaseFixture.simulate post-reboot-recovery (stop-original)",
runner.enqueue(shellResult(0, "container-1\n")); // discover
runner.enqueue(shellResult(0)); // docker stop
runner.enqueue(shellResult(0)); // forward stop
runner.enqueue(shellResult(0)); // gateway stop
runner.enqueue(shellResult(0)); // pid stop
runner.enqueue(shellResult(0)); // container stop
runner.enqueue(shellResult(0)); // user service stop
runner.enqueue(shellResult(75, "")); // no managed user service available

await expect(prepared.simulate("post-reboot-recovery", instance())).rejects.toThrow(
Expand All @@ -408,9 +396,7 @@ describe("LifecyclePhaseFixture.simulate post-reboot-recovery (rename-to-gpu-bac
runner.enqueue(shellResult(0)); // docker stop
runner.enqueue(shellResult(0)); // docker rename
runner.enqueue(shellResult(0)); // forward stop
runner.enqueue(shellResult(0)); // gateway stop
runner.enqueue(shellResult(0)); // pid stop
runner.enqueue(shellResult(0)); // container stop
runner.enqueue(shellResult(0)); // user service stop
runner.enqueue(shellResult(0)); // user service restart
runner.enqueue(shellResult(0, "Connected to nemoclaw\n")); // openshell status
runner.enqueue(shellResult(0)); // boot-owned docker start
Expand Down Expand Up @@ -497,10 +483,11 @@ describe("LifecyclePhaseFixture rebuild helpers", () => {
});

describe("LifecyclePhaseFixture gateway runtime restart helpers", () => {
it("stops PID/container runtimes, starts the previous runtime shape, and polls health", async () => {
it("falls back to PID/container controls when the selected user service is inactive (#10947)", async () => {
const runner = new FakeRunner();
runner.enqueue(shellResult(0, "12345\n")); // resolveHostRuntime pid probe
runner.enqueue(shellResult(0)); // forward stop
runner.enqueue(shellResult(75)); // selected user service is inactive
runner.enqueue(shellResult(0)); // gateway stop
runner.enqueue(shellResult(0)); // pid stop
runner.enqueue(shellResult(0)); // container stop
Expand All @@ -524,6 +511,7 @@ describe("LifecyclePhaseFixture gateway runtime restart helpers", () => {
expect(runner.calls.map((call) => `${call.command} ${call.args.join(" ")}`)).toEqual([
expect.stringContaining("sh -lc pid_file="),
"sh -lc command -v openshell >/dev/null 2>&1 && openshell forward stop 18789 || true",
expect.stringContaining("sh -lc set -eu"),
"sh -lc command -v openshell >/dev/null 2>&1 && openshell gateway stop -g nemoclaw || true",
expect.stringContaining("sh -lc pid_file="),
"docker container ps --format {{.ID}}\t{{.Names}}",
Expand Down Expand Up @@ -562,6 +550,7 @@ describe("LifecyclePhaseFixture gateway runtime restart helpers", () => {
it("stops only the exact gateway container when a sandbox has the gateway-name prefix", async () => {
const runner = new FakeRunner();
runner.enqueue(shellResult(0)); // forward stop
runner.enqueue(shellResult(75)); // no user service available
runner.enqueue(shellResult(0)); // gateway stop
runner.enqueue(shellResult(0)); // pid stop
runner.enqueue(shellResult(0, "gateway-id\topenshell-cluster-nemoclaw\n")); // discover
Expand All @@ -580,6 +569,30 @@ describe("LifecyclePhaseFixture gateway runtime restart helpers", () => {
expect(discovery?.args).toEqual(["container", "ps", "--format", "{{.ID}}\t{{.Names}}"]);
});

it("stops a supported user service without invoking legacy runtime controls (#10947)", async () => {
const runner = new FakeRunner();
runner.enqueue(shellResult(0)); // forward stop
runner.enqueue(shellResult(0)); // user service stop

await fixture(runner, new FakeCleanup()).stopGatewayRuntime();

expect(runner.calls.map((call) => call.options?.artifactName)).toEqual([
"lifecycle-gateway-forward-stop",
"lifecycle-gateway-user-service-stop",
]);
});

it("reports a user-service stop failure without invoking legacy controls (#10947)", async () => {
const runner = new FakeRunner();
runner.enqueue(shellResult(0)); // forward stop
runner.enqueue(shellResult(1, "Failed to connect to bus"));

await expect(fixture(runner, new FakeCleanup()).stopGatewayRuntime()).rejects.toThrow(
/user service stop failed.*Failed to connect to bus/,
);
expect(runner.calls).toHaveLength(2);
});

it("can recover a PID runtime through sandbox-specific status", async () => {
const runner = new FakeRunner();
runner.enqueue(shellResult(0, "status recovered\n"));
Expand Down
Loading