Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add kill test #2996

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add kill test
Signed-off-by: Akiyama <[email protected]>
  • Loading branch information
YamasouA committed Dec 7, 2024
commit cbbbf7b36b94fe5a1349cee59ced936c8ad9146d
3 changes: 3 additions & 0 deletions tests/contest/contest/src/main.rs
Original file line number Diff line number Diff line change
@@ -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));
106 changes: 106 additions & 0 deletions tests/contest/contest/src/tests/kill/kill_test.rs
Original file line number Diff line number Diff line change
@@ -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),
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use normal test instead of ConditionalTest here, that will not require a conditional function.

test_group.add(vec![Box::new(kill_test)]);
test_group
}
3 changes: 3 additions & 0 deletions tests/contest/contest/src/tests/kill/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod kill_test;

pub use kill_test::get_kill_test;
Original file line number Diff line number Diff line change
@@ -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()
}
1 change: 1 addition & 0 deletions tests/contest/contest/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -22,3 +22,4 @@ pub mod seccomp;
pub mod seccomp_notify;
pub mod sysctl;
pub mod tlb;
pub mod kill;