1
0
mirror of https://github.com/containers/youki synced 2024-11-23 09:21:57 +01:00

Tests for systemd unified controller

This commit is contained in:
Furisto 2021-11-16 22:15:35 +01:00
parent 76bcc3cdc2
commit bd97c153d1

@ -94,7 +94,7 @@ impl Unified {
}
"pids.max" => {
let pids = value.trim().parse::<i64>()?;
properties.insert(pids::TASKS_MAX, Box::new(pids));
properties.insert(pids::TASKS_MAX, Box::new(pids as u64));
}
unknown => log::warn!("could not apply {}. Unknown property.", unknown),
@ -104,3 +104,111 @@ impl Unified {
Ok(())
}
}
#[cfg(test)]
mod tests {
use dbus::arg::ArgType;
use super::*;
#[test]
fn test_set() -> Result<()> {
// arrange
let unified: HashMap<String, String> = [
("cpu.weight", "22000"),
("cpuset.cpus", "0-3"),
("cpuset.mems", "0-3"),
("memory.min", "100000"),
("memory.low", "200000"),
("memory.high", "300000"),
("memory.max", "400000"),
("pids.max", "100"),
]
.into_iter()
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.collect();
let mut expected: HashMap<&str, Box<dyn RefArg>> = HashMap::new();
expected.insert(cpu::CPU_WEIGHT, Box::new(840 as u64));
expected.insert(cpuset::ALLOWED_CPUS, Box::new(vec![15 as u64]));
expected.insert(cpuset::ALLOWED_NODES, Box::new(vec![15 as u64]));
expected.insert(memory::MEMORY_MIN, Box::new(100000 as u64));
expected.insert(memory::MEMORY_LOW, Box::new(200000 as u64));
expected.insert(memory::MEMORY_HIGH, Box::new(300000 as u64));
expected.insert(memory::MEMORY_MAX, Box::new(400000 as u64));
expected.insert(pids::TASKS_MAX, Box::new(100 as u64));
// act
let mut actual: HashMap<&str, Box<dyn RefArg>> = HashMap::new();
Unified::apply(&unified, 245, &mut actual).context("apply unified")?;
// assert
for (setting, value) in expected {
assert!(actual.contains_key(setting));
assert_eq!(value.arg_type(), actual[setting].arg_type(), "{}", setting);
match value.arg_type() {
ArgType::UInt64 => {
assert_eq!(value.as_u64(), actual[setting].as_u64(), "{}", setting)
}
ArgType::Array => assert_eq!(
value.as_iter().unwrap().next().unwrap().as_u64(),
actual[setting].as_iter().unwrap().next().unwrap().as_u64()
),
arg_type => bail!("unexpected arg type {:?}", arg_type),
}
}
Ok(())
}
#[test]
fn test_cpu_max_quota_and_period() -> Result<()> {
// arrange
let unified: HashMap<String, String> = [
("cpu.max", "500000 250000"),
]
.into_iter()
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.collect();
let mut actual: HashMap<&str, Box<dyn RefArg>> = HashMap::new();
// act
Unified::apply(&unified, 245, &mut actual).context("apply unified")?;
// assert
assert!(actual.contains_key(cpu::CPU_PERIOD));
assert!(actual.contains_key(cpu::CPU_QUOTA));
assert_eq!(actual[cpu::CPU_PERIOD].arg_type(), ArgType::UInt64);
assert_eq!(actual[cpu::CPU_QUOTA].arg_type(), ArgType::UInt64);
assert_eq!(actual[cpu::CPU_PERIOD].as_u64().unwrap(), 250000);
assert_eq!(actual[cpu::CPU_QUOTA].as_u64().unwrap(), 500000);
Ok(())
}
#[test]
fn test_cpu_max_quota_only() -> Result<()> {
// arrange
let unified: HashMap<String, String> = [
("cpu.max", "500000"),
]
.into_iter()
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.collect();
let mut actual: HashMap<&str, Box<dyn RefArg>> = HashMap::new();
// act
Unified::apply(&unified, 245, &mut actual).context("apply unified")?;
// assert
assert!(!actual.contains_key(cpu::CPU_PERIOD));
assert!(actual.contains_key(cpu::CPU_QUOTA));
assert_eq!(actual[cpu::CPU_QUOTA].arg_type(), ArgType::UInt64);
assert_eq!(actual[cpu::CPU_QUOTA].as_u64().unwrap(), 500000);
Ok(())
}
}