Skip to content

Commit

Permalink
Add domainname test (#1544)
Browse files Browse the repository at this point in the history
* Add domainname test

Signed-off-by: higuruchi <[email protected]>

* Fix error message from binary to string

Signed-off-by: higuruchi <[email protected]>

* Update runc binary to v1.1.8 from 1.1.0 for domainname test

Signed-off-by: higuruchi <[email protected]>

* Move missing domainname test

Signed-off-by: Yashodhan Joshi <[email protected]>

* Add TODO comments for self-notes

Signed-off-by: Yashodhan Joshi <[email protected]>

---------

Signed-off-by: higuruchi <[email protected]>
Signed-off-by: Yashodhan Joshi <[email protected]>
Co-authored-by: Yashodhan Joshi <[email protected]>
  • Loading branch information
higuruchi and YJDoc2 authored Jan 21, 2024
1 parent 2723aa4 commit c2e8670
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 18 deletions.
37 changes: 19 additions & 18 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions tests/contest/contest/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod tests;
mod utils;

use crate::tests::domainname::get_domainname_tests;
use crate::tests::example::get_example_test;
use crate::tests::hooks::get_hooks_tests;
use crate::tests::hostname::get_hostname_test;
Expand Down Expand Up @@ -102,6 +103,8 @@ fn main() -> Result<()> {
let ro_paths = get_ro_paths_test();
let hostname = get_hostname_test();
let mounts_recursive = get_mounts_recursive_test();
#[allow(unused_variables)]
let domainname = get_domainname_tests();
let intel_rdt = get_intel_rdt_test();
let sysctl = get_sysctl_test();
#[allow(unused_variables)]
Expand All @@ -124,6 +127,7 @@ fn main() -> Result<()> {
tm.add_test_group(Box::new(ro_paths));
tm.add_test_group(Box::new(hostname));
tm.add_test_group(Box::new(mounts_recursive));
// tm.add_test_group(Box::new(domainname)); // TODO (YJDoc2) fix in #2616
tm.add_test_group(Box::new(intel_rdt));
tm.add_test_group(Box::new(sysctl));
// tm.add_test_group(Box::new(scheduler));
Expand Down
32 changes: 32 additions & 0 deletions tests/contest/contest/src/tests/domainname/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::utils::test_inside_container;
use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder};
use test_framework::{Test, TestGroup, TestResult};

fn get_spec(domainname: &str) -> Spec {
SpecBuilder::default()
.domainname(domainname)
.process(
ProcessBuilder::default()
.args(vec![
"runtimetest".to_string(),
"domainname_test".to_string(),
])
.build()
.expect("error in creating process config"),
)
.build()
.unwrap()
}

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

pub fn get_domainname_tests() -> TestGroup {
let mut tg = TestGroup::new("domainname_test");
let set_domainname_test = Test::new("set_domainname_test", Box::new(set_domainname_test));
tg.add(vec![Box::new(set_domainname_test)]);

tg
}
1 change: 1 addition & 0 deletions tests/contest/contest/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod cgroups;
pub mod domainname;
pub mod example;
pub mod hooks;
pub mod hostname;
Expand Down
1 change: 1 addition & 0 deletions tests/contest/runtimetest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ edition = "2021"
oci-spec = { version = "0.6.4", features = ["runtime"] }
nix = "0.27.1"
anyhow = "1.0"
libc = "0.2.139" # TODO (YJDoc2) upgrade to latest
nc = "0.8.18"

1 change: 1 addition & 0 deletions tests/contest/runtimetest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn main() {
"readonly_paths" => tests::validate_readonly_paths(&spec),
"set_host_name" => tests::validate_hostname(&spec),
"mounts_recursive" => tests::validate_mounts_recursive(&spec),
"domainname_test" => tests::validate_domainname(&spec),
"seccomp" => tests::validate_seccomp(&spec),
"sysctl" => tests::validate_sysctl(&spec),
"scheduler_policy_other" => tests::validate_scheduler_policy(&spec),
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,8 +1,10 @@
use crate::utils::{self, test_read_access, test_write_access};
use anyhow::{bail, Result};
use libc::getdomainname;
use nix::errno::Errno;
use nix::unistd::getcwd;
use oci_spec::runtime::{LinuxSchedulerPolicy, Spec};
use std::ffi::CStr;
use std::fs::{self, read_dir};
use std::mem;
use std::path::Path;
Expand Down Expand Up @@ -82,6 +84,34 @@ pub fn validate_hostname(spec: &Spec) {
}
}

pub fn validate_domainname(spec: &Spec) {
if let Some(expected_domainname) = spec.domainname() {
if expected_domainname.is_empty() {
return;
}

const MAX_DOMAINNAME_SIZE: usize = 254;
let actual_domainname: [i8; MAX_DOMAINNAME_SIZE] = [0; MAX_DOMAINNAME_SIZE];

// TODO (YJDoc2) : libc now has support for getdomainname, update this to use that
let ret =
unsafe { getdomainname(actual_domainname.as_ptr() as *mut i8, MAX_DOMAINNAME_SIZE) };
if ret == -1 {
eprintln!("Failed to get domainname");
}

let actual_domainname_cstr =
unsafe { CStr::from_ptr(actual_domainname.as_ptr() as *mut i8) };
if actual_domainname_cstr.to_str().unwrap() != expected_domainname {
eprintln!(
"Unexpected domainname, expected: {:?} found: {:?}",
expected_domainname,
actual_domainname_cstr.to_str().unwrap()
);
}
}
}

// Run argument test recursively for files after base_dir
fn do_test_mounts_recursive(base_dir: &Path, test_fn: &dyn Fn(&Path) -> Result<()>) -> Result<()> {
let dirs = read_dir(base_dir).unwrap();
Expand Down

0 comments on commit c2e8670

Please sign in to comment.