@@ -2,9 +2,13 @@ use std::collections::HashMap;
22use std:: env;
33use std:: fs;
44use std:: path:: Path ;
5+ use std:: path:: PathBuf ;
6+ use std:: process:: Command ;
57use std:: sync:: mpsc:: channel;
68
79use lazy_static:: lazy_static;
10+ use log:: error;
11+ use rand:: Rng ;
812use regex:: Regex ;
913use tempfile:: tempdir_in;
1014use test_log:: test;
@@ -21,6 +25,43 @@ lazy_static! {
2125 static ref FILTER_ALL : Regex = Regex :: new( ".*" ) . unwrap( ) ;
2226}
2327
28+ fn gen_image_name ( ) -> PathBuf {
29+ let mut path = PathBuf :: new ( ) ;
30+ path. push ( "/tmp" ) ;
31+
32+ let id = rand:: thread_rng ( ) . gen_range ( 100_000 ..1_000_000 ) ;
33+ let image = format ! ( "/tmp/image-{id}.qcow2" ) ;
34+ path. push ( image) ;
35+
36+ path
37+ }
38+
39+ // Create a CoW image to ensure each test runs in a clean image.
40+ fn create_new_image ( image : PathBuf ) -> Option < PathBuf > {
41+ let out_image = gen_image_name ( ) ;
42+ let out = Command :: new ( "qemu-img" )
43+ . arg ( "create" )
44+ . arg ( "-F" )
45+ . arg ( "raw" )
46+ . arg ( "-b" )
47+ . arg ( image)
48+ . arg ( "-f" )
49+ . arg ( "qcow2" )
50+ . arg ( out_image. clone ( ) )
51+ . output ( )
52+ . expect ( "error creating image file" ) ;
53+ if !out. status . success ( ) {
54+ error ! (
55+ "error creating image file: out={} err={} status={}" ,
56+ std:: str :: from_utf8( & out. stdout) . unwrap( ) ,
57+ std:: str :: from_utf8( & out. stderr) . unwrap( ) ,
58+ out. status
59+ ) ;
60+ return None ;
61+ }
62+ Some ( out_image)
63+ }
64+
2465// Expect that we can run the entire matrix successfully
2566#[ test]
2667fn test_run ( ) {
@@ -59,7 +100,7 @@ fn test_run_one() {
59100 target : vec ! [
60101 Target {
61102 name: "uefi image boots with uefi flag" . to_string( ) ,
62- image: Some ( asset( "image-uefi.raw-efi" ) ) ,
103+ image: create_new_image ( asset( "image-uefi.raw-efi" ) ) ,
63104 uefi: true ,
64105 command: "/mnt/vmtest/main.sh nixos" . to_string( ) ,
65106 kernel: None ,
@@ -68,7 +109,7 @@ fn test_run_one() {
68109 } ,
69110 Target {
70111 name: "not uefi image boots without uefi flag" . to_string( ) ,
71- image: Some ( asset( "image-not-uefi.raw" ) ) ,
112+ image: create_new_image ( asset( "image-not-uefi.raw" ) ) ,
72113 uefi: false ,
73114 command: "/mnt/vmtest/main.sh nixos" . to_string( ) ,
74115 kernel: None ,
@@ -92,7 +133,7 @@ fn test_run_out_of_bounds() {
92133 target : vec ! [
93134 Target {
94135 name: "uefi image boots with uefi flag" . to_string( ) ,
95- image: Some ( asset( "image-uefi.raw-efi" ) ) ,
136+ image: create_new_image ( asset( "image-uefi.raw-efi" ) ) ,
96137 uefi: true ,
97138 command: "/mnt/vmtest/main.sh nixos" . to_string( ) ,
98139 kernel: None ,
@@ -101,7 +142,7 @@ fn test_run_out_of_bounds() {
101142 } ,
102143 Target {
103144 name: "not uefi image boots without uefi flag" . to_string( ) ,
104- image: Some ( asset( "image-not-uefi.raw" ) ) ,
145+ image: create_new_image ( asset( "image-not-uefi.raw" ) ) ,
105146 uefi: false ,
106147 command: "/mnt/vmtest/main.sh nixos" . to_string( ) ,
107148 kernel: None ,
@@ -122,7 +163,7 @@ fn test_not_uefi() {
122163 let config = Config {
123164 target : vec ! [ Target {
124165 name: "uefi image does not boot without uefi flag" . to_string( ) ,
125- image: Some ( asset( "image-uefi.raw-efi" ) ) ,
166+ image: create_new_image ( asset( "image-uefi.raw-efi" ) ) ,
126167 uefi: false ,
127168 command: "echo unreachable" . to_string( ) ,
128169 kernel: None ,
@@ -274,7 +315,7 @@ fn test_run_custom_resources() {
274315 target : vec ! [
275316 Target {
276317 name: "Custom number of CPUs" . to_string( ) ,
277- image: Some ( asset( "image-uefi.raw-efi" ) ) ,
318+ image: create_new_image ( asset( "image-uefi.raw-efi" ) ) ,
278319 uefi: true ,
279320 command: r#"bash -xc "[[ "$(nproc)" == "1" ]]""# . into( ) ,
280321 kernel: None ,
@@ -286,9 +327,10 @@ fn test_run_custom_resources() {
286327 } ,
287328 Target {
288329 name: "Custom amount of RAM" . to_string( ) ,
289- image: Some ( asset( "image-uefi.raw-efi" ) ) ,
330+ image: create_new_image ( asset( "image-uefi.raw-efi" ) ) ,
290331 uefi: true ,
291- command: r#"bash -xc "cat /proc/meminfo | grep 'MemTotal: 222204 kB'""#
332+ // Should be in the 200 thousands, but it's variable.
333+ command: r#"bash -xc "cat /proc/meminfo | grep 'MemTotal: 2..... kB'""#
292334 . into( ) ,
293335 kernel: None ,
294336 kernel_args: None ,
@@ -313,7 +355,7 @@ fn test_run_custom_mounts() {
313355 target : vec ! [
314356 Target {
315357 name: "mount" . to_string( ) ,
316- image: Some ( asset( "image-uefi.raw-efi" ) ) ,
358+ image: create_new_image ( asset( "image-uefi.raw-efi" ) ) ,
317359 uefi: true ,
318360 command: r#"bash -xc "[[ -e /tmp/mount/README.md ]]""# . into( ) ,
319361 kernel: None ,
@@ -331,7 +373,7 @@ fn test_run_custom_mounts() {
331373 } ,
332374 Target {
333375 name: "RO mount" . to_string( ) ,
334- image: Some ( asset( "image-uefi.raw-efi" ) ) ,
376+ image: create_new_image ( asset( "image-uefi.raw-efi" ) ) ,
335377 uefi: true ,
336378 command: r#"bash -xc "(touch /tmp/ro/hi && exit -1) || true""# . into( ) ,
337379 kernel: None ,
0 commit comments