From 7ebfe90985adc5c2d160f4145c1cde662f0542b9 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Thu, 18 Jul 2019 08:57:01 +0100 Subject: [PATCH] tests: Use a temporary directory for the temporary test files Use the tempdir crate to create a temporary directory that is deleted when the structure goes out of scope. Use this temporary directory for all temporary test files created by the tests. The cloud init file is still in /tmp as that is created by the test wrapper code. This is the first stage towards being able to run the integration tests in parallel. Signed-off-by: Rob Bradford --- Cargo.lock | 24 +++++++++++++ Cargo.toml | 1 + src/main.rs | 101 +++++++++++++++++++++++++++++----------------------- 3 files changed, 82 insertions(+), 44 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cbdfbcbc3..064083b06 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -155,6 +155,7 @@ dependencies = [ "credibility 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "dirs 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "ssh2 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "vmm 0.1.0", ] @@ -525,6 +526,18 @@ dependencies = [ "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rand" version = "0.6.5" @@ -807,6 +820,15 @@ dependencies = [ "unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tempdir" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tempfile" version = "3.0.8" @@ -1066,6 +1088,7 @@ dependencies = [ "checksum pnet_transport 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5faa55dcf725487a699adcff88dfea8f17ea34fa2640528866d9acbb4e3a104f" "checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" "checksum quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "faf4799c5d274f3868a4aae320a0a182cbd2baee377b378f080e16a23e9d80db" +"checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" "checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" "checksum rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" "checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" @@ -1098,6 +1121,7 @@ dependencies = [ "checksum syntex_errors 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)" = "04c48f32867b6114449155b2a82114b86d4b09e1bddb21c47ff104ab9172b646" "checksum syntex_pos 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3fd49988e52451813c61fecbe9abb5cfd4e1b7bb6cdbb980a6fbcbab859171a6" "checksum syntex_syntax 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7628a0506e8f9666fdabb5f265d0059b059edac9a3f810bda077abb5d826bd8d" +"checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" "checksum tempfile 3.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7dc4738f2e68ed2855de5ac9cdbe05c9216773ecde4739b2f095002ab03a13ef" "checksum term 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "fa63644f74ce96fbeb9b794f66aff2a52d601cbd5e80f4b97123e3899f4570f1" "checksum termion 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6a8fb22f7cde82c8220e5aeacb3258ed7ce996142c77cba193f203515e26c330" diff --git a/Cargo.toml b/Cargo.toml index 84e34cfba..7585aaa5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ vmm = { path = "vmm" } ssh2 = "=0.3.3" dirs = "2.0.0" credibility = "0.1.3" +tempdir="0.3.7" [features] default = [] diff --git a/src/main.rs b/src/main.rs index 8530ab6f0..a21a03e7c 100755 --- a/src/main.rs +++ b/src/main.rs @@ -162,6 +162,7 @@ mod tests { use std::process::Command; use std::string::String; use std::thread; + use tempdir::TempDir; fn ssh_command(command: &str) -> String { let mut s = String::new(); @@ -206,7 +207,7 @@ mod tests { s } - fn prepare_files() -> (Vec<&'static str>, String) { + fn prepare_files(tmp_dir: &TempDir) -> (Vec, String) { let mut workload_path = dirs::home_dir().unwrap(); workload_path.push("workloads"); @@ -219,12 +220,13 @@ mod tests { let mut osdisk_raw_base_path = workload_path.clone(); osdisk_raw_base_path.push("clear-29810-cloud-raw.img"); - let osdisk_path = "/tmp/osdisk.img"; - let osdisk_raw_path = "/tmp/osdisk_raw.img"; - let cloudinit_path = "/tmp/cloudinit.img"; + let cloudinit_path = String::from("/tmp/cloudinit.img"); - fs::copy(osdisk_base_path, osdisk_path).expect("copying of OS source disk image failed"); - fs::copy(osdisk_raw_base_path, osdisk_raw_path) + let osdisk_path = String::from(tmp_dir.path().join("osdisk.img").to_str().unwrap()); + let osdisk_raw_path = String::from(tmp_dir.path().join("osdisk_raw.img").to_str().unwrap()); + + fs::copy(osdisk_base_path, &osdisk_path).expect("copying of OS source disk image failed"); + fs::copy(osdisk_raw_base_path, &osdisk_raw_path) .expect("copying of OS source disk raw image failed"); let disks = vec![osdisk_path, cloudinit_path, osdisk_raw_path]; @@ -232,7 +234,7 @@ mod tests { (disks, String::from(fw_path.to_str().unwrap())) } - fn prepare_virtiofsd() -> (std::process::Child, String) { + fn prepare_virtiofsd(tmp_dir: &TempDir) -> (std::process::Child, String) { let mut workload_path = dirs::home_dir().unwrap(); workload_path.push("workloads"); @@ -244,7 +246,8 @@ mod tests { shared_dir_path.push("shared_dir"); let shared_dir_path = String::from(shared_dir_path.to_str().unwrap()); - let virtiofsd_socket_path = String::from("/tmp/virtiofs.sock"); + let virtiofsd_socket_path = + String::from(tmp_dir.path().join("virtiofs.sock").to_str().unwrap()); // Start the daemon let child = Command::new(virtiofsd_path.as_str()) @@ -297,12 +300,13 @@ mod tests { #[test] fn test_simple_launch() { test_block!(tb, "", { - let (disks, fw_path) = prepare_files(); + let tmp_dir = TempDir::new("ch").unwrap(); + let (disks, fw_path) = prepare_files(&tmp_dir); let mut child = Command::new("target/debug/cloud-hypervisor") .args(&["--cpus", "1"]) .args(&["--memory", "size=512M"]) .args(&["--kernel", fw_path.as_str()]) - .args(&["--disk", disks[0], disks[1]]) + .args(&["--disk", disks[0].as_str(), disks[1].as_str()]) .args(&[ "--net", "tap=,mac=12:34:56:78:90:ab,ip=192.168.2.1,mask=255.255.255.0", @@ -329,12 +333,13 @@ mod tests { #[test] fn test_multi_cpu() { test_block!(tb, "", { - let (disks, fw_path) = prepare_files(); + let tmp_dir = TempDir::new("ch").unwrap(); + let (disks, fw_path) = prepare_files(&tmp_dir); let mut child = Command::new("target/debug/cloud-hypervisor") .args(&["--cpus", "2"]) .args(&["--memory", "size=512M"]) .args(&["--kernel", fw_path.as_str()]) - .args(&["--disk", disks[0], disks[1]]) + .args(&["--disk", disks[0].as_str(), disks[1].as_str()]) .args(&[ "--net", "tap=,mac=12:34:56:78:90:ab,ip=192.168.2.1,mask=255.255.255.0", @@ -357,12 +362,13 @@ mod tests { #[test] fn test_large_memory() { test_block!(tb, "", { - let (disks, fw_path) = prepare_files(); + let tmp_dir = TempDir::new("ch").unwrap(); + let (disks, fw_path) = prepare_files(&tmp_dir); let mut child = Command::new("target/debug/cloud-hypervisor") .args(&["--cpus", "1"]) .args(&["--memory", "size=5120M"]) .args(&["--kernel", fw_path.as_str()]) - .args(&["--disk", disks[0], disks[1]]) + .args(&["--disk", disks[0].as_str(), disks[1].as_str()]) .args(&[ "--net", "tap=,mac=12:34:56:78:90:ab,ip=192.168.2.1,mask=255.255.255.0", @@ -385,12 +391,13 @@ mod tests { #[test] fn test_pci_msi() { test_block!(tb, "", { - let (disks, fw_path) = prepare_files(); + let tmp_dir = TempDir::new("ch").unwrap(); + let (disks, fw_path) = prepare_files(&tmp_dir); let mut child = Command::new("target/debug/cloud-hypervisor") .args(&["--cpus", "1"]) .args(&["--memory", "size=512M"]) .args(&["--kernel", fw_path.as_str()]) - .args(&["--disk", disks[0], disks[1]]) + .args(&["--disk", disks[0].as_str(), disks[1].as_str()]) .args(&[ "--net", "tap=,mac=12:34:56:78:90:ab,ip=192.168.2.1,mask=255.255.255.0", @@ -420,7 +427,8 @@ mod tests { #[test] fn test_vmlinux_boot() { test_block!(tb, "", { - let (disks, _) = prepare_files(); + let tmp_dir = TempDir::new("ch").unwrap(); + let (disks, _) = prepare_files(&tmp_dir); let mut workload_path = dirs::home_dir().unwrap(); workload_path.push("workloads"); @@ -431,7 +439,7 @@ mod tests { .args(&["--cpus", "1"]) .args(&["--memory", "size=512M"]) .args(&["--kernel", kernel_path.to_str().unwrap()]) - .args(&["--disk", disks[0], disks[1]]) + .args(&["--disk", disks[0].as_str(), disks[1].as_str()]) .args(&["--net", "tap=,mac=12:34:56:78:90:ab,ip=192.168.2.1,mask=255.255.255.0"]) .args(&["--cmdline", "root=PARTUUID=3cb0e0a5-925d-405e-bc55-edf0cec8f10a console=tty0 console=ttyS0,115200n8 console=hvc0 quiet init=/usr/lib/systemd/systemd-bootchart initcall_debug tsc=reliable no_timer_check noreplace-smp cryptomgr.notests rootfstype=ext4,btrfs,xfs kvm-intel.nested=1 rw"]) .spawn() @@ -462,7 +470,8 @@ mod tests { #[test] fn test_bzimage_boot() { test_block!(tb, "", { - let (disks, _) = prepare_files(); + let tmp_dir = TempDir::new("ch").unwrap(); + let (disks, _) = prepare_files(&tmp_dir); let mut workload_path = dirs::home_dir().unwrap(); workload_path.push("workloads"); @@ -473,7 +482,7 @@ mod tests { .args(&["--cpus", "1"]) .args(&["--memory", "size=512M"]) .args(&["--kernel", kernel_path.to_str().unwrap()]) - .args(&["--disk", disks[0], disks[1]]) + .args(&["--disk", disks[0].as_str(), disks[1].as_str()]) .args(&["--net", "tap=,mac=12:34:56:78:90:ab,ip=192.168.2.1,mask=255.255.255.0"]) .args(&["--cmdline", "root=PARTUUID=3cb0e0a5-925d-405e-bc55-edf0cec8f10a console=tty0 console=ttyS0,115200n8 console=hvc0 quiet init=/usr/lib/systemd/systemd-bootchart initcall_debug tsc=reliable no_timer_check noreplace-smp cryptomgr.notests rootfstype=ext4,btrfs,xfs kvm-intel.nested=1 rw"]) .spawn() @@ -504,12 +513,13 @@ mod tests { #[test] fn test_split_irqchip() { test_block!(tb, "", { - let (disks, fw_path) = prepare_files(); + let tmp_dir = TempDir::new("ch").unwrap(); + let (disks, fw_path) = prepare_files(&tmp_dir); let mut child = Command::new("target/debug/cloud-hypervisor") .args(&["--cpus", "1"]) .args(&["--memory", "size=512M"]) .args(&["--kernel", fw_path.as_str()]) - .args(&["--disk", disks[0], disks[1]]) + .args(&["--disk", disks[0].as_str(), disks[1].as_str()]) .args(&[ "--net", "tap=,mac=12:34:56:78:90:ab,ip=192.168.2.1,mask=255.255.255.0", @@ -547,8 +557,9 @@ mod tests { #[test] fn test_virtio_fs() { test_block!(tb, "", { - let (disks, _) = prepare_files(); - let (mut daemon_child, virtiofsd_socket_path) = prepare_virtiofsd(); + let tmp_dir = TempDir::new("ch").unwrap(); + let (disks, _) = prepare_files(&tmp_dir); + let (mut daemon_child, virtiofsd_socket_path) = prepare_virtiofsd(&tmp_dir); let mut workload_path = dirs::home_dir().unwrap(); workload_path.push("workloads"); @@ -559,7 +570,7 @@ mod tests { .args(&["--cpus", "1"]) .args(&["--memory", "size=512M,file=/dev/shm"]) .args(&["--kernel", kernel_path.to_str().unwrap()]) - .args(&["--disk", disks[0], disks[1]]) + .args(&["--disk", disks[0].as_str(), disks[1].as_str()]) .args(&["--net", "tap=,mac=12:34:56:78:90:ab,ip=192.168.2.1,mask=255.255.255.0"]) .args(&[ "--fs", @@ -603,19 +614,20 @@ mod tests { #[test] fn test_virtio_pmem() { test_block!(tb, "", { - let (disks, _) = prepare_files(); + let tmp_dir = TempDir::new("ch").unwrap(); + let (disks, _) = prepare_files(&tmp_dir); let mut workload_path = dirs::home_dir().unwrap(); workload_path.push("workloads"); let mut kernel_path = workload_path.clone(); kernel_path.push("vmlinux-custom"); - let pmem_backend_path = "/tmp/pmem-file"; + let pmem_backend_path = tmp_dir.path().join("/tmp/pmem-file"); let mut pmem_backend_file = OpenOptions::new() .read(true) .write(true) .create(true) - .open(pmem_backend_path) + .open(&pmem_backend_path) .unwrap(); let pmem_backend_content = "foo"; @@ -629,13 +641,13 @@ mod tests { .args(&["--cpus", "1"]) .args(&["--memory", "size=512M"]) .args(&["--kernel", kernel_path.to_str().unwrap()]) - .args(&["--disk", disks[0], disks[1]]) + .args(&["--disk", disks[0].as_str(), disks[1].as_str()]) .args(&["--net", "tap=,mac=12:34:56:78:90:ab,ip=192.168.2.1,mask=255.255.255.0"]) .args(&[ "--pmem", format!( "file={},size={}", - pmem_backend_path, + pmem_backend_path.to_str().unwrap(), pmem_backend_file_size ) .as_str(), @@ -676,9 +688,6 @@ mod tests { ssh_command("sudo reboot"); let _ = child.wait(); - // Cleanup the file - fs::remove_file(pmem_backend_path).unwrap(); - Ok(()) }); } @@ -686,7 +695,8 @@ mod tests { #[test] fn test_boot_from_virtio_pmem() { test_block!(tb, "", { - let (disks, _) = prepare_files(); + let tmp_dir = TempDir::new("ch").unwrap(); + let (disks, _) = prepare_files(&tmp_dir); let mut workload_path = dirs::home_dir().unwrap(); workload_path.push("workloads"); @@ -697,14 +707,14 @@ mod tests { .args(&["--cpus", "1"]) .args(&["--memory", "size=512M"]) .args(&["--kernel", kernel_path.to_str().unwrap()]) - .args(&["--disk", disks[1]]) + .args(&["--disk", disks[1].as_str()]) .args(&["--net", "tap=,mac=12:34:56:78:90:ab,ip=192.168.2.1,mask=255.255.255.0"]) .args(&[ "--pmem", format!( "file={},size={}", disks[2], - fs::metadata(disks[2]).unwrap().len() + fs::metadata(&disks[2]).unwrap().len() ) .as_str(), ]) @@ -728,12 +738,13 @@ mod tests { #[test] fn test_multiple_network_interfaces() { test_block!(tb, "", { - let (disks, fw_path) = prepare_files(); + let tmp_dir = TempDir::new("ch").unwrap(); + let (disks, fw_path) = prepare_files(&tmp_dir); let mut child = Command::new("target/debug/cloud-hypervisor") .args(&["--cpus", "1"]) .args(&["--memory", "size=512M"]) .args(&["--kernel", fw_path.as_str()]) - .args(&["--disk", disks[0], disks[1]]) + .args(&["--disk", disks[0].as_str(), disks[1].as_str()]) .args(&[ "--net", "tap=,mac=12:34:56:78:90:ab,ip=192.168.2.1,mask=255.255.255.0", @@ -766,12 +777,13 @@ mod tests { #[test] fn test_serial_disable() { test_block!(tb, "", { - let (disks, fw_path) = prepare_files(); + let tmp_dir = TempDir::new("ch").unwrap(); + let (disks, fw_path) = prepare_files(&tmp_dir); let mut child = Command::new("target/debug/cloud-hypervisor") .args(&["--cpus", "1"]) .args(&["--memory", "size=512M"]) .args(&["--kernel", fw_path.as_str()]) - .args(&["--disk", disks[0], disks[1]]) + .args(&["--disk", disks[0].as_str(), disks[1].as_str()]) .args(&[ "--net", "tap=,mac=12:34:56:78:90:ab,ip=192.168.2.1,mask=255.255.255.0", @@ -813,13 +825,15 @@ mod tests { #[test] fn test_serial_file() { test_block!(tb, "", { - let serial_path = std::path::Path::new("/tmp/serial-output"); - let (disks, fw_path) = prepare_files(); + let tmp_dir = TempDir::new("ch").unwrap(); + let (disks, fw_path) = prepare_files(&tmp_dir); + + let serial_path = tmp_dir.path().join("/tmp/serial-output"); let mut child = Command::new("target/debug/cloud-hypervisor") .args(&["--cpus", "1"]) .args(&["--memory", "size=512M"]) .args(&["--kernel", fw_path.as_str()]) - .args(&["--disk", disks[0], disks[1]]) + .args(&["--disk", disks[0].as_str(), disks[1].as_str()]) .args(&[ "--net", "tap=,mac=12:34:56:78:90:ab,ip=192.168.2.1,mask=255.255.255.0", @@ -852,7 +866,6 @@ mod tests { let mut buf = String::new(); f.read_to_string(&mut buf).unwrap(); aver!(tb, buf.contains("cloud login:")); - std::fs::remove_file(serial_path).unwrap(); let _ = child.kill(); let _ = child.wait();