From be22af087bb0a48956f8d9f720034603678534da Mon Sep 17 00:00:00 2001 From: Manu Bretelle Date: Wed, 1 Nov 2023 13:27:33 -0700 Subject: [PATCH] qemu: unittests to validate the guest_init_path we are generating. Signed-off-by: Manu Bretelle --- Cargo.lock | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/qemu.rs | 42 +++++++++++- 3 files changed, 221 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 33aaab7..e979e91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -66,6 +66,12 @@ version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "base64" version = "0.13.1" @@ -211,6 +217,101 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" +[[package]] +name = "futures" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da0290714b38af9b4a7b094b8a37086d1b4e61f2df9122c3cad2577669145335" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff4dd66668b557604244583e3e1e1eada8c5c2e96a6d0d6653ede395b78bbacb" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c" + +[[package]] +name = "futures-executor" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa" + +[[package]] +name = "futures-macro" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.29", +] + +[[package]] +name = "futures-sink" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e36d3378ee38c2a36ad710c5d30c2911d752cb941c00c72dbabfb786a7970817" + +[[package]] +name = "futures-task" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efd193069b0ddadc69c46389b740bbccdd97203899b48d09c5f7969591d6bae2" + +[[package]] +name = "futures-timer" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c" + +[[package]] +name = "futures-util" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + [[package]] name = "getrandom" version = "0.2.10" @@ -222,6 +323,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "heck" version = "0.4.1" @@ -302,6 +409,18 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +[[package]] +name = "pin-project-lite" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -460,6 +579,50 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" +[[package]] +name = "relative-path" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c707298afce11da2efef2f600116fa93ffa7a032b5d7b628aa17711ec81383ca" + +[[package]] +name = "rstest" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97eeab2f3c0a199bc4be135c36c924b6590b88c377d416494288c14f2db30199" +dependencies = [ + "futures", + "futures-timer", + "rstest_macros", + "rustc_version", +] + +[[package]] +name = "rstest_macros" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d428f8247852f894ee1be110b375111b586d4fa431f6c46e64ba5a0dcccbe605" +dependencies = [ + "cfg-if", + "glob", + "proc-macro2", + "quote", + "regex", + "relative-path", + "rustc_version", + "syn 2.0.29", + "unicode-ident", +] + +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + [[package]] name = "rustix" version = "0.38.8" @@ -485,6 +648,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "semver" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" + [[package]] name = "serde" version = "1.0.185" @@ -516,6 +685,15 @@ dependencies = [ "serde", ] +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + [[package]] name = "strsim" version = "0.10.0" @@ -618,6 +796,7 @@ dependencies = [ "qapi", "rand", "regex", + "rstest", "scopeguard", "serde", "serde_derive", diff --git a/Cargo.toml b/Cargo.toml index 194e364..ef4d6a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,3 +28,4 @@ toml = "0.5.9" [dev-dependencies] test-log = "0.2.11" +rstest = "0.18.2" diff --git a/src/qemu.rs b/src/qemu.rs index 39d7b77..acd027a 100644 --- a/src/qemu.rs +++ b/src/qemu.rs @@ -76,6 +76,15 @@ fn gen_sock(prefix: &str) -> PathBuf { path } +// Given a guest temp dir and a host init path, generate the path to the init file +// in the guest. +// This path is the one that will be passed to the guest via the kernel's `init=` parameter. +fn guest_init_path(guest_temp_dir: PathBuf, host_init_path: PathBuf) -> PathBuf { + let mut guest_init_path = guest_temp_dir; + guest_init_path.push(host_init_path.file_name().unwrap()); + guest_init_path +} + // Given a rootfs, generate a tempfile with the init script inside. // Returns the tempfile and the path to the init script inside the guest. // When rootfs is /, both the tempfile filename and guest init path are equal. @@ -111,7 +120,7 @@ fn gen_init(rootfs: &Path) -> Result<(NamedTempFile, PathBuf)> { // Path in the guest is our guest_temp_dir to which we append the file // name of the host init script. - let guest_init = guest_temp_dir.join(host_init.path().file_name().unwrap()); + let guest_init = guest_init_path(guest_temp_dir, host_init.path().to_path_buf()); debug!( "rootfs path: {rootfs:?}, init host path: {host_init:?}, init guest path: {guest_init:?}" ); @@ -899,3 +908,34 @@ impl Drop for Qemu { let _ = fs::remove_file(self.qmp_sock.as_path()); } } + +#[cfg(test)] +mod tests { + use super::guest_init_path; + use rstest::rstest; + + use std::path::PathBuf; + + #[rstest] + // no trailing / + #[case("/tmp", "/foo/tmp/bar.sh", "/tmp/bar.sh")] + // with trailing / + #[case("/tmp/", "/foo/tmp/bar.sh", "/tmp/bar.sh")] + // A valid case if rootfs was /foo/tmp and env::temp_dir() was /. + #[case("/", "/foo/tmp/bar.sh", "/bar.sh")] + // This should never happen given that host_init_path is made by appending guest_temp_dir to rootfs. + // for now it will return a guest_init_path which won't work. + #[case("/tmp", "/foo/bar.sh", "/tmp/bar.sh")] + // A valid case if env::temp_dir() was /foo/tmp and rootfs was /. + #[case("/foo/tmp", "/foo/tmp/bar.sh", "/foo/tmp/bar.sh")] + // Invalid case because guest_temp_dir is not a suffix of dirname(host_init_path) + #[case("/foo/tmp", "/bar/tmp/bar.sh", "/foo/tmp/bar.sh")] + fn test_guest_init_path( + #[case] guest_temp_dir: &str, + #[case] host_init_path: &str, + #[case] expected: &str, + ) { + let r = guest_init_path(guest_temp_dir.into(), host_init_path.into()); + assert_eq!(r, PathBuf::from(expected)); + } +}