added tests for container commands
This commit is contained in:
197
src/virt/buildah/containers_test.rs
Normal file
197
src/virt/buildah/containers_test.rs
Normal file
@@ -0,0 +1,197 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::process::CommandResult;
|
||||
use crate::virt::buildah::BuildahError;
|
||||
use std::sync::Mutex;
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
// Create a test-specific implementation of the containers module functions
|
||||
// that we can use to verify the correct arguments are passed
|
||||
|
||||
lazy_static! {
|
||||
static ref LAST_COMMAND: Mutex<Vec<String>> = Mutex::new(Vec::new());
|
||||
static ref SHOULD_FAIL: Mutex<bool> = Mutex::new(false);
|
||||
}
|
||||
|
||||
fn reset_test_state() {
|
||||
let mut cmd = LAST_COMMAND.lock().unwrap();
|
||||
cmd.clear();
|
||||
let mut fail = SHOULD_FAIL.lock().unwrap();
|
||||
*fail = false;
|
||||
}
|
||||
|
||||
fn set_should_fail(fail: bool) {
|
||||
let mut should_fail = SHOULD_FAIL.lock().unwrap();
|
||||
*should_fail = fail;
|
||||
}
|
||||
|
||||
fn get_last_command() -> Vec<String> {
|
||||
let cmd = LAST_COMMAND.lock().unwrap();
|
||||
cmd.clone()
|
||||
}
|
||||
|
||||
// Test-specific implementation of execute_buildah_command
|
||||
fn test_execute_buildah_command(args: &[&str]) -> Result<CommandResult, BuildahError> {
|
||||
// Record the command
|
||||
{
|
||||
let mut cmd = LAST_COMMAND.lock().unwrap();
|
||||
cmd.clear();
|
||||
for arg in args {
|
||||
cmd.push(arg.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we should fail
|
||||
let should_fail = {
|
||||
let fail = SHOULD_FAIL.lock().unwrap();
|
||||
*fail
|
||||
};
|
||||
|
||||
if should_fail {
|
||||
Err(BuildahError::CommandFailed("Command failed".to_string()))
|
||||
} else {
|
||||
Ok(CommandResult {
|
||||
stdout: "mock stdout".to_string(),
|
||||
stderr: "".to_string(),
|
||||
success: true,
|
||||
code: 0,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test implementations of the container functions
|
||||
fn test_from(image: &str) -> Result<CommandResult, BuildahError> {
|
||||
test_execute_buildah_command(&["from", image])
|
||||
}
|
||||
|
||||
fn test_run(container: &str, command: &str) -> Result<CommandResult, BuildahError> {
|
||||
test_execute_buildah_command(&["run", container, "sh", "-c", command])
|
||||
}
|
||||
|
||||
fn test_copy(container: &str, source: &str, dest: &str) -> Result<CommandResult, BuildahError> {
|
||||
test_execute_buildah_command(&["copy", container, source, dest])
|
||||
}
|
||||
|
||||
fn test_add(container: &str, source: &str, dest: &str) -> Result<CommandResult, BuildahError> {
|
||||
test_execute_buildah_command(&["add", container, source, dest])
|
||||
}
|
||||
|
||||
fn test_commit(container: &str, image_name: &str) -> Result<CommandResult, BuildahError> {
|
||||
test_execute_buildah_command(&["commit", container, image_name])
|
||||
}
|
||||
|
||||
fn test_remove(container: &str) -> Result<CommandResult, BuildahError> {
|
||||
test_execute_buildah_command(&["rm", container])
|
||||
}
|
||||
|
||||
fn test_list() -> Result<CommandResult, BuildahError> {
|
||||
test_execute_buildah_command(&["containers"])
|
||||
}
|
||||
|
||||
// Tests for each function
|
||||
#[test]
|
||||
fn test_from_function() {
|
||||
reset_test_state();
|
||||
|
||||
let image = "alpine:latest";
|
||||
let result = test_from(image);
|
||||
|
||||
assert!(result.is_ok());
|
||||
let cmd = get_last_command();
|
||||
assert_eq!(cmd, vec!["from", "alpine:latest"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_run_function() {
|
||||
reset_test_state();
|
||||
|
||||
let container = "my-container";
|
||||
let command = "echo hello";
|
||||
let result = test_run(container, command);
|
||||
|
||||
assert!(result.is_ok());
|
||||
let cmd = get_last_command();
|
||||
assert_eq!(cmd, vec!["run", "my-container", "sh", "-c", "echo hello"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_copy_function() {
|
||||
reset_test_state();
|
||||
|
||||
let container = "my-container";
|
||||
let source = "/local/path";
|
||||
let dest = "/container/path";
|
||||
let result = test_copy(container, source, dest);
|
||||
|
||||
assert!(result.is_ok());
|
||||
let cmd = get_last_command();
|
||||
assert_eq!(cmd, vec!["copy", "my-container", "/local/path", "/container/path"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_function() {
|
||||
reset_test_state();
|
||||
|
||||
let container = "my-container";
|
||||
let source = "/local/path";
|
||||
let dest = "/container/path";
|
||||
let result = test_add(container, source, dest);
|
||||
|
||||
assert!(result.is_ok());
|
||||
let cmd = get_last_command();
|
||||
assert_eq!(cmd, vec!["add", "my-container", "/local/path", "/container/path"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_commit_function() {
|
||||
reset_test_state();
|
||||
|
||||
let container = "my-container";
|
||||
let image_name = "my-image:latest";
|
||||
let result = test_commit(container, image_name);
|
||||
|
||||
assert!(result.is_ok());
|
||||
let cmd = get_last_command();
|
||||
assert_eq!(cmd, vec!["commit", "my-container", "my-image:latest"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_remove_function() {
|
||||
reset_test_state();
|
||||
|
||||
let container = "my-container";
|
||||
let result = test_remove(container);
|
||||
|
||||
assert!(result.is_ok());
|
||||
let cmd = get_last_command();
|
||||
assert_eq!(cmd, vec!["rm", "my-container"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_list_function() {
|
||||
reset_test_state();
|
||||
|
||||
let result = test_list();
|
||||
|
||||
assert!(result.is_ok());
|
||||
let cmd = get_last_command();
|
||||
assert_eq!(cmd, vec!["containers"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_handling() {
|
||||
reset_test_state();
|
||||
set_should_fail(true);
|
||||
|
||||
let image = "alpine:latest";
|
||||
let result = test_from(image);
|
||||
|
||||
assert!(result.is_err());
|
||||
match result {
|
||||
Err(BuildahError::CommandFailed(msg)) => {
|
||||
assert_eq!(msg, "Command failed");
|
||||
},
|
||||
_ => panic!("Expected CommandFailed error"),
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,6 +1,8 @@
|
||||
mod containers;
|
||||
mod images;
|
||||
mod cmd;
|
||||
#[cfg(test)]
|
||||
mod containers_test;
|
||||
|
||||
use std::fmt;
|
||||
use std::error::Error;
|
||||
|
Reference in New Issue
Block a user