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 process test #2968

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions tests/contest/contest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::tests::linux_ns_itype::get_ns_itype_tests;
use crate::tests::mounts_recursive::get_mounts_recursive_test;
use crate::tests::no_pivot::get_no_pivot_test;
use crate::tests::pidfile::get_pidfile_test;
use crate::tests::process::get_process_test;
use crate::tests::readonly_paths::get_ro_paths_test;
use crate::tests::scheduler::get_scheduler_test;
use crate::tests::seccomp::get_seccomp_test;
Expand Down Expand Up @@ -114,6 +115,7 @@ fn main() -> Result<()> {
let scheduler = get_scheduler_test();
let io_priority_test = get_io_priority_test();
let devices = get_devices_test();
let process = get_process_test();
let no_pivot = get_no_pivot_test();

tm.add_test_group(Box::new(cl));
Expand All @@ -138,6 +140,7 @@ fn main() -> Result<()> {
tm.add_test_group(Box::new(sysctl));
tm.add_test_group(Box::new(scheduler));
tm.add_test_group(Box::new(devices));
tm.add_test_group(Box::new(process));
tm.add_test_group(Box::new(no_pivot));

tm.add_test_group(Box::new(io_priority_test));
Expand Down
1 change: 1 addition & 0 deletions tests/contest/contest/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod linux_ns_itype;
pub mod mounts_recursive;
pub mod no_pivot;
pub mod pidfile;
pub mod process;
pub mod readonly_paths;
pub mod scheduler;
pub mod seccomp;
Expand Down
2 changes: 2 additions & 0 deletions tests/contest/contest/src/tests/process/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod process_test;
pub use process_test::get_process_test;
47 changes: 47 additions & 0 deletions tests/contest/contest/src/tests/process/process_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::utils::test_inside_container;

Check warning on line 1 in tests/contest/contest/src/tests/process/process_test.rs

View workflow job for this annotation

GitHub Actions / check (x86_64, gnu)

Diff in /home/runner/work/youki/youki/tests/contest/contest/src/tests/process/process_test.rs

Check warning on line 1 in tests/contest/contest/src/tests/process/process_test.rs

View workflow job for this annotation

GitHub Actions / check (x86_64, gnu)

Diff in /home/runner/work/youki/youki/tests/contest/contest/src/tests/process/process_test.rs

Check warning on line 1 in tests/contest/contest/src/tests/process/process_test.rs

View workflow job for this annotation

GitHub Actions / check (x86_64, musl)

Diff in /home/runner/work/youki/youki/tests/contest/contest/src/tests/process/process_test.rs

Check warning on line 1 in tests/contest/contest/src/tests/process/process_test.rs

View workflow job for this annotation

GitHub Actions / check (x86_64, musl)

Diff in /home/runner/work/youki/youki/tests/contest/contest/src/tests/process/process_test.rs

Check warning on line 1 in tests/contest/contest/src/tests/process/process_test.rs

View workflow job for this annotation

GitHub Actions / check (aarch64, musl)

Diff in /home/runner/work/youki/youki/tests/contest/contest/src/tests/process/process_test.rs

Check warning on line 1 in tests/contest/contest/src/tests/process/process_test.rs

View workflow job for this annotation

GitHub Actions / check (aarch64, musl)

Diff in /home/runner/work/youki/youki/tests/contest/contest/src/tests/process/process_test.rs
use anyhow::{bail, Context, Ok, Result};
use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder};
use std::fs;
use std::os::unix::fs::PermissionsExt;
use test_framework::{test_result, Test, TestGroup, TestResult};

fn create_spec() -> Result<Spec> {
let spec = SpecBuilder::default()
.process(
ProcessBuilder::default()
.cwd("/test")
.env(vec!["testa=valuea".into(), "testb=123".into()])
.build()
.expect("error in creating process config"),
)
.build()
.context("failed to build spec")?;

Ok(spec)
}

fn process_test() -> TestResult {
let spec = test_result!(create_spec());
test_inside_container(spec, &|_| {
match fs::create_dir("/test") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be /test, you should take the bundle param in the closure and append /test and create that dir. This callback is run in host's fs before running the container, so you should be using bundle path and create this dir.

Result::Ok(_) => { /*This is expected*/ }
Err(e) => {
bail!(e)
}
}
let metadata = fs::metadata("/test")?;
let mut permissions = metadata.permissions();
permissions.set_mode(0o700);

Ok(())
})
}

pub fn get_process_test() -> TestGroup {
let mut process_test_group = TestGroup::new("process");

let test = Test::new("process_test", Box::new(process_test));
process_test_group.add(vec![Box::new(test)]);

process_test_group
}
2 changes: 1 addition & 1 deletion tests/contest/runtimetest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ fn get_spec() -> Spec {
}
}

////////// ANCHOR: example_runtimetest_main
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this line removed here?

fn main() {
let spec = get_spec();
let args: Vec<String> = env::args().collect();
Expand All @@ -44,6 +43,7 @@ fn main() {
"io_priority_class_be" => tests::test_io_priority_class(&spec, IoprioClassBe),
"io_priority_class_idle" => tests::test_io_priority_class(&spec, IoprioClassIdle),
"devices" => tests::validate_devices(&spec),
"process" => tests::validate_process(&spec),
"no_pivot" => tests::validate_rootfs(),
_ => eprintln!("error due to unexpected execute test name: {execute_test}"),
}
Expand Down
30 changes: 30 additions & 0 deletions tests/contest/runtimetest/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::env;
use std::fs::{self, read_dir};
use std::os::linux::fs::MetadataExt;
use std::os::unix::fs::{FileTypeExt, PermissionsExt};
use std::path::Path;

Check warning on line 5 in tests/contest/runtimetest/src/tests.rs

View workflow job for this annotation

GitHub Actions / check (x86_64, gnu)

Diff in /home/runner/work/youki/youki/tests/contest/runtimetest/src/tests.rs

Check warning on line 5 in tests/contest/runtimetest/src/tests.rs

View workflow job for this annotation

GitHub Actions / check (x86_64, musl)

Diff in /home/runner/work/youki/youki/tests/contest/runtimetest/src/tests.rs

Check warning on line 5 in tests/contest/runtimetest/src/tests.rs

View workflow job for this annotation

GitHub Actions / check (aarch64, musl)

Diff in /home/runner/work/youki/youki/tests/contest/runtimetest/src/tests.rs

use anyhow::{bail, Result};

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this extra blank line.

use nix::errno::Errno;
use nix::libc;
use nix::sys::utsname;
Expand Down Expand Up @@ -546,6 +548,34 @@
}
}

pub fn validate_process(spec: &Spec) {
let process = spec.process().as_ref().unwrap();

if process.cwd().ne(&getcwd().unwrap()) {
eprintln!(
"error due to spec cwd want {:?}, got {:?}",
process.cwd(),
getcwd().unwrap()
)
}

if env::var("testa").unwrap().to_string().ne("valuea") {
eprintln!(
"error due to spec environment value of testa want {:?}, got {:?}",
"valuea",
env::var("testa")
)
}

if env::var("testb").unwrap().to_string().ne("123") {
eprintln!(
"error due to spec environment value of testb want {:?}, got {:?}",
"123",
env::var("testb")
)
}
}
Comment on lines +562 to +577
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for this as well, we should cross check this with env from the spec, similar to how the go test is doing.


// the validate_rootfs function is used to validate the rootfs of the container is
// as expected. This function is used in the no_pivot test to validate the rootfs
pub fn validate_rootfs() {
Expand Down
Loading