Skip to content

Commit

Permalink
refactor(test): refine function create_container (youki-dev#2973)
Browse files Browse the repository at this point in the history
* refactor(test): refine function create_container

add CreateOptions as para to function create_container for create
container with different options

Signed-off-by: xujihui1985 <xujihui1985@gmail.com>

* fix: fix failed unittest

Signed-off-by: xujihui1985 <xujihui1985@gmail.com>

---------

Signed-off-by: xujihui1985 <xujihui1985@gmail.com>
Signed-off-by: Akiyama <akiakiskyhand@gmail.com>
  • Loading branch information
xujihui1985 authored and YamasouA committed Dec 7, 2024
1 parent 9a7c592 commit bebee51
Show file tree
Hide file tree
Showing 17 changed files with 85 additions and 147 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
3 changes: 2 additions & 1 deletion tests/contest/contest/src/tests/process/process_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,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;

fn create_spec() -> Result<Spec> {
let mut process = ProcessBuilder::default()
Expand All @@ -28,7 +29,7 @@ fn create_spec() -> Result<Spec> {
fn process_test() -> TestResult {
let spec = test_result!(create_spec());

test_inside_container(spec, &|bundle| {
test_inside_container(spec, &CreateOptions::default(), &|bundle| {
match fs::create_dir(bundle.join("test")) {
Result::Ok(_) => { /*This is expected*/ }
Err(e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rand::Rng;
use test_framework::{test_result, Test, TestGroup, TestResult};

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

fn generate_random_number() -> i32 {
let mut rng = rand::thread_rng();
Expand All @@ -30,7 +31,7 @@ fn create_spec() -> Result<Spec> {

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

pub fn get_process_oom_score_adj_test() -> TestGroup {
Expand Down
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;

const GIGABYTES: u64 = 1024 * 1024 * 1024;

Expand Down Expand Up @@ -54,7 +55,7 @@ fn create_spec() -> Result<Spec> {

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

pub fn get_process_rlimits_test() -> TestGroup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rand::Rng;
use test_framework::{test_result, Test, TestGroup, TestResult};

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

// Generates a Vec<u32> with a random number of elements (between 5 and 15),
// where each element is a random u32 value between 0 and 65535.
Expand Down Expand Up @@ -43,7 +44,7 @@ fn create_spec() -> Result<Spec> {
}
fn process_user_test() -> TestResult {
let spec = test_result!(create_spec());
test_inside_container(spec, &|_| Ok(()))
test_inside_container(spec, &CreateOptions::default(), &|_| Ok(()))
}

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

0 comments on commit bebee51

Please sign in to comment.