From 4519c31c215cec149a098749dfb7ef81cc9cbcfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Giba=C5=82a?= Date: Fri, 28 Jan 2022 14:40:12 +0100 Subject: [PATCH 1/3] Remove duplication from commands MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Szymon Gibała --- .../src/tests/lifecycle/delete.rs | 12 +--- .../src/tests/lifecycle/kill.rs | 13 +--- .../src/tests/lifecycle/start.rs | 12 +--- .../src/tests/lifecycle/state.rs | 12 +--- .../src/tests/pidfile/pidfile_test.rs | 9 +-- crates/integration_test/src/utils/mod.rs | 2 +- crates/integration_test/src/utils/support.rs | 2 +- .../integration_test/src/utils/test_utils.rs | 69 ++++++++++--------- 8 files changed, 51 insertions(+), 80 deletions(-) diff --git a/crates/integration_test/src/tests/lifecycle/delete.rs b/crates/integration_test/src/tests/lifecycle/delete.rs index 123be1c71..d464773bb 100644 --- a/crates/integration_test/src/tests/lifecycle/delete.rs +++ b/crates/integration_test/src/tests/lifecycle/delete.rs @@ -1,18 +1,10 @@ use super::get_result_from_output; -use crate::utils::get_runtime_path; +use crate::utils::{delete_container}; use std::path::Path; -use std::process::{Command, Stdio}; use test_framework::TestResult; pub fn delete(project_path: &Path, id: &str) -> TestResult { - let res = Command::new(get_runtime_path()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .arg("--root") - .arg(project_path.join("runtime")) - .arg("delete") - .arg(id) - .spawn() + let res = delete_container(id, project_path) .expect("failed to execute delete command") .wait_with_output(); get_result_from_output(res) diff --git a/crates/integration_test/src/tests/lifecycle/kill.rs b/crates/integration_test/src/tests/lifecycle/kill.rs index 480a513a3..3bcc392b2 100644 --- a/crates/integration_test/src/tests/lifecycle/kill.rs +++ b/crates/integration_test/src/tests/lifecycle/kill.rs @@ -1,19 +1,10 @@ use super::get_result_from_output; -use crate::utils::get_runtime_path; +use crate::utils::{kill_container}; use std::path::Path; -use std::process::{Command, Stdio}; use test_framework::TestResult; pub fn kill(project_path: &Path, id: &str) -> TestResult { - let res = Command::new(get_runtime_path()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .arg("--root") - .arg(project_path.join("runtime")) - .arg("kill") - .arg(id) - .arg("9") - .spawn() + let res = kill_container(id, project_path) .expect("failed to execute kill command") .wait_with_output(); get_result_from_output(res) diff --git a/crates/integration_test/src/tests/lifecycle/start.rs b/crates/integration_test/src/tests/lifecycle/start.rs index 874777b28..0a05fda9f 100644 --- a/crates/integration_test/src/tests/lifecycle/start.rs +++ b/crates/integration_test/src/tests/lifecycle/start.rs @@ -1,18 +1,10 @@ use super::get_result_from_output; -use crate::utils::get_runtime_path; use std::path::Path; -use std::process::{Command, Stdio}; use test_framework::TestResult; +use crate::utils::test_utils::start_container; pub fn start(project_path: &Path, id: &str) -> TestResult { - let res = Command::new(get_runtime_path()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .arg("--root") - .arg(project_path.join("runtime")) - .arg("start") - .arg(id) - .spawn() + let res = start_container(id, project_path) .expect("failed to execute start command") .wait_with_output(); get_result_from_output(res) diff --git a/crates/integration_test/src/tests/lifecycle/state.rs b/crates/integration_test/src/tests/lifecycle/state.rs index f95a3526e..9a23ea278 100644 --- a/crates/integration_test/src/tests/lifecycle/state.rs +++ b/crates/integration_test/src/tests/lifecycle/state.rs @@ -1,18 +1,10 @@ -use crate::utils::get_runtime_path; use std::io; use std::path::Path; -use std::process::{Command, Stdio}; use test_framework::TestResult; +use crate::utils::test_utils::get_state; pub fn state(project_path: &Path, id: &str) -> TestResult { - let res = Command::new(get_runtime_path()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .arg("--root") - .arg(project_path.join("runtime")) - .arg("state") - .arg(id) - .spawn() + let res = get_state(id, project_path) .expect("failed to execute state command") .wait_with_output(); match res { diff --git a/crates/integration_test/src/tests/pidfile/pidfile_test.rs b/crates/integration_test/src/tests/pidfile/pidfile_test.rs index 1d5197f81..c94aedf97 100644 --- a/crates/integration_test/src/tests/pidfile/pidfile_test.rs +++ b/crates/integration_test/src/tests/pidfile/pidfile_test.rs @@ -1,5 +1,5 @@ use crate::utils::{ - create_temp_dir, delete_container, generate_uuid, get_runtime_path, get_state, kill_container, + create_temp_dir, delete_container, generate_uuid, get_runtime_path, get_state_output, kill_container, prepare_bundle, State, TempDir, }; use anyhow::anyhow; @@ -9,8 +9,9 @@ use uuid::Uuid; #[inline] fn cleanup(id: &Uuid, bundle: &TempDir) { - kill_container(id, bundle).unwrap().wait().unwrap(); - delete_container(id, bundle).unwrap().wait().unwrap(); + let str_id = id.to_string(); + kill_container(&str_id, bundle).unwrap().wait().unwrap(); + delete_container(&str_id, bundle).unwrap().wait().unwrap(); } // here we have to manually create and manage the container @@ -42,7 +43,7 @@ fn test_pidfile() -> TestResult { .wait() .unwrap(); - let (out, err) = get_state(&container_id, &bundle).unwrap(); + let (out, err) = get_state_output(&container_id.to_string(), &bundle).unwrap(); if !err.is_empty() { cleanup(&container_id, &bundle); diff --git a/crates/integration_test/src/utils/mod.rs b/crates/integration_test/src/utils/mod.rs index 68ae575af..76826b540 100644 --- a/crates/integration_test/src/utils/mod.rs +++ b/crates/integration_test/src/utils/mod.rs @@ -7,6 +7,6 @@ pub use support::{ }; pub use temp_dir::{create_temp_dir, TempDir}; pub use test_utils::{ - create_container, delete_container, get_state, kill_container, test_inside_container, + create_container, delete_container, get_state, get_state_output, kill_container, test_inside_container, test_outside_container, ContainerData, State, }; diff --git a/crates/integration_test/src/utils/support.rs b/crates/integration_test/src/utils/support.rs index 478892e56..298761408 100644 --- a/crates/integration_test/src/utils/support.rs +++ b/crates/integration_test/src/utils/support.rs @@ -78,7 +78,7 @@ pub fn prepare_bundle(id: &Uuid) -> Result { let mut spec = Spec::default(); let mut process = Process::default(); - process.set_args(Some(vec!["sleep".into(), "5".into()])); + process.set_args(Some(vec!["sleep".into(), "10".into()])); spec.set_process(Some(process)); set_config(&temp_dir, &spec).unwrap(); diff --git a/crates/integration_test/src/utils/test_utils.rs b/crates/integration_test/src/utils/test_utils.rs index 44023a16c..4f8ee5691 100644 --- a/crates/integration_test/src/utils/test_utils.rs +++ b/crates/integration_test/src/utils/test_utils.rs @@ -64,55 +64,49 @@ pub fn create_container>(id: &Uuid, dir: P) -> Result { } /// Sends a kill command to the given container process -pub fn kill_container>(id: &Uuid, dir: P) -> Result { - let res = Command::new(get_runtime_path()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .arg("--root") - .arg(dir.as_ref().join("runtime")) +pub fn kill_container>(id: &str, dir: P) -> Result { + let res = runtime_command(dir) .arg("kill") - .arg(id.to_string()) + .arg(id) .arg("9") .spawn() .context("could not kill container")?; Ok(res) } -pub fn delete_container>(id: &Uuid, dir: P) -> Result { - let res = Command::new(get_runtime_path()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .arg("--root") - .arg(dir.as_ref().join("runtime")) +pub fn delete_container>(id: &str, dir: P) -> Result { + let res = runtime_command(dir) .arg("delete") - .arg(id.to_string()) + .arg(id) .spawn() .context("could not delete container")?; Ok(res) } -pub fn get_state>(id: &Uuid, dir: P) -> Result<(String, String)> { - sleep(SLEEP_TIME); - let output = Command::new(get_runtime_path()) +pub fn get_state>(id: &str, dir: P) -> Result { + let res = Command::new(get_runtime_path()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .arg("--root") .arg(dir.as_ref().join("runtime")) .arg("state") .arg(id.to_string()) - .spawn()? + .spawn() + .context("could not get container state")?; + Ok(res) +} + +pub fn get_state_output>(id: &str, dir: P) -> Result<(String, String)> { + sleep(SLEEP_TIME); + let output = get_state(id, dir)? .wait_with_output()?; let stderr = String::from_utf8(output.stderr).context("failed to parse std error stream")?; let stdout = String::from_utf8(output.stdout).context("failed to parse std output stream")?; Ok((stdout, stderr)) } -pub fn start_container>(id: &Uuid, dir: P) -> Result { - let res = Command::new(get_runtime_path()) - .stderr(Stdio::piped()) - .stdout(Stdio::piped()) - .arg("--root") - .arg(dir.as_ref().join("runtime")) +pub fn start_container>(id: &str, dir: P) -> Result { + let res = runtime_command(dir) .arg("start") .arg(id.to_string()) .spawn() @@ -120,6 +114,15 @@ pub fn start_container>(id: &Uuid, dir: P) -> Result { Ok(res) } +fn runtime_command>(dir: P) -> Command { + let mut command = Command::new(get_runtime_path()); + command.stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .arg("--root") + .arg(dir.as_ref().join("runtime")); + command +} + pub fn test_outside_container( spec: Spec, execute_test: &dyn Fn(ContainerData) -> TestResult, @@ -128,7 +131,7 @@ pub fn test_outside_container( let bundle = prepare_bundle(&id).unwrap(); set_config(&bundle, &spec).unwrap(); let create_result = create_container(&id, &bundle).unwrap().wait(); - let (out, err) = get_state(&id, &bundle).unwrap(); + let (out, err) = get_state_output(&id.to_string(), &bundle).unwrap(); let state: Option = match serde_json::from_str(&out) { Ok(v) => Some(v), Err(_) => None, @@ -140,8 +143,8 @@ pub fn test_outside_container( create_result, }; let test_result = execute_test(data); - kill_container(&id, &bundle).unwrap().wait().unwrap(); - delete_container(&id, &bundle).unwrap().wait().unwrap(); + kill_container(&id.to_string(), &bundle).unwrap().wait().unwrap(); + delete_container(&id.to_string(), &bundle).unwrap().wait().unwrap(); test_result } @@ -180,7 +183,7 @@ pub fn test_inside_container( .join("bin") .join("runtimetest"), ) - .unwrap(); + .unwrap(); let create_process = create_container(&id, &bundle).unwrap(); // here we do not wait for the process by calling wait() as in the test_outside_container // function because we need the output of the runtimetest. If we call wait, it will return @@ -189,7 +192,7 @@ pub fn test_inside_container( // assume that the create command was successful. If it wasn't we can catch that error // in the start_container, as we can not start a non-created container anyways std::thread::sleep(std::time::Duration::from_millis(1000)); - match start_container(&id, &bundle).unwrap().wait_with_output() { + match start_container(&id.to_string(), &bundle).unwrap().wait_with_output() { Ok(c) => c, Err(e) => return TestResult::Failed(anyhow!("container start failed : {:?}", e)), }; @@ -207,7 +210,7 @@ pub fn test_inside_container( )); } - let (out, err) = get_state(&id, &bundle).unwrap(); + let (out, err) = get_state_output(&id.to_string(), &bundle).unwrap(); if !err.is_empty() { return TestResult::Failed(anyhow!( "error in getting state after starting the container : {}", @@ -215,15 +218,15 @@ pub fn test_inside_container( )); } - let state:State = match serde_json::from_str(&out) { + let state: State = match serde_json::from_str(&out) { Ok(v) => v, Err(e) => return TestResult::Failed(anyhow!("error in parsing state of container after start in test_inside_container : stdout : {}, parse error : {}",out,e)), }; if state.status != "stopped" { return TestResult::Failed(anyhow!("error : unexpected container status in test_inside_runtime : expected stopped, got {}, container state : {:?}",state.status,state)); } - kill_container(&id, &bundle).unwrap().wait().unwrap(); - delete_container(&id, &bundle).unwrap().wait().unwrap(); + kill_container(&id.to_string(), &bundle).unwrap().wait().unwrap(); + delete_container(&id.to_string(), &bundle).unwrap().wait().unwrap(); TestResult::Passed } From de0c77df4f21362e0a28616d4128a3613d621d53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Giba=C5=82a?= Date: Fri, 28 Jan 2022 15:03:49 +0100 Subject: [PATCH 2/3] Format code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Szymon Gibała --- .../src/tests/lifecycle/delete.rs | 2 +- .../src/tests/lifecycle/kill.rs | 2 +- .../src/tests/lifecycle/start.rs | 2 +- .../src/tests/lifecycle/state.rs | 2 +- .../src/tests/pidfile/pidfile_test.rs | 4 +-- crates/integration_test/src/utils/mod.rs | 4 +-- .../integration_test/src/utils/test_utils.rs | 33 ++++++++++++++----- 7 files changed, 32 insertions(+), 17 deletions(-) diff --git a/crates/integration_test/src/tests/lifecycle/delete.rs b/crates/integration_test/src/tests/lifecycle/delete.rs index d464773bb..31c7dcce2 100644 --- a/crates/integration_test/src/tests/lifecycle/delete.rs +++ b/crates/integration_test/src/tests/lifecycle/delete.rs @@ -1,5 +1,5 @@ use super::get_result_from_output; -use crate::utils::{delete_container}; +use crate::utils::delete_container; use std::path::Path; use test_framework::TestResult; diff --git a/crates/integration_test/src/tests/lifecycle/kill.rs b/crates/integration_test/src/tests/lifecycle/kill.rs index 3bcc392b2..6b930e595 100644 --- a/crates/integration_test/src/tests/lifecycle/kill.rs +++ b/crates/integration_test/src/tests/lifecycle/kill.rs @@ -1,5 +1,5 @@ use super::get_result_from_output; -use crate::utils::{kill_container}; +use crate::utils::kill_container; use std::path::Path; use test_framework::TestResult; diff --git a/crates/integration_test/src/tests/lifecycle/start.rs b/crates/integration_test/src/tests/lifecycle/start.rs index 0a05fda9f..5b92de228 100644 --- a/crates/integration_test/src/tests/lifecycle/start.rs +++ b/crates/integration_test/src/tests/lifecycle/start.rs @@ -1,7 +1,7 @@ use super::get_result_from_output; +use crate::utils::test_utils::start_container; use std::path::Path; use test_framework::TestResult; -use crate::utils::test_utils::start_container; pub fn start(project_path: &Path, id: &str) -> TestResult { let res = start_container(id, project_path) diff --git a/crates/integration_test/src/tests/lifecycle/state.rs b/crates/integration_test/src/tests/lifecycle/state.rs index 9a23ea278..7c6f76342 100644 --- a/crates/integration_test/src/tests/lifecycle/state.rs +++ b/crates/integration_test/src/tests/lifecycle/state.rs @@ -1,7 +1,7 @@ +use crate::utils::test_utils::get_state; use std::io; use std::path::Path; use test_framework::TestResult; -use crate::utils::test_utils::get_state; pub fn state(project_path: &Path, id: &str) -> TestResult { let res = get_state(id, project_path) diff --git a/crates/integration_test/src/tests/pidfile/pidfile_test.rs b/crates/integration_test/src/tests/pidfile/pidfile_test.rs index c94aedf97..26578ff83 100644 --- a/crates/integration_test/src/tests/pidfile/pidfile_test.rs +++ b/crates/integration_test/src/tests/pidfile/pidfile_test.rs @@ -1,6 +1,6 @@ use crate::utils::{ - create_temp_dir, delete_container, generate_uuid, get_runtime_path, get_state_output, kill_container, - prepare_bundle, State, TempDir, + create_temp_dir, delete_container, generate_uuid, get_runtime_path, get_state_output, + kill_container, prepare_bundle, State, TempDir, }; use anyhow::anyhow; use std::process::{Command, Stdio}; diff --git a/crates/integration_test/src/utils/mod.rs b/crates/integration_test/src/utils/mod.rs index 76826b540..97e4bafb7 100644 --- a/crates/integration_test/src/utils/mod.rs +++ b/crates/integration_test/src/utils/mod.rs @@ -7,6 +7,6 @@ pub use support::{ }; pub use temp_dir::{create_temp_dir, TempDir}; pub use test_utils::{ - create_container, delete_container, get_state, get_state_output, kill_container, test_inside_container, - test_outside_container, ContainerData, State, + create_container, delete_container, get_state, get_state_output, kill_container, + test_inside_container, test_outside_container, ContainerData, State, }; diff --git a/crates/integration_test/src/utils/test_utils.rs b/crates/integration_test/src/utils/test_utils.rs index 4f8ee5691..71954785e 100644 --- a/crates/integration_test/src/utils/test_utils.rs +++ b/crates/integration_test/src/utils/test_utils.rs @@ -98,8 +98,7 @@ pub fn get_state>(id: &str, dir: P) -> Result { pub fn get_state_output>(id: &str, dir: P) -> Result<(String, String)> { sleep(SLEEP_TIME); - let output = get_state(id, dir)? - .wait_with_output()?; + let output = get_state(id, dir)?.wait_with_output()?; let stderr = String::from_utf8(output.stderr).context("failed to parse std error stream")?; let stdout = String::from_utf8(output.stdout).context("failed to parse std output stream")?; Ok((stdout, stderr)) @@ -116,7 +115,8 @@ pub fn start_container>(id: &str, dir: P) -> Result { fn runtime_command>(dir: P) -> Command { let mut command = Command::new(get_runtime_path()); - command.stdout(Stdio::piped()) + command + .stdout(Stdio::piped()) .stderr(Stdio::piped()) .arg("--root") .arg(dir.as_ref().join("runtime")); @@ -143,8 +143,14 @@ pub fn test_outside_container( create_result, }; let test_result = execute_test(data); - kill_container(&id.to_string(), &bundle).unwrap().wait().unwrap(); - delete_container(&id.to_string(), &bundle).unwrap().wait().unwrap(); + kill_container(&id.to_string(), &bundle) + .unwrap() + .wait() + .unwrap(); + delete_container(&id.to_string(), &bundle) + .unwrap() + .wait() + .unwrap(); test_result } @@ -183,7 +189,7 @@ pub fn test_inside_container( .join("bin") .join("runtimetest"), ) - .unwrap(); + .unwrap(); let create_process = create_container(&id, &bundle).unwrap(); // here we do not wait for the process by calling wait() as in the test_outside_container // function because we need the output of the runtimetest. If we call wait, it will return @@ -192,7 +198,10 @@ pub fn test_inside_container( // assume that the create command was successful. If it wasn't we can catch that error // in the start_container, as we can not start a non-created container anyways std::thread::sleep(std::time::Duration::from_millis(1000)); - match start_container(&id.to_string(), &bundle).unwrap().wait_with_output() { + match start_container(&id.to_string(), &bundle) + .unwrap() + .wait_with_output() + { Ok(c) => c, Err(e) => return TestResult::Failed(anyhow!("container start failed : {:?}", e)), }; @@ -225,8 +234,14 @@ pub fn test_inside_container( if state.status != "stopped" { return TestResult::Failed(anyhow!("error : unexpected container status in test_inside_runtime : expected stopped, got {}, container state : {:?}",state.status,state)); } - kill_container(&id.to_string(), &bundle).unwrap().wait().unwrap(); - delete_container(&id.to_string(), &bundle).unwrap().wait().unwrap(); + kill_container(&id.to_string(), &bundle) + .unwrap() + .wait() + .unwrap(); + delete_container(&id.to_string(), &bundle) + .unwrap() + .wait() + .unwrap(); TestResult::Passed } From 4a79d452e99f12e41fe4a0ceb88428a1d1e31977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Giba=C5=82a?= Date: Sun, 30 Jan 2022 12:41:37 +0100 Subject: [PATCH 3/3] Review fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Szymon Gibała --- .../src/tests/lifecycle/state.rs | 23 +++----- .../src/tests/pidfile/pidfile_test.rs | 6 +- crates/integration_test/src/utils/mod.rs | 4 +- .../integration_test/src/utils/test_utils.rs | 56 +++++++------------ 4 files changed, 31 insertions(+), 58 deletions(-) diff --git a/crates/integration_test/src/tests/lifecycle/state.rs b/crates/integration_test/src/tests/lifecycle/state.rs index 7c6f76342..57318ec11 100644 --- a/crates/integration_test/src/tests/lifecycle/state.rs +++ b/crates/integration_test/src/tests/lifecycle/state.rs @@ -1,33 +1,24 @@ -use crate::utils::test_utils::get_state; -use std::io; +use crate::utils::get_state; +use anyhow::{anyhow, Result}; use std::path::Path; use test_framework::TestResult; pub fn state(project_path: &Path, id: &str) -> TestResult { - let res = get_state(id, project_path) - .expect("failed to execute state command") - .wait_with_output(); - match res { - io::Result::Ok(output) => { - let stderr = String::from_utf8(output.stderr).unwrap(); - let stdout = String::from_utf8(output.stdout).unwrap(); + match get_state(id, project_path) { + Result::Ok((stdout, stderr)) => { if stderr.contains("Error") || stderr.contains("error") { - TestResult::Failed(anyhow::anyhow!( - "Error :\nstdout : {}\nstderr : {}", - stdout, - stderr - )) + TestResult::Failed(anyhow!("Error :\nstdout : {}\nstderr : {}", stdout, stderr)) } else { // confirm that the status is stopped, as this is executed after the kill command if !(stdout.contains(&format!(r#""id": "{}""#, id)) && stdout.contains(r#""status": "stopped""#)) { - TestResult::Failed(anyhow::anyhow!("Expected state stopped, got : {}", stdout)) + TestResult::Failed(anyhow!("Expected state stopped, got : {}", stdout)) } else { TestResult::Passed } } } - io::Result::Err(e) => TestResult::Failed(anyhow::Error::new(e)), + Result::Err(e) => TestResult::Failed(e.context("failed to get container state")), } } diff --git a/crates/integration_test/src/tests/pidfile/pidfile_test.rs b/crates/integration_test/src/tests/pidfile/pidfile_test.rs index 26578ff83..4cdf4a157 100644 --- a/crates/integration_test/src/tests/pidfile/pidfile_test.rs +++ b/crates/integration_test/src/tests/pidfile/pidfile_test.rs @@ -1,6 +1,6 @@ use crate::utils::{ - create_temp_dir, delete_container, generate_uuid, get_runtime_path, get_state_output, - kill_container, prepare_bundle, State, TempDir, + create_temp_dir, delete_container, generate_uuid, get_runtime_path, get_state, kill_container, + prepare_bundle, State, TempDir, }; use anyhow::anyhow; use std::process::{Command, Stdio}; @@ -43,7 +43,7 @@ fn test_pidfile() -> TestResult { .wait() .unwrap(); - let (out, err) = get_state_output(&container_id.to_string(), &bundle).unwrap(); + let (out, err) = get_state(&container_id.to_string(), &bundle).unwrap(); if !err.is_empty() { cleanup(&container_id, &bundle); diff --git a/crates/integration_test/src/utils/mod.rs b/crates/integration_test/src/utils/mod.rs index 97e4bafb7..68ae575af 100644 --- a/crates/integration_test/src/utils/mod.rs +++ b/crates/integration_test/src/utils/mod.rs @@ -7,6 +7,6 @@ pub use support::{ }; pub use temp_dir::{create_temp_dir, TempDir}; pub use test_utils::{ - create_container, delete_container, get_state, get_state_output, kill_container, - test_inside_container, test_outside_container, ContainerData, State, + create_container, delete_container, get_state, kill_container, test_inside_container, + test_outside_container, ContainerData, State, }; diff --git a/crates/integration_test/src/utils/test_utils.rs b/crates/integration_test/src/utils/test_utils.rs index 71954785e..16705f183 100644 --- a/crates/integration_test/src/utils/test_utils.rs +++ b/crates/integration_test/src/utils/test_utils.rs @@ -11,7 +11,6 @@ use std::process::{Child, Command, ExitStatus, Stdio}; use std::thread::sleep; use std::time::Duration; use test_framework::{test_result, TestResult}; -use uuid::Uuid; const SLEEP_TIME: Duration = Duration::from_millis(150); pub const CGROUP_ROOT: &str = "/sys/fs/cgroup"; @@ -43,7 +42,7 @@ pub struct ContainerData { } /// Starts the runtime with given directory as root directory -pub fn create_container>(id: &Uuid, dir: P) -> Result { +pub fn create_container>(id: &str, dir: P) -> Result { let res = Command::new(get_runtime_path()) // set stdio so that we can get o/p of runtimetest // in test_inside_container function @@ -55,7 +54,7 @@ pub fn create_container>(id: &Uuid, dir: P) -> Result { .arg("--root") .arg(dir.as_ref().join("runtime")) .arg("create") - .arg(id.to_string()) + .arg(id) .arg("--bundle") .arg(dir.as_ref().join("bundle")) .spawn() @@ -83,22 +82,15 @@ pub fn delete_container>(id: &str, dir: P) -> Result { Ok(res) } -pub fn get_state>(id: &str, dir: P) -> Result { - let res = Command::new(get_runtime_path()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .arg("--root") - .arg(dir.as_ref().join("runtime")) +pub fn get_state>(id: &str, dir: P) -> Result<(String, String)> { + sleep(SLEEP_TIME); + let output = runtime_command(dir) .arg("state") .arg(id.to_string()) .spawn() - .context("could not get container state")?; - Ok(res) -} - -pub fn get_state_output>(id: &str, dir: P) -> Result<(String, String)> { - sleep(SLEEP_TIME); - let output = get_state(id, dir)?.wait_with_output()?; + .context("could not get container state")? + .wait_with_output() + .context("failed while waiting for state command")?; let stderr = String::from_utf8(output.stderr).context("failed to parse std error stream")?; let stdout = String::from_utf8(output.stdout).context("failed to parse std output stream")?; Ok((stdout, stderr)) @@ -128,10 +120,11 @@ pub fn test_outside_container( execute_test: &dyn Fn(ContainerData) -> TestResult, ) -> TestResult { let id = generate_uuid(); + let id_str = id.to_string(); let bundle = prepare_bundle(&id).unwrap(); set_config(&bundle, &spec).unwrap(); - let create_result = create_container(&id, &bundle).unwrap().wait(); - let (out, err) = get_state_output(&id.to_string(), &bundle).unwrap(); + let create_result = create_container(&id_str, &bundle).unwrap().wait(); + let (out, err) = get_state(&id_str, &bundle).unwrap(); let state: Option = match serde_json::from_str(&out) { Ok(v) => Some(v), Err(_) => None, @@ -143,14 +136,8 @@ pub fn test_outside_container( create_result, }; let test_result = execute_test(data); - kill_container(&id.to_string(), &bundle) - .unwrap() - .wait() - .unwrap(); - delete_container(&id.to_string(), &bundle) - .unwrap() - .wait() - .unwrap(); + kill_container(&id_str, &bundle).unwrap().wait().unwrap(); + delete_container(&id_str, &bundle).unwrap().wait().unwrap(); test_result } @@ -160,6 +147,7 @@ pub fn test_inside_container( setup_for_test: &dyn Fn(&Path) -> Result<()>, ) -> TestResult { let id = generate_uuid(); + let id_str = id.to_string(); let bundle = prepare_bundle(&id).unwrap(); // This will do the required setup for the test @@ -190,7 +178,7 @@ pub fn test_inside_container( .join("runtimetest"), ) .unwrap(); - let create_process = create_container(&id, &bundle).unwrap(); + let create_process = create_container(&id_str, &bundle).unwrap(); // here we do not wait for the process by calling wait() as in the test_outside_container // function because we need the output of the runtimetest. If we call wait, it will return // and we won't have an easy way of getting the stdio of the runtimetest. @@ -198,7 +186,7 @@ pub fn test_inside_container( // assume that the create command was successful. If it wasn't we can catch that error // in the start_container, as we can not start a non-created container anyways std::thread::sleep(std::time::Duration::from_millis(1000)); - match start_container(&id.to_string(), &bundle) + match start_container(&id_str, &bundle) .unwrap() .wait_with_output() { @@ -219,7 +207,7 @@ pub fn test_inside_container( )); } - let (out, err) = get_state_output(&id.to_string(), &bundle).unwrap(); + let (out, err) = get_state(&id_str, &bundle).unwrap(); if !err.is_empty() { return TestResult::Failed(anyhow!( "error in getting state after starting the container : {}", @@ -234,14 +222,8 @@ pub fn test_inside_container( if state.status != "stopped" { return TestResult::Failed(anyhow!("error : unexpected container status in test_inside_runtime : expected stopped, got {}, container state : {:?}",state.status,state)); } - kill_container(&id.to_string(), &bundle) - .unwrap() - .wait() - .unwrap(); - delete_container(&id.to_string(), &bundle) - .unwrap() - .wait() - .unwrap(); + kill_container(&id_str, &bundle).unwrap().wait().unwrap(); + delete_container(&id_str, &bundle).unwrap().wait().unwrap(); TestResult::Passed }