diff --git a/Cargo.lock b/Cargo.lock index 67080ca84..73ebf12d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1938,6 +1938,7 @@ dependencies = [ "serde", "serde_json", "serial_test", + "tempfile", "thiserror", ] diff --git a/crates/libcgroups/Cargo.toml b/crates/libcgroups/Cargo.toml index 14db3c4c9..976060c2c 100644 --- a/crates/libcgroups/Cargo.toml +++ b/crates/libcgroups/Cargo.toml @@ -43,3 +43,4 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" env_logger = "0.10" serial_test = "2.0.0" +tempfile = "3" diff --git a/crates/libcgroups/src/stats.rs b/crates/libcgroups/src/stats.rs index fc9ba53e3..37142f8fa 100644 --- a/crates/libcgroups/src/stats.rs +++ b/crates/libcgroups/src/stats.rs @@ -473,7 +473,7 @@ fn parse_psi(stat_line: &str, path: &Path) -> Result { #[cfg(test)] mod tests { - use crate::test::{create_temp_dir, set_fixture}; + use crate::test::set_fixture; use super::*; @@ -497,8 +497,8 @@ mod tests { #[test] fn test_parse_single_value_valid() { - let tmp = create_temp_dir("test_parse_single_value_valid").unwrap(); - let file_path = set_fixture(&tmp, "single_valued_file", "1200\n").unwrap(); + let tmp = tempfile::tempdir().unwrap(); + let file_path = set_fixture(tmp.path(), "single_valued_file", "1200\n").unwrap(); let value = parse_single_value(&file_path).unwrap(); assert_eq!(value, 1200); @@ -506,8 +506,8 @@ mod tests { #[test] fn test_parse_single_value_invalid_number() { - let tmp = create_temp_dir("test_parse_single_value_invalid_number").unwrap(); - let file_path = set_fixture(&tmp, "single_invalid_file", "noop\n").unwrap(); + let tmp = tempfile::tempdir().unwrap(); + let file_path = set_fixture(tmp.path(), "single_invalid_file", "noop\n").unwrap(); let value = parse_single_value(&file_path); assert!(value.is_err()); @@ -515,8 +515,8 @@ mod tests { #[test] fn test_parse_single_value_multiple_entries() { - let tmp = create_temp_dir("test_parse_single_value_multiple_entries").unwrap(); - let file_path = set_fixture(&tmp, "multi_valued_file", "1200\n1400\n1600").unwrap(); + let tmp = tempfile::tempdir().unwrap(); + let file_path = set_fixture(tmp.path(), "multi_valued_file", "1200\n1400\n1600").unwrap(); let value = parse_single_value(&file_path); assert!(value.is_err()); @@ -524,9 +524,9 @@ mod tests { #[test] fn test_parse_flat_keyed_data() { - let tmp = create_temp_dir("test_parse_flat_keyed_data").unwrap(); + let tmp = tempfile::tempdir().unwrap(); let file_content = ["key1 1", "key2 2", "key3 3"].join("\n"); - let file_path = set_fixture(&tmp, "flat_keyed_data", &file_content).unwrap(); + let file_path = set_fixture(tmp.path(), "flat_keyed_data", &file_content).unwrap(); let actual = parse_flat_keyed_data(&file_path).unwrap(); let mut expected = HashMap::with_capacity(3); @@ -539,9 +539,9 @@ mod tests { #[test] fn test_parse_flat_keyed_data_with_characters() { - let tmp = create_temp_dir("test_parse_flat_keyed_data_with_characters").unwrap(); + let tmp = tempfile::tempdir().unwrap(); let file_content = ["key1 1", "key2 a", "key3 b"].join("\n"); - let file_path = set_fixture(&tmp, "flat_keyed_data", &file_content).unwrap(); + let file_path = set_fixture(tmp.path(), "flat_keyed_data", &file_content).unwrap(); let result = parse_flat_keyed_data(&file_path); assert!(result.is_err()); @@ -549,9 +549,9 @@ mod tests { #[test] fn test_parse_space_separated_as_flat_keyed_data() { - let tmp = create_temp_dir("test_parse_space_separated_as_flat_keyed_data").unwrap(); + let tmp = tempfile::tempdir().unwrap(); let file_content = ["key1", "key2", "key3", "key4"].join(" "); - let file_path = set_fixture(&tmp, "space_separated", &file_content).unwrap(); + let file_path = set_fixture(tmp.path(), "space_separated", &file_content).unwrap(); let result = parse_flat_keyed_data(&file_path); assert!(result.is_err()); @@ -559,9 +559,9 @@ mod tests { #[test] fn test_parse_newline_separated_as_flat_keyed_data() { - let tmp = create_temp_dir("test_parse_newline_separated_as_flat_keyed_data").unwrap(); + let tmp = tempfile::tempdir().unwrap(); let file_content = ["key1", "key2", "key3", "key4"].join("\n"); - let file_path = set_fixture(&tmp, "newline_separated", &file_content).unwrap(); + let file_path = set_fixture(tmp.path(), "newline_separated", &file_content).unwrap(); let result = parse_flat_keyed_data(&file_path); assert!(result.is_err()); @@ -569,14 +569,14 @@ mod tests { #[test] fn test_parse_nested_keyed_data_as_flat_keyed_data() { - let tmp = create_temp_dir("test_parse_nested_keyed_data_as_flat_keyed_data").unwrap(); + let tmp = tempfile::tempdir().unwrap(); let file_content = [ "key1 subkey1=value1 subkey2=value2 subkey3=value3", "key2 subkey1=value1 subkey2=value2 subkey3=value3", "key3 subkey1=value1 subkey2=value2 subkey3=value3", ] .join("\n"); - let file_path = set_fixture(&tmp, "nested_keyed_data", &file_content).unwrap(); + let file_path = set_fixture(tmp.path(), "nested_keyed_data", &file_content).unwrap(); let result = parse_flat_keyed_data(&file_path); assert!(result.is_err()); @@ -584,14 +584,14 @@ mod tests { #[test] fn test_parse_nested_keyed_data() { - let tmp = create_temp_dir("test_parse_nested_keyed_data").unwrap(); + let tmp = tempfile::tempdir().unwrap(); let file_content = [ "key1 subkey1=value1 subkey2=value2 subkey3=value3", "key2 subkey1=value1 subkey2=value2 subkey3=value3", "key3 subkey1=value1 subkey2=value2 subkey3=value3", ] .join("\n"); - let file_path = set_fixture(&tmp, "nested_keyed_data", &file_content).unwrap(); + let file_path = set_fixture(tmp.path(), "nested_keyed_data", &file_content).unwrap(); let actual = parse_nested_keyed_data(&file_path).unwrap(); let mut expected = HashMap::with_capacity(3); @@ -625,9 +625,9 @@ mod tests { #[test] fn test_parse_space_separated_as_nested_keyed_data() { - let tmp = create_temp_dir("test_parse_space_separated_as_nested_keyed_data").unwrap(); + let tmp = tempfile::tempdir().unwrap(); let file_content = ["key1", "key2", "key3", "key4"].join(" "); - let file_path = set_fixture(&tmp, "space_separated", &file_content).unwrap(); + let file_path = set_fixture(tmp.path(), "space_separated", &file_content).unwrap(); let result = parse_nested_keyed_data(&file_path); assert!(result.is_err()); @@ -635,9 +635,9 @@ mod tests { #[test] fn test_parse_newline_separated_as_nested_keyed_data() { - let tmp = create_temp_dir("test_parse_newline_separated_as_nested_keyed_data").unwrap(); + let tmp = tempfile::tempdir().unwrap(); let file_content = ["key1", "key2", "key3", "key4"].join("\n"); - let file_path = set_fixture(&tmp, "newline_separated", &file_content).unwrap(); + let file_path = set_fixture(tmp.path(), "newline_separated", &file_content).unwrap(); let result = parse_nested_keyed_data(&file_path); assert!(result.is_err()); @@ -645,9 +645,9 @@ mod tests { #[test] fn test_parse_flat_keyed_as_nested_keyed_data() { - let tmp = create_temp_dir("test_parse_flat_keyed_as_nested_keyed_data").unwrap(); + let tmp = tempfile::tempdir().unwrap(); let file_content = ["key1 1", "key2 2", "key3 3"].join("\n"); - let file_path = set_fixture(&tmp, "newline_separated", &file_content).unwrap(); + let file_path = set_fixture(tmp.path(), "newline_separated", &file_content).unwrap(); let result = parse_nested_keyed_data(&file_path); assert!(result.is_err()); @@ -667,13 +667,13 @@ mod tests { #[test] fn test_parse_psi_full_stats() { - let tmp = create_temp_dir("test_parse_psi_full_stats").unwrap(); + let tmp = tempfile::tempdir().unwrap(); let file_content = [ "some avg10=80.00 avg60=50.00 avg300=90.00 total=0", "full avg10=10.00 avg60=30.00 avg300=50.00 total=0", ] .join("\n"); - let psi_file = set_fixture(&tmp, "psi.pressure", &file_content).unwrap(); + let psi_file = set_fixture(tmp.path(), "psi.pressure", &file_content).unwrap(); let result = psi_stats(&psi_file).unwrap(); assert_eq!( @@ -695,9 +695,9 @@ mod tests { #[test] fn test_parse_psi_only_some() { - let tmp = create_temp_dir("test_parse_psi_only_some").unwrap(); + let tmp = tempfile::tempdir().unwrap(); let file_content = ["some avg10=80.00 avg60=50.00 avg300=90.00 total=0"].join("\n"); - let psi_file = set_fixture(&tmp, "psi.pressure", &file_content).unwrap(); + let psi_file = set_fixture(tmp.path(), "psi.pressure", &file_content).unwrap(); let result = psi_stats(&psi_file).unwrap(); assert_eq!( diff --git a/crates/libcgroups/src/test.rs b/crates/libcgroups/src/test.rs index 60ffadbd6..84e6246be 100644 --- a/crates/libcgroups/src/test.rs +++ b/crates/libcgroups/src/test.rs @@ -2,65 +2,13 @@ use anyhow::{Context, Result}; use std::{ - fs, io::Write, - ops::Deref, path::{Path, PathBuf}, }; -pub struct TempDir { - path: Option, -} - -impl TempDir { - pub fn new>(path: P) -> Result { - let p = path.into(); - std::fs::create_dir_all(&p)?; - Ok(Self { path: Some(p) }) - } - - pub fn path(&self) -> &Path { - self.path - .as_ref() - .expect("temp dir has already been removed") - } - - pub fn remove(&mut self) { - if let Some(p) = &self.path { - let _ = fs::remove_dir_all(p); - self.path = None; - } - } -} - -impl Drop for TempDir { - fn drop(&mut self) { - self.remove(); - } -} - -impl AsRef for TempDir { - fn as_ref(&self) -> &Path { - self.path() - } -} - -impl Deref for TempDir { - type Target = Path; - - fn deref(&self) -> &Self::Target { - self.path() - } -} - -pub fn create_temp_dir(test_name: &str) -> Result { - let dir = TempDir::new(std::env::temp_dir().join(test_name))?; - Ok(dir) -} - -pub fn setup(testname: &str, cgroup_file: &str) -> (TempDir, PathBuf) { - let tmp = create_temp_dir(testname).expect("create temp directory for test"); - let cgroup_file = set_fixture(&tmp, cgroup_file, "") +pub fn setup(cgroup_file: &str) -> (tempfile::TempDir, PathBuf) { + let tmp = tempfile::tempdir().expect("create temp directory for test"); + let cgroup_file = set_fixture(tmp.path(), cgroup_file, "") .unwrap_or_else(|_| panic!("set test fixture for {cgroup_file}")); (tmp, cgroup_file) diff --git a/crates/libcgroups/src/v1/blkio.rs b/crates/libcgroups/src/v1/blkio.rs index ec078e8ba..a46aae954 100644 --- a/crates/libcgroups/src/v1/blkio.rs +++ b/crates/libcgroups/src/v1/blkio.rs @@ -260,20 +260,20 @@ mod tests { use std::fs; use super::*; - use crate::test::{create_temp_dir, set_fixture, setup}; + use crate::test::{set_fixture, setup}; use oci_spec::runtime::{LinuxBlockIoBuilder, LinuxThrottleDeviceBuilder}; #[test] fn test_set_blkio_weight() { for cgroup_file in &[BLKIO_WEIGHT, BLKIO_BFQ_WEIGHT] { - let (tmp, weight_file) = setup("test_set_blkio_weight", cgroup_file); + let (tmp, weight_file) = setup(cgroup_file); let blkio = LinuxBlockIoBuilder::default() .weight(200_u16) .build() .unwrap(); - Blkio::apply(&tmp, &blkio).expect("apply blkio"); + Blkio::apply(tmp.path(), &blkio).expect("apply blkio"); let content = fs::read_to_string(weight_file).expect("read blkio weight"); assert_eq!("200", content); } @@ -281,7 +281,7 @@ mod tests { #[test] fn test_set_blkio_read_bps() { - let (tmp, throttle) = setup("test_set_blkio_read_bps", BLKIO_THROTTLE_READ_BPS); + let (tmp, throttle) = setup(BLKIO_THROTTLE_READ_BPS); let blkio = LinuxBlockIoBuilder::default() .throttle_read_bps_device(vec![LinuxThrottleDeviceBuilder::default() @@ -293,7 +293,7 @@ mod tests { .build() .unwrap(); - Blkio::apply(&tmp, &blkio).expect("apply blkio"); + Blkio::apply(tmp.path(), &blkio).expect("apply blkio"); let content = fs::read_to_string(throttle) .unwrap_or_else(|_| panic!("read {BLKIO_THROTTLE_READ_BPS} content")); @@ -302,7 +302,7 @@ mod tests { #[test] fn test_set_blkio_write_bps() { - let (tmp, throttle) = setup("test_set_blkio_write_bps", BLKIO_THROTTLE_WRITE_BPS); + let (tmp, throttle) = setup(BLKIO_THROTTLE_WRITE_BPS); let blkio = LinuxBlockIoBuilder::default() .throttle_write_bps_device(vec![LinuxThrottleDeviceBuilder::default() @@ -314,7 +314,7 @@ mod tests { .build() .unwrap(); - Blkio::apply(&tmp, &blkio).expect("apply blkio"); + Blkio::apply(tmp.path(), &blkio).expect("apply blkio"); let content = fs::read_to_string(throttle) .unwrap_or_else(|_| panic!("read {BLKIO_THROTTLE_WRITE_BPS} content")); @@ -323,7 +323,7 @@ mod tests { #[test] fn test_set_blkio_read_iops() { - let (tmp, throttle) = setup("test_set_blkio_read_iops", BLKIO_THROTTLE_READ_IOPS); + let (tmp, throttle) = setup(BLKIO_THROTTLE_READ_IOPS); let blkio = LinuxBlockIoBuilder::default() .throttle_read_iops_device(vec![LinuxThrottleDeviceBuilder::default() @@ -335,7 +335,7 @@ mod tests { .build() .unwrap(); - Blkio::apply(&tmp, &blkio).expect("apply blkio"); + Blkio::apply(tmp.path(), &blkio).expect("apply blkio"); let content = fs::read_to_string(throttle) .unwrap_or_else(|_| panic!("read {BLKIO_THROTTLE_READ_IOPS} content")); @@ -344,7 +344,7 @@ mod tests { #[test] fn test_set_blkio_write_iops() { - let (tmp, throttle) = setup("test_set_blkio_write_iops", BLKIO_THROTTLE_WRITE_IOPS); + let (tmp, throttle) = setup(BLKIO_THROTTLE_WRITE_IOPS); let blkio = LinuxBlockIoBuilder::default() .throttle_write_iops_device(vec![LinuxThrottleDeviceBuilder::default() @@ -356,7 +356,7 @@ mod tests { .build() .unwrap(); - Blkio::apply(&tmp, &blkio).expect("apply blkio"); + Blkio::apply(tmp.path(), &blkio).expect("apply blkio"); let content = fs::read_to_string(throttle) .unwrap_or_else(|_| panic!("read {BLKIO_THROTTLE_WRITE_IOPS} content")); @@ -365,7 +365,7 @@ mod tests { #[test] fn test_stat_throttling_policy() -> Result<(), Box> { - let tmp = create_temp_dir("test_stat_throttling_policy").expect("create test directory"); + let tmp = tempfile::tempdir().unwrap(); let content = &[ "8:0 Read 20", "8:0 Write 20", @@ -376,10 +376,10 @@ mod tests { "Total 0", ] .join("\n"); - set_fixture(&tmp, BLKIO_THROTTLE_IO_SERVICE_BYTES, content).unwrap(); - set_fixture(&tmp, BLKIO_THROTTLE_IO_SERVICED, content).unwrap(); + set_fixture(tmp.path(), BLKIO_THROTTLE_IO_SERVICE_BYTES, content).unwrap(); + set_fixture(tmp.path(), BLKIO_THROTTLE_IO_SERVICED, content).unwrap(); - let actual = Blkio::stats(&tmp).expect("get cgroup stats"); + let actual = Blkio::stats(tmp.path()).expect("get cgroup stats"); let mut expected = BlkioStats::default(); let devices: Vec = ["Read", "Write", "Sync", "Async", "Discard", "Total"] .iter() diff --git a/crates/libcgroups/src/v1/cpu.rs b/crates/libcgroups/src/v1/cpu.rs index ac02f4163..c8e7af01a 100644 --- a/crates/libcgroups/src/v1/cpu.rs +++ b/crates/libcgroups/src/v1/cpu.rs @@ -136,20 +136,20 @@ impl Cpu { #[cfg(test)] mod tests { use super::*; - use crate::test::{create_temp_dir, set_fixture, setup}; + use crate::test::{set_fixture, setup}; use oci_spec::runtime::LinuxCpuBuilder; use std::fs; #[test] fn test_set_shares() { // arrange - let (tmp, shares) = setup("test_set_shares", CGROUP_CPU_SHARES); - let _ = set_fixture(&tmp, CGROUP_CPU_SHARES, "") + let (tmp, shares) = setup(CGROUP_CPU_SHARES); + let _ = set_fixture(tmp.path(), CGROUP_CPU_SHARES, "") .unwrap_or_else(|_| panic!("set test fixture for {CGROUP_CPU_SHARES}")); let cpu = LinuxCpuBuilder::default().shares(2048u64).build().unwrap(); // act - Cpu::apply(&tmp, &cpu).expect("apply cpu"); + Cpu::apply(tmp.path(), &cpu).expect("apply cpu"); // assert let content = fs::read_to_string(shares) @@ -161,11 +161,11 @@ mod tests { fn test_set_quota() { // arrange const QUOTA: i64 = 200000; - let (tmp, max) = setup("test_set_quota", CGROUP_CPU_QUOTA); + let (tmp, max) = setup(CGROUP_CPU_QUOTA); let cpu = LinuxCpuBuilder::default().quota(QUOTA).build().unwrap(); // act - Cpu::apply(&tmp, &cpu).expect("apply cpu"); + Cpu::apply(tmp.path(), &cpu).expect("apply cpu"); // assert let content = fs::read_to_string(max) @@ -177,11 +177,11 @@ mod tests { fn test_set_period() { // arrange const PERIOD: u64 = 100000; - let (tmp, max) = setup("test_set_period", CGROUP_CPU_PERIOD); + let (tmp, max) = setup(CGROUP_CPU_PERIOD); let cpu = LinuxCpuBuilder::default().period(PERIOD).build().unwrap(); // act - Cpu::apply(&tmp, &cpu).expect("apply cpu"); + Cpu::apply(tmp.path(), &cpu).expect("apply cpu"); // assert let content = fs::read_to_string(max) @@ -193,14 +193,14 @@ mod tests { fn test_set_rt_runtime() { // arrange const RUNTIME: i64 = 100000; - let (tmp, max) = setup("test_set_rt_runtime", CGROUP_CPU_RT_RUNTIME); + let (tmp, max) = setup(CGROUP_CPU_RT_RUNTIME); let cpu = LinuxCpuBuilder::default() .realtime_runtime(RUNTIME) .build() .unwrap(); // act - Cpu::apply(&tmp, &cpu).expect("apply cpu"); + Cpu::apply(tmp.path(), &cpu).expect("apply cpu"); // assert let content = fs::read_to_string(max) @@ -223,11 +223,11 @@ mod tests { return; } - let (tmp, max) = setup("test_set_cpu_idle", CGROUP_CPU_IDLE); + let (tmp, max) = setup(CGROUP_CPU_IDLE); let cpu = LinuxCpuBuilder::default().idle(IDLE).build().unwrap(); // act - Cpu::apply(&tmp, &cpu).expect("apply cpu"); + Cpu::apply(tmp.path(), &cpu).expect("apply cpu"); // assert let content = fs::read_to_string(max) @@ -239,14 +239,14 @@ mod tests { fn test_set_rt_period() { // arrange const PERIOD: u64 = 100000; - let (tmp, max) = setup("test_set_rt_period", CGROUP_CPU_RT_PERIOD); + let (tmp, max) = setup(CGROUP_CPU_RT_PERIOD); let cpu = LinuxCpuBuilder::default() .realtime_period(PERIOD) .build() .unwrap(); // act - Cpu::apply(&tmp, &cpu).expect("apply cpu"); + Cpu::apply(tmp.path(), &cpu).expect("apply cpu"); // assert let content = fs::read_to_string(max) @@ -256,16 +256,16 @@ mod tests { #[test] fn test_stat_cpu_throttling() { - let tmp = create_temp_dir("test_stat_cpu_throttling").expect("create test directory"); + let tmp = tempfile::tempdir().unwrap(); let stat_content = &[ "nr_periods 165000", "nr_throttled 27", "throttled_time 1080", ] .join("\n"); - set_fixture(&tmp, CGROUP_CPU_STAT, stat_content).expect("create stat file"); + set_fixture(tmp.path(), CGROUP_CPU_STAT, stat_content).expect("create stat file"); - let actual = Cpu::stats(&tmp).expect("get cgroup stats"); + let actual = Cpu::stats(tmp.path()).expect("get cgroup stats"); let expected = CpuThrottling { periods: 165000, throttled_periods: 27, @@ -278,14 +278,14 @@ mod tests { fn test_set_burst() { // arrange let expected_burst: u64 = 100_000; - let (tmp, max) = setup("test_set_burst", CGROUP_CPU_BURST); + let (tmp, max) = setup(CGROUP_CPU_BURST); let cpu = LinuxCpuBuilder::default() .burst(expected_burst) .build() .unwrap(); // act - Cpu::apply(&tmp, &cpu).expect("apply cpu"); + Cpu::apply(tmp.path(), &cpu).expect("apply cpu"); // assert let actual_burst = fs::read_to_string(max).expect("read burst"); diff --git a/crates/libcgroups/src/v1/cpuacct.rs b/crates/libcgroups/src/v1/cpuacct.rs index 58626fb6b..20ddb2f90 100644 --- a/crates/libcgroups/src/v1/cpuacct.rs +++ b/crates/libcgroups/src/v1/cpuacct.rs @@ -148,31 +148,31 @@ mod tests { use std::fs; use nix::unistd::Pid; + use tempfile::TempDir; use super::*; use crate::{ common::CGROUP_PROCS, - test::{create_temp_dir, TempDir}, test::{set_fixture, setup}, }; - fn setup_total_cpu(test_name: &str, stat_content: &str, usage_content: &str) -> TempDir { - let tmp = create_temp_dir(test_name).expect("create temp directory for test"); + fn setup_total_cpu(stat_content: &str, usage_content: &str) -> TempDir { + let tmp = tempfile::tempdir().unwrap(); - let _ = set_fixture(&tmp, CGROUP_CPUACCT_STAT, stat_content) + let _ = set_fixture(tmp.path(), CGROUP_CPUACCT_STAT, stat_content) .unwrap_or_else(|_| panic!("create {CGROUP_CPUACCT_STAT} file")); - let _ = set_fixture(&tmp, CGROUP_CPUACCT_USAGE, usage_content) + let _ = set_fixture(tmp.path(), CGROUP_CPUACCT_USAGE, usage_content) .unwrap_or_else(|_| panic!("create {CGROUP_CPUACCT_USAGE} file")); tmp } - fn setup_per_core(test_name: &str, percpu_content: &str, usage_all_content: &str) -> TempDir { - let tmp = create_temp_dir(test_name).expect("create temp directory for test"); + fn setup_per_core(percpu_content: &str, usage_all_content: &str) -> TempDir { + let tmp = tempfile::tempdir().unwrap(); - let _ = set_fixture(&tmp, CGROUP_CPUACCT_PERCPU, percpu_content) + let _ = set_fixture(tmp.path(), CGROUP_CPUACCT_PERCPU, percpu_content) .unwrap_or_else(|_| panic!("create {CGROUP_CPUACCT_PERCPU} file")); - let _ = set_fixture(&tmp, CGROUP_CPUACCT_USAGE_ALL, usage_all_content) + let _ = set_fixture(tmp.path(), CGROUP_CPUACCT_USAGE_ALL, usage_all_content) .unwrap_or_else(|_| panic!("create {CGROUP_CPUACCT_USAGE_ALL} file")); tmp @@ -180,10 +180,10 @@ mod tests { #[test] fn test_add_task() { - let (tmp, procs) = setup("test_cpuacct_apply", CGROUP_PROCS); + let (tmp, procs) = setup(CGROUP_PROCS); let pid = Pid::from_raw(1000); - CpuAcct::add_task(pid, &tmp).expect("apply cpuacct"); + CpuAcct::add_task(pid, tmp.path()).expect("apply cpuacct"); let content = fs::read_to_string(procs) .unwrap_or_else(|_| panic!("read {CGROUP_PROCS} file content")); @@ -194,7 +194,7 @@ mod tests { fn test_stat_total_cpu_usage() { let stat_content = &["user 1300888", "system 364592"].join("\n"); let usage_content = "18198092369681"; - let tmp = setup_total_cpu("test_get_total_cpu", stat_content, usage_content); + let tmp = setup_total_cpu(stat_content, usage_content); let mut stats = CpuUsage::default(); CpuAcct::get_total_cpu_usage(tmp.path(), &mut stats).expect("get cgroup stats"); @@ -215,11 +215,7 @@ mod tests { "3 4021385867300 304269989810", ] .join("\n"); - let tmp = setup_per_core( - "test_get_per_core_cpu_usage", - percpu_content, - usage_all_content, - ); + let tmp = setup_per_core(percpu_content, usage_all_content); let mut stats = CpuUsage::default(); CpuAcct::get_per_core_usage(tmp.path(), &mut stats).expect("get cgroup stats"); diff --git a/crates/libcgroups/src/v1/cpuset.rs b/crates/libcgroups/src/v1/cpuset.rs index 9cb69b376..85ec871ff 100644 --- a/crates/libcgroups/src/v1/cpuset.rs +++ b/crates/libcgroups/src/v1/cpuset.rs @@ -129,14 +129,14 @@ mod tests { #[test] fn test_set_cpus() { // arrange - let (tmp, cpus) = setup("test_set_cpus", CGROUP_CPUSET_CPUS); + let (tmp, cpus) = setup(CGROUP_CPUSET_CPUS); let cpuset = LinuxCpuBuilder::default() .cpus("1-3".to_owned()) .build() .unwrap(); // act - CpuSet::apply(&tmp, &cpuset).expect("apply cpuset"); + CpuSet::apply(tmp.path(), &cpuset).expect("apply cpuset"); // assert let content = fs::read_to_string(cpus) @@ -147,14 +147,14 @@ mod tests { #[test] fn test_set_mems() { // arrange - let (tmp, mems) = setup("test_set_mems", CGROUP_CPUSET_MEMS); + let (tmp, mems) = setup(CGROUP_CPUSET_MEMS); let cpuset = LinuxCpuBuilder::default() .mems("1-3".to_owned()) .build() .unwrap(); // act - CpuSet::apply(&tmp, &cpuset).expect("apply cpuset"); + CpuSet::apply(tmp.path(), &cpuset).expect("apply cpuset"); // assert let content = fs::read_to_string(mems) diff --git a/crates/libcgroups/src/v1/devices.rs b/crates/libcgroups/src/v1/devices.rs index 77b246090..0717c3c24 100644 --- a/crates/libcgroups/src/v1/devices.rs +++ b/crates/libcgroups/src/v1/devices.rs @@ -53,33 +53,31 @@ impl Devices { #[cfg(test)] mod tests { use super::*; - use crate::test::create_temp_dir; use crate::test::set_fixture; use oci_spec::runtime::{LinuxDeviceCgroupBuilder, LinuxDeviceType}; use std::fs::read_to_string; #[test] fn test_set_default_devices() { - let tmp = - create_temp_dir("test_set_default_devices").expect("create temp directory for test"); + let tmp = tempfile::tempdir().unwrap(); default_allow_devices().iter().for_each(|d| { // NOTE: We reset the fixtures every iteration because files aren't appended // so what happens in the tests is you get strange overwrites which can contain // remaining bytes from the last iteration. Resetting the files more appropriately // mocks the behavior of cgroup files. - set_fixture(&tmp, "devices.allow", "").expect("create allowed devices list"); - set_fixture(&tmp, "devices.deny", "").expect("create denied devices list"); + set_fixture(tmp.path(), "devices.allow", "").expect("create allowed devices list"); + set_fixture(tmp.path(), "devices.deny", "").expect("create denied devices list"); - Devices::apply_device(d, &tmp).expect("Apply default device"); + Devices::apply_device(d, tmp.path()).expect("Apply default device"); println!("Device: {}", d.to_string()); if d.allow() { let allowed_content = - read_to_string(tmp.join("devices.allow")).expect("read to string"); + read_to_string(tmp.path().join("devices.allow")).expect("read to string"); assert_eq!(allowed_content, d.to_string()); } else { let denied_content = - read_to_string(tmp.join("devices.deny")).expect("read to string"); + read_to_string(tmp.path().join("devices.deny")).expect("read to string"); assert_eq!(denied_content, d.to_string()); } }); @@ -87,7 +85,7 @@ mod tests { #[test] fn test_set_mock_devices() { - let tmp = create_temp_dir("test_set_mock_devices").expect("create temp directory for test"); + let tmp = tempfile::tempdir().unwrap(); [ LinuxDeviceCgroupBuilder::default() .allow(true) @@ -120,18 +118,18 @@ mod tests { ] .iter() .for_each(|d| { - set_fixture(&tmp, "devices.allow", "").expect("create allowed devices list"); - set_fixture(&tmp, "devices.deny", "").expect("create denied devices list"); + set_fixture(tmp.path(), "devices.allow", "").expect("create allowed devices list"); + set_fixture(tmp.path(), "devices.deny", "").expect("create denied devices list"); - Devices::apply_device(d, &tmp).expect("Apply default device"); + Devices::apply_device(d, tmp.path()).expect("Apply default device"); println!("Device: {}", d.to_string()); if d.allow() { let allowed_content = - read_to_string(tmp.join("devices.allow")).expect("read to string"); + read_to_string(tmp.path().join("devices.allow")).expect("read to string"); assert_eq!(allowed_content, d.to_string()); } else { let denied_content = - read_to_string(tmp.join("devices.deny")).expect("read to string"); + read_to_string(tmp.path().join("devices.deny")).expect("read to string"); assert_eq!(denied_content, d.to_string()); } }); @@ -139,35 +137,35 @@ mod tests { quickcheck! { fn property_test_apply_device(device: LinuxDeviceCgroup) -> bool { - let tmp = create_temp_dir("property_test_apply_device").expect("create temp directory for test"); - set_fixture(&tmp, "devices.allow", "").expect("create allowed devices list"); - set_fixture(&tmp, "devices.deny", "").expect("create denied devices list"); - Devices::apply_device(&device, &tmp).expect("Apply default device"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), "devices.allow", "").expect("create allowed devices list"); + set_fixture(tmp.path(), "devices.deny", "").expect("create denied devices list"); + Devices::apply_device(&device, tmp.path()).expect("Apply default device"); if device.allow() { let allowed_content = - read_to_string(tmp.join("devices.allow")).expect("read to string"); + read_to_string(tmp.path().join("devices.allow")).expect("read to string"); allowed_content == device.to_string() } else { let denied_content = - read_to_string(tmp.join("devices.deny")).expect("read to string"); + read_to_string(tmp.path().join("devices.deny")).expect("read to string"); denied_content == device.to_string() } } fn property_test_apply_multiple_devices(devices: Vec) -> bool { - let tmp = create_temp_dir("property_test_apply_multiple_devices").expect("create temp directory for test"); + let tmp = tempfile::tempdir().unwrap(); devices.iter() .map(|device| { - set_fixture(&tmp, "devices.allow", "").expect("create allowed devices list"); - set_fixture(&tmp, "devices.deny", "").expect("create denied devices list"); - Devices::apply_device(device, &tmp).expect("Apply default device"); + set_fixture(tmp.path(), "devices.allow", "").expect("create allowed devices list"); + set_fixture(tmp.path(), "devices.deny", "").expect("create denied devices list"); + Devices::apply_device(device, tmp.path()).expect("Apply default device"); if device.allow() { let allowed_content = - read_to_string(tmp.join("devices.allow")).expect("read to string"); + read_to_string(tmp.path().join("devices.allow")).expect("read to string"); allowed_content == device.to_string() } else { let denied_content = - read_to_string(tmp.join("devices.deny")).expect("read to string"); + read_to_string(tmp.path().join("devices.deny")).expect("read to string"); denied_content == device.to_string() } }) diff --git a/crates/libcgroups/src/v1/freezer.rs b/crates/libcgroups/src/v1/freezer.rs index 15bcaca94..edb5a014b 100644 --- a/crates/libcgroups/src/v1/freezer.rs +++ b/crates/libcgroups/src/v1/freezer.rs @@ -132,54 +132,53 @@ impl Freezer { mod tests { use super::*; use crate::common::{FreezerState, CGROUP_PROCS}; - use crate::test::{create_temp_dir, set_fixture}; + use crate::test::set_fixture; use nix::unistd::Pid; use oci_spec::runtime::LinuxResourcesBuilder; #[test] fn test_set_freezer_state() { - let tmp = - create_temp_dir("test_set_freezer_state").expect("create temp directory for test"); - set_fixture(&tmp, CGROUP_FREEZER_STATE, "").expect("Set fixure for freezer state"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_FREEZER_STATE, "").expect("Set fixure for freezer state"); // set Frozen state. { let freezer_state = FreezerState::Frozen; - Freezer::apply(&freezer_state, &tmp).expect("Set freezer state"); + Freezer::apply(&freezer_state, tmp.path()).expect("Set freezer state"); - let state_content = - std::fs::read_to_string(tmp.join(CGROUP_FREEZER_STATE)).expect("Read to string"); + let state_content = std::fs::read_to_string(tmp.path().join(CGROUP_FREEZER_STATE)) + .expect("Read to string"); assert_eq!(FREEZER_STATE_FROZEN, state_content); } // set Thawed state. { let freezer_state = FreezerState::Thawed; - Freezer::apply(&freezer_state, &tmp).expect("Set freezer state"); + Freezer::apply(&freezer_state, tmp.path()).expect("Set freezer state"); - let state_content = - std::fs::read_to_string(tmp.join(CGROUP_FREEZER_STATE)).expect("Read to string"); + let state_content = std::fs::read_to_string(tmp.path().join(CGROUP_FREEZER_STATE)) + .expect("Read to string"); assert_eq!(FREEZER_STATE_THAWED, state_content); } // set Undefined state. { - let old_state_content = - std::fs::read_to_string(tmp.join(CGROUP_FREEZER_STATE)).expect("Read to string"); + let old_state_content = std::fs::read_to_string(tmp.path().join(CGROUP_FREEZER_STATE)) + .expect("Read to string"); let freezer_state = FreezerState::Undefined; - Freezer::apply(&freezer_state, &tmp).expect("Set freezer state"); + Freezer::apply(&freezer_state, tmp.path()).expect("Set freezer state"); - let state_content = - std::fs::read_to_string(tmp.join(CGROUP_FREEZER_STATE)).expect("Read to string"); + let state_content = std::fs::read_to_string(tmp.path().join(CGROUP_FREEZER_STATE)) + .expect("Read to string"); assert_eq!(old_state_content, state_content); } } #[test] fn test_add_and_apply() { - let tmp = create_temp_dir("test_add_task").expect("create temp directory for test"); - set_fixture(&tmp, CGROUP_FREEZER_STATE, "").expect("set fixure for freezer state"); - set_fixture(&tmp, CGROUP_PROCS, "").expect("set fixture for proc file"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_FREEZER_STATE, "").expect("set fixure for freezer state"); + set_fixture(tmp.path(), CGROUP_PROCS, "").expect("set fixture for proc file"); // set Thawed state. { @@ -198,13 +197,13 @@ mod tests { }; let pid = Pid::from_raw(1000); - Freezer::add_task(pid, &tmp).expect("freezer add task"); - ::apply(&controller_opt, &tmp).expect("freezer apply"); - let state_content = - std::fs::read_to_string(tmp.join(CGROUP_FREEZER_STATE)).expect("read to string"); + Freezer::add_task(pid, tmp.path()).expect("freezer add task"); + ::apply(&controller_opt, tmp.path()).expect("freezer apply"); + let state_content = std::fs::read_to_string(tmp.path().join(CGROUP_FREEZER_STATE)) + .expect("read to string"); assert_eq!(FREEZER_STATE_THAWED, state_content); let pid_content = - std::fs::read_to_string(tmp.join(CGROUP_PROCS)).expect("read to string"); + std::fs::read_to_string(tmp.path().join(CGROUP_PROCS)).expect("read to string"); assert_eq!(pid_content, "1000"); } @@ -225,13 +224,13 @@ mod tests { }; let pid = Pid::from_raw(1001); - Freezer::add_task(pid, &tmp).expect("freezer add task"); - ::apply(&controller_opt, &tmp).expect("freezer apply"); - let state_content = - std::fs::read_to_string(tmp.join(CGROUP_FREEZER_STATE)).expect("read to string"); + Freezer::add_task(pid, tmp.path()).expect("freezer add task"); + ::apply(&controller_opt, tmp.path()).expect("freezer apply"); + let state_content = std::fs::read_to_string(tmp.path().join(CGROUP_FREEZER_STATE)) + .expect("read to string"); assert_eq!(FREEZER_STATE_FROZEN, state_content); let pid_content = - std::fs::read_to_string(tmp.join(CGROUP_PROCS)).expect("read to string"); + std::fs::read_to_string(tmp.path().join(CGROUP_PROCS)).expect("read to string"); assert_eq!(pid_content, "1001"); } @@ -253,15 +252,15 @@ mod tests { }; let pid = Pid::from_raw(1002); - let old_state_content = - std::fs::read_to_string(tmp.join(CGROUP_FREEZER_STATE)).expect("read to string"); - Freezer::add_task(pid, &tmp).expect("freezer add task"); - ::apply(&controller_opt, &tmp).expect("freezer apply"); - let state_content = - std::fs::read_to_string(tmp.join(CGROUP_FREEZER_STATE)).expect("read to string"); + let old_state_content = std::fs::read_to_string(tmp.path().join(CGROUP_FREEZER_STATE)) + .expect("read to string"); + Freezer::add_task(pid, tmp.path()).expect("freezer add task"); + ::apply(&controller_opt, tmp.path()).expect("freezer apply"); + let state_content = std::fs::read_to_string(tmp.path().join(CGROUP_FREEZER_STATE)) + .expect("read to string"); assert_eq!(old_state_content, state_content); let pid_content = - std::fs::read_to_string(tmp.join(CGROUP_PROCS)).expect("read to string"); + std::fs::read_to_string(tmp.path().join(CGROUP_PROCS)).expect("read to string"); assert_eq!(pid_content, "1002"); } } diff --git a/crates/libcgroups/src/v1/hugetlb.rs b/crates/libcgroups/src/v1/hugetlb.rs index 7ce06b189..e2e43dd87 100644 --- a/crates/libcgroups/src/v1/hugetlb.rs +++ b/crates/libcgroups/src/v1/hugetlb.rs @@ -141,15 +141,15 @@ impl HugeTlb { #[cfg(test)] mod tests { use super::*; - use crate::test::{create_temp_dir, set_fixture}; + use crate::test::set_fixture; use oci_spec::runtime::LinuxHugepageLimitBuilder; use std::fs::read_to_string; #[test] fn test_set_hugetlb() { let page_file_name = "hugetlb.2MB.limit_in_bytes"; - let tmp = create_temp_dir("test_set_hugetlb").expect("create temp directory for test"); - set_fixture(&tmp, page_file_name, "0").expect("Set fixture for 2 MB page size"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), page_file_name, "0").expect("Set fixture for 2 MB page size"); let hugetlb = LinuxHugepageLimitBuilder::default() .page_size("2MB") @@ -157,15 +157,15 @@ mod tests { .build() .unwrap(); - HugeTlb::apply(&tmp, &hugetlb).expect("apply hugetlb"); - let content = read_to_string(tmp.join(page_file_name)).expect("Read hugetlb file content"); + HugeTlb::apply(tmp.path(), &hugetlb).expect("apply hugetlb"); + let content = + read_to_string(tmp.path().join(page_file_name)).expect("Read hugetlb file content"); assert_eq!(hugetlb.limit().to_string(), content); } #[test] fn test_set_hugetlb_with_invalid_page_size() { - let tmp = create_temp_dir("test_set_hugetlb_with_invalid_page_size") - .expect("create temp directory for test"); + let tmp = tempfile::tempdir().unwrap(); let hugetlb = LinuxHugepageLimitBuilder::default() .page_size("3MB") @@ -173,7 +173,7 @@ mod tests { .build() .unwrap(); - let result = HugeTlb::apply(&tmp, &hugetlb); + let result = HugeTlb::apply(tmp.path(), &hugetlb); assert!( result.is_err(), "page size that is not a power of two should be an error" @@ -183,10 +183,10 @@ mod tests { quickcheck! { fn property_test_set_hugetlb(hugetlb: LinuxHugepageLimit) -> bool { let page_file_name = format!("hugetlb.{:?}.limit_in_bytes", hugetlb.page_size()); - let tmp = create_temp_dir("property_test_set_hugetlb").expect("create temp directory for test"); - set_fixture(&tmp, &page_file_name, "0").expect("Set fixture for page size"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), &page_file_name, "0").expect("Set fixture for page size"); - let result = HugeTlb::apply(&tmp, &hugetlb); + let result = HugeTlb::apply(tmp.path(), &hugetlb); let page_size: String = hugetlb .page_size() @@ -197,7 +197,7 @@ mod tests { if HugeTlb::is_power_of_two(page_size) && page_size != 1 { let content = - read_to_string(tmp.join(page_file_name)).expect("Read hugetlb file content"); + read_to_string(tmp.path().join(page_file_name)).expect("Read hugetlb file content"); hugetlb.limit().to_string() == content } else { result.is_err() @@ -207,13 +207,13 @@ mod tests { #[test] fn test_stat_hugetlb() { - let tmp = create_temp_dir("test_stat_hugetlb").expect("create temp directory for test"); - set_fixture(&tmp, "hugetlb.2MB.usage_in_bytes", "1024\n").expect("set hugetlb usage"); - set_fixture(&tmp, "hugetlb.2MB.max_usage_in_bytes", "4096\n") + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), "hugetlb.2MB.usage_in_bytes", "1024\n").expect("set hugetlb usage"); + set_fixture(tmp.path(), "hugetlb.2MB.max_usage_in_bytes", "4096\n") .expect("set hugetlb max usage"); - set_fixture(&tmp, "hugetlb.2MB.failcnt", "5").expect("set hugetlb fail count"); + set_fixture(tmp.path(), "hugetlb.2MB.failcnt", "5").expect("set hugetlb fail count"); - let actual = HugeTlb::stats_for_page_size(&tmp, "2MB").expect("get cgroup stats"); + let actual = HugeTlb::stats_for_page_size(tmp.path(), "2MB").expect("get cgroup stats"); let expected = HugeTlbStats { usage: 1024, diff --git a/crates/libcgroups/src/v1/memory.rs b/crates/libcgroups/src/v1/memory.rs index e1c5fd396..4d5dd702f 100644 --- a/crates/libcgroups/src/v1/memory.rs +++ b/crates/libcgroups/src/v1/memory.rs @@ -411,19 +411,20 @@ impl Memory { mod tests { use super::*; use crate::common::CGROUP_PROCS; - use crate::test::{create_temp_dir, set_fixture}; + use crate::test::set_fixture; use oci_spec::runtime::{LinuxMemoryBuilder, LinuxResourcesBuilder}; #[test] fn test_set_memory() { let limit = 1024; - let tmp = create_temp_dir("test_set_memory").expect("create temp directory for test"); - set_fixture(&tmp, CGROUP_MEMORY_USAGE, "0").expect("Set fixure for memory usage"); - set_fixture(&tmp, CGROUP_MEMORY_MAX_USAGE, "0").expect("Set fixure for max memory usage"); - set_fixture(&tmp, CGROUP_MEMORY_LIMIT, "0").expect("Set fixure for memory limit"); - Memory::set_memory(limit, &tmp).expect("Set memory limit"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_MEMORY_USAGE, "0").expect("Set fixure for memory usage"); + set_fixture(tmp.path(), CGROUP_MEMORY_MAX_USAGE, "0") + .expect("Set fixure for max memory usage"); + set_fixture(tmp.path(), CGROUP_MEMORY_LIMIT, "0").expect("Set fixure for memory limit"); + Memory::set_memory(limit, tmp.path()).expect("Set memory limit"); let content = - std::fs::read_to_string(tmp.join(CGROUP_MEMORY_LIMIT)).expect("Read to string"); + std::fs::read_to_string(tmp.path().join(CGROUP_MEMORY_LIMIT)).expect("Read to string"); assert_eq!(limit.to_string(), content) } @@ -431,46 +432,46 @@ mod tests { fn pass_set_memory_if_limit_is_zero() { let sample_val = "1024"; let limit = 0; - let tmp = create_temp_dir("pass_set_memory_if_limit_is_zero") - .expect("create temp directory for test"); - set_fixture(&tmp, CGROUP_MEMORY_LIMIT, sample_val).expect("Set fixure for memory limit"); - Memory::set_memory(limit, &tmp).expect("Set memory limit"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_MEMORY_LIMIT, sample_val) + .expect("Set fixure for memory limit"); + Memory::set_memory(limit, tmp.path()).expect("Set memory limit"); let content = - std::fs::read_to_string(tmp.join(CGROUP_MEMORY_LIMIT)).expect("Read to string"); + std::fs::read_to_string(tmp.path().join(CGROUP_MEMORY_LIMIT)).expect("Read to string"); assert_eq!(content, sample_val) } #[test] fn test_set_swap() { let limit = 512; - let tmp = create_temp_dir("test_set_swap").expect("create temp directory for test"); - set_fixture(&tmp, CGROUP_MEMORY_SWAP_LIMIT, "0").expect("Set fixure for swap limit"); - Memory::set_swap(limit, &tmp).expect("Set swap limit"); - let content = - std::fs::read_to_string(tmp.join(CGROUP_MEMORY_SWAP_LIMIT)).expect("Read to string"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_MEMORY_SWAP_LIMIT, "0").expect("Set fixure for swap limit"); + Memory::set_swap(limit, tmp.path()).expect("Set swap limit"); + let content = std::fs::read_to_string(tmp.path().join(CGROUP_MEMORY_SWAP_LIMIT)) + .expect("Read to string"); assert_eq!(limit.to_string(), content) } #[test] fn test_set_memory_and_swap() { - let tmp = - create_temp_dir("test_set_memory_and_swap").expect("create temp directory for test"); - set_fixture(&tmp, CGROUP_MEMORY_USAGE, "0").expect("Set fixure for memory usage"); - set_fixture(&tmp, CGROUP_MEMORY_MAX_USAGE, "0").expect("Set fixure for max memory usage"); - set_fixture(&tmp, CGROUP_MEMORY_LIMIT, "0").expect("Set fixure for memory limit"); - set_fixture(&tmp, CGROUP_MEMORY_SWAP_LIMIT, "0").expect("Set fixure for swap limit"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_MEMORY_USAGE, "0").expect("Set fixure for memory usage"); + set_fixture(tmp.path(), CGROUP_MEMORY_MAX_USAGE, "0") + .expect("Set fixure for max memory usage"); + set_fixture(tmp.path(), CGROUP_MEMORY_LIMIT, "0").expect("Set fixure for memory limit"); + set_fixture(tmp.path(), CGROUP_MEMORY_SWAP_LIMIT, "0").expect("Set fixure for swap limit"); // test unlimited memory with no set swap { let limit = -1; let linux_memory = LinuxMemoryBuilder::default().limit(limit).build().unwrap(); - Memory::apply(&linux_memory, &tmp).expect("Set memory and swap"); + Memory::apply(&linux_memory, tmp.path()).expect("Set memory and swap"); - let limit_content = - std::fs::read_to_string(tmp.join(CGROUP_MEMORY_LIMIT)).expect("Read to string"); + let limit_content = std::fs::read_to_string(tmp.path().join(CGROUP_MEMORY_LIMIT)) + .expect("Read to string"); assert_eq!(limit.to_string(), limit_content); - let swap_content = std::fs::read_to_string(tmp.join(CGROUP_MEMORY_SWAP_LIMIT)) + let swap_content = std::fs::read_to_string(tmp.path().join(CGROUP_MEMORY_SWAP_LIMIT)) .expect("Read to string"); // swap should be set to -1 also assert_eq!(limit.to_string(), swap_content); @@ -485,13 +486,13 @@ mod tests { .swap(swap) .build() .unwrap(); - Memory::apply(&linux_memory, &tmp).expect("Set memory and swap"); + Memory::apply(&linux_memory, tmp.path()).expect("Set memory and swap"); - let limit_content = - std::fs::read_to_string(tmp.join(CGROUP_MEMORY_LIMIT)).expect("Read to string"); + let limit_content = std::fs::read_to_string(tmp.path().join(CGROUP_MEMORY_LIMIT)) + .expect("Read to string"); assert_eq!(limit.to_string(), limit_content); - let swap_content = std::fs::read_to_string(tmp.join(CGROUP_MEMORY_SWAP_LIMIT)) + let swap_content = std::fs::read_to_string(tmp.path().join(CGROUP_MEMORY_SWAP_LIMIT)) .expect("Read to string"); assert_eq!(swap.to_string(), swap_content); } @@ -499,18 +500,17 @@ mod tests { quickcheck! { fn property_test_set_memory(linux_memory: LinuxMemory, disable_oom_killer: bool) -> bool { - let tmp = - create_temp_dir("property_test_set_memory").expect("create temp directory for test"); - set_fixture(&tmp, CGROUP_MEMORY_USAGE, "0").expect("Set fixure for memory usage"); - set_fixture(&tmp, CGROUP_MEMORY_MAX_USAGE, "0").expect("Set fixure for max memory usage"); - set_fixture(&tmp, CGROUP_MEMORY_LIMIT, "0").expect("Set fixure for memory limit"); - set_fixture(&tmp, CGROUP_MEMORY_SWAP_LIMIT, "0").expect("Set fixure for swap limit"); - set_fixture(&tmp, CGROUP_MEMORY_SWAPPINESS, "0").expect("Set fixure for swappiness"); - set_fixture(&tmp, CGROUP_MEMORY_RESERVATION, "0").expect("Set fixture for memory reservation"); - set_fixture(&tmp, CGROUP_MEMORY_OOM_CONTROL, "0").expect("Set fixture for oom control"); - set_fixture(&tmp, CGROUP_KERNEL_MEMORY_LIMIT, "0").expect("Set fixture for kernel memory limit"); - set_fixture(&tmp, CGROUP_KERNEL_TCP_MEMORY_LIMIT, "0").expect("Set fixture for kernel tcp memory limit"); - set_fixture(&tmp, CGROUP_PROCS, "").expect("set fixture for proc file"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_MEMORY_USAGE, "0").expect("Set fixure for memory usage"); + set_fixture(tmp.path(), CGROUP_MEMORY_MAX_USAGE, "0").expect("Set fixure for max memory usage"); + set_fixture(tmp.path(), CGROUP_MEMORY_LIMIT, "0").expect("Set fixure for memory limit"); + set_fixture(tmp.path(), CGROUP_MEMORY_SWAP_LIMIT, "0").expect("Set fixure for swap limit"); + set_fixture(tmp.path(), CGROUP_MEMORY_SWAPPINESS, "0").expect("Set fixure for swappiness"); + set_fixture(tmp.path(), CGROUP_MEMORY_RESERVATION, "0").expect("Set fixture for memory reservation"); + set_fixture(tmp.path(), CGROUP_MEMORY_OOM_CONTROL, "0").expect("Set fixture for oom control"); + set_fixture(tmp.path(), CGROUP_KERNEL_MEMORY_LIMIT, "0").expect("Set fixture for kernel memory limit"); + set_fixture(tmp.path(), CGROUP_KERNEL_TCP_MEMORY_LIMIT, "0").expect("Set fixture for kernel tcp memory limit"); + set_fixture(tmp.path(), CGROUP_PROCS, "").expect("set fixture for proc file"); // clone to avoid use of moved value later on @@ -525,7 +525,7 @@ mod tests { freezer_state: None, }; - let result = ::apply(&controller_opt, &tmp); + let result = ::apply(&controller_opt, tmp.path()); if result.is_err() { @@ -543,7 +543,7 @@ mod tests { } // check memory reservation - let reservation_content = std::fs::read_to_string(tmp.join(CGROUP_MEMORY_RESERVATION)).expect("read memory reservation"); + let reservation_content = std::fs::read_to_string(tmp.path().join(CGROUP_MEMORY_RESERVATION)).expect("read memory reservation"); let reservation_check = match memory_limits.reservation() { Some(reservation) => { reservation_content == reservation.to_string() @@ -552,7 +552,7 @@ mod tests { }; // check kernel memory limit - let kernel_content = std::fs::read_to_string(tmp.join(CGROUP_KERNEL_MEMORY_LIMIT)).expect("read kernel memory limit"); + let kernel_content = std::fs::read_to_string(tmp.path().join(CGROUP_KERNEL_MEMORY_LIMIT)).expect("read kernel memory limit"); let kernel_check = match memory_limits.kernel() { Some(kernel) => { kernel_content == kernel.to_string() @@ -561,7 +561,7 @@ mod tests { }; // check kernel tcp memory limit - let kernel_tcp_content = std::fs::read_to_string(tmp.join(CGROUP_KERNEL_TCP_MEMORY_LIMIT)).expect("read kernel tcp memory limit"); + let kernel_tcp_content = std::fs::read_to_string(tmp.path().join(CGROUP_KERNEL_TCP_MEMORY_LIMIT)).expect("read kernel tcp memory limit"); let kernel_tcp_check = match memory_limits.kernel_tcp() { Some(kernel_tcp) => { kernel_tcp_content == kernel_tcp.to_string() @@ -570,7 +570,7 @@ mod tests { }; // check swappiness - let swappiness_content = std::fs::read_to_string(tmp.join(CGROUP_MEMORY_SWAPPINESS)).expect("read swappiness"); + let swappiness_content = std::fs::read_to_string(tmp.path().join(CGROUP_MEMORY_SWAPPINESS)).expect("read swappiness"); let swappiness_check = match memory_limits.swappiness() { Some(swappiness) if swappiness <= 100 => { swappiness_content == swappiness.to_string() @@ -581,8 +581,8 @@ mod tests { }; // check limit and swap - let limit_content = std::fs::read_to_string(tmp.join(CGROUP_MEMORY_LIMIT)).expect("read memory limit"); - let swap_content = std::fs::read_to_string(tmp.join(CGROUP_MEMORY_SWAP_LIMIT)).expect("read swap memory limit"); + let limit_content = std::fs::read_to_string(tmp.path().join(CGROUP_MEMORY_LIMIT)).expect("read memory limit"); + let swap_content = std::fs::read_to_string(tmp.path().join(CGROUP_MEMORY_SWAP_LIMIT)).expect("read swap memory limit"); let limit_swap_check = match memory_limits.limit() { Some(limit) => { match memory_limits.swap() { @@ -626,28 +626,33 @@ mod tests { #[test] fn test_stat_memory_data() { - let tmp = create_temp_dir("test_stat_memory_data").expect("create test directory"); + let tmp = tempfile::tempdir().unwrap(); set_fixture( - &tmp, + tmp.path(), &format!("{MEMORY_PREFIX}{MEMORY_USAGE_IN_BYTES}"), "1024\n", ) .unwrap(); set_fixture( - &tmp, + tmp.path(), &format!("{MEMORY_PREFIX}{MEMORY_MAX_USAGE_IN_BYTES}"), "2048\n", ) .unwrap(); set_fixture( - &tmp, + tmp.path(), &format!("{MEMORY_PREFIX}{MEMORY_LIMIT_IN_BYTES}"), "4096\n", ) .unwrap(); - set_fixture(&tmp, &format!("{MEMORY_PREFIX}{MEMORY_FAIL_COUNT}"), "5\n").unwrap(); + set_fixture( + tmp.path(), + &format!("{MEMORY_PREFIX}{MEMORY_FAIL_COUNT}"), + "5\n", + ) + .unwrap(); - let actual = Memory::get_memory_data(&tmp, MEMORY_PREFIX).expect("get cgroup stats"); + let actual = Memory::get_memory_data(tmp.path(), MEMORY_PREFIX).expect("get cgroup stats"); let expected = MemoryData { usage: 1024, max_usage: 2048, @@ -660,25 +665,25 @@ mod tests { #[test] fn test_stat_hierarchy_enabled() { - let tmp = create_temp_dir("test_stat_hierarchy_enabled").expect("create test directory"); - set_fixture(&tmp, MEMORY_USE_HIERARCHY, "1").unwrap(); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), MEMORY_USE_HIERARCHY, "1").unwrap(); - let enabled = Memory::hierarchy_enabled(&tmp).expect("get cgroup stats"); + let enabled = Memory::hierarchy_enabled(tmp.path()).expect("get cgroup stats"); assert!(enabled) } #[test] fn test_stat_hierarchy_disabled() { - let tmp = create_temp_dir("test_stat_hierarchy_disabled").expect("create test directory"); - set_fixture(&tmp, MEMORY_USE_HIERARCHY, "0").unwrap(); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), MEMORY_USE_HIERARCHY, "0").unwrap(); - let enabled = Memory::hierarchy_enabled(&tmp).expect("get cgroup stats"); + let enabled = Memory::hierarchy_enabled(tmp.path()).expect("get cgroup stats"); assert!(!enabled) } #[test] fn test_stat_memory_stats() { - let tmp = create_temp_dir("test_stat_memory_stats").expect("create test directory"); + let tmp = tempfile::tempdir().unwrap(); let content = [ "cache 0", "rss 0", @@ -690,9 +695,9 @@ mod tests { "hierarchical_memsw_limit 9223372036854771712", ] .join("\n"); - set_fixture(&tmp, MEMORY_STAT, &content).unwrap(); + set_fixture(tmp.path(), MEMORY_STAT, &content).unwrap(); - let actual = Memory::get_stat_data(&tmp).expect("get cgroup data"); + let actual = Memory::get_stat_data(tmp.path()).expect("get cgroup data"); let expected: HashMap = [ ("cache".to_owned(), 0), ("rss".to_owned(), 0), diff --git a/crates/libcgroups/src/v1/network_classifier.rs b/crates/libcgroups/src/v1/network_classifier.rs index a0760c885..f9078a809 100644 --- a/crates/libcgroups/src/v1/network_classifier.rs +++ b/crates/libcgroups/src/v1/network_classifier.rs @@ -39,14 +39,13 @@ impl NetworkClassifier { #[cfg(test)] mod tests { use super::*; - use crate::test::{create_temp_dir, set_fixture}; + use crate::test::set_fixture; use oci_spec::runtime::LinuxNetworkBuilder; #[test] fn test_apply_network_classifier() { - let tmp = create_temp_dir("test_apply_network_classifier") - .expect("create temp directory for test"); - set_fixture(&tmp, "net_cls.classid", "0").expect("set fixture for classID"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), "net_cls.classid", "0").expect("set fixture for classID"); let id = 0x100001u32; let network = LinuxNetworkBuilder::default() @@ -55,10 +54,10 @@ mod tests { .build() .unwrap(); - NetworkClassifier::apply(&tmp, &network).expect("apply network classID"); + NetworkClassifier::apply(tmp.path(), &network).expect("apply network classID"); - let content = - std::fs::read_to_string(tmp.join("net_cls.classid")).expect("Read classID contents"); + let content = std::fs::read_to_string(tmp.path().join("net_cls.classid")) + .expect("Read classID contents"); assert_eq!(id.to_string(), content); } } diff --git a/crates/libcgroups/src/v1/network_priority.rs b/crates/libcgroups/src/v1/network_priority.rs index cc922a88d..6f4664ca9 100644 --- a/crates/libcgroups/src/v1/network_priority.rs +++ b/crates/libcgroups/src/v1/network_priority.rs @@ -40,14 +40,13 @@ impl NetworkPriority { #[cfg(test)] mod tests { use super::*; - use crate::test::{create_temp_dir, set_fixture}; + use crate::test::set_fixture; use oci_spec::runtime::{LinuxInterfacePriorityBuilder, LinuxNetworkBuilder}; #[test] fn test_apply_network_priorites() { - let tmp = create_temp_dir("test_apply_network_priorites") - .expect("create temp directory for test"); - set_fixture(&tmp, "net_prio.ifpriomap", "").expect("set fixture for priority map"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), "net_prio.ifpriomap", "").expect("set fixture for priority map"); let priorities = vec![ LinuxInterfacePriorityBuilder::default() .name("a") @@ -66,10 +65,10 @@ mod tests { .build() .unwrap(); - NetworkPriority::apply(&tmp, &network).expect("apply network priorities"); + NetworkPriority::apply(tmp.path(), &network).expect("apply network priorities"); - let content = - std::fs::read_to_string(tmp.join("net_prio.ifpriomap")).expect("Read classID contents"); + let content = std::fs::read_to_string(tmp.path().join("net_prio.ifpriomap")) + .expect("Read classID contents"); assert_eq!(priorities_string.trim(), content); } } diff --git a/crates/libcgroups/src/v1/perf_event.rs b/crates/libcgroups/src/v1/perf_event.rs index a795cd252..cd220d763 100644 --- a/crates/libcgroups/src/v1/perf_event.rs +++ b/crates/libcgroups/src/v1/perf_event.rs @@ -29,10 +29,10 @@ mod tests { #[test] fn test_add_task() { - let (tmp, procs) = setup("test_perf_event_add_task", CGROUP_PROCS); + let (tmp, procs) = setup(CGROUP_PROCS); let pid = Pid::from_raw(1000); - PerfEvent::add_task(pid, &tmp).expect("apply perf_event"); + PerfEvent::add_task(pid, tmp.path()).expect("apply perf_event"); let content = fs::read_to_string(procs) .unwrap_or_else(|_| panic!("read {CGROUP_PROCS} file content")); diff --git a/crates/libcgroups/src/v1/pids.rs b/crates/libcgroups/src/v1/pids.rs index 5ee4325c9..30ab2ddd1 100644 --- a/crates/libcgroups/src/v1/pids.rs +++ b/crates/libcgroups/src/v1/pids.rs @@ -57,7 +57,7 @@ impl Pids { #[cfg(test)] mod tests { use super::*; - use crate::test::{create_temp_dir, set_fixture}; + use crate::test::set_fixture; use oci_spec::runtime::LinuxPidsBuilder; // Contains the current number of active pids @@ -65,38 +65,38 @@ mod tests { #[test] fn test_set_pids() { - let tmp = create_temp_dir("test_set_pids").expect("create temp directory for test"); - set_fixture(&tmp, CGROUP_PIDS_MAX, "1000").expect("Set fixture for 1000 pids"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_PIDS_MAX, "1000").expect("Set fixture for 1000 pids"); let pids = LinuxPidsBuilder::default().limit(1000).build().unwrap(); - Pids::apply(&tmp, &pids).expect("apply pids"); + Pids::apply(tmp.path(), &pids).expect("apply pids"); let content = - std::fs::read_to_string(tmp.join(CGROUP_PIDS_MAX)).expect("Read pids contents"); + std::fs::read_to_string(tmp.path().join(CGROUP_PIDS_MAX)).expect("Read pids contents"); assert_eq!(pids.limit().to_string(), content); } #[test] fn test_set_pids_max() { - let tmp = create_temp_dir("test_set_pids_max").expect("create temp directory for test"); - set_fixture(&tmp, CGROUP_PIDS_MAX, "0").expect("set fixture for 0 pids"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_PIDS_MAX, "0").expect("set fixture for 0 pids"); let pids = LinuxPidsBuilder::default().limit(0).build().unwrap(); - Pids::apply(&tmp, &pids).expect("apply pids"); + Pids::apply(tmp.path(), &pids).expect("apply pids"); let content = - std::fs::read_to_string(tmp.join(CGROUP_PIDS_MAX)).expect("Read pids contents"); + std::fs::read_to_string(tmp.path().join(CGROUP_PIDS_MAX)).expect("Read pids contents"); assert_eq!("max".to_string(), content); } #[test] fn test_stat_pids() { - let tmp = create_temp_dir("test_stat_pids").expect("create temp dir for test"); - set_fixture(&tmp, CGROUP_PIDS_CURRENT, "5\n").unwrap(); - set_fixture(&tmp, CGROUP_PIDS_MAX, "30\n").unwrap(); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_PIDS_CURRENT, "5\n").unwrap(); + set_fixture(tmp.path(), CGROUP_PIDS_MAX, "30\n").unwrap(); - let stats = Pids::stats(&tmp).expect("get cgroup stats"); + let stats = Pids::stats(tmp.path()).expect("get cgroup stats"); assert_eq!(stats.current, 5); assert_eq!(stats.limit, 30); @@ -104,11 +104,11 @@ mod tests { #[test] fn test_stat_pids_max() { - let tmp = create_temp_dir("test_stat_pids_max").expect("create temp dir for test"); - set_fixture(&tmp, CGROUP_PIDS_CURRENT, "5\n").unwrap(); - set_fixture(&tmp, CGROUP_PIDS_MAX, "max\n").unwrap(); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_PIDS_CURRENT, "5\n").unwrap(); + set_fixture(tmp.path(), CGROUP_PIDS_MAX, "max\n").unwrap(); - let stats = Pids::stats(&tmp).expect("get cgroup stats"); + let stats = Pids::stats(tmp.path()).expect("get cgroup stats"); assert_eq!(stats.current, 5); assert_eq!(stats.limit, 0); diff --git a/crates/libcgroups/src/v2/cpu.rs b/crates/libcgroups/src/v2/cpu.rs index 8b4fc5e98..bf0f444ba 100644 --- a/crates/libcgroups/src/v2/cpu.rs +++ b/crates/libcgroups/src/v2/cpu.rs @@ -177,7 +177,7 @@ mod tests { use super::*; use crate::{ stats::CpuUsage, - test::{create_temp_dir, set_fixture, setup}, + test::{set_fixture, setup}, }; use oci_spec::runtime::LinuxCpuBuilder; use std::fs; @@ -185,13 +185,13 @@ mod tests { #[test] fn test_set_valid_shares() { // arrange - let (tmp, weight) = setup("test_set_shares", CGROUP_CPU_WEIGHT); - let _ = set_fixture(&tmp, CGROUP_CPU_MAX, "") + let (tmp, weight) = setup(CGROUP_CPU_WEIGHT); + let _ = set_fixture(tmp.path(), CGROUP_CPU_MAX, "") .unwrap_or_else(|_| panic!("set test fixture for {CGROUP_CPU_MAX}")); let cpu = LinuxCpuBuilder::default().shares(22000u64).build().unwrap(); // act - Cpu::apply(&tmp, &cpu).expect("apply cpu"); + Cpu::apply(tmp.path(), &cpu).expect("apply cpu"); // assert let content = fs::read_to_string(weight) @@ -214,11 +214,11 @@ mod tests { return; } - let (tmp, max) = setup("test_set_cpu_idle", CGROUP_CPU_IDLE); + let (tmp, max) = setup(CGROUP_CPU_IDLE); let cpu = LinuxCpuBuilder::default().idle(IDLE).build().unwrap(); // act - Cpu::apply(&tmp, &cpu).expect("apply cpu"); + Cpu::apply(tmp.path(), &cpu).expect("apply cpu"); // assert let content = fs::read_to_string(max) @@ -230,11 +230,11 @@ mod tests { fn test_set_positive_quota() { // arrange const QUOTA: i64 = 200000; - let (tmp, max) = setup("test_set_positive_quota", CGROUP_CPU_MAX); + let (tmp, max) = setup(CGROUP_CPU_MAX); let cpu = LinuxCpuBuilder::default().quota(QUOTA).build().unwrap(); // act - Cpu::apply(&tmp, &cpu).expect("apply cpu"); + Cpu::apply(tmp.path(), &cpu).expect("apply cpu"); // assert let content = fs::read_to_string(max) @@ -245,11 +245,11 @@ mod tests { #[test] fn test_set_negative_quota() { // arrange - let (tmp, max) = setup("test_set_negative_quota", CGROUP_CPU_MAX); + let (tmp, max) = setup(CGROUP_CPU_MAX); let cpu = LinuxCpuBuilder::default().quota(-500).build().unwrap(); // act - Cpu::apply(&tmp, &cpu).expect("apply cpu"); + Cpu::apply(tmp.path(), &cpu).expect("apply cpu"); // assert let content = fs::read_to_string(max) @@ -262,12 +262,12 @@ mod tests { // arrange const QUOTA: u64 = 50000; const PERIOD: u64 = 100000; - let (tmp, max) = setup("test_set_positive_period", CGROUP_CPU_MAX); + let (tmp, max) = setup(CGROUP_CPU_MAX); common::write_cgroup_file(&max, QUOTA).unwrap(); let cpu = LinuxCpuBuilder::default().period(PERIOD).build().unwrap(); // act - Cpu::apply(&tmp, &cpu).expect("apply cpu"); + Cpu::apply(tmp.path(), &cpu).expect("apply cpu"); // assert let content = fs::read_to_string(max) @@ -280,7 +280,7 @@ mod tests { // arrange const QUOTA: i64 = 200000; const PERIOD: u64 = 100000; - let (tmp, max) = setup("test_set_quota_and_period", CGROUP_CPU_MAX); + let (tmp, max) = setup(CGROUP_CPU_MAX); let cpu = LinuxCpuBuilder::default() .quota(QUOTA) .period(PERIOD) @@ -288,7 +288,7 @@ mod tests { .unwrap(); // act - Cpu::apply(&tmp, &cpu).expect("apply cpu"); + Cpu::apply(tmp.path(), &cpu).expect("apply cpu"); // assert let content = fs::read_to_string(max) @@ -299,15 +299,14 @@ mod tests { #[test] fn test_realtime_runtime_not_supported() { // arrange - let tmp = create_temp_dir("test_realtime_runtime_not_supported") - .expect("create temp directory for test"); + let tmp = tempfile::tempdir().unwrap(); let cpu = LinuxCpuBuilder::default() .realtime_runtime(5) .build() .unwrap(); // act - let result = Cpu::apply(&tmp, &cpu); + let result = Cpu::apply(tmp.path(), &cpu); // assert assert!( @@ -319,15 +318,14 @@ mod tests { #[test] fn test_realtime_period_not_supported() { // arrange - let tmp = create_temp_dir("test_realtime_period_not_supported") - .expect("create temp directory for test"); + let tmp = tempfile::tempdir().unwrap(); let cpu = LinuxCpuBuilder::default() .realtime_period(5u64) .build() .unwrap(); // act - let result = Cpu::apply(&tmp, &cpu); + let result = Cpu::apply(tmp.path(), &cpu); // assert assert!( @@ -338,12 +336,12 @@ mod tests { #[test] fn test_stat_usage() { - let tmp = create_temp_dir("test_stat_usage").expect("create temp directory for test"); + let tmp = tempfile::tempdir().unwrap(); let content = ["usage_usec 7730", "user_usec 4387", "system_usec 3498"].join("\n"); - set_fixture(&tmp, CPU_STAT, &content).expect("create stat file"); - set_fixture(&tmp, CPU_PSI, "").expect("create psi file"); + set_fixture(tmp.path(), CPU_STAT, &content).expect("create stat file"); + set_fixture(tmp.path(), CPU_PSI, "").expect("create psi file"); - let actual = Cpu::stats(&tmp).expect("get cgroup stats"); + let actual = Cpu::stats(tmp.path()).expect("get cgroup stats"); let expected = CpuUsage { usage_total: 7730, usage_user: 4387, @@ -357,10 +355,10 @@ mod tests { #[test] fn test_burst() { let expected = 100000u64; - let (tmp, burst_file) = setup("test_burst", CGROUP_CPU_BURST); + let (tmp, burst_file) = setup(CGROUP_CPU_BURST); let cpu = LinuxCpuBuilder::default().burst(expected).build().unwrap(); - Cpu::apply(&tmp, &cpu).expect("apply cpu"); + Cpu::apply(tmp.path(), &cpu).expect("apply cpu"); let actual = fs::read_to_string(burst_file).expect("read burst file"); assert_eq!(actual, expected.to_string()); diff --git a/crates/libcgroups/src/v2/cpuset.rs b/crates/libcgroups/src/v2/cpuset.rs index dfbce6e37..ba93789b5 100644 --- a/crates/libcgroups/src/v2/cpuset.rs +++ b/crates/libcgroups/src/v2/cpuset.rs @@ -47,14 +47,14 @@ mod tests { #[test] fn test_set_cpus() { // arrange - let (tmp, cpus) = setup("test_set_cpus", CGROUP_CPUSET_CPUS); + let (tmp, cpus) = setup(CGROUP_CPUSET_CPUS); let cpuset = LinuxCpuBuilder::default() .cpus("1-3".to_owned()) .build() .unwrap(); // act - CpuSet::apply(&tmp, &cpuset).expect("apply cpuset"); + CpuSet::apply(tmp.path(), &cpuset).expect("apply cpuset"); // assert let content = fs::read_to_string(cpus) @@ -65,14 +65,14 @@ mod tests { #[test] fn test_set_mems() { // arrange - let (tmp, mems) = setup("test_set_mems", CGROUP_CPUSET_MEMS); + let (tmp, mems) = setup(CGROUP_CPUSET_MEMS); let cpuset = LinuxCpuBuilder::default() .mems("1-3".to_owned()) .build() .unwrap(); // act - CpuSet::apply(&tmp, &cpuset).expect("apply cpuset"); + CpuSet::apply(tmp.path(), &cpuset).expect("apply cpuset"); // assert let content = fs::read_to_string(mems) diff --git a/crates/libcgroups/src/v2/devices/controller.rs b/crates/libcgroups/src/v2/devices/controller.rs index 8c324417f..91aa207b4 100644 --- a/crates/libcgroups/src/v2/devices/controller.rs +++ b/crates/libcgroups/src/v2/devices/controller.rs @@ -129,7 +129,7 @@ mod tests { #[serial(bpf)] // mock contexts are shared fn test_apply_devices() { // arrange - let (tmp, _) = setup("test_apply_devices", "some.value"); + let (tmp, _) = setup("some.value"); let a_type = LinuxDeviceCgroupBuilder::default() .typ(LinuxDeviceType::A) .build() @@ -151,14 +151,14 @@ mod tests { detach2.expect().never(); // act - Devices::apply_devices(&tmp, &Some(vec![a_type])).expect("Could not apply devices"); + Devices::apply_devices(tmp.path(), &Some(vec![a_type])).expect("Could not apply devices"); } #[test] #[serial(bpf)] // mock contexts are shared fn test_existing_programs() { // arrange - let (tmp, _) = setup("test_existing_programs", "some.value"); + let (tmp, _) = setup("some.value"); let a_type = LinuxDeviceCgroupBuilder::default() .typ(LinuxDeviceType::A) .build() @@ -187,6 +187,6 @@ mod tests { detach2.expect().once().returning(|_, _| Ok(())); // act - Devices::apply_devices(&tmp, &Some(vec![a_type])).expect("Could not apply devices"); + Devices::apply_devices(tmp.path(), &Some(vec![a_type])).expect("Could not apply devices"); } } diff --git a/crates/libcgroups/src/v2/freezer.rs b/crates/libcgroups/src/v2/freezer.rs index 0428568e1..7a8c7e6bf 100644 --- a/crates/libcgroups/src/v2/freezer.rs +++ b/crates/libcgroups/src/v2/freezer.rs @@ -149,16 +149,14 @@ impl Freezer { mod tests { use super::*; use crate::common::FreezerState; - use crate::test::{create_temp_dir, set_fixture}; + use crate::test::set_fixture; use std::sync::Arc; #[test] fn test_set_freezer_state() { - let tmp = Arc::new( - create_temp_dir("test_set_freezer_state").expect("create temp directory for test"), - ); - set_fixture(&tmp, CGROUP_FREEZE, "").expect("Set fixure for freezer state"); - set_fixture(&tmp, CGROUP_EVENTS, "populated 0\nfrozen 0") + let tmp = Arc::new(tempfile::tempdir().unwrap()); + set_fixture(tmp.path(), CGROUP_FREEZE, "").expect("Set fixure for freezer state"); + set_fixture(tmp.path(), CGROUP_EVENTS, "populated 0\nfrozen 0") .expect("Set fixure for freezer state"); // set Frozen state. @@ -167,51 +165,50 @@ mod tests { let p = Arc::clone(&tmp); thread::spawn(move || { thread::sleep(Duration::from_millis(100)); - set_fixture(&p, CGROUP_EVENTS, "populated 0\nfrozen 1") + set_fixture(p.path(), CGROUP_EVENTS, "populated 0\nfrozen 1") .expect("Set fixure for freezer state"); }); let freezer_state = FreezerState::Frozen; - Freezer::apply(freezer_state, &tmp).expect("Set freezer state"); + Freezer::apply(freezer_state, tmp.path()).expect("Set freezer state"); let state_content = - std::fs::read_to_string(tmp.join(CGROUP_FREEZE)).expect("Read to string"); + std::fs::read_to_string(tmp.path().join(CGROUP_FREEZE)).expect("Read to string"); assert_eq!("1", state_content); } // set Thawed state. { let freezer_state = FreezerState::Thawed; - Freezer::apply(freezer_state, &tmp).expect("Set freezer state"); + Freezer::apply(freezer_state, tmp.path()).expect("Set freezer state"); let state_content = - std::fs::read_to_string(tmp.join(CGROUP_FREEZE)).expect("Read to string"); + std::fs::read_to_string(tmp.path().join(CGROUP_FREEZE)).expect("Read to string"); assert_eq!("0", state_content); } // set Undefined state. { let old_state_content = - std::fs::read_to_string(tmp.join(CGROUP_FREEZE)).expect("Read to string"); + std::fs::read_to_string(tmp.path().join(CGROUP_FREEZE)).expect("Read to string"); let freezer_state = FreezerState::Undefined; - Freezer::apply(freezer_state, &tmp).expect("Set freezer state"); + Freezer::apply(freezer_state, tmp.path()).expect("Set freezer state"); let state_content = - std::fs::read_to_string(tmp.join(CGROUP_FREEZE)).expect("Read to string"); + std::fs::read_to_string(tmp.path().join(CGROUP_FREEZE)).expect("Read to string"); assert_eq!(old_state_content, state_content); } } #[test] fn test_set_freezer_state_error() { - let tmp = create_temp_dir("test_set_freezer_state_error") - .expect("create temp directory for test"); - set_fixture(&tmp, CGROUP_FREEZE, "").expect("Set fixure for freezer state"); - set_fixture(&tmp, CGROUP_EVENTS, "").expect("Set fixure for freezer state"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_FREEZE, "").expect("Set fixure for freezer state"); + set_fixture(tmp.path(), CGROUP_EVENTS, "").expect("Set fixure for freezer state"); // events file does not contain "frozen 1" { let freezer_state = FreezerState::Frozen; - let r = Freezer::apply(freezer_state, &tmp); + let r = Freezer::apply(freezer_state, tmp.path()); assert!(r.is_err()); } } diff --git a/crates/libcgroups/src/v2/hugetlb.rs b/crates/libcgroups/src/v2/hugetlb.rs index 576543114..f02ba18ee 100644 --- a/crates/libcgroups/src/v2/hugetlb.rs +++ b/crates/libcgroups/src/v2/hugetlb.rs @@ -140,30 +140,30 @@ impl HugeTlb { #[cfg(test)] mod tests { use super::*; - use crate::test::{create_temp_dir, set_fixture}; + use crate::test::set_fixture; use oci_spec::runtime::LinuxHugepageLimitBuilder; use std::fs::read_to_string; #[test] fn test_set_hugetlb() { let page_file_name = "hugetlb.2MB.max"; - let tmp = create_temp_dir("test_set_hugetlbv2").expect("create temp directory for test"); - set_fixture(&tmp, page_file_name, "0").expect("Set fixture for 2 MB page size"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), page_file_name, "0").expect("Set fixture for 2 MB page size"); let hugetlb = LinuxHugepageLimitBuilder::default() .page_size("2MB") .limit(16384) .build() .unwrap(); - HugeTlb::apply(&tmp, &hugetlb).expect("apply hugetlb"); - let content = read_to_string(tmp.join(page_file_name)).expect("Read hugetlb file content"); + HugeTlb::apply(tmp.path(), &hugetlb).expect("apply hugetlb"); + let content = + read_to_string(tmp.path().join(page_file_name)).expect("Read hugetlb file content"); assert_eq!(hugetlb.limit().to_string(), content); } #[test] fn test_set_hugetlb_with_invalid_page_size() { - let tmp = create_temp_dir("test_set_hugetlbv2_with_invalid_page_size") - .expect("create temp directory for test"); + let tmp = tempfile::tempdir().unwrap(); let hugetlb = LinuxHugepageLimitBuilder::default() .page_size("3MB") @@ -171,7 +171,7 @@ mod tests { .build() .unwrap(); - let result = HugeTlb::apply(&tmp, &hugetlb); + let result = HugeTlb::apply(tmp.path(), &hugetlb); assert!( result.is_err(), "page size that is not a power of two should be an error" @@ -181,9 +181,9 @@ mod tests { quickcheck! { fn property_test_set_hugetlb(hugetlb: LinuxHugepageLimit) -> bool { let page_file_name = format!("hugetlb.{:?}.max", hugetlb.page_size()); - let tmp = create_temp_dir("property_test_set_hugetlbv2").expect("create temp directory for test"); - set_fixture(&tmp, &page_file_name, "0").expect("Set fixture for page size"); - let result = HugeTlb::apply(&tmp, &hugetlb); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), &page_file_name, "0").expect("Set fixture for page size"); + let result = HugeTlb::apply(tmp.path(), &hugetlb); let page_size: String = hugetlb .page_size() @@ -194,7 +194,7 @@ mod tests { if HugeTlb::is_power_of_two(page_size) && page_size != 1 { let content = - read_to_string(tmp.join(page_file_name)).expect("Read hugetlb file content"); + read_to_string(tmp.path().join(page_file_name)).expect("Read hugetlb file content"); hugetlb.limit().to_string() == content } else { result.is_err() @@ -204,11 +204,11 @@ mod tests { #[test] fn test_stat_hugetbl() { - let tmp = create_temp_dir("test_stat_hugetlb").expect("create temp directory for test"); - set_fixture(&tmp, "hugetlb.2MB.current", "1024\n").expect("set hugetlb current"); - set_fixture(&tmp, "hugetlb.2MB.events", "max 5\n").expect("set hugetlb events"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), "hugetlb.2MB.current", "1024\n").expect("set hugetlb current"); + set_fixture(tmp.path(), "hugetlb.2MB.events", "max 5\n").expect("set hugetlb events"); - let actual = HugeTlb::stats_for_page_size(&tmp, "2MB").expect("get cgroup stats"); + let actual = HugeTlb::stats_for_page_size(tmp.path(), "2MB").expect("get cgroup stats"); let expected = HugeTlbStats { usage: 1024, diff --git a/crates/libcgroups/src/v2/io.rs b/crates/libcgroups/src/v2/io.rs index 5a00b916b..9c523f0e9 100644 --- a/crates/libcgroups/src/v2/io.rs +++ b/crates/libcgroups/src/v2/io.rs @@ -196,7 +196,7 @@ impl Io { #[cfg(test)] mod test { use super::*; - use crate::test::{create_temp_dir, set_fixture, setup}; + use crate::test::{set_fixture, setup}; use oci_spec::runtime::{ LinuxBlockIoBuilder, LinuxThrottleDeviceBuilder, LinuxWeightDeviceBuilder, @@ -205,7 +205,7 @@ mod test { #[test] fn test_set_io_read_bps() { - let (tmp, throttle) = setup("test_set_io_read_bps", "io.max"); + let (tmp, throttle) = setup("io.max"); let blkio = LinuxBlockIoBuilder::default() .throttle_read_bps_device(vec![LinuxThrottleDeviceBuilder::default() @@ -217,7 +217,7 @@ mod test { .build() .unwrap(); - Io::apply(&tmp, &blkio).expect("apply blkio"); + Io::apply(tmp.path(), &blkio).expect("apply blkio"); let content = fs::read_to_string(throttle).unwrap_or_else(|_| panic!("read rbps content")); assert_eq!("8:0 rbps=102400", content); @@ -225,7 +225,7 @@ mod test { #[test] fn test_set_io_write_bps() { - let (tmp, throttle) = setup("test_set_io_write_bps", "io.max"); + let (tmp, throttle) = setup("io.max"); let blkio = LinuxBlockIoBuilder::default() .throttle_write_bps_device(vec![LinuxThrottleDeviceBuilder::default() @@ -237,7 +237,7 @@ mod test { .build() .unwrap(); - Io::apply(&tmp, &blkio).expect("apply blkio"); + Io::apply(tmp.path(), &blkio).expect("apply blkio"); let content = fs::read_to_string(throttle).unwrap_or_else(|_| panic!("read rbps content")); assert_eq!("8:0 wbps=102400", content); @@ -245,7 +245,7 @@ mod test { #[test] fn test_set_io_read_iops() { - let (tmp, throttle) = setup("test_set_io_read_iops", "io.max"); + let (tmp, throttle) = setup("io.max"); let blkio = LinuxBlockIoBuilder::default() .throttle_read_iops_device(vec![LinuxThrottleDeviceBuilder::default() @@ -257,7 +257,7 @@ mod test { .build() .unwrap(); - Io::apply(&tmp, &blkio).expect("apply blkio"); + Io::apply(tmp.path(), &blkio).expect("apply blkio"); let content = fs::read_to_string(throttle).unwrap_or_else(|_| panic!("read riops content")); assert_eq!("8:0 riops=102400", content); @@ -265,7 +265,7 @@ mod test { #[test] fn test_set_io_write_iops() { - let (tmp, throttle) = setup("test_set_io_write_iops", "io.max"); + let (tmp, throttle) = setup("io.max"); let blkio = LinuxBlockIoBuilder::default() .throttle_write_iops_device(vec![LinuxThrottleDeviceBuilder::default() @@ -277,7 +277,7 @@ mod test { .build() .unwrap(); - Io::apply(&tmp, &blkio).expect("apply blkio"); + Io::apply(tmp.path(), &blkio).expect("apply blkio"); let content = fs::read_to_string(throttle).unwrap_or_else(|_| panic!("read wiops content")); assert_eq!("8:0 wiops=102400", content); @@ -285,7 +285,7 @@ mod test { #[test] fn test_set_ioweight_device() { - let (tmp, throttle) = setup("test_set_io_weight_device", CGROUP_BFQ_IO_WEIGHT); + let (tmp, throttle) = setup(CGROUP_BFQ_IO_WEIGHT); let blkio = LinuxBlockIoBuilder::default() .weight_device(vec![LinuxWeightDeviceBuilder::default() .major(8) @@ -297,7 +297,7 @@ mod test { .build() .unwrap(); - Io::apply(&tmp, &blkio).expect("apply blkio"); + Io::apply(tmp.path(), &blkio).expect("apply blkio"); let content = fs::read_to_string(throttle).unwrap_or_else(|_| panic!("read bfq_io_weight content")); @@ -323,13 +323,13 @@ mod test { expected_weight: String::from("1"), }, ] { - let (tmp, weight_file) = setup("test_set_io_weight", case.cgroup_file); + let (tmp, weight_file) = setup(case.cgroup_file); let blkio = LinuxBlockIoBuilder::default() .weight(case.weight) .build() .unwrap(); - Io::apply(&tmp, &blkio).expect("apply blkio"); + Io::apply(tmp.path(), &blkio).expect("apply blkio"); let content = fs::read_to_string(weight_file).expect("read blkio weight"); assert_eq!(case.expected_weight, content); } @@ -337,16 +337,16 @@ mod test { #[test] fn test_stat_io() { - let tmp = create_temp_dir("test_stat_io").expect("create test directory"); + let tmp = tempfile::tempdir().unwrap(); let stat_content = [ "7:10 rbytes=18432 wbytes=16842 rios=12 wios=0 dbytes=0 dios=0", "7:9 rbytes=34629632 wbytes=274965 rios=1066 wios=319 dbytes=0 dios=0", ] .join("\n"); - set_fixture(&tmp, "io.stat", &stat_content).unwrap(); - set_fixture(&tmp, CGROUP_IO_PSI, "").expect("create psi file"); + set_fixture(tmp.path(), "io.stat", &stat_content).unwrap(); + set_fixture(tmp.path(), CGROUP_IO_PSI, "").expect("create psi file"); - let mut actual = Io::stats(&tmp).expect("get cgroup stats"); + let mut actual = Io::stats(tmp.path()).expect("get cgroup stats"); let expected = BlkioStats { service_bytes: vec![ BlkioDeviceStat { diff --git a/crates/libcgroups/src/v2/memory.rs b/crates/libcgroups/src/v2/memory.rs index 9b915eff7..05ed7d149 100644 --- a/crates/libcgroups/src/v2/memory.rs +++ b/crates/libcgroups/src/v2/memory.rs @@ -167,16 +167,17 @@ impl Memory { #[cfg(test)] mod tests { use super::*; - use crate::test::{create_temp_dir, set_fixture}; + use crate::test::set_fixture; use oci_spec::runtime::LinuxMemoryBuilder; use std::fs::read_to_string; #[test] fn test_set_memory() { - let tmp = create_temp_dir("test_set_memory_v2").expect("create temp directory for test"); - set_fixture(&tmp, CGROUP_MEMORY_MAX, "0").expect("set fixture for memory limit"); - set_fixture(&tmp, CGROUP_MEMORY_LOW, "0").expect("set fixture for memory reservation"); - set_fixture(&tmp, CGROUP_MEMORY_SWAP, "0").expect("set fixture for swap limit"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_MEMORY_MAX, "0").expect("set fixture for memory limit"); + set_fixture(tmp.path(), CGROUP_MEMORY_LOW, "0") + .expect("set fixture for memory reservation"); + set_fixture(tmp.path(), CGROUP_MEMORY_SWAP, "0").expect("set fixture for swap limit"); let limit = 1024; let reservation = 512; @@ -189,73 +190,79 @@ mod tests { .build() .unwrap(); - Memory::apply(&tmp, &memory_limits).expect("apply memory limits"); + Memory::apply(tmp.path(), &memory_limits).expect("apply memory limits"); - let limit_content = read_to_string(tmp.join(CGROUP_MEMORY_MAX)).expect("read memory limit"); + let limit_content = + read_to_string(tmp.path().join(CGROUP_MEMORY_MAX)).expect("read memory limit"); assert_eq!(limit_content, limit.to_string()); - let swap_content = read_to_string(tmp.join(CGROUP_MEMORY_SWAP)).expect("read swap limit"); + let swap_content = + read_to_string(tmp.path().join(CGROUP_MEMORY_SWAP)).expect("read swap limit"); assert_eq!(swap_content, (swap - limit).to_string()); let reservation_content = - read_to_string(tmp.join(CGROUP_MEMORY_LOW)).expect("read memory reservation"); + read_to_string(tmp.path().join(CGROUP_MEMORY_LOW)).expect("read memory reservation"); assert_eq!(reservation_content, reservation.to_string()); } #[test] fn test_set_memory_unlimited() { - let tmp = create_temp_dir("test_set_memory_unlimited_v2") - .expect("create temp directory for test"); - set_fixture(&tmp, CGROUP_MEMORY_MAX, "0").expect("set fixture for memory limit"); - set_fixture(&tmp, CGROUP_MEMORY_LOW, "0").expect("set fixture for memory reservation"); - set_fixture(&tmp, CGROUP_MEMORY_SWAP, "0").expect("set fixture for swap limit"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_MEMORY_MAX, "0").expect("set fixture for memory limit"); + set_fixture(tmp.path(), CGROUP_MEMORY_LOW, "0") + .expect("set fixture for memory reservation"); + set_fixture(tmp.path(), CGROUP_MEMORY_SWAP, "0").expect("set fixture for swap limit"); let memory_limits = LinuxMemoryBuilder::default().limit(-1).build().unwrap(); - Memory::apply(&tmp, &memory_limits).expect("apply memory limits"); + Memory::apply(tmp.path(), &memory_limits).expect("apply memory limits"); - let limit_content = read_to_string(tmp.join(CGROUP_MEMORY_MAX)).expect("read memory limit"); + let limit_content = + read_to_string(tmp.path().join(CGROUP_MEMORY_MAX)).expect("read memory limit"); assert_eq!(limit_content, "max"); - let swap_content = read_to_string(tmp.join(CGROUP_MEMORY_SWAP)).expect("read swap limit"); + let swap_content = + read_to_string(tmp.path().join(CGROUP_MEMORY_SWAP)).expect("read swap limit"); assert_eq!(swap_content, "max"); } #[test] fn test_err_swap_no_memory() { - let tmp = - create_temp_dir("test_err_swap_no_memory_v2").expect("create temp directory for test"); - set_fixture(&tmp, CGROUP_MEMORY_MAX, "0").expect("set fixture for memory limit"); - set_fixture(&tmp, CGROUP_MEMORY_LOW, "0").expect("set fixture for memory reservation"); - set_fixture(&tmp, CGROUP_MEMORY_SWAP, "0").expect("set fixture for swap limit"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_MEMORY_MAX, "0").expect("set fixture for memory limit"); + set_fixture(tmp.path(), CGROUP_MEMORY_LOW, "0") + .expect("set fixture for memory reservation"); + set_fixture(tmp.path(), CGROUP_MEMORY_SWAP, "0").expect("set fixture for swap limit"); let memory_limits = LinuxMemoryBuilder::default().swap(512).build().unwrap(); - let result = Memory::apply(&tmp, &memory_limits); + let result = Memory::apply(tmp.path(), &memory_limits); assert!(result.is_err()); } #[test] fn test_err_bad_limit() { - let tmp = create_temp_dir("test_err_bad_limit_v2").expect("create temp directory for test"); - set_fixture(&tmp, CGROUP_MEMORY_MAX, "0").expect("set fixture for memory limit"); - set_fixture(&tmp, CGROUP_MEMORY_LOW, "0").expect("set fixture for memory reservation"); - set_fixture(&tmp, CGROUP_MEMORY_SWAP, "0").expect("set fixture for swap limit"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_MEMORY_MAX, "0").expect("set fixture for memory limit"); + set_fixture(tmp.path(), CGROUP_MEMORY_LOW, "0") + .expect("set fixture for memory reservation"); + set_fixture(tmp.path(), CGROUP_MEMORY_SWAP, "0").expect("set fixture for swap limit"); let memory_limits = LinuxMemoryBuilder::default().limit(-2).build().unwrap(); - let result = Memory::apply(&tmp, &memory_limits); + let result = Memory::apply(tmp.path(), &memory_limits); assert!(result.is_err()); } #[test] fn test_err_bad_swap() { - let tmp = create_temp_dir("test_err_bad_swap_v2").expect("create temp directory for test"); - set_fixture(&tmp, CGROUP_MEMORY_MAX, "0").expect("set fixture for memory limit"); - set_fixture(&tmp, CGROUP_MEMORY_LOW, "0").expect("set fixture for memory reservation"); - set_fixture(&tmp, CGROUP_MEMORY_SWAP, "0").expect("set fixture for swap limit"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_MEMORY_MAX, "0").expect("set fixture for memory limit"); + set_fixture(tmp.path(), CGROUP_MEMORY_LOW, "0") + .expect("set fixture for memory reservation"); + set_fixture(tmp.path(), CGROUP_MEMORY_SWAP, "0").expect("set fixture for swap limit"); let memory_limits = LinuxMemoryBuilder::default() .limit(512) @@ -263,19 +270,19 @@ mod tests { .build() .unwrap(); - let result = Memory::apply(&tmp, &memory_limits); + let result = Memory::apply(tmp.path(), &memory_limits); assert!(result.is_err()); } quickcheck! { fn property_test_set_memory(linux_memory: LinuxMemory) -> bool { - let tmp = create_temp_dir("property_test_set_memory_v2").expect("create temp directory for test"); - set_fixture(&tmp, CGROUP_MEMORY_MAX, "0").expect("set fixture for memory limit"); - set_fixture(&tmp, CGROUP_MEMORY_LOW, "0").expect("set fixture for memory reservation"); - set_fixture(&tmp, CGROUP_MEMORY_SWAP, "0").expect("set fixture for swap limit"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), CGROUP_MEMORY_MAX, "0").expect("set fixture for memory limit"); + set_fixture(tmp.path(), CGROUP_MEMORY_LOW, "0").expect("set fixture for memory reservation"); + set_fixture(tmp.path(), CGROUP_MEMORY_SWAP, "0").expect("set fixture for swap limit"); - let result = Memory::apply(&tmp, &linux_memory); + let result = Memory::apply(tmp.path(), &linux_memory); // we need to check for expected errors first and foremost or we'll get false negatives // later @@ -306,7 +313,7 @@ mod tests { } // check the limit file is set as expected - let limit_content = read_to_string(tmp.join(CGROUP_MEMORY_MAX)).expect("read memory limit to string"); + let limit_content = read_to_string(tmp.path().join(CGROUP_MEMORY_MAX)).expect("read memory limit to string"); let limit_check = match linux_memory.limit() { Some(limit) if limit == -1 => limit_content == "max", Some(limit) => limit_content == limit.to_string(), @@ -314,7 +321,7 @@ mod tests { }; // check the swap file is set as expected - let swap_content = read_to_string(tmp.join(CGROUP_MEMORY_SWAP)).expect("read swap limit to string"); + let swap_content = read_to_string(tmp.path().join(CGROUP_MEMORY_SWAP)).expect("read swap limit to string"); let swap_check = match linux_memory.swap() { Some(swap) if swap == -1 => swap_content == "max", Some(swap) => { @@ -338,7 +345,7 @@ mod tests { // check the resevation file is set as expected - let reservation_content = read_to_string(tmp.join(CGROUP_MEMORY_LOW)).expect("read memory reservation to string"); + let reservation_content = read_to_string(tmp.path().join(CGROUP_MEMORY_LOW)).expect("read memory reservation to string"); let reservation_check = match linux_memory.reservation() { Some(reservation) if reservation == -1 => reservation_content == "max", Some(reservation) => reservation_content == reservation.to_string(), @@ -354,13 +361,14 @@ mod tests { #[test] fn test_get_memory_data() { - let tmp = create_temp_dir("test_stat_memory").expect("create test directory"); - set_fixture(&tmp, "memory.current", "12500\n").unwrap(); - set_fixture(&tmp, "memory.max", "25000\n").unwrap(); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), "memory.current", "12500\n").unwrap(); + set_fixture(tmp.path(), "memory.max", "25000\n").unwrap(); let events = ["slab 5", "anon 13", "oom 3"].join("\n"); - set_fixture(&tmp, "memory.events", &events).unwrap(); + set_fixture(tmp.path(), "memory.events", &events).unwrap(); - let actual = Memory::get_memory_data(&tmp, "memory", "oom").expect("get cgroup stats"); + let actual = + Memory::get_memory_data(tmp.path(), "memory", "oom").expect("get cgroup stats"); let expected = MemoryData { usage: 12500, limit: 25000, diff --git a/crates/libcgroups/src/v2/pids.rs b/crates/libcgroups/src/v2/pids.rs index 766332869..e5060140f 100644 --- a/crates/libcgroups/src/v2/pids.rs +++ b/crates/libcgroups/src/v2/pids.rs @@ -48,35 +48,35 @@ impl Pids { #[cfg(test)] mod tests { use super::*; - use crate::test::{create_temp_dir, set_fixture}; + use crate::test::set_fixture; use oci_spec::runtime::LinuxPidsBuilder; #[test] fn test_set_pids() { let pids_file_name = "pids.max"; - let tmp = create_temp_dir("v2_test_set_pids").expect("create temp directory for test"); - set_fixture(&tmp, pids_file_name, "1000").expect("Set fixture for 1000 pids"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), pids_file_name, "1000").expect("Set fixture for 1000 pids"); let pids = LinuxPidsBuilder::default().limit(1000).build().unwrap(); - Pids::apply(&tmp, &pids).expect("apply pids"); + Pids::apply(tmp.path(), &pids).expect("apply pids"); let content = - std::fs::read_to_string(tmp.join(pids_file_name)).expect("Read pids contents"); + std::fs::read_to_string(tmp.path().join(pids_file_name)).expect("Read pids contents"); assert_eq!(pids.limit().to_string(), content); } #[test] fn test_set_pids_max() { let pids_file_name = "pids.max"; - let tmp = create_temp_dir("v2_test_set_pids_max").expect("create temp directory for test"); - set_fixture(&tmp, pids_file_name, "0").expect("set fixture for 0 pids"); + let tmp = tempfile::tempdir().unwrap(); + set_fixture(tmp.path(), pids_file_name, "0").expect("set fixture for 0 pids"); let pids = LinuxPidsBuilder::default().limit(0).build().unwrap(); - Pids::apply(&tmp, &pids).expect("apply pids"); + Pids::apply(tmp.path(), &pids).expect("apply pids"); let content = - std::fs::read_to_string(tmp.join(pids_file_name)).expect("Read pids contents"); + std::fs::read_to_string(tmp.path().join(pids_file_name)).expect("Read pids contents"); assert_eq!("max".to_string(), content); } } diff --git a/crates/libcgroups/src/v2/unified.rs b/crates/libcgroups/src/v2/unified.rs index 8b6af1d1b..133ee2187 100644 --- a/crates/libcgroups/src/v2/unified.rs +++ b/crates/libcgroups/src/v2/unified.rs @@ -61,7 +61,7 @@ mod tests { use oci_spec::runtime::LinuxResourcesBuilder; - use crate::test::{create_temp_dir, set_fixture}; + use crate::test::set_fixture; use crate::v2::controller_type::ControllerType; use super::*; @@ -69,9 +69,9 @@ mod tests { #[test] fn test_set_unified() { // arrange - let tmp = create_temp_dir("test_set_unified").unwrap(); - let hugetlb_limit_path = set_fixture(&tmp, "hugetlb.1GB.limit_in_bytes", "").unwrap(); - let cpu_weight_path = set_fixture(&tmp, "cpu.weight", "").unwrap(); + let tmp = tempfile::tempdir().unwrap(); + let hugetlb_limit_path = set_fixture(tmp.path(), "hugetlb.1GB.limit_in_bytes", "").unwrap(); + let cpu_weight_path = set_fixture(tmp.path(), "cpu.weight", "").unwrap(); let unified = { let mut u = HashMap::new(); @@ -96,7 +96,7 @@ mod tests { }; // act - Unified::apply(&controller_opt, &tmp, vec![]).expect("apply unified"); + Unified::apply(&controller_opt, tmp.path(), vec![]).expect("apply unified"); // assert let hugetlb_limit = fs::read_to_string(hugetlb_limit_path).expect("read hugetlb limit"); @@ -108,8 +108,7 @@ mod tests { #[test] fn test_set_unified_failed_to_write_subsystem_not_enabled() { // arrange - let tmp = - create_temp_dir("test_set_unified_failed_to_write_subsystem_not_enabled").unwrap(); + let tmp = tempfile::tempdir().unwrap(); let unified = { let mut u = HashMap::new(); @@ -134,7 +133,7 @@ mod tests { }; // act - let result = Unified::apply(&controller_opt, &tmp, vec![]); + let result = Unified::apply(&controller_opt, tmp.path(), vec![]); // assert assert!(result.is_err()); @@ -143,7 +142,7 @@ mod tests { #[test] fn test_set_unified_failed_to_write_subsystem_enabled() { // arrange - let tmp = create_temp_dir("test_set_unified_failed_to_write_subsystem_enabled").unwrap(); + let tmp = tempfile::tempdir().unwrap(); let unified = { let mut u = HashMap::new(); @@ -170,7 +169,7 @@ mod tests { // act let result = Unified::apply( &controller_opt, - &tmp, + tmp.path(), vec![ControllerType::HugeTlb, ControllerType::Cpu], );