diff --git a/.github/workflows/e2e-vitest-scenarios.yaml b/.github/workflows/e2e-vitest-scenarios.yaml index 2393329a5b3..25cd2009261 100644 --- a/.github/workflows/e2e-vitest-scenarios.yaml +++ b/.github/workflows/e2e-vitest-scenarios.yaml @@ -3457,6 +3457,109 @@ jobs: if-no-files-found: ignore retention-days: 14 + device-auth-health-vitest: + needs: generate-matrix + if: ${{ (inputs.jobs == '' && inputs.scenarios == '') || contains(format(',{0},', inputs.jobs), ',device-auth-health-vitest,') || contains(format(',{0},', inputs.scenarios), ',device-auth-health,') }} + runs-on: ubuntu-latest + timeout-minutes: 40 + env: + FREE_STANDING_VITEST_JOB: "1" + FREE_STANDING_SCENARIO_ID: "device-auth-health" + E2E_ARTIFACT_DIR: ${{ github.workspace }}/e2e-artifacts/vitest/device-auth-health + NEMOCLAW_CLI_BIN: ${{ github.workspace }}/bin/nemoclaw.js + NEMOCLAW_RUN_E2E_SCENARIOS: "1" + NEMOCLAW_NON_INTERACTIVE: "1" + NEMOCLAW_ACCEPT_THIRD_PARTY_SOFTWARE: "1" + NEMOCLAW_SANDBOX_NAME: "e2e-health-auth" + NEMOCLAW_DASHBOARD_PORT: "18789" + OPENSHELL_GATEWAY: "nemoclaw" + steps: + - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 + with: + persist-credentials: false + + - name: Configure isolated Docker auth directory + run: echo "DOCKER_CONFIG=${RUNNER_TEMP}/docker-config-device-auth-health" >> "$GITHUB_ENV" + + - name: Authenticate to Docker Hub + env: + DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + shell: bash + run: | + set -euo pipefail + if [[ -z "${DOCKERHUB_USERNAME}" || -z "${DOCKERHUB_TOKEN}" ]]; then + echo "::notice::Docker Hub credentials not configured; continuing with anonymous pulls." + exit 0 + fi + mkdir -p "${DOCKER_CONFIG}" + chmod 700 "${DOCKER_CONFIG}" + login_succeeded=0 + for attempt in 1 2 3; do + if echo "${DOCKERHUB_TOKEN}" | timeout 30s docker login docker.io --username "${DOCKERHUB_USERNAME}" --password-stdin; then + login_succeeded=1 + break + fi + if [[ "$attempt" -lt 3 ]]; then + echo "::warning::Docker Hub login attempt ${attempt} failed; retrying." + sleep 5 + fi + done + if [[ "$login_succeeded" -ne 1 ]]; then + echo "::warning::Docker Hub login failed after 3 attempts; continuing with anonymous pulls." + fi + + - name: Set up Node + uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.0.0 + with: + node-version: 22 + cache: npm + + - name: Install root dependencies + run: npm ci --ignore-scripts + + - name: Build CLI + run: npm run build:cli + + - name: Install OpenShell CLI + run: bash scripts/install-openshell.sh + + - name: Run device auth health live Vitest test + env: + NVIDIA_INFERENCE_API_KEY: ${{ secrets.NVIDIA_INFERENCE_API_KEY }} + run: | + set -euo pipefail + export PATH="$HOME/.local/bin:$HOME/.npm-global/bin:$PATH" + if command -v openshell >/dev/null 2>&1; then + OPENSHELL_BIN="$(command -v openshell)" + elif [ -x "$HOME/.local/bin/openshell" ]; then + OPENSHELL_BIN="$HOME/.local/bin/openshell" + else + echo "::error::OpenShell CLI not found after install" + ls -la /usr/local/bin/openshell "$HOME/.local/bin/openshell" 2>&1 || true + exit 1 + fi + export OPENSHELL_BIN + "$OPENSHELL_BIN" --version + npx vitest run --project e2e-scenarios-live test/e2e-scenario/live/device-auth-health.test.ts --silent=false --reporter=default + + - name: Upload device auth health artifacts + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: e2e-vitest-scenarios-device-auth-health + path: e2e-artifacts/vitest/device-auth-health/ + include-hidden-files: false + if-no-files-found: ignore + retention-days: 14 + + - name: Clean up Docker auth + if: always() + run: | + set -euo pipefail + docker logout docker.io || true + rm -rf "${DOCKER_CONFIG}" + channels-add-remove-vitest: needs: generate-matrix if: ${{ (inputs.jobs == '' && inputs.scenarios == '') || contains(format(',{0},', inputs.jobs), ',channels-add-remove-vitest,') || contains(format(',{0},', inputs.scenarios), ',channels-add-remove,') }} @@ -3729,6 +3832,7 @@ jobs: bedrock-runtime-compatible-anthropic-vitest, issue-2478-crash-loop-recovery-vitest, gateway-health-honest-vitest, + device-auth-health-vitest, channels-add-remove-vitest, ] if: ${{ always() && github.event_name == 'workflow_dispatch' }} diff --git a/test/e2e-scenario/live/device-auth-health-helpers.ts b/test/e2e-scenario/live/device-auth-health-helpers.ts new file mode 100644 index 00000000000..bc809a6d810 --- /dev/null +++ b/test/e2e-scenario/live/device-auth-health-helpers.ts @@ -0,0 +1,157 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import fs from "node:fs"; +import path from "node:path"; + +import { buildAvailabilityProbeEnv } from "../fixtures/availability-env.ts"; +import type { HostCliClient } from "../fixtures/clients/host.ts"; +import { resultText } from "../fixtures/clients/index.ts"; +import { + type SandboxClient, + trustedSandboxShellScript, + validateSandboxName, +} from "../fixtures/clients/sandbox.ts"; +import type { ShellProbeResult } from "../fixtures/shell-probe.ts"; +import { isTransientProviderValidationFailure } from "./network-policy-transient-provider.ts"; + +const REPO_ROOT = path.resolve(import.meta.dirname, "../../.."); +export const SANDBOX_NAME = process.env.NEMOCLAW_SANDBOX_NAME ?? "e2e-health-auth"; +validateSandboxName(SANDBOX_NAME); +export const DASHBOARD_PORT = process.env.NEMOCLAW_DASHBOARD_PORT ?? "18789"; +const INSTALL_ATTEMPTS = process.env.CI === "true" || process.env.GITHUB_ACTIONS === "true" ? 3 : 1; + +export function commandEnv(apiKey?: string): NodeJS.ProcessEnv { + const env: NodeJS.ProcessEnv = { + ...buildAvailabilityProbeEnv(), + NEMOCLAW_ACCEPT_THIRD_PARTY_SOFTWARE: "1", + NEMOCLAW_DASHBOARD_PORT: DASHBOARD_PORT, + NEMOCLAW_NON_INTERACTIVE: "1", + NEMOCLAW_RECREATE_SANDBOX: "1", + NEMOCLAW_SANDBOX_NAME: SANDBOX_NAME, + OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY ?? "nemoclaw", + }; + apiKey && Object.assign(env, { NVIDIA_INFERENCE_API_KEY: apiKey, NVIDIA_API_KEY: apiKey }); + return env; +} + +export async function bestEffort(run: () => Promise): Promise { + try { + await run(); + } catch { + // Best-effort cleanup/recovery probes should not hide primary failures. + } +} + +export function assertDockerAvailable( + result: ShellProbeResult, + skip: (note?: string) => never, +): void { + result.exitCode === 0 || process.env.GITHUB_ACTIONS === "true" + ? undefined + : skip(`Docker is required for device auth health E2E: ${resultText(result)}`); + result.exitCode === 0 || + process.env.GITHUB_ACTIONS !== "true" || + (() => { + throw new Error(`Docker is required for device auth health E2E: ${resultText(result)}`); + })(); +} + +export async function httpCodeFromSandbox( + sandbox: SandboxClient, + urlPath: string, + artifactName: string, +): Promise { + return await sandbox.execShell( + SANDBOX_NAME, + trustedSandboxShellScript( + `curl -so /dev/null -w '%{http_code}' --max-time 3 http://localhost:${DASHBOARD_PORT}${urlPath}`, + ), + { + artifactName, + env: commandEnv(), + timeoutMs: 30_000, + }, + ); +} + +export async function cleanupDeviceAuthSandbox( + host: HostCliClient, + sandbox: SandboxClient, +): Promise { + await bestEffort(() => + host.nemoclaw([SANDBOX_NAME, "destroy", "--yes"], { + artifactName: "cleanup-nemoclaw-destroy-device-auth-health", + env: commandEnv(), + timeoutMs: 120_000, + }), + ); + await bestEffort(() => + sandbox.openshell(["sandbox", "delete", SANDBOX_NAME], { + artifactName: "cleanup-openshell-delete-device-auth-health", + env: commandEnv(), + timeoutMs: 60_000, + }), + ); +} + +export async function installDeviceAuthSandbox( + host: HostCliClient, + apiKey: string, + installLog: string, +): Promise { + let install: ShellProbeResult | undefined; + for (let attempt = 1; attempt <= INSTALL_ATTEMPTS; attempt += 1) { + install = await host.command("bash", ["install.sh", "--non-interactive"], { + artifactName: + attempt === 1 + ? "phase-1-install-device-auth-health" + : `phase-1-install-device-auth-health-attempt-${attempt}`, + cwd: REPO_ROOT, + env: commandEnv(apiKey), + redactionValues: [apiKey], + timeoutMs: 20 * 60_000, + }); + fs.writeFileSync(installLog, resultText(install)); + const shouldRetry = + install.exitCode !== 0 && + isTransientProviderValidationFailure(install) && + attempt < INSTALL_ATTEMPTS; + install.exitCode === 0 && (attempt = INSTALL_ATTEMPTS + 1); + shouldRetry && (await new Promise((resolve) => setTimeout(resolve, 10_000 * attempt))); + !shouldRetry && install.exitCode !== 0 && (attempt = INSTALL_ATTEMPTS + 1); + } + if (!install) throw new Error("install command did not run"); + return install; +} + +export async function maybeWriteHostHealthExpectation( + hostHealth: ShellProbeResult, + expectCode: (codes: string[], message: string, actual: string) => void, +): Promise { + const code = hostHealth.stdout.trim(); + code && + code !== "000" && + expectCode(["200", "401"], `host dashboard health returned ${code}`, code); +} + +export async function waitForRecoveryArtifact( + artifacts: { writeJson(path: string, value: unknown): Promise }, + sandbox: SandboxClient, +): Promise { + for (let attempt = 1; attempt <= 30; attempt += 1) { + const recoveredHealth = await httpCodeFromSandbox( + sandbox, + "/health", + `phase-5-recovery-health-code-attempt-${attempt}`, + ); + const code = recoveredHealth.stdout.trim(); + const recovered = code === "200" || code === "401"; + recovered && (await artifacts.writeJson("gateway-recovered.json", { attempt, code })); + if (recovered) return; + await new Promise((resolve) => setTimeout(resolve, 5_000)); + } + await artifacts.writeJson("gateway-recovery-inconclusive.json", { + reason: "Gateway did not recover within 150s; legacy shell treated this as optional.", + }); +} diff --git a/test/e2e-scenario/live/device-auth-health.test.ts b/test/e2e-scenario/live/device-auth-health.test.ts new file mode 100644 index 00000000000..c6de3431c28 --- /dev/null +++ b/test/e2e-scenario/live/device-auth-health.test.ts @@ -0,0 +1,142 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +/** + * Live Vitest replacement for test/e2e/test-device-auth-health.sh. + * + * Preserves the legacy #2342 contract with real install/onboard, sandbox HTTP + * probes, `nemoclaw status`, host port-forward checks, and gateway recovery: + * device-auth 401 responses must not be misreported as Health Offline. + */ + +import { buildAvailabilityProbeEnv } from "../fixtures/availability-env.ts"; +import { resultText } from "../fixtures/clients/index.ts"; +import { trustedSandboxShellScript } from "../fixtures/clients/sandbox.ts"; +import { expect, test } from "../fixtures/e2e-test.ts"; +import { shouldRunLiveE2EScenarios } from "../fixtures/live-project-gate.ts"; +import { + assertDockerAvailable, + bestEffort, + cleanupDeviceAuthSandbox, + commandEnv, + DASHBOARD_PORT, + httpCodeFromSandbox, + installDeviceAuthSandbox, + maybeWriteHostHealthExpectation, + SANDBOX_NAME, + waitForRecoveryArtifact, +} from "./device-auth-health-helpers.ts"; + +const LIVE_TIMEOUT_MS = 30 * 60_000; + +function assertStatusNotOffline(output: string, context: string): void { + expect(output, `${context} must not report the #2342 false Health Offline state`).not.toMatch( + /offline/i, + ); +} + +test.skipIf(!shouldRunLiveE2EScenarios())( + "device auth health probes treat 401 as live instead of offline (#2342)", + { timeout: LIVE_TIMEOUT_MS }, + async ({ artifacts, cleanup, host, sandbox, secrets, skip }) => { + const apiKey = secrets.required("NVIDIA_INFERENCE_API_KEY"); + const installLog = artifacts.pathFor("phase-1-install-device-auth-health.log"); + + await artifacts.writeJson("scenario.json", { + id: "device-auth-health", + runner: "vitest", + legacySource: "test/e2e/test-device-auth-health.sh", + boundary: "install.sh + OpenShell sandbox exec + NemoClaw status + host curl", + sandboxName: SANDBOX_NAME, + dashboardPort: DASHBOARD_PORT, + contracts: [ + "onboard succeeds with device auth enabled", + "/health is reachable from inside the sandbox", + "the authenticated dashboard root may return 401 without being treated as offline", + "nemoclaw status reports the gateway as live, not Health Offline", + "status remains non-offline after a gateway kill/recovery attempt", + ], + }); + + const dockerInfo = await host.command("docker", ["info"], { + artifactName: "phase-0-docker-info", + env: buildAvailabilityProbeEnv(), + timeoutMs: 30_000, + }); + assertDockerAvailable(dockerInfo, skip); + + cleanup.add(`destroy device-auth sandbox ${SANDBOX_NAME}`, () => + cleanupDeviceAuthSandbox(host, sandbox), + ); + await bestEffort(() => cleanupDeviceAuthSandbox(host, sandbox)); + + const install = await installDeviceAuthSandbox(host, apiKey, installLog); + expect(install.exitCode, resultText(install)).toBe(0); + + await host.expectListed(SANDBOX_NAME, { + artifactName: "phase-1-nemoclaw-list-device-auth-health", + env: commandEnv(), + timeoutMs: 60_000, + }); + + const health = await httpCodeFromSandbox(sandbox, "/health", "phase-2-sandbox-health-code"); + expect(health.exitCode, resultText(health)).toBe(0); + expect(health.stdout.trim()).toBe("200"); + + const root = await httpCodeFromSandbox(sandbox, "/", "phase-2-sandbox-root-code"); + expect(root.exitCode, resultText(root)).toBe(0); + expect(["200", "401"], `dashboard root returned ${root.stdout.trim()}`).toContain( + root.stdout.trim(), + ); + + const status = await host.nemoclaw([SANDBOX_NAME, "status"], { + artifactName: "phase-3-nemoclaw-status-device-auth-health", + env: commandEnv(), + timeoutMs: 120_000, + }); + expect(status.exitCode, resultText(status)).toBe(0); + assertStatusNotOffline(resultText(status), "initial status"); + expect(resultText(status)).toMatch(/running|online|healthy|OpenClaw|Ready/i); + + const hostHealth = await host.command( + "curl", + [ + "-so", + "/dev/null", + "-w", + "%{http_code}", + "--max-time", + "5", + `http://127.0.0.1:${DASHBOARD_PORT}/health`, + ], + { + artifactName: "phase-4-host-health-code", + env: buildAvailabilityProbeEnv(), + timeoutMs: 30_000, + }, + ); + await maybeWriteHostHealthExpectation(hostHealth, (codes, message, actual) => + expect(codes, message).toContain(actual), + ); + + await sandbox.execShell( + SANDBOX_NAME, + trustedSandboxShellScript("pkill -f 'openclaw.*gateway' 2>/dev/null || true"), + { + artifactName: "phase-5-kill-gateway-process", + env: commandEnv(), + timeoutMs: 30_000, + }, + ); + await new Promise((resolve) => setTimeout(resolve, 3_000)); + + const recoveryStatus = await host.nemoclaw([SANDBOX_NAME, "status"], { + artifactName: "phase-5-nemoclaw-status-after-gateway-kill", + env: commandEnv(), + timeoutMs: 120_000, + }); + expect(recoveryStatus.exitCode, resultText(recoveryStatus)).toBe(0); + assertStatusNotOffline(resultText(recoveryStatus), "recovery status"); + await waitForRecoveryArtifact(artifacts, sandbox); + }, +); diff --git a/test/e2e-scenario/support-tests/e2e-scenarios-workflow.test.ts b/test/e2e-scenario/support-tests/e2e-scenarios-workflow.test.ts index 2e3e22e0a7c..c812fb20caa 100644 --- a/test/e2e-scenario/support-tests/e2e-scenarios-workflow.test.ts +++ b/test/e2e-scenario/support-tests/e2e-scenarios-workflow.test.ts @@ -8,14 +8,13 @@ import path from "node:path"; import { describe, expect, it } from "vitest"; import YAML from "yaml"; - -import { testTimeoutOptions } from "../../helpers/timeouts"; import { evaluateE2eVitestWorkflowDispatchSelectors, readFreeStandingJobsInventory, validateE2eVitestScenariosWorkflowBoundary, validateFreeStandingWorkflowInventory, } from "../../../tools/e2e-scenarios/workflow-boundary.mts"; +import { testTimeoutOptions } from "../../helpers/timeouts"; function readWorkflow(): Record { return YAML.parse( @@ -565,7 +564,7 @@ describe("e2e-vitest-scenarios workflow boundary", () => { }, ); - it("derives the free-standing inventory from workflow job metadata", () => { + it("derives the free-standing inventory from workflow job metadata", { timeout: 60_000 }, () => { const inventory = readFreeStandingJobsInventory(); expect(validateFreeStandingWorkflowInventory()).toEqual([]); expect(inventory.allowedJobs).toContain("openshell-version-pin-vitest");