From 2152a3f20791d0d11591bc5e2676e7848120a8bc Mon Sep 17 00:00:00 2001
From: Daniel Barlow <dan@telent.net>
Date: Fri, 29 Dec 2023 22:11:04 +0000
Subject: [PATCH] a test for liminix-rebuild

it's hacky as Selby, but it's better than no test
---
 pkgs/run-liminix-vm/run-liminix-vm.fnl        |  2 +-
 tests/min-copy-closure/config-ext4.nix        |  8 +++
 .../min-copy-closure/test-liminix-rebuild.sh  | 58 +++++++++++++++++++
 tests/min-copy-closure/wait-for-reboot.expect | 18 ++++++
 .../min-copy-closure/wait-until-ready.expect  |  4 ++
 tests/min-copy-closure/with-figlet.nix        |  7 +++
 6 files changed, 96 insertions(+), 1 deletion(-)
 create mode 100644 tests/min-copy-closure/config-ext4.nix
 create mode 100644 tests/min-copy-closure/test-liminix-rebuild.sh
 create mode 100644 tests/min-copy-closure/wait-for-reboot.expect
 create mode 100644 tests/min-copy-closure/with-figlet.nix

diff --git a/pkgs/run-liminix-vm/run-liminix-vm.fnl b/pkgs/run-liminix-vm/run-liminix-vm.fnl
index bbefd55..bce1a20 100644
--- a/pkgs/run-liminix-vm/run-liminix-vm.fnl
+++ b/pkgs/run-liminix-vm/run-liminix-vm.fnl
@@ -101,7 +101,7 @@
        "-drive" (.. "if=none,format=raw,id=hd0,file=" disk)
        "-device" "virtio-blk-pci,drive=hd0"
        ]
-      (let [cmdline (.. cmdline " mem=256M liminix mtdparts=phram0:16M(rootfs) phram.phram=phram0," options.phram-address ",16Mi,65536 root=/dev/mtdblock0")]
+      (let [cmdline (.. "root=/dev/mtdblock0" " " cmdline " mem=256M liminix mtdparts=phram0:16M(rootfs) phram.phram=phram0," options.phram-address ",16Mi,65536")]
         ["-kernel" options.kernel "-append" cmdline])))
 
 (local bin {
diff --git a/tests/min-copy-closure/config-ext4.nix b/tests/min-copy-closure/config-ext4.nix
new file mode 100644
index 0000000..696e408
--- /dev/null
+++ b/tests/min-copy-closure/config-ext4.nix
@@ -0,0 +1,8 @@
+{ config, pkgs, lib, ... } :
+{
+  imports= [
+    ./configuration.nix
+    ../../modules/outputs/ext4fs.nix
+  ];
+  rootfsType = lib.mkForce "ext4";
+}
diff --git a/tests/min-copy-closure/test-liminix-rebuild.sh b/tests/min-copy-closure/test-liminix-rebuild.sh
new file mode 100644
index 0000000..787e375
--- /dev/null
+++ b/tests/min-copy-closure/test-liminix-rebuild.sh
@@ -0,0 +1,58 @@
+# This is a test for liminix-rebuild. It's not a CI test because
+# liminix-rebuild calls nix-build so won't run inside a derivation,
+# meaning you have to remember to run it manually when changing
+# liminix-rebuild
+
+# nix-shell -p  expect socat --run "sh ./tests/min-copy-closure/test-liminix-rebuild.sh "
+
+. tests/test-helpers.sh
+
+set -e
+
+here=$(pwd)/tests/min-copy-closure
+top=$(pwd)
+
+work=$(mktemp -d -t "test-lim-rebuild-XXXXXX")
+echo $work
+cd $work
+
+deriv(){
+    (cd $top && nix-build -I liminix-config=${here}/config-ext4.nix  --arg device "import ./devices/qemu-armv7l" -A $1 );
+}
+
+PATH=$(deriv pkgs.pkgsBuildBuild.min-copy-closure)/bin:$(deriv pkgs.pkgsBuildBuild.run-liminix-vm)/bin:$PATH
+
+rootfs=$(deriv outputs.rootfs)
+kernel=$(deriv outputs.zimage)
+uboot=$(deriv outputs.u-boot)
+
+test -d ./vm && rm -rf vm
+mkdir ./vm
+
+cat ${rootfs} > rootfs
+
+truncate -s 24M rootfs
+resize2fs rootfs
+
+dd if=rootfs of=disk-image bs=512 seek=4 conv=sync
+echo '4,-,L,*' | sfdisk disk-image
+
+run-liminix-vm --background vm  \
+	       --command-line "console=ttyAMA0 panic=10 oops=panic loglevel=8 root=/dev/vda1 rootfstype=ext4" \
+	       --phram-address 0x50000000 --arch arm \
+	       --lan "user,hostfwd=tcp::2022-:22" \
+	       --flag -append --flag "root=/dev/vda1" --flag -hda \
+	       --flag disk-image $kernel /dev/null
+
+expect ${here}/wait-until-ready.expect
+echo "READY"
+
+touch known_hosts
+export SSH_COMMAND="ssh -o UserKnownHostsFile=${work}/known_hosts -o StrictHostKeyChecking=no -p 2022 -i ${here}/id"
+
+(cd ${top} && liminix-rebuild root@localhost -I liminix-config=${here}/with-figlet.nix  --arg device "import ./devices/qemu-armv7l")
+
+ls -l vm
+
+cd ${work} && expect $here/wait-for-reboot.expect
+cd / ; rm -rf $work
diff --git a/tests/min-copy-closure/wait-for-reboot.expect b/tests/min-copy-closure/wait-for-reboot.expect
new file mode 100644
index 0000000..41ee417
--- /dev/null
+++ b/tests/min-copy-closure/wait-for-reboot.expect
@@ -0,0 +1,18 @@
+set timeout 60
+
+spawn socat unix-connect:vm/console -
+
+expect {
+       "s6-svscan exited" {  }
+       timeout { exit 1 }
+}
+expect {
+       "s6-linux-init" { send "\r\n" };
+       "# " { send "\r\n" };
+}
+
+expect {
+       "# " { send "echo \$PATH; md5sum /persist/activate; figlet Yes\r\n" };
+}
+
+expect "#"
\ No newline at end of file
diff --git a/tests/min-copy-closure/wait-until-ready.expect b/tests/min-copy-closure/wait-until-ready.expect
index 7fb9a66..3da5adb 100644
--- a/tests/min-copy-closure/wait-until-ready.expect
+++ b/tests/min-copy-closure/wait-until-ready.expect
@@ -2,3 +2,7 @@ set timeout 60
 
 spawn socat unix-connect:vm/console -
 send "\r\n"
+expect {
+       "# " { send "hostname\r\n" };
+}
+expect  "(none)"
\ No newline at end of file
diff --git a/tests/min-copy-closure/with-figlet.nix b/tests/min-copy-closure/with-figlet.nix
new file mode 100644
index 0000000..5614d92
--- /dev/null
+++ b/tests/min-copy-closure/with-figlet.nix
@@ -0,0 +1,7 @@
+{ pkgs, ... } :
+{
+  imports= [./config-ext4.nix];
+  defaultProfile.packages = with pkgs; [
+    figlet
+  ];
+}