sal/virt/tests/nerdctl_tests.rs
Mahmoud-Emad 455f84528b
Some checks are pending
Rhai Tests / Run Rhai Tests (push) Waiting to run
feat: Add support for virt package
- Add sal-virt package to the workspace members
- Update MONOREPO_CONVERSION_PLAN.md to reflect the
  completion of sal-process and sal-virt packages
- Update src/lib.rs to include sal-virt
- Update src/postgresclient to use sal-virt instead of local
  virt module
- Update tests to use sal-virt
2025-06-23 02:37:14 +03:00

163 lines
6.4 KiB
Rust

use sal_virt::nerdctl::{Container, NerdctlError};
#[test]
fn test_container_creation() {
// Test creating a new container
let result = Container::new("test-container");
match result {
Ok(container) => {
assert_eq!(container.name, "test-container");
// Container ID should be None if container doesn't exist
assert!(container.container_id.is_none());
}
Err(NerdctlError::CommandExecutionFailed(_)) => {
// Nerdctl not available - this is expected in CI/test environments
println!("Nerdctl not available - skipping test");
}
Err(e) => {
println!("Nerdctl error (expected in test environment): {:?}", e);
}
}
}
#[test]
fn test_container_from_image() {
// Test creating a container from an image
let result = Container::from_image("test-container", "alpine:latest");
match result {
Ok(container) => {
assert_eq!(container.name, "test-container");
assert_eq!(container.image, Some("alpine:latest".to_string()));
assert!(container.container_id.is_none());
}
Err(NerdctlError::CommandExecutionFailed(_)) => {
// Nerdctl not available - this is expected in CI/test environments
println!("Nerdctl not available - skipping test");
}
Err(e) => {
println!("Nerdctl error (expected in test environment): {:?}", e);
}
}
}
#[test]
fn test_container_builder_pattern() {
let result = Container::from_image("test-app", "nginx:alpine");
match result {
Ok(container) => {
// Test builder pattern methods
let configured_container = container
.with_port("8080:80")
.with_volume("/host/data:/app/data")
.with_env("ENV_VAR", "test_value")
.with_network("test-network")
.with_network_alias("app-alias")
.with_cpu_limit("0.5")
.with_memory_limit("512m")
.with_restart_policy("always")
.with_health_check("curl -f http://localhost/ || exit 1")
.with_detach(true);
// Verify configuration
assert_eq!(configured_container.name, "test-app");
assert_eq!(configured_container.image, Some("nginx:alpine".to_string()));
assert_eq!(configured_container.ports, vec!["8080:80"]);
assert_eq!(configured_container.volumes, vec!["/host/data:/app/data"]);
assert_eq!(configured_container.env_vars.get("ENV_VAR"), Some(&"test_value".to_string()));
assert_eq!(configured_container.network, Some("test-network".to_string()));
assert_eq!(configured_container.network_aliases, vec!["app-alias"]);
assert_eq!(configured_container.cpu_limit, Some("0.5".to_string()));
assert_eq!(configured_container.memory_limit, Some("512m".to_string()));
assert_eq!(configured_container.restart_policy, Some("always".to_string()));
assert!(configured_container.health_check.is_some());
assert!(configured_container.detach);
}
Err(NerdctlError::CommandExecutionFailed(_)) => {
// Nerdctl not available - this is expected in CI/test environments
println!("Nerdctl not available - skipping test");
}
Err(e) => {
println!("Nerdctl error (expected in test environment): {:?}", e);
}
}
}
#[test]
fn test_container_reset() {
let result = Container::from_image("test-container", "alpine:latest");
match result {
Ok(container) => {
// Configure the container
let configured = container
.with_port("8080:80")
.with_env("TEST", "value");
// Reset should clear configuration but keep name and image
let reset_container = configured.reset();
assert_eq!(reset_container.name, "test-container");
assert_eq!(reset_container.image, Some("alpine:latest".to_string()));
assert!(reset_container.ports.is_empty());
assert!(reset_container.env_vars.is_empty());
assert!(reset_container.container_id.is_none());
}
Err(NerdctlError::CommandExecutionFailed(_)) => {
// Nerdctl not available - this is expected in CI/test environments
println!("Nerdctl not available - skipping test");
}
Err(e) => {
println!("Nerdctl error (expected in test environment): {:?}", e);
}
}
}
#[test]
fn test_nerdctl_error_types() {
// Test that our error types work correctly
let error = NerdctlError::CommandFailed("Test error".to_string());
assert!(matches!(error, NerdctlError::CommandFailed(_)));
let error_msg = format!("{}", error);
assert!(error_msg.contains("Test error"));
}
#[test]
fn test_container_multiple_ports_and_volumes() {
let result = Container::from_image("multi-config", "nginx:latest");
match result {
Ok(container) => {
let configured = container
.with_port("8080:80")
.with_port("8443:443")
.with_volume("/data1:/app/data1")
.with_volume("/data2:/app/data2")
.with_env("VAR1", "value1")
.with_env("VAR2", "value2");
assert_eq!(configured.ports.len(), 2);
assert!(configured.ports.contains(&"8080:80".to_string()));
assert!(configured.ports.contains(&"8443:443".to_string()));
assert_eq!(configured.volumes.len(), 2);
assert!(configured.volumes.contains(&"/data1:/app/data1".to_string()));
assert!(configured.volumes.contains(&"/data2:/app/data2".to_string()));
assert_eq!(configured.env_vars.len(), 2);
assert_eq!(configured.env_vars.get("VAR1"), Some(&"value1".to_string()));
assert_eq!(configured.env_vars.get("VAR2"), Some(&"value2".to_string()));
}
Err(NerdctlError::CommandExecutionFailed(_)) => {
// Nerdctl not available - this is expected in CI/test environments
println!("Nerdctl not available - skipping test");
}
Err(e) => {
println!("Nerdctl error (expected in test environment): {:?}", e);
}
}
}