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 domainname test #1544

Merged
merged 9 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
66 changes: 29 additions & 37 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 @@ -101,6 +102,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();

Expand All @@ -121,6 +124,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));

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,4 +7,5 @@ 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

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),
_ => 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,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::Spec;
use std::ffi::CStr;
use std::fs::{self, read_dir};
use std::path::Path;

Expand Down Expand Up @@ -81,6 +83,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