This commit is contained in:
2025-04-04 21:21:50 +02:00
parent bf5eb2f6fc
commit 5b006ff328
33 changed files with 2406 additions and 201 deletions

View File

@@ -24,32 +24,32 @@ pub fn run(container: &str, command: &str) -> Result<CommandResult, BuildahError
/// * `container` - The container ID or name
/// * `command` - The command to run
/// * `isolation` - Isolation method (e.g., "chroot", "rootless", "oci")
pub fn run_with_isolation(container: &str, command: &str, isolation: &str) -> Result<CommandResult, BuildahError> {
pub fn bah_run_with_isolation(container: &str, command: &str, isolation: &str) -> Result<CommandResult, BuildahError> {
execute_buildah_command(&["run", "--isolation", isolation, container, "sh", "-c", command])
}
/// Copy files into a container
pub fn copy(container: &str, source: &str, dest: &str) -> Result<CommandResult, BuildahError> {
pub fn bah_copy(container: &str, source: &str, dest: &str) -> Result<CommandResult, BuildahError> {
execute_buildah_command(&["copy", container, source, dest])
}
pub fn add(container: &str, source: &str, dest: &str) -> Result<CommandResult, BuildahError> {
pub fn bah_add(container: &str, source: &str, dest: &str) -> Result<CommandResult, BuildahError> {
execute_buildah_command(&["add", container, source, dest])
}
/// Commit a container to an image
pub fn commit(container: &str, image_name: &str) -> Result<CommandResult, BuildahError> {
pub fn bah_commit(container: &str, image_name: &str) -> Result<CommandResult, BuildahError> {
execute_buildah_command(&["commit", container, image_name])
}
/// Remove a container
pub fn remove(container: &str) -> Result<CommandResult, BuildahError> {
pub fn bah_remove(container: &str) -> Result<CommandResult, BuildahError> {
execute_buildah_command(&["rm", container])
}
/// List containers
pub fn list() -> Result<CommandResult, BuildahError> {
pub fn bah_list() -> Result<CommandResult, BuildahError> {
execute_buildah_command(&["containers"])
}
@@ -61,7 +61,7 @@ pub fn list() -> Result<CommandResult, BuildahError> {
/// * `context_dir` - The directory containing the Containerfile/Dockerfile (usually ".")
/// * `file` - Optional path to a specific Containerfile/Dockerfile
/// * `isolation` - Optional isolation method (e.g., "chroot", "rootless", "oci")
pub fn build(tag: Option<&str>, context_dir: &str, file: &str, isolation: Option<&str>) -> Result<CommandResult, BuildahError> {
pub fn bah_build(tag: Option<&str>, context_dir: &str, file: &str, isolation: Option<&str>) -> Result<CommandResult, BuildahError> {
let mut args = Vec::new();
args.push("build");

View File

@@ -64,34 +64,35 @@ mod tests {
test_execute_buildah_command(&["from", image])
}
fn test_run(container: &str, command: &str, isolation: Option<&str>) -> Result<CommandResult, BuildahError> {
match isolation {
Some(iso) => test_execute_buildah_command(&["run", "--isolation", iso, container, "sh", "-c", command]),
None => test_execute_buildah_command(&["run", container, "sh", "-c", command])
}
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> {
fn test_bah_run_with_isolation(container: &str, command: &str, isolation: &str) -> Result<CommandResult, BuildahError> {
test_execute_buildah_command(&["run", "--isolation", isolation, container, "sh", "-c", command])
}
fn test_bah_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> {
fn test_bah_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> {
fn test_bah_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> {
fn test_bah_remove(container: &str) -> Result<CommandResult, BuildahError> {
test_execute_buildah_command(&["rm", container])
}
fn test_list() -> Result<CommandResult, BuildahError> {
fn test_bah_list() -> Result<CommandResult, BuildahError> {
test_execute_buildah_command(&["containers"])
}
fn test_build(tag: Option<&str>, context_dir: &str, file: Option<&str>) -> Result<CommandResult, BuildahError> {
fn test_bah_build(tag: Option<&str>, context_dir: &str, file: &str, isolation: Option<&str>) -> Result<CommandResult, BuildahError> {
let mut args = Vec::new();
args.push("build");
@@ -100,11 +101,14 @@ mod tests {
args.push(tag_value);
}
if let Some(file_path) = file {
args.push("-f");
args.push(file_path);
if let Some(isolation_value) = isolation {
args.push("--isolation");
args.push(isolation_value);
}
args.push("-f");
args.push(file);
args.push(context_dir);
test_execute_buildah_command(&args)
@@ -131,26 +135,34 @@ mod tests {
let command = "echo hello";
// Test without isolation
let result = test_run(container, command, None);
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_bah_run_with_isolation_function() {
reset_test_state();
// Test with isolation
let result = test_run(container, command, Some("chroot"));
let container = "my-container";
let command = "echo hello";
let isolation = "chroot";
let result = test_bah_run_with_isolation(container, command, isolation);
assert!(result.is_ok());
let cmd = get_last_command();
assert_eq!(cmd, vec!["run", "--isolation", "chroot", "my-container", "sh", "-c", "echo hello"]);
}
#[test]
fn test_copy_function() {
fn test_bah_copy_function() {
reset_test_state();
let container = "my-container";
let source = "/local/path";
let dest = "/container/path";
let result = test_copy(container, source, dest);
let result = test_bah_copy(container, source, dest);
assert!(result.is_ok());
let cmd = get_last_command();
@@ -158,13 +170,13 @@ mod tests {
}
#[test]
fn test_add_function() {
fn test_bah_add_function() {
reset_test_state();
let container = "my-container";
let source = "/local/path";
let dest = "/container/path";
let result = test_add(container, source, dest);
let result = test_bah_add(container, source, dest);
assert!(result.is_ok());
let cmd = get_last_command();
@@ -172,12 +184,12 @@ mod tests {
}
#[test]
fn test_commit_function() {
fn test_bah_commit_function() {
reset_test_state();
let container = "my-container";
let image_name = "my-image:latest";
let result = test_commit(container, image_name);
let result = test_bah_commit(container, image_name);
assert!(result.is_ok());
let cmd = get_last_command();
@@ -185,11 +197,11 @@ mod tests {
}
#[test]
fn test_remove_function() {
fn test_bah_remove_function() {
reset_test_state();
let container = "my-container";
let result = test_remove(container);
let result = test_bah_remove(container);
assert!(result.is_ok());
let cmd = get_last_command();
@@ -197,10 +209,10 @@ mod tests {
}
#[test]
fn test_list_function() {
fn test_bah_list_function() {
reset_test_state();
let result = test_list();
let result = test_bah_list();
assert!(result.is_ok());
let cmd = get_last_command();
@@ -208,26 +220,26 @@ mod tests {
}
#[test]
fn test_build_function() {
fn test_bah_build_function() {
reset_test_state();
// Test with tag and context directory
let result = test_build(Some("my-app:latest"), ".", None);
// Test with tag, context directory, file, and no isolation
let result = test_bah_build(Some("my-app:latest"), ".", "Dockerfile", None);
assert!(result.is_ok());
let cmd = get_last_command();
assert_eq!(cmd, vec!["build", "-t", "my-app:latest", "."]);
assert_eq!(cmd, vec!["build", "-t", "my-app:latest", "-f", "Dockerfile", "."]);
// Test with tag, context directory, and file
let result = test_build(Some("my-app:latest"), ".", Some("Dockerfile.custom"));
// Test with tag, context directory, file, and isolation
let result = test_bah_build(Some("my-app:latest"), ".", "Dockerfile.custom", Some("chroot"));
assert!(result.is_ok());
let cmd = get_last_command();
assert_eq!(cmd, vec!["build", "-t", "my-app:latest", "-f", "Dockerfile.custom", "."]);
assert_eq!(cmd, vec!["build", "-t", "my-app:latest", "--isolation", "chroot", "-f", "Dockerfile.custom", "."]);
// Test with just context directory
let result = test_build(None, ".", None);
// Test with just context directory and file
let result = test_bah_build(None, ".", "Dockerfile", None);
assert!(result.is_ok());
let cmd = get_last_command();
assert_eq!(cmd, vec!["build", "."]);
assert_eq!(cmd, vec!["build", "-f", "Dockerfile", "."]);
}
#[test]

View File

@@ -190,7 +190,7 @@ pub fn image_commit(container: &str, image_name: &str, format: Option<&str>, squ
///
/// # Returns
/// * Result with command output or error
pub fn config(container: &str, options: HashMap<String, String>) -> Result<CommandResult, BuildahError> {
pub fn bah_config(container: &str, options: HashMap<String, String>) -> Result<CommandResult, BuildahError> {
let mut args_owned: Vec<String> = Vec::new();
args_owned.push("config".to_string());