# A comprehensive test for NixOS.
#
# This test suite is designed to run on GitHub's larger runners,
# leveraging their extended storage capacity.
# Here, we can thoroughly test a wider range of NixOS commands and various applications.
# As GitHub charges more for these premium runners,
# these tests will only run when changes are detected in AsterNixOS-specific content.
name: Test AsterNixOS (full)

on:
  workflow_dispatch:
  pull_request:
    paths:
      - distro/**
      - .github/workflows/test_nixos_full.yml
      - tools/nixos/**
  push:
    branches:
      - main
    paths:
      - distro/**
      - .github/workflows/test_nixos_full.yml
      - tools/nixos/**

jobs:
  full-nixos-test:
    runs-on: ubuntu-4-cores-150GB-ssd
    container:
      image: asterinas/asterinas:0.17.0-20251213
      options: -v /dev:/dev --privileged
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@v4
      - name: Modify configuration.nix to include test-asterinas
        run: |
          sed -i 's/environment.systemPackages = with pkgs; \[ hello-asterinas \];/environment.systemPackages = with pkgs; \[ test-asterinas \];/' \
            distro/etc_nixos/configuration.nix

      - name: Test nix commands
        run: |
          make nixos NIXOS_DISK_SIZE_IN_MB=6144 NIXOS_TEST_COMMAND='test-nix-commands'
          make run_nixos || true

      - name: Check results
        run: |
          tail --lines 50 qemu.log | awk '
          BEGIN { hello_count=0 } {
            if ($0 ~ /^Hello, world!/) hello_count++
          }
          END {
            if (hello_count == 4) exit 0
            else exit 1
          }' || (echo "Test nix commands failed" && exit 1)
          echo "Test nix commands succeeds!"

  podman-test:
    runs-on: ubuntu-4-cores-150GB-ssd
    container:
      image: asterinas/asterinas:0.17.0-20251213
      options: -v /dev:/dev --privileged
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v4
      - name: Modify configuration.nix to include test-asterinas and podman
        run: |
          sed -i \
            -e 's/environment.systemPackages = with pkgs; \[ hello-asterinas \];/environment.systemPackages = with pkgs; \[ test-asterinas \];/' \
            -e 's/^[[:space:]]*# virtualisation\.podman\.enable = true;/  virtualisation.podman.enable = true;/' \
            distro/etc_nixos/configuration.nix

      - name: Run podman commands
        run: |
          make nixos NIXOS_DISK_SIZE_IN_MB=8192 NIXOS_TEST_COMMAND='test-podman'
          make run_nixos || true

      - name: Check results
        run: |
          tail --lines 50 qemu.log | grep -q "^Test podman succeeds" || (echo "Test podman failed" && exit 1)
          echo "Test podman succeeds!"
