From d193ba821a1c782b26d5390dc901fa89cf18a8a2 Mon Sep 17 00:00:00 2001 From: Chen Chengjun Date: Tue, 13 Jan 2026 08:58:30 +0000 Subject: [PATCH] Migrate the original NixOS tests using the new framework --- Makefile | 24 ++++++++-- .../templates/aster_configuration.nix | 6 --- .../templates/install_nixos.sh | 2 +- distro/cachix/default.nix | 4 +- distro/etc_nixos/modules/core.nix | 9 ---- .../overlays/test-asterinas/default.nix | 13 ------ .../test-asterinas/test-nix-commands.sh | 25 ---------- .../overlays/test-asterinas/test-podman.sh | 21 --------- test/nixos/tests/hello/Cargo.toml | 9 ++++ test/nixos/tests/hello/src/main.rs | 20 ++++++++ test/nixos/tests/nix/Cargo.toml | 9 ++++ test/nixos/tests/nix/src/main.rs | 46 +++++++++++++++++++ test/nixos/tests/podman/Cargo.toml | 9 ++++ test/nixos/tests/podman/extra_config.nix | 5 ++ test/nixos/tests/podman/src/main.rs | 34 ++++++++++++++ 15 files changed, 154 insertions(+), 82 deletions(-) delete mode 100644 distro/etc_nixos/overlays/test-asterinas/default.nix delete mode 100644 distro/etc_nixos/overlays/test-asterinas/test-nix-commands.sh delete mode 100644 distro/etc_nixos/overlays/test-asterinas/test-podman.sh create mode 100644 test/nixos/tests/hello/Cargo.toml create mode 100644 test/nixos/tests/hello/src/main.rs create mode 100644 test/nixos/tests/nix/Cargo.toml create mode 100644 test/nixos/tests/nix/src/main.rs create mode 100644 test/nixos/tests/podman/Cargo.toml create mode 100644 test/nixos/tests/podman/extra_config.nix create mode 100644 test/nixos/tests/podman/src/main.rs diff --git a/Makefile b/Makefile index f057ac691..3abb2734a 100644 --- a/Makefile +++ b/Makefile @@ -58,7 +58,6 @@ DNS_SERVER ?= none # NixOS settings NIXOS_DISK_SIZE_IN_MB ?= 8192 NIXOS_DISABLE_SYSTEMD ?= false -NIXOS_TEST_COMMAND ?= # The following option is only effective when NIXOS_DISABLE_SYSTEMD is set to 'true'. # Use a login shell to ensure that environment variables are initialized correctly. NIXOS_STAGE_2_INIT ?= /bin/sh -l @@ -326,7 +325,11 @@ endif iso: BOOT_PROTOCOL = linux-efi-handover64 iso: @make kernel - @./tools/nixos/build_iso.sh + @if [ -n "$(NIXOS_TEST_SUITE)" ]; then \ + $(MAKE) --no-print-directory -C test/nixos iso; \ + else \ + ./tools/nixos/build_iso.sh; \ + fi # Build the Asterinas NixOS ISO installer image and then do installation run_iso: OVMF = off @@ -337,18 +340,25 @@ run_iso: nixos: BOOT_PROTOCOL = linux-efi-handover64 nixos: @make kernel - @./tools/nixos/build_nixos.sh + @if [ -n "$(NIXOS_TEST_SUITE)" ]; then \ + $(MAKE) --no-print-directory -C test/nixos nixos; \ + else \ + ./tools/nixos/build_nixos.sh; \ + fi # After creating a Asterinas NixOS installation (via either the `run_iso` or `nixos` target), # run the NixOS run_nixos: OVMF = off run_nixos: - @./tools/nixos/run.sh nixos + @if [ -n "$(NIXOS_TEST_SUITE)" ]; then \ + $(MAKE) --no-print-directory -C test/nixos run_nixos; \ + else \ + ./tools/nixos/run.sh nixos; \ + fi # Build the Asterinas NixOS patched packages cachix: @nix-build distro/cachix \ - --argstr test-command "${NIXOS_TEST_COMMAND}" \ --option extra-substituters "${RELEASE_SUBSTITUTER} ${DEV_SUBSTITUTER}" \ --option extra-trusted-public-keys "${RELEASE_TRUSTED_PUBLIC_KEY} ${DEV_TRUSTED_PUBLIC_KEY}" \ --out-link cachix.list @@ -426,6 +436,7 @@ format: @./tools/format_all.sh @nixfmt ./distro @$(MAKE) --no-print-directory -C test/initramfs format + @$(MAKE) --no-print-directory -C test/nixos format .PHONY: check check: initramfs $(CARGO_OSDK) @@ -471,6 +482,9 @@ check: initramfs $(CARGO_OSDK) @# Check formatting issues of the C code and Nix files (regression tests) @$(MAKE) --no-print-directory -C test/initramfs check @ + @# Check formatting issues of the Rust code in NixOS tests + @$(MAKE) --no-print-directory -C test/nixos check + @ @# Check typos @typos @# Check formatting issues of Nix files under distro directory diff --git a/distro/aster_nixos_installer/templates/aster_configuration.nix b/distro/aster_nixos_installer/templates/aster_configuration.nix index df34d0c3f..00c3ae954 100644 --- a/distro/aster_nixos_installer/templates/aster_configuration.nix +++ b/distro/aster_nixos_installer/templates/aster_configuration.nix @@ -33,7 +33,6 @@ (import ./overlays/podman/default.nix) (import ./overlays/switch-to-configuration-ng/default.nix) (import ./overlays/systemd/default.nix) - (import ./overlays/test-asterinas/default.nix) ]; # The Asterinas NixOS special options. @@ -64,11 +63,6 @@ default = "@aster-console@"; description = "The console device."; }; - test-command = lib.mkOption { - type = lib.types.str; - default = "@aster-test-command@"; - description = "The test command."; - }; break-into-stage-1-shell = lib.mkOption { type = lib.types.bool; default = false; diff --git a/distro/aster_nixos_installer/templates/install_nixos.sh b/distro/aster_nixos_installer/templates/install_nixos.sh index db0ee31b7..58c69aa11 100644 --- a/distro/aster_nixos_installer/templates/install_nixos.sh +++ b/distro/aster_nixos_installer/templates/install_nixos.sh @@ -126,7 +126,7 @@ cleanup() { } trap cleanup EXIT INT TERM ERR -cp $CONFIG_PATH ${BUILD_DIR}/etc/nixos +cp $CONFIG_PATH ${BUILD_DIR}/etc/nixos/configuration.nix cp @aster-configuration@ ${BUILD_DIR}/etc/nixos/aster_configuration.nix cp -r @aster-etc-nixos@/modules ${BUILD_DIR}/etc/nixos cp -r @aster-etc-nixos@/overlays ${BUILD_DIR}/etc/nixos diff --git a/distro/cachix/default.nix b/distro/cachix/default.nix index 7c10f869f..13449842d 100644 --- a/distro/cachix/default.nix +++ b/distro/cachix/default.nix @@ -1,8 +1,8 @@ -{ pkgs ? import { }, test-command ? "", extra-substituters ? "" +{ pkgs ? import { }, extra-substituters ? "" , extra-trusted-public-keys ? "", ... }: let installer = pkgs.callPackage ../aster_nixos_installer { - inherit test-command extra-substituters extra-trusted-public-keys; + inherit extra-substituters extra-trusted-public-keys; }; nixos = pkgs.nixos (import "${installer}/etc_nixos/configuration.nix"); cachixPkgs = with nixos.pkgs; diff --git a/distro/etc_nixos/modules/core.nix b/distro/etc_nixos/modules/core.nix index 2a231f6bc..b2bbae0b8 100644 --- a/distro/etc_nixos/modules/core.nix +++ b/distro/etc_nixos/modules/core.nix @@ -74,15 +74,6 @@ in { ${config.aster_nixos.stage-2-hook} fi ''; - # Execute test-command on hvc0 console after boot if the test-command is - # not empty (for CI testing). - environment.loginShellInit = - lib.mkIf ("${config.aster_nixos.test-command}" != "") '' - if [ "$(tty)" = "/dev/hvc0" ]; then - ${config.aster_nixos.test-command} - poweroff - fi - ''; # Suppress error and warning messages of systemd. # TODO: Fix errors and warnings from systemd and remove this setting. environment.sessionVariables = { SYSTEMD_LOG_LEVEL = "crit"; }; diff --git a/distro/etc_nixos/overlays/test-asterinas/default.nix b/distro/etc_nixos/overlays/test-asterinas/default.nix deleted file mode 100644 index 7d907f317..000000000 --- a/distro/etc_nixos/overlays/test-asterinas/default.nix +++ /dev/null @@ -1,13 +0,0 @@ -final: prev: { - test-asterinas = prev.stdenv.mkDerivation { - name = "test-asterinas"; - version = "0.1.0"; - - src = ./.; - - installPhase = '' - install -m755 -D $src/test-nix-commands.sh $out/bin/test-nix-commands - install -m755 -D $src/test-podman.sh $out/bin/test-podman - ''; - }; -} diff --git a/distro/etc_nixos/overlays/test-asterinas/test-nix-commands.sh b/distro/etc_nixos/overlays/test-asterinas/test-nix-commands.sh deleted file mode 100644 index 921c05108..000000000 --- a/distro/etc_nixos/overlays/test-asterinas/test-nix-commands.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/sh - -# SPDX-License-Identifier: MPL-2.0 - -set -e - -# Test nix-env -nix-env -iA nixos.hello -hello -nix-env -e hello - -# Test nix-shell -nix-shell -p hello --command hello - -# Test nix-build -nix-build "" -A hello -./result/bin/hello - -# Test nixos-rebuild -sed -i "s/environment.systemPackages = with pkgs; \[ test-asterinas \];/environment.systemPackages = with pkgs; \[ test-asterinas hello \];/" \ - /etc/nixos/configuration.nix -nixos-rebuild test -# Clean the hash cache to use the hello installed by nixos-rebuild -hash -r -hello diff --git a/distro/etc_nixos/overlays/test-asterinas/test-podman.sh b/distro/etc_nixos/overlays/test-asterinas/test-podman.sh deleted file mode 100644 index feb7c544f..000000000 --- a/distro/etc_nixos/overlays/test-asterinas/test-podman.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/sh - -# SPDX-License-Identifier: MPL-2.0 - -set -e - -CONTAINER_NAME=c1 -IMAGE_NAME=docker.io/library/alpine - -podman run --name=${CONTAINER_NAME} ${IMAGE_NAME} ls /etc \ - | grep -q "^alpine-release" \ - || (echo "Test 'podman run' failed" && exit 1) -podman image ls \ - | grep -q ${IMAGE_NAME} \ - || (echo "Test 'podman image ls' failed" && exit 1) -podman ps -a \ - | grep -q "Exited (0)" \ - || (echo "Test 'podman ps -a' failed" && exit 1) -podman rm ${CONTAINER_NAME} || (echo "Test 'podman rm' failed" && exit 1) - -echo "Test podman succeeds" \ No newline at end of file diff --git a/test/nixos/tests/hello/Cargo.toml b/test/nixos/tests/hello/Cargo.toml new file mode 100644 index 000000000..82d5ec7cd --- /dev/null +++ b/test/nixos/tests/hello/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "test-hello" +version.workspace = true +repository.workspace = true +license.workspace = true +edition.workspace = true + +[dependencies] +nixos-test-framework.workspace = true \ No newline at end of file diff --git a/test/nixos/tests/hello/src/main.rs b/test/nixos/tests/hello/src/main.rs new file mode 100644 index 000000000..b9596bef1 --- /dev/null +++ b/test/nixos/tests/hello/src/main.rs @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: MPL-2.0 + +//! The test suite for hello-asterinas on Asterinas NixOS. +//! +//! # Document maintenance +//! +//! An application's test suite and its "Verified Usage" section in Asterinas Book +//! should always be kept in sync. +//! So whenever you modify the test suite, +//! review the documentation and see if should be updated accordingly. + +use nixos_test_framework::*; + +nixos_test_main!(); + +#[nixos_test] +fn hello(nixos_shell: &mut Session) -> Result<(), Error> { + nixos_shell.run_cmd_and_expect("hello-asterinas", "Hello Asterinas!")?; + Ok(()) +} diff --git a/test/nixos/tests/nix/Cargo.toml b/test/nixos/tests/nix/Cargo.toml new file mode 100644 index 000000000..f13adc91e --- /dev/null +++ b/test/nixos/tests/nix/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "test-nix" +version.workspace = true +repository.workspace = true +license.workspace = true +edition.workspace = true + +[dependencies] +nixos-test-framework.workspace = true \ No newline at end of file diff --git a/test/nixos/tests/nix/src/main.rs b/test/nixos/tests/nix/src/main.rs new file mode 100644 index 000000000..d200fdad5 --- /dev/null +++ b/test/nixos/tests/nix/src/main.rs @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: MPL-2.0 + +//! The test suite for nix on Asterinas NixOS. +//! +//! # Document maintenance +//! +//! An application's test suite and its "Verified Usage" section in Asterinas Book +//! should always be kept in sync. +//! So whenever you modify the test suite, +//! review the documentation and see if should be updated accordingly. + +use nixos_test_framework::*; + +nixos_test_main!(); + +#[nixos_test] +fn nix_env_install(nixos_shell: &mut Session) -> Result<(), Error> { + nixos_shell.run_cmd("nix-env -iA nixos.hello")?; + nixos_shell.run_cmd_and_expect("hello", "Hello, world!")?; + nixos_shell.run_cmd("nix-env -e hello")?; + Ok(()) +} + +#[nixos_test] +fn nix_shell(nixos_shell: &mut Session) -> Result<(), Error> { + nixos_shell.run_cmd_and_expect("nix-shell -p hello --command hello", "Hello, world!")?; + Ok(()) +} + +#[nixos_test] +fn nix_build(nixos_shell: &mut Session) -> Result<(), Error> { + nixos_shell.run_cmd("nix-build '' -A hello")?; + nixos_shell.run_cmd_and_expect("./result/bin/hello", "Hello, world!")?; + Ok(()) +} + +#[nixos_test] +fn nixos_rebuild(nixos_shell: &mut Session) -> Result<(), Error> { + nixos_shell.run_cmd("echo '{ pkgs, ... }: { environment.systemPackages = [ pkgs.hello ]; }' > /tmp/add-hello.nix")?; + nixos_shell.run_cmd("echo '{ imports = [ /etc/nixos/configuration.nix /tmp/add-hello.nix ]; }' > /tmp/test-config.nix")?; + nixos_shell.run_cmd("nixos-rebuild test -I nixos-config=/tmp/test-config.nix")?; + nixos_shell.run_cmd("rm /tmp/*")?; + nixos_shell.run_cmd("hash -r")?; + nixos_shell.run_cmd_and_expect("hello", "Hello, world!")?; + Ok(()) +} diff --git a/test/nixos/tests/podman/Cargo.toml b/test/nixos/tests/podman/Cargo.toml new file mode 100644 index 000000000..828f19f11 --- /dev/null +++ b/test/nixos/tests/podman/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "test-podman" +version.workspace = true +repository.workspace = true +license.workspace = true +edition.workspace = true + +[dependencies] +nixos-test-framework.workspace = true \ No newline at end of file diff --git a/test/nixos/tests/podman/extra_config.nix b/test/nixos/tests/podman/extra_config.nix new file mode 100644 index 000000000..47e14dad3 --- /dev/null +++ b/test/nixos/tests/podman/extra_config.nix @@ -0,0 +1,5 @@ +{ config, lib, pkgs, ... }: + +{ + virtualisation.podman.enable = true; +} diff --git a/test/nixos/tests/podman/src/main.rs b/test/nixos/tests/podman/src/main.rs new file mode 100644 index 000000000..2c748dc1d --- /dev/null +++ b/test/nixos/tests/podman/src/main.rs @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: MPL-2.0 + +//! The test suite for podman on Asterinas NixOS. +//! +//! # Document maintenance +//! +//! An application's test suite and its "Verified Usage" section in Asterinas Book +//! should always be kept in sync. +//! So whenever you modify the test suite, +//! review the documentation and see if should be updated accordingly. + +use nixos_test_framework::*; + +nixos_test_main!(); + +#[nixos_test] +fn alpine_container_basic(nixos_shell: &mut Session) -> Result<(), Error> { + // Run alpine container + nixos_shell.run_cmd_and_expect( + "podman run --name=c1 docker.io/library/alpine ls /etc", + "alpine-release", + )?; + + // List images + nixos_shell.run_cmd_and_expect("podman image ls", "docker.io/library/alpine")?; + + // List containers + nixos_shell.run_cmd_and_expect("podman ps -a", "Exited (0)")?; + + // Remove container + nixos_shell.run_cmd("podman rm c1")?; + + Ok(()) +}