1
0
mirror of https://github.com/containers/youki synced 2024-11-23 01:11:58 +01:00

refactored create call

Signed-off-by: yihuaf <yihuaf@unkies.org>
This commit is contained in:
yihuaf 2023-05-01 19:33:11 +00:00
parent 8f3018bf06
commit c57df38bb9
3 changed files with 37 additions and 32 deletions

@ -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
}
}
}
}

@ -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)]

@ -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),
}
}