Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ns_itype test #389

Merged
merged 4 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 24 additions & 25 deletions youki_integration_test/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion youki_integration_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ flate2 = "1.0"
test_framework = { path = "./test_framework"}
anyhow = "1.0"
once_cell = "1.8.0"
oci-spec = "0.5.1"
oci-spec = { git = "https://github.com/containers/oci-spec-rs", rev = "3d5132a18c305be59d58187201429d8f0243b513" }
which = "4.2.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
4 changes: 3 additions & 1 deletion youki_integration_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod tests;
mod utils;

use crate::tests::lifecycle::{ContainerCreate, ContainerLifecycle};
use crate::tests::linux_ns_itype::get_ns_itype_tests;
use crate::tests::pidfile::get_pidfile_test;
use crate::tests::tlb::get_tlb_test;
use crate::utils::support::set_runtime_path;
Expand Down Expand Up @@ -54,19 +55,20 @@ fn main() -> Result<()> {
}
},
}

let mut tm = TestManager::new();

let cl = ContainerLifecycle::new();
let cc = ContainerCreate::new();
let huge_tlb = get_tlb_test();
let pidfile = get_pidfile_test();
let ns_itype = get_ns_itype_tests();
let cgroup_v1_pids = cgroups::pids::get_test_group();

tm.add_test_group(&cl);
tm.add_test_group(&cc);
tm.add_test_group(&huge_tlb);
tm.add_test_group(&pidfile);
tm.add_test_group(&ns_itype);
tm.add_test_group(&cgroup_v1_pids);

tm.add_cleanup(Box::new(cgroups::cleanup));
Expand Down
2 changes: 2 additions & 0 deletions youki_integration_test/src/tests/linux_ns_itype/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod ns_itype_test;
pub use ns_itype_test::get_ns_itype_tests;
73 changes: 73 additions & 0 deletions youki_integration_test/src/tests/linux_ns_itype/ns_itype_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use crate::utils::test_outside_container;
use anyhow::anyhow;
use oci_spec::runtime::LinuxBuilder;
use oci_spec::runtime::{Spec, SpecBuilder};
use procfs::process::Process;
use test_framework::{Test, TestGroup, TestResult};

// get spec for the test
fn get_spec() -> Spec {
let mut r = SpecBuilder::default()
.linux(
LinuxBuilder::default()
.namespaces(
// we have to remove all namespaces, so we directly
// provide an empty vec here
vec![],
)
// if these both are not empty, we cannot set a inherited
// mnt namespace, as these both require a private mnt namespace
.masked_paths(vec![])
.readonly_paths(vec![])
.build()
.expect("could not build spec"),
)
.build()
.unwrap();
// We need to remove hostname to avoid test failures when not creating UTS namespace
r.set_hostname(None);
r
}

fn get_test<'a>(test_name: &'static str) -> Test<'a> {
Test::new(
test_name,
Box::new(move || {
let host_proc = Process::myself().expect("error in getting /proc/self");
let host_namespaces = match host_proc.namespaces() {
Ok(n) => n,
Err(e) => {
return TestResult::Failed(anyhow!(
"error in resolving host namespaces : {}",
e
))
}
};
let spec = get_spec();
test_outside_container(spec, &move |data| {
let pid = match data.state {
Some(s) => s.pid.unwrap(),
None => return TestResult::Failed(anyhow!("state command returned error")),
};
let container_process =
Process::new(pid).expect("error in getting /proc for container process");
let container_namespaces = container_process
.namespaces()
.expect("error in getting namespaces of container process");
if container_namespaces != host_namespaces {
return TestResult::Failed(anyhow!(
"error : namespaces are not correctly inherited"
));
}
TestResult::Passed
})
}),
)
}

