diff --git a/src/cgroups/common.rs b/src/cgroups/common.rs index b25a8906e..48bf51970 100644 --- a/src/cgroups/common.rs +++ b/src/cgroups/common.rs @@ -6,7 +6,7 @@ use std::{ path::{Path, PathBuf}, }; -use anyhow::{anyhow, Result}; +use anyhow::{bail, Result}; use nix::unistd::Pid; use oci_spec::LinuxResources; use procfs::process::Process; @@ -74,30 +74,28 @@ pub fn create_cgroup_manager>(cgroup_path: P) -> Result { log::info!("cgroup manager V1 will be used"); - return Ok(Box::new(v1::manager::Manager::new(cgroup_path.into())?)); + Ok(Box::new(v1::manager::Manager::new(cgroup_path.into())?)) } (None, Some(cgroup2)) => { log::info!("cgroup manager V2 will be used"); - return Ok(Box::new(v2::manager::Manager::new( + Ok(Box::new(v2::manager::Manager::new( cgroup2.mount_point, cgroup_path.into(), - )?)); + )?)) } (Some(_), Some(cgroup2)) => { let cgroup_override = env::var("YOUKI_PREFER_CGROUPV2"); match cgroup_override { Ok(v) if v == "true" => { log::info!("cgroup manager V2 will be used"); - return Ok(Box::new(v2::manager::Manager::new( + Ok(Box::new(v2::manager::Manager::new( cgroup2.mount_point, cgroup_path.into(), - )?)); - } - _ => { - return Ok(Box::new(v1::manager::Manager::new(cgroup_path.into())?)) + )?)) } + _ => Ok(Box::new(v1::manager::Manager::new(cgroup_path.into())?)), } } - _ => return Err(anyhow!("could not find cgroup filesystem")), + _ => bail!("could not find cgroup filesystem"), } } diff --git a/src/cgroups/v2/cpu.rs b/src/cgroups/v2/cpu.rs index 50bae166f..26b1e17be 100644 --- a/src/cgroups/v2/cpu.rs +++ b/src/cgroups/v2/cpu.rs @@ -71,11 +71,11 @@ impl Cpu { } fn is_realtime_requested(cpu: &LinuxCpu) -> bool { - if let Some(_) = cpu.realtime_period { + if cpu.realtime_period.is_some() { return true; } - if let Some(_) = cpu.realtime_runtime { + if cpu.realtime_runtime.is_some() { return true; } @@ -92,7 +92,7 @@ mod tests { fn setup(testname: &str, cgroup_file: &str) -> (PathBuf, PathBuf) { let tmp = create_temp_dir(testname).expect("create temp directory for test"); let cgroup_file = set_fixture(&tmp, cgroup_file, "") - .expect(&format!("set test fixture for {}", cgroup_file)); + .unwrap_or_else(|_| panic!("set test fixture for {}", cgroup_file)); (tmp, cgroup_file) } @@ -102,15 +102,15 @@ mod tests { // arrange let (tmp, weight) = setup("test_set_shares", CGROUP_CPU_WEIGHT); let _ = set_fixture(&tmp, CGROUP_CPU_MAX, "") - .expect(&format!("set test fixture for {}", CGROUP_CPU_MAX)); + .unwrap_or_else(|_| panic!("set test fixture for {}", CGROUP_CPU_MAX)); let cpu = LinuxCpuBuilder::new().with_shares(22000).build(); // act Cpu::apply(&tmp, &cpu).expect("apply cpu"); // assert - let content = - fs::read_to_string(weight).expect(&format!("read {} file content", CGROUP_CPU_WEIGHT)); + let content = fs::read_to_string(weight) + .unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPU_WEIGHT)); assert_eq!(content, 840.to_string()); } @@ -125,8 +125,8 @@ mod tests { Cpu::apply(&tmp, &cpu).expect("apply cpu"); // assert - let content = - fs::read_to_string(max).expect(&format!("read {} file content", CGROUP_CPU_MAX)); + let content = fs::read_to_string(max) + .unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPU_MAX)); assert_eq!(content, format!("{} {}", QUOTA, DEFAULT_PERIOD)) } @@ -140,8 +140,8 @@ mod tests { Cpu::apply(&tmp, &cpu).expect("apply cpu"); // assert - let content = - fs::read_to_string(max).expect(&format!("read {} file content", CGROUP_CPU_MAX)); + let content = fs::read_to_string(max) + .unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPU_MAX)); assert_eq!( content, format!("{} {}", UNRESTRICTED_QUOTA, DEFAULT_PERIOD) @@ -159,8 +159,8 @@ mod tests { Cpu::apply(&tmp, &cpu).expect("apply cpu"); // assert - let content = - fs::read_to_string(max).expect(&format!("read {} file content", CGROUP_CPU_MAX)); + let content = fs::read_to_string(max) + .unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPU_MAX)); assert_eq!(content, format!("{} {}", UNRESTRICTED_QUOTA, PERIOD)) } @@ -174,8 +174,8 @@ mod tests { Cpu::apply(&tmp, &cpu).expect("apply cpu"); // assert - let content = - fs::read_to_string(max).expect(&format!("read {} file content", CGROUP_CPU_MAX)); + let content = fs::read_to_string(max) + .unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPU_MAX)); assert_eq!( content, format!("{} {}", UNRESTRICTED_QUOTA, DEFAULT_PERIOD) @@ -197,8 +197,8 @@ mod tests { Cpu::apply(&tmp, &cpu).expect("apply cpu"); // assert - let content = - fs::read_to_string(max).expect(&format!("read {} file content", CGROUP_CPU_MAX)); + let content = fs::read_to_string(max) + .unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPU_MAX)); assert_eq!(content, format!("{} {}", QUOTA, PERIOD)); } diff --git a/src/cgroups/v2/cpuset.rs b/src/cgroups/v2/cpuset.rs index 825b44dc7..3fe0e5756 100644 --- a/src/cgroups/v2/cpuset.rs +++ b/src/cgroups/v2/cpuset.rs @@ -45,7 +45,7 @@ mod tests { fn setup(testname: &str, cgroup_file: &str) -> (PathBuf, PathBuf) { let tmp = create_temp_dir(testname).expect("create temp directory for test"); let cgroup_file = set_fixture(&tmp, cgroup_file, "") - .expect(&format!("set test fixture for {}", cgroup_file)); + .unwrap_or_else(|_| panic!("set test fixture for {}", cgroup_file)); (tmp, cgroup_file) } @@ -60,8 +60,8 @@ mod tests { CpuSet::apply(&tmp, &cpuset).expect("apply cpuset"); // assert - let content = - fs::read_to_string(&cpus).expect(&format!("read {} file content", CGROUP_CPUSET_CPUS)); + let content = fs::read_to_string(&cpus) + .unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPUSET_CPUS)); assert_eq!(content, "1-3"); } @@ -75,8 +75,8 @@ mod tests { CpuSet::apply(&tmp, &cpuset).expect("apply cpuset"); // assert - let content = - fs::read_to_string(&mems).expect(&format!("read {} file content", CGROUP_CPUSET_MEMS)); + let content = fs::read_to_string(&mems) + .unwrap_or_else(|_| panic!("read {} file content", CGROUP_CPUSET_MEMS)); assert_eq!(content, "1-3"); } } diff --git a/src/create.rs b/src/create.rs index 91ccb0df4..5cd967c52 100644 --- a/src/create.rs +++ b/src/create.rs @@ -18,7 +18,7 @@ use crate::stdio::FileDescriptor; use crate::tty; use crate::utils; use crate::{capabilities, command::Command}; -use oci_spec; + #[derive(Clap, Debug)] pub struct Create { #[clap(short, long)] diff --git a/src/process/fork.rs b/src/process/fork.rs index ebc75230f..07f3a6cf9 100644 --- a/src/process/fork.rs +++ b/src/process/fork.rs @@ -17,7 +17,6 @@ use crate::container::ContainerStatus; use crate::process::{child, init, parent, Process}; use crate::utils; use crate::{cond::Cond, container::Container}; -use oci_spec; pub fn fork_first>( pid_file: Option

,