From fd494c183d1c7a1eb5f4934454cafe0067045e3a Mon Sep 17 00:00:00 2001 From: Akiyama Date: Sat, 7 Dec 2024 05:00:29 +0000 Subject: [PATCH] split test func Signed-off-by: Akiyama --- .../contest/src/tests/kill/kill_test.rs | 144 +++++++++--------- 1 file changed, 73 insertions(+), 71 deletions(-) diff --git a/tests/contest/contest/src/tests/kill/kill_test.rs b/tests/contest/contest/src/tests/kill/kill_test.rs index 1f9f7514f..ad793a3c9 100644 --- a/tests/contest/contest/src/tests/kill/kill_test.rs +++ b/tests/contest/contest/src/tests/kill/kill_test.rs @@ -1,111 +1,113 @@ use anyhow::anyhow; -use test_framework::{ConditionalTest, TestGroup, TestResult}; +use test_framework::{Test, TestGroup, TestResult}; use crate::tests::lifecycle::ContainerLifecycle; -fn run_kill_test_cases() -> TestResult { +fn kill_with_empty_id_test() -> TestResult { let mut container = ContainerLifecycle::new(); - let mut results = vec![]; - let container_id = container.get_id().to_string(); // kill with empty id container.set_id(""); - results.push(( - "kill without ID", - match container.kill() { - TestResult::Failed(_) => TestResult::Passed, - TestResult::Passed => TestResult::Failed(anyhow!("Expected failure but got success")), - _ => TestResult::Failed(anyhow!("Unexpected test result")), - }, - )); + match container.kill() { + TestResult::Failed(_) => TestResult::Passed, + TestResult::Passed => TestResult::Failed(anyhow!("Expected failure but got success")), + _ => TestResult::Failed(anyhow!("Unexpected test result")), + } +} +fn kill_non_existed_container() -> TestResult { + let mut container = ContainerLifecycle::new(); // kill for non existed container container.set_id("non-existent-container-id"); - results.push(( - "kill non-existent container", - match container.kill() { - TestResult::Failed(_) => TestResult::Passed, - TestResult::Passed => TestResult::Failed(anyhow!("Expected failure but got success")), - _ => TestResult::Failed(anyhow!("Unexpected test result")), - }, - )); - + match container.kill() { + TestResult::Failed(_) => TestResult::Passed, + TestResult::Passed => TestResult::Failed(anyhow!("Expected failure but got success")), + _ => TestResult::Failed(anyhow!("Unexpected test result")), + } +} +fn kill_created_container_test() -> TestResult { + let container = ContainerLifecycle::new(); // kill created container - container.set_id(&container_id); match container.create() { TestResult::Passed => {} _ => return TestResult::Failed(anyhow!("Failed to create container")), } - results.push(( - "kill created container", - match container.kill() { - TestResult::Passed => TestResult::Passed, - TestResult::Failed(_) => { - TestResult::Failed(anyhow!("Expected success but got failure")) - } - _ => TestResult::Failed(anyhow!("Unexpected test result")), - }, - )); + match container.kill() { + TestResult::Passed => TestResult::Passed, + TestResult::Failed(_) => { + TestResult::Failed(anyhow!("Expected success but got failure")) + } + _ => TestResult::Failed(anyhow!("Unexpected test result")), + } +} +fn kill_stopped_container_test() -> TestResult { + let container = ContainerLifecycle::new(); // kill stopped container match container.delete() { TestResult::Passed => {} _ => return TestResult::Failed(anyhow!("Failed to delete container")), } - results.push(( - "kill stopped container", - match container.kill() { - TestResult::Failed(_) => TestResult::Passed, - TestResult::Passed => TestResult::Failed(anyhow!("Expected failure but got success")), - _ => TestResult::Failed(anyhow!("Unexpected test result")), - }, - )); + match container.kill() { + TestResult::Failed(_) => TestResult::Passed, + TestResult::Passed => TestResult::Failed(anyhow!("Expected failure but got success")), + _ => TestResult::Failed(anyhow!("Unexpected test result")), + } +} + +fn kill_start_container_test() -> TestResult { + let container = ContainerLifecycle::new(); // kill start container match container.create() { - TestResult::Passed => {} - _ => return TestResult::Failed(anyhow!("Failed to recreate container")), + TestResult::Passed => {} + _ => return TestResult::Failed(anyhow!("Failed to recreate container")), } match container.start() { - TestResult::Passed => {} - TestResult::Failed(err) => { - return TestResult::Failed(anyhow!("Failed to start container: {:?}", err)); - } - _ => unreachable!(), + TestResult::Passed => {} + TestResult::Failed(err) => { + return TestResult::Failed(anyhow!("Failed to start container: {:?}", err)); } - results.push(( - "kill running container", - match container.kill() { - TestResult::Passed => TestResult::Passed, - TestResult::Failed(_) => { - TestResult::Failed(anyhow!("Expected success but got failure")) - } - _ => TestResult::Failed(anyhow!("Unexpected test result")), - }, - )); - - match container.delete() { - TestResult::Passed => {} - _ => return TestResult::Failed(anyhow!("Failed to delete container")), + _ => unreachable!(), } - - for (name, result) in results { - if let TestResult::Failed(err) = result { - return TestResult::Failed(anyhow!("Test '{}' failed: {:?}", name, err)); + match container.kill() { + TestResult::Passed => TestResult::Passed, + TestResult::Failed(_) => { + TestResult::Failed(anyhow!("Expected success but got failure")) } + _ => TestResult::Failed(anyhow!("Unexpected test result")), } - TestResult::Passed + // match container.delete() { + // TestResult::Passed => {} + // _ => return TestResult::Failed(anyhow!("Failed to delete container")), + // } } + pub fn get_kill_test() -> TestGroup { let mut test_group = TestGroup::new("kill_container"); - let kill_test = ConditionalTest::new( - "test_kill_container", - Box::new(|| true), - Box::new(run_kill_test_cases), + let kill_with_empty_id_test = Test::new( + "kill_with_empty_id_test", + Box::new(kill_with_empty_id_test), + ); + let kill_non_existed_container = Test::new( + "kill_non_existed_container", + Box::new(kill_non_existed_container) + ); + let kill_created_container_test = Test::new( + "kill_created_container_test", + Box::new(kill_created_container_test) + ); + let kill_stopped_container_test = Test::new( + "kill_stopped_container_test", + Box::new(kill_stopped_container_test) + ); + let kill_start_container_test = Test::new( + "kill_start_container_test", + Box::new(kill_start_container_test) ); - test_group.add(vec![Box::new(kill_test)]); + test_group.add(vec![Box::new(kill_with_empty_id_test), Box::new(kill_non_existed_container), Box::new(kill_created_container_test), Box::new(kill_stopped_container_test), Box::new(kill_start_container_test)]); test_group }