pub fn get_ns_itype_tests<'a>() -> TestGroup<'a> {
let mut tg = TestGroup::new("ns_itype");
let tests: Vec<_> = vec![Box::new(get_test("ns_itype"))];
tg.add(tests);
tg
}
1 change: 1 addition & 0 deletions youki_integration_test/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod cgroups;
pub mod lifecycle;
pub mod linux_ns_itype;
pub mod pidfile;
pub mod tlb;
8 changes: 4 additions & 4 deletions youki_integration_test/src/tests/pidfile/pidfile_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ fn test_pidfile() -> TestResult {

if !err.is_empty() {
cleanup(&container_id, &bundle);
return TestResult::Failed(anyhow!("Error in state : {}", err));
return TestResult::Failed(anyhow!("error in state : {}", err));
}

let state: State = serde_json::from_str(&out).unwrap();

if state.id != container_id.to_string() {
cleanup(&container_id, &bundle);
return TestResult::Failed(anyhow!(
"Error in state : ID not matched ,expected {} got {}",
"error in state : id not matched ,expected {} got {}",
container_id,
state.id
));
Expand All @@ -63,7 +63,7 @@ fn test_pidfile() -> TestResult {
if state.status != "created" {
cleanup(&container_id, &bundle);
return TestResult::Failed(anyhow!(
"Error in state : Status not matched ,expected 'created' got {}",
"error in state : status not matched ,expected 'created' got {}",
state.status
));
}
Expand All @@ -78,7 +78,7 @@ fn test_pidfile() -> TestResult {
if state.pid.unwrap() != pidfile {
cleanup(&container_id, &bundle);
return TestResult::Failed(anyhow!(
"Error : Pid not matched ,expected {} as per state, but got {} from pidfile instead",
"error : pid not matched ,expected {} as per state, but got {} from pidfile instead",
state.pid.unwrap(),
pidfile
));
Expand Down
16 changes: 9 additions & 7 deletions youki_integration_test/src/tests/tlb/tlb_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use oci_spec::runtime::LinuxBuilder;
use oci_spec::runtime::{LinuxHugepageLimitBuilder, LinuxResourcesBuilder};
use oci_spec::runtime::{Spec, SpecBuilder};
use std::path::PathBuf;
use test_framework::{ConditionalTest, TestGroup, TestResult};
use test_framework::{test_result, ConditionalTest, TestGroup, TestResult};

fn check_hugetlb() -> bool {
PathBuf::from("/sys/fs/cgroup/hugetlb").exists()
Expand All @@ -21,7 +21,7 @@ fn make_hugetlb_spec(page_size: &str, limit: i64) -> Spec {
.page_size(page_size.to_owned())
.limit(limit)
.build()
.expect("Could not build")])
.expect("could not build")])
.build()
.unwrap(),
)
Expand Down Expand Up @@ -52,7 +52,7 @@ fn test_wrong_tlb() -> TestResult {
}
if res.success() {
// The operation should not have succeeded as pagesize was not power of 2
TestResult::Failed(anyhow!("Invalid page size of {} was allowed", page))
TestResult::Failed(anyhow!("invalid page size of {} was allowed", page))
} else {
TestResult::Passed
}
Expand All @@ -77,8 +77,10 @@ fn extract_page_size(dir_name: &str) -> String {

fn get_tlb_sizes() -> Vec<String> {
let mut sizes = Vec::new();
for hugetlb_entry in std::fs::read_dir("/sys/kernel/mm/hugepages").unwrap() {
let hugetlb_entry = hugetlb_entry.unwrap();
for hugetlb_entry in std::fs::read_dir("/sys/kernel/mm/hugepages")
.expect("error in reading /sys/kernel/mm/hugepages")
{
let hugetlb_entry = hugetlb_entry.expect("error in reading /sys/kernel/mm/hugepages entry");
if !hugetlb_entry.path().is_dir() {
continue;
}
Expand All @@ -100,7 +102,7 @@ fn validate_tlb(id: &str, size: &str, limit: i64) -> TestResult {
TestResult::Passed
} else {
TestResult::Failed(anyhow!(
"Page limit not set correctly : for size {}, expected {}, got {}",
"page limit not set correctly : for size {}, expected {}, got {}",
size,
limit,
val
Expand All @@ -117,7 +119,7 @@ fn test_valid_tlb() -> TestResult {
for size in tlb_sizes.iter() {
let spec = make_hugetlb_spec(size, limit);
let res = test_outside_container(spec, &|data| {
check_container_created(&data).unwrap();
test_result!(check_container_created(&data));

let r = validate_tlb(&data.id, size, limit);
if matches!(r, TestResult::Failed(_)) {
Expand Down
1 change: 1 addition & 0 deletions youki_integration_test/src/utils/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct State {
pub use_systemd: Option<bool>,
}

#[derive(Debug)]
pub struct ContainerData {
pub id: String,
pub state: Option<State>,
Expand Down