Skip to content

Commit

Permalink
Merge pull request #959 from tony84727/integration-test/hooks
Browse files Browse the repository at this point in the history
test: hooks integration test
  • Loading branch information
utam0k authored Jun 18, 2022
2 parents 77d8ecf + e3aa2c0 commit c1d8b62
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
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::hooks::get_hooks_tests;
use crate::tests::lifecycle::{ContainerCreate, ContainerLifecycle};
use crate::tests::linux_ns_itype::get_ns_itype_tests;
use crate::tests::pidfile::get_pidfile_test;
Expand Down Expand Up @@ -78,6 +79,7 @@ fn main() -> Result<()> {
let huge_tlb = get_tlb_test();
let pidfile = get_pidfile_test();
let ns_itype = get_ns_itype_tests();
let hooks = get_hooks_tests();
let cgroup_v1_pids = cgroups::pids::get_test_group();
let cgroup_v1_cpu = cgroups::cpu::v1::get_test_group();
let cgroup_v2_cpu = cgroups::cpu::v2::get_test_group();
Expand All @@ -92,6 +94,7 @@ fn main() -> Result<()> {
tm.add_test_group(&huge_tlb);
tm.add_test_group(&pidfile);
tm.add_test_group(&ns_itype);
tm.add_test_group(&hooks);
tm.add_test_group(&cgroup_v1_pids);
tm.add_test_group(&cgroup_v1_cpu);
tm.add_test_group(&cgroup_v2_cpu);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use anyhow::anyhow;
use oci_spec::runtime::{Hook, HookBuilder, HooksBuilder, ProcessBuilder, Spec, SpecBuilder};
use std::{fs::File, io::Read};
use test_framework::{Test, TestGroup, TestResult};

use crate::utils::{
create_container, delete_container, generate_uuid, prepare_bundle, set_config,
test_utils::start_container,
};

const HOOK_OUTPUT_FILE: &str = "output";

fn create_hook_output_file() {
std::fs::File::create(HOOK_OUTPUT_FILE).expect("fail to create hook output file");
}

fn delete_hook_output_file() {
std::fs::remove_file(HOOK_OUTPUT_FILE).expect("fail to remove hook output file");
}

fn write_log_hook(content: &str) -> Hook {
let output = std::fs::canonicalize(HOOK_OUTPUT_FILE).unwrap();
let output = output.to_str().unwrap();
HookBuilder::default()
.path("/bin/sh")
.args(vec![
"sh".to_string(),
"-c".to_string(),
format!("echo '{}' >> {}", content, output,),
])
.build()
.expect("could not build hook")
}

fn get_spec() -> Spec {
SpecBuilder::default()
.process(
ProcessBuilder::default()
.args(vec!["true".to_string()])
.build()
.unwrap(),
)
.hooks(
HooksBuilder::default()
.prestart(vec![
write_log_hook("pre-start1 called"),
write_log_hook("pre-start2 called"),
])
.poststart(vec![
write_log_hook("post-start1 called"),
write_log_hook("post-start2 called"),
])
.poststop(vec![
write_log_hook("post-stop1 called"),
write_log_hook("post-stop2 called"),
])
.build()
.expect("could not build hooks"),
)
.build()
.unwrap()
}

fn get_test<'a>(test_name: &'static str) -> Test<'a> {
Test::new(
test_name,
Box::new(move || {
create_hook_output_file();
let spec = get_spec();
let id = generate_uuid();
let id_str = id.to_string();
let bundle = prepare_bundle(&id).unwrap();
set_config(&bundle, &spec).unwrap();
create_container(&id_str, &bundle).unwrap().wait().unwrap();
start_container(&id_str, &bundle).unwrap().wait().unwrap();
delete_container(&id_str, &bundle).unwrap().wait().unwrap();
let log = {
let mut output = File::open("output").expect("cannot open hook log");
let mut log = String::new();
output
.read_to_string(&mut log)
.expect("fail to read hook log");
log
};
delete_hook_output_file();
if log != "pre-start1 called\npre-start2 called\npost-start1 called\npost-start2 called\npost-stop1 called\npost-stop2 called\n".to_string() {
return TestResult::Failed(anyhow!(
"error : hooks must be called in the listed order"
));
}
TestResult::Passed
}),
)
}

pub fn get_hooks_tests<'a>() -> TestGroup<'a> {
let mut tg = TestGroup::new("hooks");
tg.add(vec![Box::new(get_test("hooks"))]);
tg
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod invoke;
pub use invoke::get_hooks_tests;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod cgroups;
pub mod hooks;
pub mod lifecycle;
pub mod linux_ns_itype;
pub mod pidfile;
Expand Down

0 comments on commit c1d8b62

Please sign in to comment.