From cbbbf7b36b94fe5a1349cee59ced936c8ad9146d Mon Sep 17 00:00:00 2001 From: Akiyama Date: Sun, 17 Nov 2024 04:23:39 +0000 Subject: [PATCH 01/11] add kill test Signed-off-by: Akiyama --- tests/contest/contest/src/main.rs | 3 + .../contest/src/tests/kill/kill_test.rs | 106 ++++++++++++++++++ tests/contest/contest/src/tests/kill/mod.rs | 3 + .../tests/lifecycle/container_lifecycle.rs | 8 ++ tests/contest/contest/src/tests/mod.rs | 1 + 5 files changed, 121 insertions(+) create mode 100644 tests/contest/contest/src/tests/kill/kill_test.rs create mode 100644 tests/contest/contest/src/tests/kill/mod.rs diff --git a/tests/contest/contest/src/main.rs b/tests/contest/contest/src/main.rs index bb1019ca8..1a499f063 100644 --- a/tests/contest/contest/src/main.rs +++ b/tests/contest/contest/src/main.rs @@ -32,6 +32,7 @@ use crate::tests::seccomp::get_seccomp_test; use crate::tests::seccomp_notify::get_seccomp_notify_test; use crate::tests::sysctl::get_sysctl_test; use crate::tests::tlb::get_tlb_test; +use crate::tests::kill::get_kill_test; use crate::utils::support::{set_runtime_path, set_runtimetest_path}; #[derive(Parser, Debug)] @@ -125,6 +126,7 @@ fn main() -> Result<()> { let process_rlimtis = get_process_rlimits_test(); let no_pivot = get_no_pivot_test(); let process_oom_score_adj = get_process_oom_score_adj_test(); + let kill = get_kill_test(); tm.add_test_group(Box::new(cl)); tm.add_test_group(Box::new(cc)); @@ -154,6 +156,7 @@ fn main() -> Result<()> { tm.add_test_group(Box::new(process_rlimtis)); tm.add_test_group(Box::new(no_pivot)); tm.add_test_group(Box::new(process_oom_score_adj)); + tm.add_test_group(Box::new(kill)); tm.add_test_group(Box::new(io_priority_test)); tm.add_cleanup(Box::new(cgroups::cleanup_v1)); diff --git a/tests/contest/contest/src/tests/kill/kill_test.rs b/tests/contest/contest/src/tests/kill/kill_test.rs new file mode 100644 index 000000000..4edc79ef1 --- /dev/null +++ b/tests/contest/contest/src/tests/kill/kill_test.rs @@ -0,0 +1,106 @@ +use test_framework::{ConditionalTest, TestGroup, TestResult}; +use crate::tests::lifecycle::ContainerLifecycle; +use anyhow::anyhow; + +fn run_kill_test_cases() -> 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")), + }, + )); + + // 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")), + }, + )); + + // 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")), + }, + )); + + // 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")), + }, + )); + + // kill start container + match container.create() { + 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!(), + } + 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")), + } + + for (name, result) in results { + if let TestResult::Failed(err) = result { + return TestResult::Failed(anyhow!("Test '{}' failed: {:?}", name, err)); + } + } + + TestResult::Passed +} + +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), + ); + test_group.add(vec![Box::new(kill_test)]); + test_group +} diff --git a/tests/contest/contest/src/tests/kill/mod.rs b/tests/contest/contest/src/tests/kill/mod.rs new file mode 100644 index 000000000..ff6cfe9fd --- /dev/null +++ b/tests/contest/contest/src/tests/kill/mod.rs @@ -0,0 +1,3 @@ +mod kill_test; + +pub use kill_test::get_kill_test; diff --git a/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs b/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs index 6b925feea..639c00c53 100644 --- a/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs +++ b/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs @@ -33,6 +33,14 @@ impl ContainerLifecycle { } } + pub fn set_id(&mut self, id: &str) { + self.container_id = id.to_string(); + } + + pub fn get_id(&self) -> &str { + &self.container_id + } + pub fn create(&self) -> TestResult { create::create(self.project_path.path(), &self.container_id).into() } diff --git a/tests/contest/contest/src/tests/mod.rs b/tests/contest/contest/src/tests/mod.rs index 6e8e39be8..0c46d42e7 100644 --- a/tests/contest/contest/src/tests/mod.rs +++ b/tests/contest/contest/src/tests/mod.rs @@ -22,3 +22,4 @@ pub mod seccomp; pub mod seccomp_notify; pub mod sysctl; pub mod tlb; +pub mod kill; From 00d3c5eb8c6933ff4c574a908fffc04d3d67c22f Mon Sep 17 00:00:00 2001 From: Akiyama Date: Sun, 17 Nov 2024 06:08:10 +0000 Subject: [PATCH 02/11] lint Signed-off-by: Akiyama --- tests/contest/contest/src/main.rs | 2 +- tests/contest/contest/src/tests/kill/kill_test.rs | 11 ++++++++--- .../src/tests/lifecycle/container_lifecycle.rs | 2 +- tests/contest/contest/src/tests/mod.rs | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/contest/contest/src/main.rs b/tests/contest/contest/src/main.rs index 1a499f063..e2eda3671 100644 --- a/tests/contest/contest/src/main.rs +++ b/tests/contest/contest/src/main.rs @@ -16,6 +16,7 @@ use crate::tests::hooks::get_hooks_tests; use crate::tests::hostname::get_hostname_test; use crate::tests::intel_rdt::get_intel_rdt_test; use crate::tests::io_priority::get_io_priority_test; +use crate::tests::kill::get_kill_test; use crate::tests::lifecycle::{ContainerCreate, ContainerLifecycle}; use crate::tests::linux_ns_itype::get_ns_itype_tests; use crate::tests::mounts_recursive::get_mounts_recursive_test; @@ -32,7 +33,6 @@ use crate::tests::seccomp::get_seccomp_test; use crate::tests::seccomp_notify::get_seccomp_notify_test; use crate::tests::sysctl::get_sysctl_test; use crate::tests::tlb::get_tlb_test; -use crate::tests::kill::get_kill_test; use crate::utils::support::{set_runtime_path, set_runtimetest_path}; #[derive(Parser, Debug)] diff --git a/tests/contest/contest/src/tests/kill/kill_test.rs b/tests/contest/contest/src/tests/kill/kill_test.rs index 4edc79ef1..1f9f7514f 100644 --- a/tests/contest/contest/src/tests/kill/kill_test.rs +++ b/tests/contest/contest/src/tests/kill/kill_test.rs @@ -1,6 +1,7 @@ +use anyhow::anyhow; use test_framework::{ConditionalTest, TestGroup, TestResult}; + use crate::tests::lifecycle::ContainerLifecycle; -use anyhow::anyhow; fn run_kill_test_cases() -> TestResult { let mut container = ContainerLifecycle::new(); @@ -39,7 +40,9 @@ fn run_kill_test_cases() -> TestResult { "kill created container", match container.kill() { TestResult::Passed => TestResult::Passed, - TestResult::Failed(_) => TestResult::Failed(anyhow!("Expected success but got failure")), + TestResult::Failed(_) => { + TestResult::Failed(anyhow!("Expected success but got failure")) + } _ => TestResult::Failed(anyhow!("Unexpected test result")), }, )); @@ -75,7 +78,9 @@ fn run_kill_test_cases() -> TestResult { "kill running container", match container.kill() { TestResult::Passed => TestResult::Passed, - TestResult::Failed(_) => TestResult::Failed(anyhow!("Expected success but got failure")), + TestResult::Failed(_) => { + TestResult::Failed(anyhow!("Expected success but got failure")) + } _ => TestResult::Failed(anyhow!("Unexpected test result")), }, )); diff --git a/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs b/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs index 639c00c53..231a2ea1b 100644 --- a/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs +++ b/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs @@ -33,7 +33,7 @@ impl ContainerLifecycle { } } - pub fn set_id(&mut self, id: &str) { + pub fn set_id(&mut self, id: &str) { self.container_id = id.to_string(); } diff --git a/tests/contest/contest/src/tests/mod.rs b/tests/contest/contest/src/tests/mod.rs index 0c46d42e7..a741016f3 100644 --- a/tests/contest/contest/src/tests/mod.rs +++ b/tests/contest/contest/src/tests/mod.rs @@ -6,6 +6,7 @@ pub mod hooks; pub mod hostname; pub mod intel_rdt; pub mod io_priority; +pub mod kill; pub mod lifecycle; pub mod linux_ns_itype; pub mod mounts_recursive; @@ -22,4 +23,3 @@ pub mod seccomp; pub mod seccomp_notify; pub mod sysctl; pub mod tlb; -pub mod kill; From 72ad34d3e428f141707460089e69463f8015dce3 Mon Sep 17 00:00:00 2001 From: Yusuke Sakurai Date: Sun, 27 Oct 2024 19:13:33 +0900 Subject: [PATCH 03/11] add test process_oom_score_adj Signed-off-by: Yusuke Sakurai Signed-off-by: Akiyama --- tests/contest/contest/src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/contest/contest/src/main.rs b/tests/contest/contest/src/main.rs index e2eda3671..06e6a543e 100644 --- a/tests/contest/contest/src/main.rs +++ b/tests/contest/contest/src/main.rs @@ -128,6 +128,7 @@ fn main() -> Result<()> { let process_oom_score_adj = get_process_oom_score_adj_test(); let kill = get_kill_test(); + tm.add_test_group(Box::new(cl)); tm.add_test_group(Box::new(cc)); tm.add_test_group(Box::new(huge_tlb)); From 6d9ecd5fe34f27aeb5626971675bd83c4e43191c Mon Sep 17 00:00:00 2001 From: Akiyama Date: Sat, 7 Dec 2024 05:00:29 +0000 Subject: [PATCH 04/11] 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 } From 01761ffa3a3cea155b1760c9b3717b38269a1f7c Mon Sep 17 00:00:00 2001 From: Akiyama Date: Sat, 7 Dec 2024 08:36:29 +0000 Subject: [PATCH 05/11] fix test Signed-off-by: Akiyama --- .../contest/src/tests/kill/kill_test.rs | 56 ++++++++++++------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/tests/contest/contest/src/tests/kill/kill_test.rs b/tests/contest/contest/src/tests/kill/kill_test.rs index ad793a3c9..78678e6f5 100644 --- a/tests/contest/contest/src/tests/kill/kill_test.rs +++ b/tests/contest/contest/src/tests/kill/kill_test.rs @@ -8,42 +8,55 @@ fn kill_with_empty_id_test() -> TestResult { // kill with empty id container.set_id(""); - match container.kill() { + let result = match container.kill() { TestResult::Failed(_) => TestResult::Passed, TestResult::Passed => TestResult::Failed(anyhow!("Expected failure but got success")), _ => TestResult::Failed(anyhow!("Unexpected test result")), - } + }; + container.delete(); + result } fn kill_non_existed_container() -> TestResult { let mut container = ContainerLifecycle::new(); + // kill for non existed container container.set_id("non-existent-container-id"); - match container.kill() { + let result = match container.kill() { TestResult::Failed(_) => TestResult::Passed, TestResult::Passed => TestResult::Failed(anyhow!("Expected failure but got success")), _ => TestResult::Failed(anyhow!("Unexpected test result")), - } + }; + container.delete(); + result } fn kill_created_container_test() -> TestResult { let container = ContainerLifecycle::new(); + // kill created container match container.create() { TestResult::Passed => {} _ => return TestResult::Failed(anyhow!("Failed to create container")), } - match container.kill() { + let result = match container.kill() { TestResult::Passed => TestResult::Passed, TestResult::Failed(_) => { TestResult::Failed(anyhow!("Expected success but got failure")) } _ => TestResult::Failed(anyhow!("Unexpected test result")), - } + }; + container.delete(); + result } fn kill_stopped_container_test() -> TestResult { let container = ContainerLifecycle::new(); + // kill stopped container + match container.create() { + TestResult::Passed => {} + _ => return TestResult::Failed(anyhow!("Failed to create container")), + } match container.delete() { TestResult::Passed => {} _ => return TestResult::Failed(anyhow!("Failed to delete container")), @@ -58,6 +71,7 @@ fn kill_stopped_container_test() -> TestResult { fn kill_start_container_test() -> TestResult { let container = ContainerLifecycle::new(); + // kill start container match container.create() { TestResult::Passed => {} @@ -65,29 +79,27 @@ fn kill_start_container_test() -> TestResult { } 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)); + } + _ => unreachable!(), } - match container.kill() { + let result = 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")), - // } + }; + container.delete(); + result } pub fn get_kill_test() -> TestGroup { let mut test_group = TestGroup::new("kill_container"); + let kill_with_empty_id_test = Test::new( "kill_with_empty_id_test", Box::new(kill_with_empty_id_test), @@ -108,6 +120,12 @@ pub fn get_kill_test() -> TestGroup { "kill_start_container_test", Box::new(kill_start_container_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.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 } From c2ed1170ab745fb7ed31c5b3844a7c8bca8eb3b9 Mon Sep 17 00:00:00 2001 From: Yusuke Sakurai Date: Sun, 27 Oct 2024 19:13:33 +0900 Subject: [PATCH 06/11] add test process_oom_score_adj Signed-off-by: Yusuke Sakurai Signed-off-by: Akiyama --- tests/contest/contest/src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/contest/contest/src/main.rs b/tests/contest/contest/src/main.rs index 06e6a543e..e2eda3671 100644 --- a/tests/contest/contest/src/main.rs +++ b/tests/contest/contest/src/main.rs @@ -128,7 +128,6 @@ fn main() -> Result<()> { let process_oom_score_adj = get_process_oom_score_adj_test(); let kill = get_kill_test(); - tm.add_test_group(Box::new(cl)); tm.add_test_group(Box::new(cc)); tm.add_test_group(Box::new(huge_tlb)); From c736a237b27856a394203ae05ce50143edca9287 Mon Sep 17 00:00:00 2001 From: Akiyama Date: Mon, 9 Dec 2024 14:51:40 +0000 Subject: [PATCH 07/11] fix format, error message, use test_result! Signed-off-by: Akiyama --- .../contest/src/tests/kill/kill_test.rs | 56 ++++++++----------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/tests/contest/contest/src/tests/kill/kill_test.rs b/tests/contest/contest/src/tests/kill/kill_test.rs index 78678e6f5..f823fb4ea 100644 --- a/tests/contest/contest/src/tests/kill/kill_test.rs +++ b/tests/contest/contest/src/tests/kill/kill_test.rs @@ -1,5 +1,5 @@ use anyhow::anyhow; -use test_framework::{Test, TestGroup, TestResult}; +use test_framework::{test_result, Test, TestGroup, TestResult}; use crate::tests::lifecycle::ContainerLifecycle; @@ -10,8 +10,12 @@ fn kill_with_empty_id_test() -> TestResult { container.set_id(""); let result = match container.kill() { TestResult::Failed(_) => TestResult::Passed, - TestResult::Passed => TestResult::Failed(anyhow!("Expected failure but got success")), - _ => TestResult::Failed(anyhow!("Unexpected test result")), + TestResult::Passed => TestResult::Failed(anyhow!( + "Expected killing container with empty id to fail, but was successful" + )), + _ => TestResult::Failed(anyhow!( + "Unexpected killing container with empty id test result" + )), }; container.delete(); result @@ -24,8 +28,12 @@ fn kill_non_existed_container() -> TestResult { container.set_id("non-existent-container-id"); let result = match container.kill() { TestResult::Failed(_) => TestResult::Passed, - TestResult::Passed => TestResult::Failed(anyhow!("Expected failure but got success")), - _ => TestResult::Failed(anyhow!("Unexpected test result")), + TestResult::Passed => TestResult::Failed(anyhow!( + "Expected killing non existed container to fail, but was successful" + )), + _ => TestResult::Failed(anyhow!( + "Unexpected killing non existed container test result" + )), }; container.delete(); result @@ -38,13 +46,7 @@ fn kill_created_container_test() -> TestResult { TestResult::Passed => {} _ => return TestResult::Failed(anyhow!("Failed to create container")), } - let result = match container.kill() { - TestResult::Passed => TestResult::Passed, - TestResult::Failed(_) => { - TestResult::Failed(anyhow!("Expected success but got failure")) - } - _ => TestResult::Failed(anyhow!("Unexpected test result")), - }; + let result = test_result!(container.kill()); container.delete(); result } @@ -67,15 +69,14 @@ fn kill_stopped_container_test() -> TestResult { _ => 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() { @@ -85,47 +86,38 @@ fn kill_start_container_test() -> TestResult { } _ => unreachable!(), } - let result = match container.kill() { - TestResult::Passed => TestResult::Passed, - TestResult::Failed(_) => { - TestResult::Failed(anyhow!("Expected success but got failure")) - } - _ => TestResult::Failed(anyhow!("Unexpected test result")), - }; + let result = test_result!(container.kill()); container.delete(); result } - pub fn get_kill_test() -> TestGroup { let mut test_group = TestGroup::new("kill_container"); - let kill_with_empty_id_test = Test::new( - "kill_with_empty_id_test", - Box::new(kill_with_empty_id_test), - ); + 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) + Box::new(kill_non_existed_container), ); let kill_created_container_test = Test::new( "kill_created_container_test", - Box::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) + Box::new(kill_stopped_container_test), ); let kill_start_container_test = Test::new( "kill_start_container_test", - Box::new(kill_start_container_test) + Box::new(kill_start_container_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) + Box::new(kill_start_container_test), ]); test_group } From 9035e8083dfbaf74a0cd11b8d6de04d0b3b2a073 Mon Sep 17 00:00:00 2001 From: Akiyama Date: Thu, 12 Dec 2024 12:58:57 +0000 Subject: [PATCH 08/11] don't use test_result macro Signed-off-by: Akiyama --- tests/contest/contest/src/tests/kill/kill_test.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/contest/contest/src/tests/kill/kill_test.rs b/tests/contest/contest/src/tests/kill/kill_test.rs index f823fb4ea..10c944092 100644 --- a/tests/contest/contest/src/tests/kill/kill_test.rs +++ b/tests/contest/contest/src/tests/kill/kill_test.rs @@ -1,5 +1,5 @@ use anyhow::anyhow; -use test_framework::{test_result, Test, TestGroup, TestResult}; +use test_framework::{Test, TestGroup, TestResult}; use crate::tests::lifecycle::ContainerLifecycle; @@ -46,7 +46,7 @@ fn kill_created_container_test() -> TestResult { TestResult::Passed => {} _ => return TestResult::Failed(anyhow!("Failed to create container")), } - let result = test_result!(container.kill()); + let result = container.kill(); container.delete(); result } @@ -86,7 +86,7 @@ fn kill_start_container_test() -> TestResult { } _ => unreachable!(), } - let result = test_result!(container.kill()); + let result = container.kill(); container.delete(); result } From 71e82b2c105858676d5f009451b5a1b56e9f9ce9 Mon Sep 17 00:00:00 2001 From: YamasouA Date: Sun, 15 Dec 2024 22:46:23 +0900 Subject: [PATCH 09/11] add util func for kill_test Signed-off-by: Akiyama --- .../contest/src/tests/kill/kill_test.rs | 30 +++++++++++++++---- .../tests/lifecycle/container_lifecycle.rs | 30 +++++++++++++++++-- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/tests/contest/contest/src/tests/kill/kill_test.rs b/tests/contest/contest/src/tests/kill/kill_test.rs index 10c944092..129b38649 100644 --- a/tests/contest/contest/src/tests/kill/kill_test.rs +++ b/tests/contest/contest/src/tests/kill/kill_test.rs @@ -1,8 +1,22 @@ use anyhow::anyhow; +use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder}; use test_framework::{Test, TestGroup, TestResult}; use crate::tests::lifecycle::ContainerLifecycle; +fn create_spec(args: &[&str]) -> Result { + let spec = SpecBuilder::default() + .process( + ProcessBuilder::default() + .args(args.iter().map(|&a| a.to_string()).collect()) + .build() + .context("failed to build process spec")? + ) + .build() + .context("failed to build spec")?; + Ok(spec) +} + fn kill_with_empty_id_test() -> TestResult { let mut container = ContainerLifecycle::new(); @@ -53,28 +67,32 @@ fn kill_created_container_test() -> TestResult { fn kill_stopped_container_test() -> TestResult { let container = ContainerLifecycle::new(); + let spec = create_spec(&["true"]).unwrap(); // kill stopped container - match container.create() { + match container.create_with_spec(spec) { TestResult::Passed => {} _ => return TestResult::Failed(anyhow!("Failed to create container")), } - match container.delete() { + match container.start() { TestResult::Passed => {} - _ => return TestResult::Failed(anyhow!("Failed to delete container")), + _ => return TestResult::Failed(anyhow!("Failed to start container")), } - match container.kill() { + let result = match container.kill() { TestResult::Failed(_) => TestResult::Passed, TestResult::Passed => TestResult::Failed(anyhow!("Expected failure but got success")), _ => TestResult::Failed(anyhow!("Unexpected test result")), - } + }; + container.delete(); + result } fn kill_start_container_test() -> TestResult { let container = ContainerLifecycle::new(); + let spec = create_spec(&["sleep", "30"]).unwrap(); // kill start container - match container.create() { + match container.create_with_spec(spec) { TestResult::Passed => {} _ => return TestResult::Failed(anyhow!("Failed to recreate container")), } diff --git a/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs b/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs index 231a2ea1b..ff1f2459b 100644 --- a/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs +++ b/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs @@ -1,11 +1,12 @@ +use std::string; use std::thread::sleep; -use std::time::Duration; +use std::time::{Duration, Instant}; use test_framework::{TestResult, TestableGroup}; use super::util::criu_installed; use super::{checkpoint, create, delete, exec, kill, start, state}; -use crate::utils::{generate_uuid, prepare_bundle}; +use crate::utils::{generate_uuid, prepare_bundle, get_state}; // By experimenting, somewhere around 50 is enough for youki process // to get the kill signal and shut down @@ -45,6 +46,11 @@ impl ContainerLifecycle { create::create(self.project_path.path(), &self.container_id).into() } + pub fn create_with_spec(&self, spec: Spec) -> TestResult { + set_config(&self.project_path, &spec).unwrap(); + create::create(self.project_path.path(), &self.container_id).into() + } + #[allow(dead_code)] pub fn exec(&self, cmd: Vec<&str>, expected_output: Option<&str>) -> TestResult { exec::exec( @@ -94,6 +100,26 @@ impl ContainerLifecycle { &self.container_id, ) } + + pub fn waiting_for_status(&self, retry_timeout: Duration, poll_interval: Duration, target_status: string) -> TestResult { + let start = Instant::now(); + while start.elapsed() < retry_timeout { + let (out, err) = get_state(&self.container_id, self.project_path).unwrap(); + if !err.is_empty() { + self.kill(); + self.delete(); + return TestResult::Failed(anyhow!("error in state : {}", err)); + } + + let state: State = serde_json::from_str(&out).unwrap(); + + if state.status == target_status { + return TestResult::Passed + } + sleep(poll_interval); + } + return TestResult::Failed(anyhow!("error pod status is not update")); + } } impl TestableGroup for ContainerLifecycle { From c623e215455e8637083a864a51c2a97608fcaf06 Mon Sep 17 00:00:00 2001 From: Akiyama Date: Sun, 15 Dec 2024 14:03:04 +0000 Subject: [PATCH 10/11] can compile and use formatter Signed-off-by: Akiyama --- .../contest/src/tests/kill/kill_test.rs | 7 ++++--- .../tests/lifecycle/container_lifecycle.rs | 20 ++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/tests/contest/contest/src/tests/kill/kill_test.rs b/tests/contest/contest/src/tests/kill/kill_test.rs index 129b38649..f033eb0a6 100644 --- a/tests/contest/contest/src/tests/kill/kill_test.rs +++ b/tests/contest/contest/src/tests/kill/kill_test.rs @@ -1,16 +1,17 @@ -use anyhow::anyhow; +use anyhow::{anyhow, Context, Result}; use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder}; use test_framework::{Test, TestGroup, TestResult}; use crate::tests::lifecycle::ContainerLifecycle; fn create_spec(args: &[&str]) -> Result { + let args_vec: Vec = args.iter().map(|&a| a.to_string()).collect(); let spec = SpecBuilder::default() .process( ProcessBuilder::default() - .args(args.iter().map(|&a| a.to_string()).collect()) + .args(args_vec) .build() - .context("failed to build process spec")? + .context("failed to build process spec")?, ) .build() .context("failed to build spec")?; diff --git a/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs b/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs index ff1f2459b..d69653ada 100644 --- a/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs +++ b/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs @@ -1,12 +1,13 @@ -use std::string; use std::thread::sleep; use std::time::{Duration, Instant}; +use anyhow::anyhow; +use oci_spec::runtime::Spec; use test_framework::{TestResult, TestableGroup}; use super::util::criu_installed; use super::{checkpoint, create, delete, exec, kill, start, state}; -use crate::utils::{generate_uuid, prepare_bundle, get_state}; +use crate::utils::{generate_uuid, get_state, prepare_bundle, set_config, State}; // By experimenting, somewhere around 50 is enough for youki process // to get the kill signal and shut down @@ -101,20 +102,25 @@ impl ContainerLifecycle { ) } - pub fn waiting_for_status(&self, retry_timeout: Duration, poll_interval: Duration, target_status: string) -> TestResult { + pub fn waiting_for_status( + &self, + retry_timeout: Duration, + poll_interval: Duration, + target_status: String, + ) -> TestResult { let start = Instant::now(); while start.elapsed() < retry_timeout { - let (out, err) = get_state(&self.container_id, self.project_path).unwrap(); + let (out, err) = get_state(&self.container_id, &self.project_path).unwrap(); if !err.is_empty() { self.kill(); self.delete(); return TestResult::Failed(anyhow!("error in state : {}", err)); } - + let state: State = serde_json::from_str(&out).unwrap(); - + if state.status == target_status { - return TestResult::Passed + return TestResult::Passed; } sleep(poll_interval); } From 5dc2dbfedb06b1b731d46ee479d83856563ead33 Mon Sep 17 00:00:00 2001 From: Akiyama Date: Sun, 15 Dec 2024 14:24:32 +0000 Subject: [PATCH 11/11] use waiting_for_status Signed-off-by: Akiyama --- tests/contest/contest/src/tests/kill/kill_test.rs | 4 ++++ .../contest/src/tests/lifecycle/container_lifecycle.rs | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/contest/contest/src/tests/kill/kill_test.rs b/tests/contest/contest/src/tests/kill/kill_test.rs index f033eb0a6..4ffcb31da 100644 --- a/tests/contest/contest/src/tests/kill/kill_test.rs +++ b/tests/contest/contest/src/tests/kill/kill_test.rs @@ -1,3 +1,5 @@ +use std::time::Duration; + use anyhow::{anyhow, Context, Result}; use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder}; use test_framework::{Test, TestGroup, TestResult}; @@ -79,6 +81,7 @@ fn kill_stopped_container_test() -> TestResult { TestResult::Passed => {} _ => return TestResult::Failed(anyhow!("Failed to start container")), } + container.waiting_for_status(Duration::from_secs(10), Duration::from_secs(1), "stopped"); let result = match container.kill() { TestResult::Failed(_) => TestResult::Passed, TestResult::Passed => TestResult::Failed(anyhow!("Expected failure but got success")), @@ -105,6 +108,7 @@ fn kill_start_container_test() -> TestResult { } _ => unreachable!(), } + container.waiting_for_status(Duration::from_secs(10), Duration::from_secs(1), "running"); let result = container.kill(); container.delete(); result diff --git a/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs b/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs index d69653ada..c4cdcfd88 100644 --- a/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs +++ b/tests/contest/contest/src/tests/lifecycle/container_lifecycle.rs @@ -106,7 +106,7 @@ impl ContainerLifecycle { &self, retry_timeout: Duration, poll_interval: Duration, - target_status: String, + target_status: &str, ) -> TestResult { let start = Instant::now(); while start.elapsed() < retry_timeout { @@ -124,7 +124,7 @@ impl ContainerLifecycle { } sleep(poll_interval); } - return TestResult::Failed(anyhow!("error pod status is not update")); + TestResult::Failed(anyhow!("error pod status is not update")) } }