Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add hostname test #1376

Merged
merged 4 commits into from
Dec 2, 2022
Merged
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/rust-integration-tests/integration_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::tests::pidfile::get_pidfile_test;
use crate::tests::readonly_paths::get_ro_paths_test;
use crate::tests::seccomp_notify::get_seccomp_notify_test;
use crate::tests::tlb::get_tlb_test;
use crate::tests::hostname::get_hostname_test;
use crate::utils::support::{set_runtime_path, set_runtimetest_path};
use anyhow::{Context, Result};
use clap::Parser;
Expand Down Expand Up @@ -88,6 +89,7 @@ fn main() -> Result<()> {
let cgroup_v1_blkio = cgroups::blkio::get_test_group();
let seccomp_notify = get_seccomp_notify_test();
let ro_paths = get_ro_paths_test();
let hostname = get_hostname_test();

tm.add_test_group(Box::new(cl));
tm.add_test_group(Box::new(cc));
Expand All @@ -103,6 +105,7 @@ fn main() -> Result<()> {
tm.add_test_group(Box::new(cgroup_v1_blkio));
tm.add_test_group(Box::new(seccomp_notify));
tm.add_test_group(Box::new(ro_paths));
tm.add_test_group(Box::new(hostname));

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,51 @@
use oci_spec::runtime::{LinuxBuilder, ProcessBuilder, Spec, SpecBuilder};
use test_framework::{Test, TestGroup, TestResult};

use crate::utils::test_inside_container;

fn create_spec(hostname: &str) -> Spec {
SpecBuilder::default()
.hostname(hostname)
.linux(
// Need to reset the read-only paths
LinuxBuilder::default()
.readonly_paths(vec![])
.build()
.expect("error in building linux config"),
)
.process(
ProcessBuilder::default()
.args(vec!["runtimetest".to_string()])
.build()
.expect("error in creating process config"),
)
.build()
.unwrap()
}

fn hostname_test() -> TestResult {
let spec = create_spec("hostname-specific");
test_inside_container(spec, &|_| {
// As long as the container is created, we expect the hostname to be determined
// by the spec, so nothing to prepare prior.
Ok(())
})
}

fn empty_hostname() -> TestResult {
let spec = create_spec("");
test_inside_container(spec, &|_| {
// As long as the container is created, we expect the hostname to be determined
// by the spec, so nothing to prepare prior.
Ok(())
})
}

pub fn get_hostname_test() -> TestGroup {
let mut test_group = TestGroup::new("set_host_name");
let hostname_test = Test::new("set_host_name_test", Box::new(hostname_test));
let empty_hostname_test = Test::new("set_empty_host_name_test", Box::new(empty_hostname));
test_group.add(vec![Box::new(hostname_test), Box::new(empty_hostname_test)]);

test_group
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod pidfile;
pub mod readonly_paths;
pub mod seccomp_notify;
pub mod tlb;
pub mod hostname;
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 @@ -20,4 +20,5 @@ fn get_spec() -> Spec {
fn main() {
let spec = get_spec();
tests::validate_readonly_paths(&spec);
tests::validate_hostname(&spec);
}
21 changes: 19 additions & 2 deletions tests/rust-integration-tests/runtimetest/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ pub fn validate_readonly_paths(spec: &Spec) {
return;
}
};

if ro_paths.is_empty() {
eprintln!("in readonly paths, expected some readonly paths to be set, found none");
return;
}

// TODO when https://github.com/rust-lang/rust/issues/86442 stabilizes,
// change manual matching of i32 to e.kind() and match statement

for path in ro_paths {
if let std::io::Result::Err(e) = test_read_access(path) {
let errno = Errno::from_i32(e.raw_os_error().unwrap());
Expand Down Expand Up @@ -59,3 +59,20 @@ pub fn validate_readonly_paths(spec: &Spec) {
}
}
}

pub fn validate_hostname(spec: &Spec) {
if let Some(expected_hostname) = spec.hostname() {
if expected_hostname.is_empty() {
// Skipping empty hostname
return;
}
let actual_hostname = nix::unistd::gethostname().expect("failed to get current hostname");
let actual_hostname = actual_hostname.to_str().unwrap();
if &actual_hostname != expected_hostname {
eprintln!(
"Unexpected hostname, expected: {:?} found: {:?}",
expected_hostname, actual_hostname
);
}
}
}