1
0
mirror of https://github.com/containers/youki synced 2025-04-30 13:20:17 +02:00

Add create container tests

This commit is contained in:
Yashodhan Joshi 2021-08-16 14:16:41 +05:30
parent cb9b04fc35
commit b9c3661024
5 changed files with 88 additions and 105 deletions

@ -3,17 +3,17 @@ use crate::testable::{TestResult, TestableGroup};
use std::collections::BTreeMap;
/// This manages all test groups, and thus the tests
pub struct TestManager<'a, T: TestableGroup> {
test_groups: BTreeMap<String, &'a T>,
pub struct TestManager<'a> {
test_groups: BTreeMap<String, &'a dyn TestableGroup>,
}
impl<'a, T: TestableGroup> Default for TestManager<'a, T> {
impl<'a> Default for TestManager<'a> {
fn default() -> Self {
Self::new()
}
}
impl<'a, T: TestableGroup> TestManager<'a, T> {
impl<'a> TestManager<'a> {
/// Create new TestManager
pub fn new() -> Self {
TestManager {
@ -22,7 +22,7 @@ impl<'a, T: TestableGroup> TestManager<'a, T> {
}
/// add a test group to the test manager
pub fn add_test_group(&mut self, tg: &'a T) {
pub fn add_test_group(&mut self, tg: &'a dyn TestableGroup) {
self.test_groups.insert(tg.get_name(), tg);
}
@ -49,7 +49,7 @@ impl<'a, T: TestableGroup> TestManager<'a, T> {
}
/// Run all tests from given group
fn run_test_group(&self, name: &str, tg: &'a T) {
fn run_test_group(&self, name: &str, tg: &'a dyn TestableGroup) {
let results = tg.run_all();
let mut test_vec = Vec::new();
for (name, res) in results.iter() {
@ -61,7 +61,7 @@ impl<'a, T: TestableGroup> TestManager<'a, T> {
/// Run all tests from all tests group
pub fn run_all(&self) {
for (name, tg) in self.test_groups.iter() {
self.run_test_group(name, tg);
self.run_test_group(name, *tg);
}
}
@ -70,7 +70,7 @@ impl<'a, T: TestableGroup> TestManager<'a, T> {
for (test_group_name, tests) in tests.iter() {
if let Some(tg) = self.test_groups.get(test_group_name) {
match tests {
Option::None => self.run_test_group(test_group_name, tg),
Option::None => self.run_test_group(test_group_name, *tg),
Option::Some(tests) => {
let results = tg.run_selected(tests);
let mut test_vec = Vec::new();

@ -6,110 +6,27 @@ mod tests;
use crate::support::cleanup_test;
use crate::support::get_project_path;
use crate::support::initialize_test;
use crate::tests::lifecycle::ContainerLifecycle;
use crate::tests::lifecycle::{ContainerCreate, ContainerLifecycle};
use test_framework::TestManager;
fn main() -> Result<()> {
let mut tm = TestManager::new();
let project_path = get_project_path();
let cl = ContainerLifecycle::new(&project_path);
let cc = ContainerCreate::new(&project_path);
tm.add_test_group(&cl);
if initialize_test(&project_path).is_err() {
bail!("Can not initilize test.")
}
tm.run_all();
//life_cycle_test(&project_path);
if cleanup_test(&project_path).is_err() {
bail!("Can not cleanup test.")
}
tm.add_test_group(&cc);
if initialize_test(&project_path).is_err() {
bail!("Can not initilize test.")
}
//container_create_test(&project_path);
tm.run_all();
if cleanup_test(&project_path).is_err() {
bail!("Can not cleanup test.")
}
Ok(())
}
// This tests the entire lifecycle of the container.
// fn life_cycle_test(project_path: &Path) {
// let container_runtime = Container::new(project_path);
// let create_test = test_builder(
// container_runtime.create(),
// "Create a new container test",
// "This operation must create a new container.",
// );
// let state_test = test_builder(
// container_runtime.state(),
// "Execute state test",
// "This operation must state the container.",
// );
// let start_test = test_builder(
// container_runtime.start(),
// "Execute start test",
// "This operation must start the container.",
// );
// let state_again_test = test_builder(
// container_runtime.state(),
// "Execute state test",
// "This operation must state the container.",
// );
// let kill_test = test_builder(
// container_runtime.kill(),
// "Execute kill test",
// "This operation must kill the container.",
// );
// let delete_test = test_builder(
// container_runtime.delete(),
// "Execute delete test",
// "This operation must delete the container.",
// );
// // print to stdout
// print_test_results(
// "Create comand test suite",
// vec![
// create_test,
// state_test,
// start_test,
// state_again_test,
// kill_test,
// delete_test,
// ],
// );
// }
// This is a test of the create command.
// It follows the `opencontainers/runtime-tools` test case.
// fn container_create_test(project_path: &Path) {
// let container_runtime_with_empty_id = Container::with_container_id(project_path, "");
// let empty_id_test = test_builder(
// !container_runtime_with_empty_id.create(),
// "create with no ID test",
// "This operation MUST generate an error if it is not provided a path to the bundle and the container ID to associate with the container.",
// );
// let uuid = generate_uuid();
// let container_runtime_with_id = Container::with_container_id(project_path, &uuid.to_string());
// let with_id_test = test_builder(
// container_runtime_with_id.create(),
// "create with ID test",
// "This operation MUST create a new container.",
// );
// let container_id_with_exist_id = Container::with_container_id(project_path, &uuid.to_string());
// let exist_id_test = test_builder(
// !container_id_with_exist_id.create(),
// "create with an already existing ID test",
// "If the ID provided is not unique across all containers within the scope of the runtime, or is not valid in any other way, the implementation MUST generate an error and a new container MUST NOT be created.",
// );
// // print to stdout
// print_test_results(
// "Create comand test suite",
// vec![empty_id_test, with_id_test, exist_id_test],
// );
// }

@ -0,0 +1,70 @@
use super::create;
use crate::support::generate_uuid;
use std::path::{Path, PathBuf};
use test_framework::{TestResult, TestableGroup};
pub struct ContainerCreate {
project_path: PathBuf,
container_id: String,
}
impl ContainerCreate {
pub fn new(project_path: &Path) -> Self {
ContainerCreate {
project_path: project_path.to_owned(),
container_id: generate_uuid().to_string(),
}
}
fn create_empty_id(&self) -> TestResult {
let temp = create::create(&self.project_path, "");
match temp {
TestResult::Ok => TestResult::Err(anyhow::anyhow!(
"Container should not have been created with empty id, but was created."
)),
TestResult::Err(_) => TestResult::Ok,
TestResult::Skip => TestResult::Skip,
}
}
fn create_valid_id(&self) -> TestResult {
create::create(&self.project_path, &self.container_id)
}
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);
match temp {
TestResult::Ok => TestResult::Err(anyhow::anyhow!(
"Container should not have been created with same id, but was created."
)),
TestResult::Err(_) => TestResult::Ok,
TestResult::Skip => TestResult::Skip,
}
}
}
impl TestableGroup for ContainerCreate {
fn get_name(&self) -> String {
"create".to_owned()
}
fn run_all(&self) -> Vec<(String, TestResult)> {
vec![
("empty_id".to_owned(), self.create_empty_id()),
("valid_id".to_owned(), self.create_valid_id()),
("duplicate_id".to_owned(), self.create_duplicate_id()),
]
}
fn run_selected(&self, selected: &[&str]) -> Vec<(String, TestResult)> {
let mut ret = Vec::new();
for name in selected {
match *name {
"empty_id" => ret.push(("empty_id".to_owned(), self.create_empty_id())),
"valid_id" => ret.push(("valid_id".to_owned(), self.create_valid_id())),
"duplicate_id" => ret.push(("duplicate_id".to_owned(), self.create_duplicate_id())),
_ => eprintln!("No test named {} in lifecycle", name),
};
}
ret
}
}

@ -17,12 +17,6 @@ impl ContainerLifecycle {
container_id: generate_uuid().to_string(),
}
}
pub fn with_container_id(project_path: &Path, container_id: &str) -> Self {
ContainerLifecycle {
project_path: project_path.to_owned(),
container_id: container_id.to_string(),
}
}
pub fn create(&self) -> TestResult {
create::create(&self.project_path, &self.container_id)

@ -1,3 +1,4 @@
mod container_create;
mod container_lifecycle;
mod create;
mod delete;
@ -5,5 +6,6 @@ mod kill;
mod start;
mod state;
mod util;
pub use container_create::ContainerCreate;
pub use container_lifecycle::ContainerLifecycle;
pub use util::get_result_from_output;