From 3b470d4f4b0d26b31c2152ae8dd112516d185fab Mon Sep 17 00:00:00 2001 From: Damjan Georgievski Date: Mon, 23 Mar 2020 00:38:00 +0100 Subject: [PATCH] tests: add support for initramfs the integration test creates an initramfs image based on AlpineLinux mini root filesystem with a simple /init script that just echoes a string to the console. The string is passed via the kernel cmdline as an environment variable. Signed-off-by: Damjan Georgievski --- scripts/run_integration_tests.sh | 26 ++++++++++++++++++++ scripts/sha1sums | 1 + tests/integration.rs | 41 ++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/scripts/run_integration_tests.sh b/scripts/run_integration_tests.sh index 75359d9e9..f101c9d9d 100755 --- a/scripts/run_integration_tests.sh +++ b/scripts/run_integration_tests.sh @@ -68,6 +68,32 @@ if [ ! -f "$EOAN_OS_RAW_IMAGE" ]; then popd fi +ALPINE_MINIROOTFS_URL="http://dl-cdn.alpinelinux.org/alpine/v3.11/releases/x86_64/alpine-minirootfs-3.11.3-x86_64.tar.gz" +ALPINE_MINIROOTFS_TARBALL="$WORKLOADS_DIR/alpine-minirootfs-x86_64.tar.gz" +if [ ! -f "$ALPINE_MINIROOTFS_TARBALL" ]; then + pushd $WORKLOADS_DIR + time wget --quiet $ALPINE_MINIROOTFS_URL -O $ALPINE_MINIROOTFS_TARBALL || exit 1 + popd +fi + +ALPINE_INITRAMFS_IMAGE="$WORKLOADS_DIR/alpine_initramfs.img" +if [ ! -f "$ALPINE_INITRAMFS_IMAGE" ]; then + pushd $WORKLOADS_DIR + mkdir alpine-minirootfs + tar xf "$ALPINE_MINIROOTFS_TARBALL" -C alpine-minirootfs + cat > alpine-minirootfs/init <<-EOF + #! /bin/sh + mount -t devtmpfs dev /dev + echo \$TEST_STRING > /dev/console + poweroff -f + EOF + chmod +x alpine-minirootfs/init + cd alpine-minirootfs + find . -print0 | + cpio --null --create --verbose --owner root:root --format=newc > "$ALPINE_INITRAMFS_IMAGE" + popd +fi + pushd $WORKLOADS_DIR sha1sum sha1sums --check if [ $? -ne 0 ]; then diff --git a/scripts/sha1sums b/scripts/sha1sums index ff2590a6a..ff6ed8d5f 100644 --- a/scripts/sha1sums +++ b/scripts/sha1sums @@ -4,3 +4,4 @@ cf7cfa783082fc4d6b4d1c0a53e4402648c14b82 clear-31311-cloudguest.img 8db9cc58b01452ce2d06c313177e6e74d8582d93 bionic-server-cloudimg-amd64-raw.img 4a452cdcf781f95d31a1668ecb92a937c176709a eoan-server-cloudimg-amd64.img 91fdfb21df8920fd55915edf7669282cda2505f6 eoan-server-cloudimg-amd64-raw.img +d4a44acc6014d5f83dea1c625c43d677a95fa75f alpine-minirootfs-x86_64.tar.gz diff --git a/tests/integration.rs b/tests/integration.rs index 1beeeebe2..54418899b 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -3842,4 +3842,45 @@ mod tests { Ok(()) }); } + + #[test] + fn test_initramfs() { + test_block!(tb, "", { + let mut clear = ClearDiskConfig::new(); + let guest = Guest::new(&mut clear); + let mut workload_path = dirs::home_dir().unwrap(); + workload_path.push("workloads"); + + let mut kernel_path = workload_path.clone(); + kernel_path.push("vmlinux"); + + let mut initramfs_path = workload_path; + initramfs_path.push("alpine_initramfs.img"); + + let test_string = String::from("axz34i9rylotd8n50wbv6kcj7f2qushme1pg"); + let cmdline = format!("console=hvc0 quiet TEST_STRING={}", test_string); + + let mut child = GuestCommand::new(&guest) + .args(&["--kernel", kernel_path.to_str().unwrap()]) + .args(&["--initramfs", initramfs_path.to_str().unwrap()]) + .args(&["--cmdline", &cmdline]) + .capture_output() + .spawn() + .unwrap(); + + thread::sleep(std::time::Duration::new(20, 0)); + + let _ = child.kill(); + match child.wait_with_output() { + Ok(out) => { + let s = String::from_utf8_lossy(&out.stdout); + println!("{}", s); + aver_ne!(tb, s.lines().position(|line| line == test_string), None); + } + Err(_) => aver!(tb, false), + } + + Ok(()) + }); + } }