Skip to content

Commit 367ea55

Browse files
committed
tests: move tests to tests/ directory and give the user cgroup perms
Signed-off-by: Levente Kurusa <[email protected]>
1 parent c8b029b commit 367ea55

File tree

8 files changed

+103
-67
lines changed

8 files changed

+103
-67
lines changed

src/cgroup.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl Cgroup {
9292
///
9393
/// ## Example:
9494
///
95-
/// ```
95+
/// ```text
9696
/// let pids: &PidController = control_group.controller_of()
9797
/// .expect("No pids controller attached!");
9898
/// let cpu: &CpuController = control_group.controller_of()

src/cpuacct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct CpuAcct {
2828
/// time spent is `user` time or `system` time.
2929
///
3030
/// An example is as follows:
31-
/// ```
31+
/// ```text
3232
/// cpu user system
3333
/// 0 8348363768 0
3434
/// 1 8324369100 0

src/lib.rs

+2-64
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ use net_prio::NetPrioController;
3232
use hugetlb::HugeTlbController;
3333
use rdma::RdmaController;
3434

35+
pub use cgroup::Cgroup;
36+
3537
/// Contains all the subsystems that are available in this crate.
3638
#[derive(Debug)]
3739
pub enum Subsystem {
@@ -500,67 +502,3 @@ impl Subsystem {
500502
}
501503
}
502504
}
503-
504-
505-
#[cfg(test)]
506-
mod tests {
507-
use {Resources, PidResources, Hierarchy, Controller, Controllers, Subsystem};
508-
use pid::{PidMax, PidController};
509-
use cgroup::Cgroup;
510-
511-
#[test]
512-
fn create_and_delete_cgroup() {
513-
let hier = ::hierarchies::V1::new();
514-
let cg = Cgroup::new(&hier, String::from("ltest2"));
515-
{
516-
let pidcontroller: &PidController = cg.controller_of().unwrap();
517-
pidcontroller.set_pid_max(PidMax::Value(1337));
518-
assert_eq!(pidcontroller.get_pid_max(), Some(PidMax::Value(1337)));
519-
}
520-
cg.delete();
521-
}
522-
523-
#[test]
524-
fn test_pid_pids_current_is_zero() {
525-
let hier = ::hierarchies::V1::new();
526-
let cg = Cgroup::new(&hier, String::from("ltest3"));
527-
{
528-
let pidcontroller: &PidController = cg.controller_of().unwrap();
529-
assert_eq!(pidcontroller.get_pid_current(), 0);
530-
}
531-
cg.delete();
532-
}
533-
534-
#[test]
535-
fn test_pid_pids_events_is_zero() {
536-
let hier = ::hierarchies::V1::new();
537-
let cg = Cgroup::new(&hier, String::from("ltest4"));
538-
{
539-
let pidcontroller: &PidController = cg.controller_of().unwrap();
540-
assert_eq!(pidcontroller.get_pid_events(), 0);
541-
}
542-
cg.delete();
543-
}
544-
545-
#[test]
546-
fn test_setting_resources() {
547-
let hier = ::hierarchies::V1::new();
548-
let cg = Cgroup::new(&hier, String::from("ltest5"));
549-
{
550-
let res = Resources {
551-
pid: PidResources {
552-
update_values: true,
553-
maximum_number_of_processes: PidMax::Value(512),
554-
},
555-
..Default::default()
556-
};
557-
cg.apply(&res);
558-
559-
/* verify */
560-
let pidcontroller: &PidController = cg.controller_of().unwrap();
561-
assert_eq!(pidcontroller.get_pid_max(), Some(PidMax::Value(512)));
562-
}
563-
cg.delete();
564-
}
565-
566-
}

src/memory.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub struct Memory {
5151
/// Contains various statistics about the NUMA locality of the control group's tasks.
5252
///
5353
/// The format of this field (as lifted from the kernel sources):
54-
/// ```
54+
/// ```text
5555
/// total=<total pages> N0=<node 0 pages> N1=<node 1 pages> ...
5656
/// file=<total file pages> N0=<node 0 pages> N1=<node 1 pages> ...
5757
/// anon=<total anon pages> N0=<node 0 pages> N1=<node 1 pages> ...

tests/pids.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//! Integration tests about the pids subsystem
2+
extern crate cgroups;
3+
4+
use cgroups::{Cgroup, Resources, PidResources};
5+
use cgroups::pid::{PidController, PidMax};
6+
7+
#[test]
8+
fn create_and_delete_cgroup() {
9+
let hier = cgroups::hierarchies::V1::new();
10+
let cg = Cgroup::new(&hier, String::from("create_and_delete_cgroup"));
11+
{
12+
let pidcontroller: &PidController = cg.controller_of().unwrap();
13+
pidcontroller.set_pid_max(PidMax::Value(1337));
14+
assert_eq!(pidcontroller.get_pid_max(), Some(PidMax::Value(1337)));
15+
}
16+
cg.delete();
17+
}
18+
19+
#[test]
20+
fn test_pid_pids_current_is_zero() {
21+
let hier = cgroups::hierarchies::V1::new();
22+
let cg = Cgroup::new(&hier, String::from("test_pid_pids_current_is_zero"));
23+
{
24+
let pidcontroller: &PidController = cg.controller_of().unwrap();
25+
assert_eq!(pidcontroller.get_pid_current(), 0);
26+
}
27+
cg.delete();
28+
}
29+
30+
#[test]
31+
fn test_pid_pids_events_is_zero() {
32+
let hier = cgroups::hierarchies::V1::new();
33+
let cg = Cgroup::new(&hier, String::from("test_pid_pids_events_is_zero"));
34+
{
35+
let pidcontroller: &PidController = cg.controller_of().unwrap();
36+
assert_eq!(pidcontroller.get_pid_events(), 0);
37+
}
38+
cg.delete();
39+
}

tests/resources.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//! Integration test about setting resources using `apply()`
2+
extern crate cgroups;
3+
4+
use cgroups::{Cgroup, Resources, PidResources};
5+
use cgroups::pid::{PidController, PidMax};
6+
7+
#[test]
8+
fn pid_resources() {
9+
let hier = cgroups::hierarchies::V1::new();
10+
let cg = Cgroup::new(&hier, String::from("pid_resources"));
11+
{
12+
let res = Resources {
13+
pid: PidResources {
14+
update_values: true,
15+
maximum_number_of_processes: PidMax::Value(512),
16+
},
17+
..Default::default()
18+
};
19+
cg.apply(&res);
20+
21+
/* verify */
22+
let pidcontroller: &PidController = cg.controller_of().unwrap();
23+
assert_eq!(pidcontroller.get_pid_max(), Some(PidMax::Value(512)));
24+
}
25+
cg.delete();
26+
}

tools/create_cgroup.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/sh
2+
3+
CONTROL_GROUPS=`cargo test -- --list 2>/dev/null | egrep 'test$' | egrep -v '^src' | cut -d':' -f1`
4+
5+
echo This script will create a control group in every subsystem of the V1 hierarchy.
6+
echo For this, we will need your sudo privileges. Please do not trust this shell script and have a look to check that it does something that you are okay with.
7+
sudo -v
8+
9+
for i in ${CONTROL_GROUPS}
10+
do sudo mkdir -p /sys/fs/cgroup/{blkio,cpu,cpuacct,cpuset,devices,freezer,hugetlb,memory,net_cls,net_prio,perf_event,pids}/$i/
11+
12+
done
13+
echo
14+
echo We will now set up permissions...
15+
echo
16+
17+
for i in ${CONTROL_GROUPS}
18+
do sudo chown -R ${USER} /sys/fs/cgroup/{blkio,cpu,cpuacct,cpuset,devices,freezer,hugetlb,memory,net_cls,net_prio,perf_event,pids}/$i/
19+
done

tools/delete_cgroup.sh

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
3+
CONTROL_GROUPS=`cargo test -- --list 2>/dev/null | egrep 'test$' | egrep -v '^src' | cut -d':' -f1`
4+
5+
echo This script will delete the control groups created by the create_cgroup.sh shell script.
6+
echo
7+
echo It may spit out some errors, but that is fine.
8+
echo
9+
echo For this, we will need your sudo privileges. Please do not trust this shell script and have a look to check that it does something that you are okay with.
10+
sudo -v
11+
12+
for i in ${CONTROL_GROUPS}
13+
do sudo rmdir /sys/fs/cgroup/{blkio,cpu,cpuacct,cpuset,devices,freezer,hugetlb,memory,net_cls,net_prio,perf_event,pids}/$i/
14+
done

0 commit comments

Comments
 (0)