Skip to content

Commit c774e9e

Browse files
committed
manager: Add unit test cases
The tests cover the methods of `conv` mod, `FsManager`, and `SystemdManager`. Since we have to manipulate the cgroups during testing, the tests related to this part are set to be run in sequence. Signed-off-by: Xuewei Niu <[email protected]>
1 parent d695178 commit c774e9e

File tree

6 files changed

+667
-4
lines changed

6 files changed

+667
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ bit-vec = "0.6"
2424
[dev-dependencies]
2525
libc = "0.2.76"
2626
rand = "0.8"
27+
nix = "0.25"
2728

2829
[features]
2930
default = []

Makefile

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ build: debug
1919
# Tests and linters
2020
#
2121

22-
.PHONY: test
23-
test: test-systemd
24-
cargo test -- --color always --nocapture \
25-
--skip systemd::dbus::client::tests
22+
# Tests that manipulate cgroups should run in sequence, so that
23+
# `--test-threads=1` is used.
24+
test: test-systemd test-fs-manager test-systemd-manager
25+
cargo test --all-features -- --color always \
26+
--nocapture \
27+
--skip systemd::dbus::client::tests \
28+
--skip manager::fs::tests \
29+
--skip manager::systemd::tests
2630

2731
.PHONY: test-systemd
2832
# Tests that manipulate cgroups should run in sequence, so that
@@ -33,6 +37,20 @@ test-systemd:
3337
--color always --nocapture \
3438
--test-threads=1
3539

40+
.PHONY: test-fs-manager
41+
# See test-systemd
42+
test-fs-manager:
43+
cargo test --all-features --package cgroups-rs \
44+
--lib -- manager::fs::tests \
45+
--color always --nocapture --test-threads=1
46+
47+
.PHONY: test-systemd-manager
48+
# See test-systemd
49+
test-systemd-manager:
50+
cargo test --all-features --package cgroups-rs \
51+
--lib -- manager::systemd::tests \
52+
--color always --nocapture --test-threads=1
53+
3654
.PHONY: check
3755
check: fmt clippy
3856

src/manager/conv.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,47 @@ pub(crate) fn memory_swap_to_cgroup_v2(memswap_limit: i64, mem_limit: i64) -> Re
6767

6868
Ok(memswap_limit - mem_limit)
6969
}
70+
71+
#[cfg(test)]
72+
mod tests {
73+
use crate::manager::conv::*;
74+
75+
#[test]
76+
fn test_cpu_shares_to_cgroup_v2() {
77+
assert_eq!(cpu_shares_to_cgroup_v2(0), 0);
78+
assert_eq!(cpu_shares_to_cgroup_v2(1), 1);
79+
assert_eq!(cpu_shares_to_cgroup_v2(2), 1);
80+
assert_eq!(cpu_shares_to_cgroup_v2(100), 4);
81+
assert_eq!(
82+
cpu_shares_to_cgroup_v2(CPU_SHARES_V1_MAX),
83+
CPU_WEIGHT_V2_MAX
84+
);
85+
assert_eq!(
86+
cpu_shares_to_cgroup_v2(CPU_SHARES_V1_MAX - 1),
87+
CPU_WEIGHT_V2_MAX - 1
88+
);
89+
assert_eq!(cpu_shares_to_cgroup_v2(u64::MAX), CPU_WEIGHT_V2_MAX);
90+
}
91+
92+
#[test]
93+
fn test_memory_swap_to_cgroup_v2() {
94+
// memory no limit and swap is 0, treat it as no limit
95+
assert_eq!(memory_swap_to_cgroup_v2(0, -1).unwrap(), -1);
96+
// -1 is "max", 0 is "unset", so treat as is
97+
assert_eq!(memory_swap_to_cgroup_v2(-1, 0).unwrap(), -1);
98+
assert_eq!(memory_swap_to_cgroup_v2(0, 0).unwrap(), 0);
99+
100+
// Now swap cannot be 0 or -1
101+
102+
// Unlimited memory, so treat swap as is.
103+
assert_eq!(memory_swap_to_cgroup_v2(100, -1).unwrap(), 100);
104+
// Unset or unknown memory, can't calculate swap.
105+
assert!(memory_swap_to_cgroup_v2(100, 0).is_err());
106+
// Does not make sense to subtract a negative value.
107+
assert!(memory_swap_to_cgroup_v2(100, -2).is_err());
108+
// Swap + mem < mem
109+
assert!(memory_swap_to_cgroup_v2(50, 100).is_err());
110+
// Real swap
111+
assert_eq!(memory_swap_to_cgroup_v2(200, 100).unwrap(), 100);
112+
}
113+
}

0 commit comments

Comments
 (0)