Skip to content

Commit

Permalink
Add domainname test
Browse files Browse the repository at this point in the history
Signed-off-by: higuruchi <[email protected]>
  • Loading branch information
higuruchi committed Feb 2, 2023
1 parent ab60bfc commit 09a3be6
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions tests/rust-integration-tests/integration_test/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::hooks::get_hooks_tests;
use crate::tests::hostname::get_hostname_test;
use crate::tests::lifecycle::{ContainerCreate, ContainerLifecycle};
Expand Down Expand Up @@ -92,6 +93,7 @@ fn main() -> Result<()> {
let ro_paths = get_ro_paths_test();
let hostname = get_hostname_test();
let mounts_recursive = get_mounts_recursive_test();
let domainname = get_domainname_tests();

tm.add_test_group(Box::new(cl));
tm.add_test_group(Box::new(cc));
Expand All @@ -109,6 +111,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));

tm.add_cleanup(Box::new(cgroups::cleanup_v1));
tm.add_cleanup(Box::new(cgroups::cleanup_v2));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::utils::test_inside_container;
use oci_spec::runtime::{
Capability, LinuxBuilder, LinuxCapabilitiesBuilder, ProcessBuilder, Spec, SpecBuilder,
};
use std::collections::hash_set::HashSet;
use test_framework::{Test, TestGroup, TestResult};

fn get_spec(domainname: &str) -> Spec {
let caps = vec![
Capability::AuditWrite,
Capability::Kill,
Capability::NetBindService,
];
let mut cap_bounding = HashSet::new();
let mut cap_effective = HashSet::new();
let mut cap_permitted = HashSet::new();

for cap in caps {
cap_bounding.insert(cap);
cap_effective.insert(cap);
cap_permitted.insert(cap);
}

SpecBuilder::default()
.domainname(domainname)
.linux(
LinuxBuilder::default()
.readonly_paths(vec![])
.build()
.expect("error in building linux config"),
)
.process(
ProcessBuilder::default()
.args(vec![
"runtimetest".to_string(),
"domainname_test".to_string(),
])
.capabilities(
LinuxCapabilitiesBuilder::default()
.bounding(cap_bounding)
.effective(cap_effective)
.permitted(cap_permitted)
.build()
.unwrap(),
)
.rlimits(vec![])
.no_new_privileges(true)
.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
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod cgroups;
pub mod domainname;
pub mod hooks;
pub mod hostname;
pub mod lifecycle;
Expand Down
1 change: 1 addition & 0 deletions tests/rust-integration-tests/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.0", features = ["runtime"] }
nix = "0.25.0"
anyhow = "1.0"
libc = "0.2.139"

1 change: 1 addition & 0 deletions tests/rust-integration-tests/runtimetest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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),
_ => eprintln!(
"error due to unexpected execute test name: {}",
execute_test
Expand Down
28 changes: 28 additions & 0 deletions tests/rust-integration-tests/runtimetest/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::utils::{self, test_read_access, test_write_access};
use anyhow::{bail, Result};
use libc::getdomainname;
use nix::errno::Errno;
use oci_spec::runtime::Spec;
use std::ffi::CStr;
use std::fs::read_dir;
use std::path::Path;

Expand Down Expand Up @@ -80,6 +82,32 @@ 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];

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
);
}
}
}

// 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 09a3be6

Please sign in to comment.