Skip to content

Commit

Permalink
refactor(test): refine function create_container
Browse files Browse the repository at this point in the history
add CreateOptions as para to function create_container for create
container with different options

Signed-off-by: xujihui1985 <[email protected]>
  • Loading branch information
xujihui1985 committed Nov 25, 2024
1 parent 3b78801 commit 17f2131
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 143 deletions.
3 changes: 2 additions & 1 deletion tests/contest/contest/src/tests/devices/devices_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use oci_spec::runtime::{
use test_framework::{test_result, Test, TestGroup, TestResult};

use crate::utils::test_inside_container;
use crate::utils::test_utils::CreateOptions;

fn create_spec() -> Result<Spec> {
let device1 = LinuxDeviceBuilder::default()
Expand Down Expand Up @@ -59,7 +60,7 @@ fn create_spec() -> Result<Spec> {

fn devices_test() -> TestResult {
let spec = test_result!(create_spec());
test_inside_container(spec, &|_| Ok(()))
test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()))
}

pub fn get_devices_test() -> TestGroup {
Expand Down
3 changes: 2 additions & 1 deletion tests/contest/contest/src/tests/domainname/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder};
use test_framework::{ConditionalTest, TestGroup, TestResult};

use crate::utils::test_utils::CreateOptions;
use crate::utils::{is_runtime_runc, test_inside_container};

fn get_spec(domainname: &str) -> Spec {
Expand All @@ -21,7 +22,7 @@ fn get_spec(domainname: &str) -> Spec {

fn set_domainname_test() -> TestResult {
let spec = get_spec("domainname");
test_inside_container(spec, &|_| Ok(()))
test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()))
}

pub fn get_domainname_tests() -> TestGroup {
Expand Down
3 changes: 2 additions & 1 deletion tests/contest/contest/src/tests/example/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder};
use test_framework::{test_result, Test, TestGroup, TestResult};

use crate::utils::test_inside_container;
use crate::utils::test_utils::CreateOptions;

////////// ANCHOR: get_example_spec
fn create_spec() -> Result<Spec> {
Expand All @@ -25,7 +26,7 @@ fn create_spec() -> Result<Spec> {
////////// ANCHOR: example_test
fn example_test() -> TestResult {
let spec = test_result!(create_spec());
test_inside_container(spec, &|_| Ok(()))
test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()))
}
////////// ANCHOR_END: example_test

Expand Down
7 changes: 5 additions & 2 deletions tests/contest/contest/src/tests/hooks/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::anyhow;
use oci_spec::runtime::{Hook, HookBuilder, HooksBuilder, ProcessBuilder, Spec, SpecBuilder};
use test_framework::{Test, TestGroup, TestResult};

use crate::utils::test_utils::start_container;
use crate::utils::test_utils::{start_container, CreateOptions};
use crate::utils::{create_container, delete_container, generate_uuid, prepare_bundle, set_config};

const HOOK_OUTPUT_FILE: &str = "output";
Expand Down Expand Up @@ -71,7 +71,10 @@ fn get_test(test_name: &'static str) -> Test {
let id_str = id.to_string();
let bundle = prepare_bundle().unwrap();
set_config(&bundle, &spec).unwrap();
create_container(&id_str, &bundle).unwrap().wait().unwrap();
create_container(&id_str, &bundle, &CreateOptions::default())
.unwrap()
.wait()
.unwrap();
start_container(&id_str, &bundle).unwrap().wait().unwrap();
delete_container(&id_str, &bundle).unwrap().wait().unwrap();
let log = {
Expand Down
5 changes: 3 additions & 2 deletions tests/contest/contest/src/tests/hostname/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use oci_spec::runtime::{LinuxBuilder, ProcessBuilder, Spec, SpecBuilder};
use test_framework::{Test, TestGroup, TestResult};

use crate::utils::test_inside_container;
use crate::utils::test_utils::CreateOptions;

fn create_spec(hostname: &str) -> Spec {
SpecBuilder::default()
Expand All @@ -25,7 +26,7 @@ fn create_spec(hostname: &str) -> Spec {

fn hostname_test() -> TestResult {
let spec = create_spec("hostname-specific");
test_inside_container(spec, &|_| {
test_inside_container(spec, &CreateOptions::default(), &|_| {
// As long as the container is created, we expect the hostname to be determined
// by the spec, so nothing to prepare prior.
Ok(())
Expand All @@ -34,7 +35,7 @@ fn hostname_test() -> TestResult {

fn empty_hostname() -> TestResult {
let spec = create_spec("");
test_inside_container(spec, &|_| {
test_inside_container(spec, &CreateOptions::default(), &|_| {
// As long as the container is created, we expect the hostname to be determined
// by the spec, so nothing to prepare prior.
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use oci_spec::runtime::{
};
use test_framework::{test_result, ConditionalTest, TestGroup, TestResult};

use crate::utils::test_utils::CreateOptions;
use crate::utils::{is_runtime_runc, test_inside_container};

fn create_spec(
Expand Down Expand Up @@ -38,7 +39,7 @@ fn io_priority_class_rt_test() -> TestResult {
"io_priority_class_rt",
1,
));
test_inside_container(spec, &|_| Ok(()))
test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()))
}

fn io_priority_class_be_test() -> TestResult {
Expand All @@ -47,7 +48,7 @@ fn io_priority_class_be_test() -> TestResult {
"io_priority_class_be",
2,
));
test_inside_container(spec, &|_| Ok(()))
test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()))
}

fn io_priority_class_idle_test() -> TestResult {
Expand All @@ -56,7 +57,7 @@ fn io_priority_class_idle_test() -> TestResult {
"io_priority_class_idle",
3,
));
test_inside_container(spec, &|_| Ok(()))
test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()))
}

pub fn get_io_priority_test() -> TestGroup {
Expand Down
33 changes: 17 additions & 16 deletions tests/contest/contest/src/tests/mounts_recursive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use oci_spec::runtime::{
use test_framework::{Test, TestGroup, TestResult};

use crate::utils::test_inside_container;
use crate::utils::test_utils::CreateOptions;

fn get_spec(added_mounts: Vec<Mount>, process_args: Vec<String>) -> Spec {
let mut mounts = get_default_mounts();
Expand Down Expand Up @@ -112,7 +113,7 @@ fn check_recursive_readonly() -> TestResult {
vec!["runtimetest".to_string(), "mounts_recursive".to_string()],
);

let result = test_inside_container(spec, &|_| {
let result = test_inside_container(spec, &CreateOptions::default(), &|_| {
setup_mount(&rro_dir_path, &rro_subdir_path);
Ok(())
});
Expand Down Expand Up @@ -152,7 +153,7 @@ fn check_recursive_nosuid() -> TestResult {
],
);

let result = test_inside_container(spec, &|bundle_path| {
let result = test_inside_container(spec, &CreateOptions::default(), &|bundle_path| {
setup_mount(&rnosuid_dir_path, &rnosuid_subdir_path);

let executable_file_path = bundle_path.join("bin").join(executable_file_name);
Expand Down Expand Up @@ -225,7 +226,7 @@ fn check_recursive_rsuid() -> TestResult {
vec![mount_spec],
vec!["runtimetest".to_string(), "mounts_recursive".to_string()],
);
test_inside_container(spec, &|_| {
test_inside_container(spec, &CreateOptions::default(), &|_| {
let original_file_path = rsuid_dir_path.join("file");
let file = File::create(original_file_path)?;
let mut permission = file.metadata()?.permissions();
Expand Down Expand Up @@ -256,7 +257,7 @@ fn check_recursive_noexec() -> TestResult {
vec!["runtimetest".to_string(), "mounts_recursive".to_string()],
);

let result = test_inside_container(spec, &|bundle_path| {
let result = test_inside_container(spec, &CreateOptions::default(), &|bundle_path| {
setup_mount(&rnoexec_dir_path, &rnoexec_subdir_path);

let executable_file_name = "echo";
Expand Down Expand Up @@ -297,7 +298,7 @@ fn check_recursive_rexec() -> TestResult {
vec!["runtimetest".to_string(), "mounts_recursive".to_string()],
);

let result = test_inside_container(spec, &|bundle_path| {
let result = test_inside_container(spec, &CreateOptions::default(), &|bundle_path| {
setup_mount(&rnoexec_dir_path, &rnoexec_subdir_path);

let executable_file_name = "echo";
Expand Down Expand Up @@ -338,7 +339,7 @@ fn check_recursive_rdiratime() -> TestResult {
vec!["runtimetest".to_string(), "mounts_recursive".to_string()],
);

let result = test_inside_container(spec, &|_| Ok(()));
let result = test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()));

fs::remove_dir(rdiratime_base_dir).unwrap();
result
Expand All @@ -362,7 +363,7 @@ fn check_recursive_rnodiratime() -> TestResult {
vec!["runtimetest".to_string(), "mounts_recursive".to_string()],
);

let result = test_inside_container(spec, &|_| Ok(()));
let result = test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()));
fs::remove_dir(rnodiratime_base_dir).unwrap();
result
}
Expand All @@ -383,7 +384,7 @@ fn check_recursive_rdev() -> TestResult {
vec!["runtimetest".to_string(), "mounts_recursive".to_string()],
);

test_inside_container(spec, &|_| Ok(()))
test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()))
}

fn check_recursive_rnodev() -> TestResult {
Expand All @@ -402,7 +403,7 @@ fn check_recursive_rnodev() -> TestResult {
vec!["runtimetest".to_string(), "mounts_recursive".to_string()],
);

test_inside_container(spec, &|_| Ok(()))
test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()))
}

fn check_recursive_readwrite() -> TestResult {
Expand All @@ -423,7 +424,7 @@ fn check_recursive_readwrite() -> TestResult {
vec!["runtimetest".to_string(), "mounts_recursive".to_string()],
);

let result = test_inside_container(spec, &|_| {
let result = test_inside_container(spec, &CreateOptions::default(), &|_| {
setup_mount(&rrw_dir_path, &rrw_subdir_path);
Ok(())
});
Expand Down Expand Up @@ -451,7 +452,7 @@ fn check_recursive_rrelatime() -> TestResult {
vec![mount_spec],
vec!["runtimetest".to_string(), "mounts_recursive".to_string()],
);
let result = test_inside_container(spec, &|_| Ok(()));
let result = test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()));

fs::remove_dir_all(rrelatime_dir_path).unwrap();
result
Expand All @@ -475,7 +476,7 @@ fn check_recursive_rnorelatime() -> TestResult {
vec!["runtimetest".to_string(), "mounts_recursive".to_string()],
);

let result = test_inside_container(spec, &|_| Ok(()));
let result = test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()));

fs::remove_dir_all(rnorelatime_dir_path).unwrap();
result
Expand All @@ -499,7 +500,7 @@ fn check_recursive_rnoatime() -> TestResult {
vec!["runtimetest".to_string(), "mounts_recursive".to_string()],
);

let result = test_inside_container(spec, &|_| Ok(()));
let result = test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()));

fs::remove_dir_all(rnoatime_dir_path).unwrap();
result
Expand All @@ -522,7 +523,7 @@ fn check_recursive_rstrictatime() -> TestResult {
vec![mount_spec],
vec!["runtimetest".to_string(), "mounts_recursive".to_string()],
);
let result = test_inside_container(spec, &|_| Ok(()));
let result = test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()));

fs::remove_dir_all(rstrictatime_dir_path).unwrap();
result
Expand All @@ -548,7 +549,7 @@ fn check_recursive_rnosymfollow() -> TestResult {
vec![mount_spec],
vec!["runtimetest".to_string(), "mounts_recursive".to_string()],
);
let result = test_inside_container(spec, &|_| {
let result = test_inside_container(spec, &CreateOptions::default(), &|_| {
let original_file_path = format!("{}/{}", rnosymfollow_dir_path.to_str().unwrap(), "file");
let file = File::create(&original_file_path)?;
let link_file_path = format!("{}/{}", rnosymfollow_dir_path.to_str().unwrap(), "link");
Expand Down Expand Up @@ -587,7 +588,7 @@ fn check_recursive_rsymfollow() -> TestResult {
vec![mount_spec],
vec!["runtimetest".to_string(), "mounts_recursive".to_string()],
);
let result = test_inside_container(spec, &|_| {
let result = test_inside_container(spec, &CreateOptions::default(), &|_| {
let original_file_path = format!("{}/{}", rsymfollow_dir_path.to_str().unwrap(), "file");
let file = File::create(&original_file_path)?;
let link_file_path = format!("{}/{}", rsymfollow_dir_path.to_str().unwrap(), "link");
Expand Down
8 changes: 6 additions & 2 deletions tests/contest/contest/src/tests/no_pivot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{Context, Result};
use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder};
use test_framework::{test_result, Test, TestGroup, TestResult};

use crate::utils::test_utils::test_inside_container_with_no_pivot;
use crate::utils::test_utils::{test_inside_container, CreateOptions};

fn create_spec() -> Result<Spec> {
SpecBuilder::default()
Expand All @@ -17,7 +17,11 @@ fn create_spec() -> Result<Spec> {

fn no_pivot_test() -> TestResult {
let spec = test_result!(create_spec());
test_inside_container_with_no_pivot(spec, &|_| Ok(()))
test_inside_container(
spec,
&CreateOptions::default().with_no_pivot_root(),
&|_| Ok(()),
)
}

pub fn get_no_pivot_test() -> TestGroup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use oci_spec::runtime::{LinuxBuilder, ProcessBuilder, Spec, SpecBuilder};
use test_framework::{Test, TestGroup, TestResult};

use crate::utils::test_inside_container;
use crate::utils::test_utils::CreateOptions;

fn get_spec(readonly_paths: Vec<String>) -> Spec {
SpecBuilder::default()
Expand Down Expand Up @@ -60,7 +61,7 @@ fn check_readonly_paths() -> TestResult {
];

let spec = get_spec(ro_paths);
test_inside_container(spec, &|bundle_path| {
test_inside_container(spec, &CreateOptions::default(), &|bundle_path| {
use std::{fs, io};
let test_dir = bundle_path.join(&ro_dir_sub);

Expand Down Expand Up @@ -111,7 +112,7 @@ fn check_readonly_rel_path() -> TestResult {
let ro_paths = vec![ro_rel_path.to_string()];
let spec = get_spec(ro_paths);

test_inside_container(spec, &|bundle_path| {
test_inside_container(spec, &CreateOptions::default(), &|bundle_path| {
use std::{fs, io};
let test_file = bundle_path.join(ro_rel_path);

Expand Down Expand Up @@ -142,7 +143,7 @@ fn check_readonly_symlinks() -> TestResult {

let spec = get_spec(ro_paths);

let res = test_inside_container(spec, &|bundle_path| {
let res = test_inside_container(spec, &CreateOptions::default(), &|bundle_path| {
use std::{fs, io};
let test_file = bundle_path.join(ro_symlink);

Expand Down Expand Up @@ -193,7 +194,7 @@ fn test_node(mode: u32) -> TestResult {

let spec = get_spec(ro_paths);

test_inside_container(spec, &|bundle_path| {
test_inside_container(spec, &CreateOptions::default(), &|bundle_path| {
use std::os::unix::fs::OpenOptionsExt;
use std::{fs, io};
let test_file = bundle_path.join(ro_device);
Expand Down
5 changes: 3 additions & 2 deletions tests/contest/contest/src/tests/scheduler/scheduler_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use oci_spec::runtime::{
};
use test_framework::{test_result, ConditionalTest, TestGroup, TestResult};

use crate::utils::test_utils::CreateOptions;
use crate::utils::{is_runtime_runc, test_inside_container};

fn create_spec(policy: LinuxSchedulerPolicy, execute_test: &str) -> Result<Spec> {
Expand Down Expand Up @@ -33,15 +34,15 @@ fn scheduler_policy_other_test() -> TestResult {
LinuxSchedulerPolicy::SchedOther,
"scheduler_policy_other"
));
test_inside_container(spec, &|_| Ok(()))
test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()))
}

fn scheduler_policy_batch_test() -> TestResult {
let spec = test_result!(create_spec(
LinuxSchedulerPolicy::SchedBatch,
"scheduler_policy_batch"
));
test_inside_container(spec, &|_| Ok(()))
test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()))
}

pub fn get_scheduler_test() -> TestGroup {
Expand Down
3 changes: 2 additions & 1 deletion tests/contest/contest/src/tests/seccomp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use oci_spec::runtime::{
use test_framework::{Test, TestGroup, TestResult};

use crate::utils::test_inside_container;
use crate::utils::test_utils::CreateOptions;

fn create_spec(seccomp: LinuxSeccomp) -> Spec {
SpecBuilder::default()
Expand Down Expand Up @@ -36,7 +37,7 @@ fn seccomp_test() -> TestResult {
.build()
.unwrap(),
);
test_inside_container(spec, &|_| Ok(()))
test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()))
}

pub fn get_seccomp_test() -> TestGroup {
Expand Down
Loading

0 comments on commit 17f2131

Please sign in to comment.