diff --git a/crates/integration_test/src/tests/lifecycle/container_lifecycle.rs b/crates/integration_test/src/tests/lifecycle/container_lifecycle.rs index 94cd94fce..022c4523d 100644 --- a/crates/integration_test/src/tests/lifecycle/container_lifecycle.rs +++ b/crates/integration_test/src/tests/lifecycle/container_lifecycle.rs @@ -3,7 +3,7 @@ use std::thread::sleep; use std::time::Duration; use test_framework::{TestResult, TestableGroup}; -use super::{create, delete, kill, start, state}; +use super::{create, delete, exec, kill, start, state}; // By experimenting, somewhere around 50 is enough for youki process // to get the kill signal and shut down @@ -35,6 +35,11 @@ impl ContainerLifecycle { create::create(&self.project_path, &self.container_id) } + #[allow(dead_code)] + pub fn exec(&self, cmd: Vec<&str>, expected_output: Option<&str>) -> TestResult { + exec::exec(&self.project_path, &self.container_id, cmd, expected_output) + } + pub fn start(&self) -> TestResult { start::start(&self.project_path, &self.container_id) } @@ -60,15 +65,18 @@ impl<'a> TestableGroup<'a> for ContainerLifecycle { fn get_name(&self) -> &'a str { "lifecycle" } + fn run_all(&self) -> Vec<(&'a str, TestResult)> { vec![ ("create", self.create()), ("start", self.start()), + // ("exec", self.exec(vec!["echo", "Hello"], Some("Hello\n"))), ("kill", self.kill()), ("state", self.state()), ("delete", self.delete()), ] } + fn run_selected(&self, selected: &[&str]) -> Vec<(&'a str, TestResult)> { let mut ret = Vec::new(); for name in selected { diff --git a/crates/integration_test/src/tests/lifecycle/exec.rs b/crates/integration_test/src/tests/lifecycle/exec.rs new file mode 100644 index 000000000..d604c5a4f --- /dev/null +++ b/crates/integration_test/src/tests/lifecycle/exec.rs @@ -0,0 +1,29 @@ +use super::get_result_from_output; +use crate::utils::get_runtime_path; +use std::path::Path; +use std::process::{Command, Stdio}; +use test_framework::{assert_result_eq, TestResult}; + +pub fn exec( + project_path: &Path, + id: &str, + exec_cmd: Vec<&str>, + expected_output: Option<&str>, +) -> TestResult { + let res = Command::new(get_runtime_path()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .arg("--root") + .arg(project_path.join("runtime")) + .arg("exec") + .arg(id) + .args(exec_cmd) + .spawn() + .expect("failed to execute exec command") + .wait_with_output(); + if let Some(expect) = expected_output { + let act = String::from_utf8(res.as_ref().unwrap().stdout.clone()).unwrap(); + assert_result_eq!(expect, act.as_str(), "unexpected stdout.").unwrap(); + } + get_result_from_output(res) +} diff --git a/crates/integration_test/src/tests/lifecycle/mod.rs b/crates/integration_test/src/tests/lifecycle/mod.rs index def9ce296..88eb75602 100644 --- a/crates/integration_test/src/tests/lifecycle/mod.rs +++ b/crates/integration_test/src/tests/lifecycle/mod.rs @@ -2,6 +2,7 @@ mod container_create; mod container_lifecycle; mod create; mod delete; +mod exec; mod kill; mod start; mod state;