From c57df38bb9802a58cb52a7b7a6c36aba5f6f7590 Mon Sep 17 00:00:00 2001 From: yihuaf Date: Mon, 1 May 2023 19:33:11 +0000 Subject: [PATCH] refactored `create` call Signed-off-by: yihuaf --- .../src/tests/lifecycle/container_create.rs | 44 +++++++++++-------- .../tests/lifecycle/container_lifecycle.rs | 5 ++- .../src/tests/lifecycle/create.rs | 20 ++++----- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/tests/rust-integration-tests/integration_test/src/tests/lifecycle/container_create.rs b/tests/rust-integration-tests/integration_test/src/tests/lifecycle/container_create.rs index a77bf62f..1abf827d 100644 --- a/tests/rust-integration-tests/integration_test/src/tests/lifecycle/container_create.rs +++ b/tests/rust-integration-tests/integration_test/src/tests/lifecycle/container_create.rs @@ -26,39 +26,45 @@ impl ContainerCreate { // runtime should not create container with empty id fn create_empty_id(&self) -> TestResult { - let temp = create::create(&self.project_path, ""); - match temp { - TestResult::Passed => TestResult::Failed(anyhow::anyhow!( + match create::create(&self.project_path, "") { + Ok(()) => TestResult::Failed(anyhow::anyhow!( "Container should not have been created with empty id, but was created." )), - TestResult::Failed(_) => TestResult::Passed, - TestResult::Skipped => TestResult::Skipped, + Err(_) => TestResult::Passed, } } // runtime should create container with valid id fn create_valid_id(&self) -> TestResult { - let temp = create::create(&self.project_path, &self.container_id); - if let TestResult::Passed = temp { - kill::kill(&self.project_path, &self.container_id); - delete::delete(&self.project_path, &self.container_id); + match create::create(&self.project_path, &self.container_id) { + Ok(_) => { + kill::kill(&self.project_path, &self.container_id); + delete::delete(&self.project_path, &self.container_id); + TestResult::Passed + } + Err(_) => TestResult::Failed(anyhow::anyhow!( + "Container should have been created with valid id, but was not created." + )), } - temp } // runtime should not create container with is that already exists fn create_duplicate_id(&self) -> TestResult { let id = generate_uuid().to_string(); let _ = create::create(&self.project_path, &id); - let temp = create::create(&self.project_path, &id); - kill::kill(&self.project_path, &id); - delete::delete(&self.project_path, &id); - match temp { - TestResult::Passed => TestResult::Failed(anyhow::anyhow!( - "Container should not have been created with same id, but was created." - )), - TestResult::Failed(_) => TestResult::Passed, - TestResult::Skipped => TestResult::Skipped, + match create::create(&self.project_path, &id) { + Ok(()) => { + kill::kill(&self.project_path, &id); + delete::delete(&self.project_path, &id); + TestResult::Failed(anyhow::anyhow!( + "Container should not have been created with same id, but was created." + )) + } + Err(_) => { + kill::kill(&self.project_path, &id); + delete::delete(&self.project_path, &id); + TestResult::Passed + } } } } diff --git a/tests/rust-integration-tests/integration_test/src/tests/lifecycle/container_lifecycle.rs b/tests/rust-integration-tests/integration_test/src/tests/lifecycle/container_lifecycle.rs index aff5fc08..f122954a 100644 --- a/tests/rust-integration-tests/integration_test/src/tests/lifecycle/container_lifecycle.rs +++ b/tests/rust-integration-tests/integration_test/src/tests/lifecycle/container_lifecycle.rs @@ -32,7 +32,10 @@ impl ContainerLifecycle { } pub fn create(&self) -> TestResult { - create::create(&self.project_path, &self.container_id) + match create::create(&self.project_path, &self.container_id) { + Ok(_) => TestResult::Passed, + Err(err) => TestResult::Failed(err), + } } #[allow(dead_code)] diff --git a/tests/rust-integration-tests/integration_test/src/tests/lifecycle/create.rs b/tests/rust-integration-tests/integration_test/src/tests/lifecycle/create.rs index c690ff7f..b28d7fd9 100644 --- a/tests/rust-integration-tests/integration_test/src/tests/lifecycle/create.rs +++ b/tests/rust-integration-tests/integration_test/src/tests/lifecycle/create.rs @@ -1,14 +1,13 @@ use crate::utils::get_runtime_path; +use anyhow::{bail, Result}; use std::io; use std::path::Path; use std::process::{Command, Stdio}; -use test_framework::TestResult; -// There are still some issues here -// in case we put stdout and stderr as piped -// the youki process created halts indefinitely -// which is why we pass null, and use wait instead of wait_with_output -pub fn create(project_path: &Path, id: &str) -> TestResult { +// There are still some issues here in case we put stdout and stderr as piped +// the youki process created halts indefinitely which is why we pass null, and +// use wait instead of wait_with_output +pub fn create(project_path: &Path, id: &str) -> Result<()> { let res = Command::new(get_runtime_path()) .stdin(Stdio::null()) .stdout(Stdio::null()) @@ -25,14 +24,11 @@ pub fn create(project_path: &Path, id: &str) -> TestResult { match res { io::Result::Ok(status) => { if status.success() { - TestResult::Passed + Ok(()) } else { - TestResult::Failed(anyhow::anyhow!( - "Error : create exited with nonzero status : {}", - status - )) + bail!("create exited with nonzero status : {}", status) } } - io::Result::Err(e) => TestResult::Failed(anyhow::Error::new(e)), + io::Result::Err(e) => bail!("create failed : {}", e), } }