diff --git a/scripts/checks/vitest-project-overlap.mts b/scripts/checks/vitest-project-overlap.mts index ecc6abd57d1..7a80a4348c1 100644 --- a/scripts/checks/vitest-project-overlap.mts +++ b/scripts/checks/vitest-project-overlap.mts @@ -49,6 +49,7 @@ const INSTALLER_INTEGRATION_TESTS = new Set([ "test/install-station-dgx-os.test.ts", "test/install-station-docker-repository.test.ts", "test/install-station-host-preparation.test.ts", + "test/install-station-package-state.test.ts", "test/install-station-package-transaction.test.ts", ]); diff --git a/scripts/prepare-dgx-station-host.sh b/scripts/prepare-dgx-station-host.sh index 0dcf7f6c36f..4733336d5fa 100755 --- a/scripts/prepare-dgx-station-host.sh +++ b/scripts/prepare-dgx-station-host.sh @@ -33,6 +33,7 @@ readonly DRIVER_VERSION="610.43.02" readonly BASEOS_DRIVER_VERSION="595.58.03" readonly DOCKER_VERSION="29.6.1" readonly TOOLKIT_VERSION="1.19.1" +readonly STATION_PACKAGE_ARCH="arm64" readonly FACTORY_DKMS_VERSION="3.0.11-1ubuntu13" readonly TARGET_DKMS_VERSION="1:3.4.0-1ubuntu1" # Keep this as a plain Ubuntu image: NVIDIA Container Toolkit injects the host @@ -583,23 +584,51 @@ installed_version() { dpkg-query -W -f='${Version}' "$1" 2>/dev/null || true } +installed_package_record() { + local record status + if record="$(LC_ALL=C dpkg-query -W -f='${db:Status-Abbrev}|${Architecture}|${Version}\n' "$1" 2>/dev/null)"; then + printf '%s' "$record" + return 0 + else + status=$? + fi + return "$status" +} + package_is_exact() { - local spec=$1 - local name expected actual - name="$(package_name "$spec")" - expected="$(package_expected_version "$spec")" - actual="$(installed_version "$name")" - [[ "$actual" == "$expected" ]] + [[ "$(package_state "$1")" == "exact" ]] } package_state() { local spec=$1 - local name expected actual + local name expected record query_status status architecture actual extra name="$(package_name "$spec")" expected="$(package_expected_version "$spec")" - actual="$(installed_version "$name")" - if [[ -z "$actual" ]]; then + + if record="$(installed_package_record "$name")"; then + query_status=0 + else + query_status=$? + fi + if ((query_status == 1)); then printf 'missing\n' + return + elif ((query_status != 0)); then + printf 'query-error\n' + return + fi + if [[ -z "$record" || "$record" == *$'\n'* ]]; then + printf 'invalid-record\n' + return + fi + + IFS='|' read -r status architecture actual extra <<<"$record" + if [[ -n "$extra" || "${status}|${architecture}|${actual}" != "$record" ]]; then + printf 'invalid-record\n' + elif [[ "$status" != "ii " ]]; then + printf 'unhealthy-status\n' + elif [[ "$architecture" != "$STATION_PACKAGE_ARCH" && "$architecture" != "all" ]]; then + printf 'wrong-architecture\n' elif [[ "$actual" == "$expected" ]]; then printf 'exact\n' elif [[ "$name" == "dkms" && "$actual" == "$FACTORY_DKMS_VERSION" && "$expected" == "$TARGET_DKMS_VERSION" ]]; then @@ -610,24 +639,34 @@ package_state() { } assert_no_package_mismatches() { - local spec state name expected actual mismatch=0 + local spec state name expected actual rejected=0 for spec in "${PACKAGE_SPECS[@]}"; do state="$(package_state "$spec")" - if [[ "$state" == "approved-transition" ]]; then - name="$(package_name "$spec")" - expected="$(package_expected_version "$spec")" - actual="$(installed_version "$name")" - info "package=${name} status=approved_transition actual=${actual} expected=${expected}" - continue - fi - [[ "$state" == "mismatch" ]] || continue name="$(package_name "$spec")" expected="$(package_expected_version "$spec")" - actual="$(installed_version "$name")" - warn "package=${name} status=mismatch actual=${actual} expected=${expected}" - mismatch=1 + case "$state" in + exact | missing) ;; + approved-transition) + actual="$(installed_version "$name")" + info "package=${name} status=approved_transition actual=${actual} expected=${expected}" + ;; + mismatch) + actual="$(installed_version "$name")" + warn "package=${name} status=mismatch actual=${actual} expected=${expected}" + rejected=1 + ;; + unhealthy-status | wrong-architecture | invalid-record | query-error) + warn "package=${name} status=${state} expected=${expected}" + rejected=1 + ;; + *) + warn "package=${name} status=unknown_state expected=${expected}" + rejected=1 + ;; + esac done - ((mismatch == 0)) || fatal "Existing Station prerequisite versions differ from the validated pins or approved factory transition; refusing to change them automatically" + ((rejected == 0)) \ + || fatal "Station prerequisite package state is unhealthy or differs from the validated pins; repair dpkg status or architecture issues before retrying" } all_packages_exact() { @@ -647,9 +686,11 @@ all_baseos_packages_exact() { } verify_baseos_packages() { - local spec + local spec state for spec in "${BASEOS_PACKAGE_SPECS[@]}"; do - package_is_exact "$spec" || fatal "BaseOS package does not match the qualified image: ${spec}" + state="$(package_state "$spec")" + [[ "$state" == "exact" ]] \ + || fatal "BaseOS package does not match the qualified image: ${spec} (${state})" done info "baseos_packages=exact" } @@ -756,11 +797,49 @@ check_network() { info "network=required_vendor_hosts_resolve" } +station_checks_package_inventory() { + [[ "$STATION_HOST_PROFILE" == "generic-ubuntu" || "$STATION_HOST_PROFILE" == "colossus-baseos" ]] +} + +check_dpkg_database_health() { + local audit + if ! audit="$(LC_ALL=C dpkg --audit 2>&1)"; then + fatal "Unable to audit the dpkg database; run 'sudo dpkg --audit' and repair the package database before retrying" + fi + [[ -z "${audit//[[:space:]]/}" ]] \ + || fatal "dpkg reports unfinished package work; run 'sudo dpkg --audit' and repair every listed package before retrying" + info "dpkg_audit=clean" +} + +package_manager_lock_inventory() { + local inventory + if [[ "$MODE" == "--apply" ]]; then + inventory="$(sudo -n lslocks --noheadings --raw --notruncate --output PID,COMMAND,PATH)" || return 1 + else + inventory="$(lslocks --noheadings --raw --notruncate --output PID,COMMAND,PATH)" || return 1 + fi + awk '$3 == "/var/lib/dpkg/lock-frontend" || + $3 == "/var/lib/dpkg/lock" || + $3 == "/var/cache/apt/archives/lock" || + $3 == "/var/lib/apt/lists/lock" {print}' <<<"$inventory" +} + check_package_managers_idle() { - local active - active="$(ps -eo pid=,comm= | awk '$2 ~ /^(apt|apt-get|dpkg|unattended-upgrade)$/ {print}')" - [[ -z "$active" ]] || fatal "A package-manager process is active: ${active}" - info "package_manager=idle" + local phase=${1:-Station preflight} active locks + active="$(ps -eo pid=,comm= | awk '$2 ~ /^(apt|apt-get|apt.systemd.dai|apt.systemd.daily|dpkg|packagekitd|unattended-upgr|unattended-upgrade)$/ {print}')" + [[ -z "$active" ]] || fatal "A package-manager process is active during ${phase}: ${active}" + if [[ "$STATION_HOST_PROFILE" == "generic-ubuntu" ]]; then + locks="$(package_manager_lock_inventory)" \ + || fatal "Unable to inspect APT and dpkg locks during ${phase}" + [[ -z "$locks" ]] || fatal "An APT or dpkg lock is active during ${phase}: ${locks}" + fi + info "package_manager=idle phase=${phase// /_}" +} + +assert_package_transaction_ready() { + local phase=$1 + check_package_managers_idle "$phase" + check_dpkg_database_health } check_dgx_os_docker_selection() { @@ -1065,20 +1144,28 @@ install_boot_marker_matches_current_boot() { } print_package_status() { - local spec name expected actual + local spec state name expected actual for spec in "${PACKAGE_SPECS[@]}"; do name="$(package_name "$spec")" expected="$(package_expected_version "$spec")" - actual="$(installed_version "$name")" - if [[ "$actual" == "$expected" ]]; then - info "package=${name} status=exact version=${actual}" - elif [[ -z "$actual" ]]; then - info "package=${name} status=missing expected=${expected}" - elif [[ "$name" == "dkms" && "$actual" == "$FACTORY_DKMS_VERSION" ]]; then - info "package=${name} status=approved_transition actual=${actual} expected=${expected}" - else - warn "package=${name} status=mismatch actual=${actual} expected=${expected}" - fi + state="$(package_state "$spec")" + case "$state" in + exact) + info "package=${name} status=exact version=${expected}" + ;; + missing) + info "package=${name} status=missing expected=${expected}" + ;; + approved-transition) + actual="$(installed_version "$name")" + info "package=${name} status=approved_transition actual=${actual} expected=${expected}" + ;; + mismatch) + actual="$(installed_version "$name")" + warn "package=${name} status=mismatch actual=${actual} expected=${expected}" + ;; + *) warn "package=${name} status=${state} expected=${expected}" ;; + esac done } @@ -1096,6 +1183,18 @@ common_preflight() { require_command stat require_command systemctl check_platform + if station_checks_package_inventory; then + require_command dpkg + if [[ "$STATION_HOST_PROFILE" == "generic-ubuntu" ]]; then + require_command lslocks + assert_package_transaction_ready "initial Station package preflight" + else + check_package_managers_idle "initial Station package preflight" + check_dpkg_database_health + fi + else + check_package_managers_idle "initial Station package preflight" + fi if station_uses_factory_runtime; then info "factory_packages=preserved package_and_driver_mutation=disabled" check_dgx_os_docker_selection @@ -1108,7 +1207,6 @@ common_preflight() { fi check_capacity check_network - check_package_managers_idle check_failed_units check_agent_and_inference_conflicts capture_docker_container_baseline @@ -1177,23 +1275,33 @@ assert_root_regular_file_safe() { } ensure_cuda_keyring() { - local cuda_deb=$1 actual verification + local cuda_deb=$1 actual state verification assert_root_directory_safe /usr/share/keyrings "CUDA repository keyring directory" - actual="$(installed_version cuda-keyring)" - if [[ -z "$actual" ]]; then - curl --fail --silent --show-error --location "$CUDA_KEYRING_URL" --output "$cuda_deb" - verify_file_sha256 "$cuda_deb" "$CUDA_KEYRING_SHA256" - sudo dpkg -i "$cuda_deb" - package_is_exact "cuda-keyring=${CUDA_KEYRING_PACKAGE_VERSION}" \ - || fatal "Installed cuda-keyring does not match ${CUDA_KEYRING_PACKAGE_VERSION}" - elif [[ "$actual" == "$CUDA_KEYRING_PACKAGE_VERSION" ]]; then - verification="$(dpkg -V cuda-keyring 2>&1)" \ - || fatal "Unable to verify the installed cuda-keyring package" - [[ -z "$verification" ]] || fatal "Installed cuda-keyring files differ from the package manifest: ${verification}" - info "cuda_keyring=exact version=${actual}" - else - fatal "Existing cuda-keyring version ${actual} differs from validated pin ${CUDA_KEYRING_PACKAGE_VERSION}; refusing to upgrade or downgrade it automatically" - fi + state="$(package_state "cuda-keyring=${CUDA_KEYRING_PACKAGE_VERSION}")" + case "$state" in + missing) + curl --fail --silent --show-error --location "$CUDA_KEYRING_URL" --output "$cuda_deb" + verify_file_sha256 "$cuda_deb" "$CUDA_KEYRING_SHA256" + assert_package_transaction_ready "CUDA keyring installation" + sudo dpkg -i "$cuda_deb" + check_dpkg_database_health + package_is_exact "cuda-keyring=${CUDA_KEYRING_PACKAGE_VERSION}" \ + || fatal "Installed cuda-keyring does not match ${CUDA_KEYRING_PACKAGE_VERSION}" + ;; + exact) + verification="$(dpkg -V cuda-keyring 2>&1)" \ + || fatal "Unable to verify the installed cuda-keyring package" + [[ -z "$verification" ]] || fatal "Installed cuda-keyring files differ from the package manifest: ${verification}" + info "cuda_keyring=exact version=${CUDA_KEYRING_PACKAGE_VERSION}" + ;; + mismatch) + actual="$(installed_version cuda-keyring)" + fatal "Existing cuda-keyring version ${actual} differs from validated pin ${CUDA_KEYRING_PACKAGE_VERSION}; refusing to upgrade or downgrade it automatically" + ;; + *) + fatal "Existing cuda-keyring package state is not safe to reuse: ${state}; repair dpkg status or architecture issues before retrying" + ;; + esac assert_root_regular_file_safe /usr/share/keyrings/cuda-archive-keyring.gpg 0644 "CUDA repository keyring" verify_key_fingerprint /usr/share/keyrings/cuda-archive-keyring.gpg "$CUDA_KEY_FINGERPRINT" @@ -1266,6 +1374,7 @@ configure_repositories() { verify_file_sha256 "$docker_asc" "$DOCKER_KEY_SHA256" verify_key_fingerprint "$docker_asc" "$DOCKER_KEY_FINGERPRINT" gpg --batch --yes --dearmor --output "$docker_gpg" "$docker_asc" + assert_package_transaction_ready "Docker repository configuration" ensure_root_directory_safe /etc/apt/keyrings /etc/apt 0755 "Docker repository key directory" assert_root_directory_safe /etc/apt/sources.list.d "Docker repository source directory" printf '%s\n' \ @@ -1546,14 +1655,18 @@ simulate_install() { } install_packages() { - collect_package_transaction_specs + assert_package_transaction_ready "Station repository configuration" configure_repositories + assert_package_transaction_ready "APT metadata refresh" + collect_package_transaction_specs info "Refreshing package metadata" sudo apt-get update validate_package_availability "${PACKAGE_TRANSACTION_SPECS[@]}" create_apt_transaction_guard + assert_package_transaction_ready "APT transaction simulation" simulate_install "${PACKAGE_TRANSACTION_SPECS[@]}" require_docker_restart_quiescence "Station prerequisite package installation" + assert_package_transaction_ready "Station prerequisite package installation" info "Installing missing pinned Station prerequisites" sudo env DEBIAN_FRONTEND=noninteractive LC_ALL=C \ apt-get install -y --no-install-recommends --no-remove \ @@ -1561,6 +1674,7 @@ install_packages() { -o "DPkg::Tools::options::/bin/bash::Version=3" \ "${PACKAGE_TRANSACTION_SPECS[@]}" cleanup_apt_transaction_guard + check_dpkg_database_health local spec for spec in "${PACKAGE_SPECS[@]}"; do @@ -1990,6 +2104,7 @@ run_check() { return 0 fi print_package_status + assert_no_package_mismatches if all_packages_exact; then if install_boot_marker_matches_current_boot; then warn "Package installation completed in the current boot; reboot is required" diff --git a/test/install-station-container-coexistence.test.ts b/test/install-station-container-coexistence.test.ts index 812d8f5558f..7caa5f7b21d 100644 --- a/test/install-station-container-coexistence.test.ts +++ b/test/install-station-container-coexistence.test.ts @@ -107,6 +107,7 @@ check_kernel_headers() { :; } check_capacity() { :; } check_network() { :; } check_package_managers_idle() { :; } +check_dpkg_database_health() { :; } check_failed_units() { :; } check_agent_and_inference_conflicts() { :; } driver_loaded_exact() { return 0; } diff --git a/test/install-station-dgx-os.test.ts b/test/install-station-dgx-os.test.ts index da26acb6c2e..5cd33ee346a 100644 --- a/test/install-station-dgx-os.test.ts +++ b/test/install-station-dgx-os.test.ts @@ -647,11 +647,12 @@ run_apply const exact = runSourced( STATION_PREPARE, ` -installed_version() { +installed_package_record() { local spec for spec in "\${BASEOS_PACKAGE_SPECS[@]}"; do - if [[ "\${spec%%=*}" == "$1" ]]; then printf '%s' "\${spec#*=}"; return; fi + if [[ "\${spec%%=*}" == "$1" ]]; then printf 'ii |arm64|%s' "\${spec#*=}"; return; fi done + return 1 } all_baseos_packages_exact `, @@ -661,12 +662,13 @@ all_baseos_packages_exact const drifted = runSourced( STATION_PREPARE, ` -installed_version() { - if [[ "$1" == "docker-ce" ]]; then printf '5:30.0.0-1~ubuntu.24.04~noble'; return; fi +installed_package_record() { + if [[ "$1" == "docker-ce" ]]; then printf 'ii |arm64|5:30.0.0-1~ubuntu.24.04~noble'; return; fi local spec for spec in "\${BASEOS_PACKAGE_SPECS[@]}"; do - if [[ "\${spec%%=*}" == "$1" ]]; then printf '%s' "\${spec#*=}"; return; fi + if [[ "\${spec%%=*}" == "$1" ]]; then printf 'ii |arm64|%s' "\${spec#*=}"; return; fi done + return 1 } all_baseos_packages_exact `, diff --git a/test/install-station-host-preparation.test.ts b/test/install-station-host-preparation.test.ts index 592a75a151a..7a9b17adcc2 100644 --- a/test/install-station-host-preparation.test.ts +++ b/test/install-station-host-preparation.test.ts @@ -99,72 +99,6 @@ run_gpus_test_sudo expect(output).toContain("gpu_bdf=0000:01:00.0 gpu=NVIDIA GB300 role=inference"); }); - it.each([ - ["", "missing"], - ["5:29.6.1-1~ubuntu.24.04~noble", "exact"], - ["5:30.0.0-1~ubuntu.24.04~noble", "mismatch"], - ])("classifies an installed package version as %s -> %s", (actual, expected) => { - const { result, output } = runSourced( - STATION_PREPARE, - ` -installed_version() { - if [[ "$1" == "docker-ce" ]]; then printf '%s' "$PACKAGE_ACTUAL"; fi -} -package_state 'docker-ce=5:29.6.1-1~ubuntu.24.04~noble' -`, - { PACKAGE_ACTUAL: actual }, - ); - - expect(result.status, output).toBe(0); - expect(result.stdout.trim()).toBe(expected); - }); - - it("allows only the reviewed factory DKMS transition", () => { - const approved = runSourced( - STATION_PREPARE, - ` -installed_version() { - if [[ "$1" == "dkms" ]]; then printf '%s' "$DKMS_ACTUAL"; fi -} -package_state 'dkms=1:3.4.0-1ubuntu1' -assert_no_package_mismatches -`, - { DKMS_ACTUAL: "3.0.11-1ubuntu13" }, - ); - expect(approved.result.status, approved.output).toBe(0); - expect(approved.output).toContain("approved-transition"); - expect(approved.output).toContain("status=approved_transition"); - - const arbitrary = runSourced( - STATION_PREPARE, - ` -installed_version() { - if [[ "$1" == "dkms" ]]; then printf '%s' "$DKMS_ACTUAL"; fi -} -assert_no_package_mismatches -`, - { DKMS_ACTUAL: "3.2.0-1" }, - ); - expect(arbitrary.result.status, arbitrary.output).not.toBe(0); - expect(arbitrary.output).toMatch(/dkms status=mismatch/); - }); - - it("refuses to change an existing mismatched prerequisite", () => { - const { result, output } = runSourced( - STATION_PREPARE, - ` -installed_version() { - if [[ "$1" == "docker-ce" ]]; then printf '5:30.0.0-1~ubuntu.24.04~noble'; fi -} -assert_no_package_mismatches -`, - ); - - expect(result.status, output).not.toBe(0); - expect(output).toMatch(/docker-ce status=mismatch/); - expect(output).toMatch(/refusing to change them automatically/); - }); - it("allows only condition-qualified factory failures and blocks other failed units", () => { const qualified = runSourced( STATION_PREPARE, @@ -385,9 +319,10 @@ common_preflight() { :; } require_command() { :; } acquire_sudo() { :; } all_packages_exact() { return 1; } -installed_version() { - if [[ "$1" == "dkms" ]]; then printf '3.0.11-1ubuntu13'; fi +installed_package_record() { + if [[ "$1" == "dkms" ]]; then printf 'ii |all|3.0.11-1ubuntu13'; else return 1; fi } +installed_version() { if [[ "$1" == "dkms" ]]; then printf '3.0.11-1ubuntu13'; fi; } install_packages() { printf 'INSTALL_PACKAGES\n'; } ensure_docker_group() { printf 'ENSURE_DOCKER_GROUP\n'; } require_docker_restart_quiescence() { printf 'RECHECK_RESTART_QUIESCENCE\n'; } @@ -419,6 +354,8 @@ simulate_install() { printf 'SIMULATE_INSTALL\n'; } require_docker_restart_quiescence() { printf 'RECHECK_RESTART_QUIESCENCE\n'; } package_state() { printf 'missing\n'; } package_is_exact() { return 0; } +assert_package_transaction_ready() { printf 'PACKAGE_TRANSACTION_READY %s\n' "$1"; } +check_dpkg_database_health() { printf 'DPKG_AUDIT_CLEAN\n'; } create_apt_transaction_guard() { APT_TRANSACTION_GUARD_DIR=/run/nemoclaw-apt-transaction.TEST APT_TRANSACTION_HOOK="/bin/bash $APT_TRANSACTION_GUARD_DIR/verify-plan" @@ -518,6 +455,7 @@ check_agent_and_inference_conflicts STATION_PREPARE, ` assert_root_directory_safe() { :; } +installed_package_record() { printf 'ii |all|2.0-1'; } installed_version() { printf '2.0-1'; } ensure_cuda_keyring "$HOME/cuda-keyring.deb" `, @@ -533,7 +471,7 @@ ensure_cuda_keyring "$HOME/cuda-keyring.deb" ` assert_root_directory_safe() { :; } assert_root_regular_file_safe() { :; } -installed_version() { printf '1.1-1'; } +installed_package_record() { printf 'ii |all|1.1-1'; } dpkg() { :; } curl() { printf 'DOWNLOAD\n'; } sudo() { "$@"; } diff --git a/test/install-station-package-state.test.ts b/test/install-station-package-state.test.ts new file mode 100644 index 00000000000..d5b000d8668 --- /dev/null +++ b/test/install-station-package-state.test.ts @@ -0,0 +1,261 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { spawnSync } from "node:child_process"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { describe, expect, it } from "vitest"; +import { TEST_SYSTEM_PATH } from "./helpers/installer-sourced-env"; + +const REPO_ROOT = path.resolve(import.meta.dirname, ".."); +const STATION_PREPARE = path.join(REPO_ROOT, "scripts", "prepare-dgx-station-host.sh"); + +function runSourced(body: string, extraEnv: Record = {}) { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-station-package-state-")); + const result = spawnSync( + "bash", + ["--noprofile", "--norc", "-c", `source "$STATION_PREPARE" >/dev/null\n${body}`], + { + cwd: REPO_ROOT, + encoding: "utf-8", + env: { HOME: home, PATH: TEST_SYSTEM_PATH, STATION_PREPARE, ...extraEnv }, + timeout: 15_000, + killSignal: "SIGKILL", + }, + ); + return { result, output: `${result.stdout}${result.stderr}` }; +} + +function runRealApplyPackagePreflight(body: string) { + return runSourced(` +mkdir -p "$HOME/pci/0000:01:00.0" +printf '%s\n' 'ID=ubuntu' 'VERSION_ID="24.04"' 'PRETTY_NAME="Ubuntu 24.04 fixture"' >"$HOME/os-release" +printf 'NVIDIA DGX Station GB300\n' >"$HOME/product-name" +printf '0x10de\n' >"$HOME/pci/0000:01:00.0/vendor" +printf '0x31c2\n' >"$HOME/pci/0000:01:00.0/device" +printf '0x030200\n' >"$HOME/pci/0000:01:00.0/class" +station_os_release_path() { printf '%s\n' "$HOME/os-release"; } +station_product_name_path() { printf '%s\n' "$HOME/product-name"; } +station_pci_devices_path() { printf '%s\n' "$HOME/pci"; } +dgx_station_release_path() { printf '%s\n' "$HOME/no-dgx-release"; } +uname() { + case "$1" in + -m) printf 'aarch64\n' ;; + -r) printf '6.8.0-fixture\n' ;; + *) return 1 ;; + esac +} +dpkg-query() { return 1; } +dpkg() { :; } +getent() { :; } +ps() { :; } +lslocks() { :; } +sha256sum() { :; } +ss() { :; } +systemctl() { :; } +mokutil() { printf 'SECURE_BOOT_CHECK_REACHED\n'; return 1; } +sudo() { + if [[ "$*" == "-n true" ]]; then return 0; fi + if [[ "$1" == "-n" && "$2" == "lslocks" ]]; then shift; "$@"; return; fi + printf 'UNEXPECTED_SUDO %s\n' "$*" + return 1 +} +MODE='--apply' +${body} +run_apply +`); +} + +describe("DGX Station package state", () => { + it.each([ + { label: "missing", queryStatus: "1", record: "", expected: "missing" }, + { + label: "native exact", + queryStatus: "0", + record: "ii |arm64|5:29.6.1-1~ubuntu.24.04~noble", + expected: "exact", + }, + { + label: "architecture-independent exact", + queryStatus: "0", + record: "ii |all|5:29.6.1-1~ubuntu.24.04~noble", + expected: "exact", + }, + { + label: "version mismatch", + queryStatus: "0", + record: "ii |arm64|5:30.0.0-1~ubuntu.24.04~noble", + expected: "mismatch", + }, + { + label: "unpacked", + queryStatus: "0", + record: "iU |arm64|5:29.6.1-1~ubuntu.24.04~noble", + expected: "unhealthy-status", + }, + { + label: "held", + queryStatus: "0", + record: "hi |arm64|5:29.6.1-1~ubuntu.24.04~noble", + expected: "unhealthy-status", + }, + { + label: "foreign architecture", + queryStatus: "0", + record: "ii |amd64|5:29.6.1-1~ubuntu.24.04~noble", + expected: "wrong-architecture", + }, + { label: "empty record", queryStatus: "0", record: "", expected: "invalid-record" }, + { + label: "multiple records", + queryStatus: "0", + record: "ii |arm64|5:29.6.1-1~ubuntu.24.04~noble\nii |all|5:29.6.1-1~ubuntu.24.04~noble", + expected: "invalid-record", + }, + { label: "query failure", queryStatus: "2", record: "", expected: "query-error" }, + ])("classifies $label package state as $expected", ({ queryStatus, record, expected }) => { + const { result, output } = runSourced( + ` +installed_package_record() { + if ((PACKAGE_QUERY_STATUS != 0)); then return "$PACKAGE_QUERY_STATUS"; fi + printf '%s' "$PACKAGE_RECORD" +} +package_state 'docker-ce=5:29.6.1-1~ubuntu.24.04~noble' +`, + { PACKAGE_QUERY_STATUS: queryStatus, PACKAGE_RECORD: record }, + ); + + expect(result.status, output).toBe(0); + expect(result.stdout.trim()).toBe(expected); + }); + + it.each([ + { queryStatus: 1, expected: "missing" }, + { queryStatus: 2, expected: "query-error" }, + ])("maps dpkg-query status $queryStatus to $expected without triggering ERR", ({ + queryStatus, + expected, + }) => { + const { result, output } = runSourced( + ` +dpkg-query() { return "$PACKAGE_QUERY_STATUS"; } +package_state 'docker-ce=5:29.6.1-1~ubuntu.24.04~noble' +`, + { PACKAGE_QUERY_STATUS: String(queryStatus) }, + ); + + expect(result.status, output).toBe(0); + expect(result.stdout.trim()).toBe(expected); + expect(output).not.toContain("command failed"); + }); + + it("allows only the reviewed factory DKMS transition", () => { + const body = ` +installed_package_record() { + if [[ "$1" == "dkms" ]]; then printf 'ii |all|%s' "$DKMS_ACTUAL"; else return 1; fi +} +installed_version() { if [[ "$1" == "dkms" ]]; then printf '%s' "$DKMS_ACTUAL"; fi; } +package_state 'dkms=1:3.4.0-1ubuntu1' +assert_no_package_mismatches +`; + const approved = runSourced(body, { DKMS_ACTUAL: "3.0.11-1ubuntu13" }); + expect(approved.result.status, approved.output).toBe(0); + expect(approved.output).toContain("approved-transition"); + expect(approved.output).toContain("status=approved_transition"); + + const arbitrary = runSourced(body, { DKMS_ACTUAL: "3.2.0-1" }); + expect(arbitrary.result.status, arbitrary.output).not.toBe(0); + expect(arbitrary.output).toMatch(/dkms status=mismatch/); + }); + + it("refuses to change an existing mismatched prerequisite", () => { + const { result, output } = runSourced(` +installed_package_record() { + if [[ "$1" == "docker-ce" ]]; then printf 'ii |arm64|5:30.0.0-1~ubuntu.24.04~noble'; else return 1; fi +} +installed_version() { if [[ "$1" == "docker-ce" ]]; then printf '5:30.0.0-1~ubuntu.24.04~noble'; fi; } +assert_no_package_mismatches +`); + + expect(result.status, output).not.toBe(0); + expect(output).toMatch(/docker-ce status=mismatch/); + expect(output).toMatch(/package state is unhealthy or differs from the validated pins/); + }); + + it.each([ + "unattended-upgr", + "packagekitd", + "apt.systemd.dai", + ])("detects the Linux package-manager process name %s", (processName) => { + const { result, output } = runSourced( + ` +STATION_HOST_PROFILE=generic-ubuntu +ps() { printf '4242 %s\n' "$PACKAGE_PROCESS"; } +lslocks() { :; } +check_package_managers_idle test +`, + { PACKAGE_PROCESS: processName }, + ); + + expect(result.status, output).not.toBe(0); + expect(output).toContain(`4242 ${processName}`); + expect(output).toMatch(/package-manager process is active/); + }); + + it.each([ + "/var/lib/dpkg/lock-frontend", + "/var/lib/dpkg/lock", + "/var/cache/apt/archives/lock", + "/var/lib/apt/lists/lock", + ])("rejects active package lock %s even when no process name is visible", (lockPath) => { + const { result, output } = runSourced( + ` +STATION_HOST_PROFILE=generic-ubuntu +MODE='--apply' +ps() { :; } +lslocks() { printf '4242 helper %s\n' "$PACKAGE_LOCK"; } +sudo() { if [[ "$1" == "-n" ]]; then shift; fi; "$@"; } +check_package_managers_idle test +`, + { PACKAGE_LOCK: lockPath }, + ); + + expect(result.status, output).not.toBe(0); + expect(output).toContain(`4242 helper ${lockPath}`); + expect(output).toMatch(/APT or dpkg lock is active/); + }); + + it("runs the real apply preflight and rejects unfinished dpkg work before later checks", () => { + const { result, output } = runRealApplyPackagePreflight(` +dpkg() { [[ "$*" == "--audit" ]] && printf ' package is unpacked but not configured\n'; } +`); + + expect(result.status, output).not.toBe(0); + expect(output).toMatch(/dpkg reports unfinished package work/); + expect(output).not.toContain("SECURE_BOOT_CHECK_REACHED"); + expect(output).not.toContain("UNEXPECTED_SUDO"); + }); + + it("fails closed when dpkg audit itself cannot run", () => { + const { result, output } = runSourced(` +dpkg() { return 2; } +check_dpkg_database_health +`); + + expect(result.status, output).not.toBe(0); + expect(output).toMatch(/Unable to audit the dpkg database/); + }); + + it("runs the real apply preflight and rejects an active package lock before mutation", () => { + const { result, output } = runRealApplyPackagePreflight(` +lslocks() { printf '5151 apt-get /var/cache/apt/archives/lock\n'; } +`); + + expect(result.status, output).not.toBe(0); + expect(output).toContain("5151 apt-get /var/cache/apt/archives/lock"); + expect(output).toMatch(/APT or dpkg lock is active/); + expect(output).not.toContain("SECURE_BOOT_CHECK_REACHED"); + expect(output).not.toContain("UNEXPECTED_SUDO"); + }); +}); diff --git a/test/install-station-package-transaction.test.ts b/test/install-station-package-transaction.test.ts index 94786bf1e8d..8ffccb39f7f 100644 --- a/test/install-station-package-transaction.test.ts +++ b/test/install-station-package-transaction.test.ts @@ -92,6 +92,8 @@ apt-get() { require_docker_restart_quiescence() { printf 'RECHECK_DOCKER_RESTART %s\n' "$1"; } package_state() { printf 'missing\n'; } package_is_exact() { return 0; } +assert_package_transaction_ready() { printf 'PACKAGE_TRANSACTION_READY %s\n' "$1"; } +check_dpkg_database_health() { printf 'DPKG_AUDIT_CLEAN\n'; } create_apt_transaction_guard() { APT_TRANSACTION_GUARD_DIR=/run/nemoclaw-apt-transaction.TEST APT_TRANSACTION_HOOK="/bin/bash $APT_TRANSACTION_GUARD_DIR/verify-plan" @@ -162,6 +164,8 @@ package_state() { if [[ "$1" == '${retainedSpec}' ]]; then printf 'exact\n'; else printf 'missing\n'; fi } package_is_exact() { return 0; } +assert_package_transaction_ready() { :; } +check_dpkg_database_health() { :; } create_apt_transaction_guard() { APT_TRANSACTION_GUARD_DIR=/run/nemoclaw-apt-transaction.TEST APT_TRANSACTION_HOOK="/bin/bash $APT_TRANSACTION_GUARD_DIR/verify-plan" @@ -215,6 +219,8 @@ package_state() { } installed_version() { if [[ "$1" == "libc6" ]]; then printf '2.39-0ubuntu8'; fi; } package_is_exact() { return 0; } +assert_package_transaction_ready() { :; } +check_dpkg_database_health() { :; } create_apt_transaction_guard() { APT_TRANSACTION_GUARD_DIR=/run/nemoclaw-apt-transaction.TEST APT_TRANSACTION_HOOK="/bin/bash $APT_TRANSACTION_GUARD_DIR/verify-plan" diff --git a/test/test-boundary-guards.test.ts b/test/test-boundary-guards.test.ts index 35f10961f3f..7824d7df48c 100644 --- a/test/test-boundary-guards.test.ts +++ b/test/test-boundary-guards.test.ts @@ -756,6 +756,7 @@ describe("Vitest project membership boundary", () => { ["test/install-station-dgx-os.test.ts", "installer-integration"], ["test/install-station-docker-repository.test.ts", "installer-integration"], ["test/install-station-host-preparation.test.ts", "installer-integration"], + ["test/install-station-package-state.test.ts", "installer-integration"], ["test/install-station-package-transaction.test.ts", "installer-integration"], ["test/package-contract/example.test.js", "package-contract"], ["test/e2e/support/example.test.js", "e2e-support"], diff --git a/vitest.config.ts b/vitest.config.ts index 64474df7ee0..e4a702cdadc 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -127,6 +127,7 @@ export default defineConfig({ "test/install-station-dgx-os.test.ts", "test/install-station-docker-repository.test.ts", "test/install-station-host-preparation.test.ts", + "test/install-station-package-state.test.ts", "test/install-station-package-transaction.test.ts", "test/install-openshell-version-check.test.ts", ], @@ -149,6 +150,7 @@ export default defineConfig({ "test/install-station-dgx-os.test.ts", "test/install-station-docker-repository.test.ts", "test/install-station-host-preparation.test.ts", + "test/install-station-package-state.test.ts", "test/install-station-package-transaction.test.ts", "test/install-openshell-version-check.test.ts", ],