Migrate the original NixOS tests using the new framework

This commit is contained in:
Chen Chengjun 2026-01-13 08:58:30 +00:00 committed by Tate, Hongliang Tian
parent c439df3d02
commit d193ba821a
15 changed files with 154 additions and 82 deletions

View File

@ -58,7 +58,6 @@ DNS_SERVER ?= none
# NixOS settings # NixOS settings
NIXOS_DISK_SIZE_IN_MB ?= 8192 NIXOS_DISK_SIZE_IN_MB ?= 8192
NIXOS_DISABLE_SYSTEMD ?= false NIXOS_DISABLE_SYSTEMD ?= false
NIXOS_TEST_COMMAND ?=
# The following option is only effective when NIXOS_DISABLE_SYSTEMD is set to 'true'. # 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. # Use a login shell to ensure that environment variables are initialized correctly.
NIXOS_STAGE_2_INIT ?= /bin/sh -l NIXOS_STAGE_2_INIT ?= /bin/sh -l
@ -326,7 +325,11 @@ endif
iso: BOOT_PROTOCOL = linux-efi-handover64 iso: BOOT_PROTOCOL = linux-efi-handover64
iso: iso:
@make kernel @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 # Build the Asterinas NixOS ISO installer image and then do installation
run_iso: OVMF = off run_iso: OVMF = off
@ -337,18 +340,25 @@ run_iso:
nixos: BOOT_PROTOCOL = linux-efi-handover64 nixos: BOOT_PROTOCOL = linux-efi-handover64
nixos: nixos:
@make kernel @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), # After creating a Asterinas NixOS installation (via either the `run_iso` or `nixos` target),
# run the NixOS # run the NixOS
run_nixos: OVMF = off run_nixos: OVMF = off
run_nixos: 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 # Build the Asterinas NixOS patched packages
cachix: cachix:
@nix-build distro/cachix \ @nix-build distro/cachix \
--argstr test-command "${NIXOS_TEST_COMMAND}" \
--option extra-substituters "${RELEASE_SUBSTITUTER} ${DEV_SUBSTITUTER}" \ --option extra-substituters "${RELEASE_SUBSTITUTER} ${DEV_SUBSTITUTER}" \
--option extra-trusted-public-keys "${RELEASE_TRUSTED_PUBLIC_KEY} ${DEV_TRUSTED_PUBLIC_KEY}" \ --option extra-trusted-public-keys "${RELEASE_TRUSTED_PUBLIC_KEY} ${DEV_TRUSTED_PUBLIC_KEY}" \
--out-link cachix.list --out-link cachix.list
@ -426,6 +436,7 @@ format:
@./tools/format_all.sh @./tools/format_all.sh
@nixfmt ./distro @nixfmt ./distro
@$(MAKE) --no-print-directory -C test/initramfs format @$(MAKE) --no-print-directory -C test/initramfs format
@$(MAKE) --no-print-directory -C test/nixos format
.PHONY: check .PHONY: check
check: initramfs $(CARGO_OSDK) check: initramfs $(CARGO_OSDK)
@ -471,6 +482,9 @@ check: initramfs $(CARGO_OSDK)
@# Check formatting issues of the C code and Nix files (regression tests) @# Check formatting issues of the C code and Nix files (regression tests)
@$(MAKE) --no-print-directory -C test/initramfs check @$(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 @# Check typos
@typos @typos
@# Check formatting issues of Nix files under distro directory @# Check formatting issues of Nix files under distro directory

View File

@ -33,7 +33,6 @@
(import ./overlays/podman/default.nix) (import ./overlays/podman/default.nix)
(import ./overlays/switch-to-configuration-ng/default.nix) (import ./overlays/switch-to-configuration-ng/default.nix)
(import ./overlays/systemd/default.nix) (import ./overlays/systemd/default.nix)
(import ./overlays/test-asterinas/default.nix)
]; ];
# The Asterinas NixOS special options. # The Asterinas NixOS special options.
@ -64,11 +63,6 @@
default = "@aster-console@"; default = "@aster-console@";
description = "The console device."; 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 { break-into-stage-1-shell = lib.mkOption {
type = lib.types.bool; type = lib.types.bool;
default = false; default = false;

View File

@ -126,7 +126,7 @@ cleanup() {
} }
trap cleanup EXIT INT TERM ERR 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 @aster-configuration@ ${BUILD_DIR}/etc/nixos/aster_configuration.nix
cp -r @aster-etc-nixos@/modules ${BUILD_DIR}/etc/nixos cp -r @aster-etc-nixos@/modules ${BUILD_DIR}/etc/nixos
cp -r @aster-etc-nixos@/overlays ${BUILD_DIR}/etc/nixos cp -r @aster-etc-nixos@/overlays ${BUILD_DIR}/etc/nixos

View File

@ -1,8 +1,8 @@
{ pkgs ? import <nixpkgs> { }, test-command ? "", extra-substituters ? "" { pkgs ? import <nixpkgs> { }, extra-substituters ? ""
, extra-trusted-public-keys ? "", ... }: , extra-trusted-public-keys ? "", ... }:
let let
installer = pkgs.callPackage ../aster_nixos_installer { 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"); nixos = pkgs.nixos (import "${installer}/etc_nixos/configuration.nix");
cachixPkgs = with nixos.pkgs; cachixPkgs = with nixos.pkgs;

View File

@ -74,15 +74,6 @@ in {
${config.aster_nixos.stage-2-hook} ${config.aster_nixos.stage-2-hook}
fi 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. # Suppress error and warning messages of systemd.
# TODO: Fix errors and warnings from systemd and remove this setting. # TODO: Fix errors and warnings from systemd and remove this setting.
environment.sessionVariables = { SYSTEMD_LOG_LEVEL = "crit"; }; environment.sessionVariables = { SYSTEMD_LOG_LEVEL = "crit"; };

View File

@ -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
'';
};
}

View File

@ -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 "<nixpkgs>" -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

View File

@ -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"

View File

@ -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

View File

@ -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(())
}

View File

@ -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

View File

@ -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 '<nixpkgs>' -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(())
}

View File

@ -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

View File

@ -0,0 +1,5 @@
{ config, lib, pkgs, ... }:
{
virtualisation.podman.enable = true;
}

View File

@ -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(())
}