tests: move tests to tests/ directory and give the user cgroup perms

Signed-off-by: Levente Kurusa <lkurusa@acm.org>
This commit is contained in:
Levente Kurusa
2018-08-30 13:40:15 +02:00
parent c8b029b154
commit 367ea556ca
8 changed files with 103 additions and 67 deletions

View File

@@ -92,7 +92,7 @@ impl Cgroup {
///
/// ## Example:
///
/// ```
/// ```text
/// let pids: &PidController = control_group.controller_of()
/// .expect("No pids controller attached!");
/// let cpu: &CpuController = control_group.controller_of()

View File

@@ -28,7 +28,7 @@ pub struct CpuAcct {
/// time spent is `user` time or `system` time.
///
/// An example is as follows:
/// ```
/// ```text
/// cpu user system
/// 0 8348363768 0
/// 1 8324369100 0

View File

@@ -32,6 +32,8 @@ use net_prio::NetPrioController;
use hugetlb::HugeTlbController;
use rdma::RdmaController;
pub use cgroup::Cgroup;
/// Contains all the subsystems that are available in this crate.
#[derive(Debug)]
pub enum Subsystem {
@@ -500,67 +502,3 @@ impl Subsystem {
}
}
}
#[cfg(test)]
mod tests {
use {Resources, PidResources, Hierarchy, Controller, Controllers, Subsystem};
use pid::{PidMax, PidController};
use cgroup::Cgroup;
#[test]
fn create_and_delete_cgroup() {
let hier = ::hierarchies::V1::new();
let cg = Cgroup::new(&hier, String::from("ltest2"));
{
let pidcontroller: &PidController = cg.controller_of().unwrap();
pidcontroller.set_pid_max(PidMax::Value(1337));
assert_eq!(pidcontroller.get_pid_max(), Some(PidMax::Value(1337)));
}
cg.delete();
}
#[test]
fn test_pid_pids_current_is_zero() {
let hier = ::hierarchies::V1::new();
let cg = Cgroup::new(&hier, String::from("ltest3"));
{
let pidcontroller: &PidController = cg.controller_of().unwrap();
assert_eq!(pidcontroller.get_pid_current(), 0);
}
cg.delete();
}
#[test]
fn test_pid_pids_events_is_zero() {
let hier = ::hierarchies::V1::new();
let cg = Cgroup::new(&hier, String::from("ltest4"));
{
let pidcontroller: &PidController = cg.controller_of().unwrap();
assert_eq!(pidcontroller.get_pid_events(), 0);
}
cg.delete();
}
#[test]
fn test_setting_resources() {
let hier = ::hierarchies::V1::new();
let cg = Cgroup::new(&hier, String::from("ltest5"));
{
let res = Resources {
pid: PidResources {
update_values: true,
maximum_number_of_processes: PidMax::Value(512),
},
..Default::default()
};
cg.apply(&res);
/* verify */
let pidcontroller: &PidController = cg.controller_of().unwrap();
assert_eq!(pidcontroller.get_pid_max(), Some(PidMax::Value(512)));
}
cg.delete();
}
}

View File

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

39
tests/pids.rs Normal file
View File

@@ -0,0 +1,39 @@
//! Integration tests about the pids subsystem
extern crate cgroups;
use cgroups::{Cgroup, Resources, PidResources};
use cgroups::pid::{PidController, PidMax};
#[test]
fn create_and_delete_cgroup() {
let hier = cgroups::hierarchies::V1::new();
let cg = Cgroup::new(&hier, String::from("create_and_delete_cgroup"));
{
let pidcontroller: &PidController = cg.controller_of().unwrap();
pidcontroller.set_pid_max(PidMax::Value(1337));
assert_eq!(pidcontroller.get_pid_max(), Some(PidMax::Value(1337)));
}
cg.delete();
}
#[test]
fn test_pid_pids_current_is_zero() {
let hier = cgroups::hierarchies::V1::new();
let cg = Cgroup::new(&hier, String::from("test_pid_pids_current_is_zero"));
{
let pidcontroller: &PidController = cg.controller_of().unwrap();
assert_eq!(pidcontroller.get_pid_current(), 0);
}
cg.delete();
}
#[test]
fn test_pid_pids_events_is_zero() {
let hier = cgroups::hierarchies::V1::new();
let cg = Cgroup::new(&hier, String::from("test_pid_pids_events_is_zero"));
{
let pidcontroller: &PidController = cg.controller_of().unwrap();
assert_eq!(pidcontroller.get_pid_events(), 0);
}
cg.delete();
}

26
tests/resources.rs Normal file
View File

@@ -0,0 +1,26 @@
//! Integration test about setting resources using `apply()`
extern crate cgroups;
use cgroups::{Cgroup, Resources, PidResources};
use cgroups::pid::{PidController, PidMax};
#[test]
fn pid_resources() {
let hier = cgroups::hierarchies::V1::new();
let cg = Cgroup::new(&hier, String::from("pid_resources"));
{
let res = Resources {
pid: PidResources {
update_values: true,
maximum_number_of_processes: PidMax::Value(512),
},
..Default::default()
};
cg.apply(&res);
/* verify */
let pidcontroller: &PidController = cg.controller_of().unwrap();
assert_eq!(pidcontroller.get_pid_max(), Some(PidMax::Value(512)));
}
cg.delete();
}

19
tools/create_cgroup.sh Executable file
View File

@@ -0,0 +1,19 @@
#!/bin/sh
CONTROL_GROUPS=`cargo test -- --list 2>/dev/null | egrep 'test$' | egrep -v '^src' | cut -d':' -f1`
echo This script will create a control group in every subsystem of the V1 hierarchy.
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.
sudo -v
for i in ${CONTROL_GROUPS}
do sudo mkdir -p /sys/fs/cgroup/{blkio,cpu,cpuacct,cpuset,devices,freezer,hugetlb,memory,net_cls,net_prio,perf_event,pids}/$i/
done
echo
echo We will now set up permissions...
echo
for i in ${CONTROL_GROUPS}
do sudo chown -R ${USER} /sys/fs/cgroup/{blkio,cpu,cpuacct,cpuset,devices,freezer,hugetlb,memory,net_cls,net_prio,perf_event,pids}/$i/
done

14
tools/delete_cgroup.sh Executable file
View File

@@ -0,0 +1,14 @@
#!/bin/sh
CONTROL_GROUPS=`cargo test -- --list 2>/dev/null | egrep 'test$' | egrep -v '^src' | cut -d':' -f1`
echo This script will delete the control groups created by the create_cgroup.sh shell script.
echo
echo It may spit out some errors, but that is fine.
echo
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.
sudo -v
for i in ${CONTROL_GROUPS}
do sudo rmdir /sys/fs/cgroup/{blkio,cpu,cpuacct,cpuset,devices,freezer,hugetlb,memory,net_cls,net_prio,perf_event,pids}/$i/
done