|
| 1 | +//! Integration tests for Ignition config injection in libvirt VMs |
| 2 | +
|
| 3 | +use integration_tests::integration_test; |
| 4 | +use itest::TestResult; |
| 5 | +use scopeguard::defer; |
| 6 | +use tempfile::TempDir; |
| 7 | +use xshell::cmd; |
| 8 | + |
| 9 | +use std::fs; |
| 10 | + |
| 11 | +use camino::Utf8Path; |
| 12 | + |
| 13 | +use crate::{get_bck_command, shell, LIBVIRT_INTEGRATION_TEST_LABEL}; |
| 14 | + |
| 15 | +/// Generate a random alphanumeric suffix for VM names to avoid collisions |
| 16 | +fn random_suffix() -> String { |
| 17 | + use rand::{distr::Alphanumeric, Rng}; |
| 18 | + rand::rng() |
| 19 | + .sample_iter(&Alphanumeric) |
| 20 | + .take(8) |
| 21 | + .map(char::from) |
| 22 | + .collect() |
| 23 | +} |
| 24 | + |
| 25 | +/// Fedora CoreOS image that supports Ignition |
| 26 | +const FCOS_IMAGE: &str = "quay.io/fedora/fedora-coreos:stable"; |
| 27 | + |
| 28 | +/// Test that Ignition config injection mechanism works for libvirt |
| 29 | +/// |
| 30 | +/// This test verifies that the Ignition config injection mechanism is working |
| 31 | +/// by checking that the VM can be created with --ignition flag and that the |
| 32 | +/// config file is properly stored. |
| 33 | +fn test_libvirt_ignition_works() -> TestResult { |
| 34 | + let sh = shell()?; |
| 35 | + let bck = get_bck_command()?; |
| 36 | + let label = LIBVIRT_INTEGRATION_TEST_LABEL; |
| 37 | + |
| 38 | + // Pull FCOS image first |
| 39 | + cmd!(sh, "podman pull -q {FCOS_IMAGE}").run()?; |
| 40 | + |
| 41 | + // Create a temporary Ignition config |
| 42 | + let temp_dir = TempDir::new()?; |
| 43 | + let config_path = Utf8Path::from_path(temp_dir.path()) |
| 44 | + .expect("temp dir is not utf8") |
| 45 | + .join("config.ign"); |
| 46 | + |
| 47 | + // Minimal valid Ignition config (v3.3.0 for FCOS) |
| 48 | + let ignition_config = r#"{"ignition": {"version": "3.3.0"}}"#; |
| 49 | + fs::write(&config_path, ignition_config)?; |
| 50 | + |
| 51 | + // Generate a unique VM name to avoid conflicts |
| 52 | + let vm_name = format!("test-ignition-{}", random_suffix()); |
| 53 | + |
| 54 | + // Create VM with Ignition config |
| 55 | + // We use --ssh-wait to wait for the VM to boot and verify SSH connectivity |
| 56 | + let output = cmd!( |
| 57 | + sh, |
| 58 | + "{bck} libvirt run --name {vm_name} --label {label} --ignition {config_path} --ssh-wait --memory 2G --cpus 2 {FCOS_IMAGE}" |
| 59 | + ) |
| 60 | + .ignore_status() |
| 61 | + .output()?; |
| 62 | + |
| 63 | + // Cleanup: remove the VM |
| 64 | + defer! { |
| 65 | + let _ = cmd!(sh, "{bck} libvirt rm {vm_name} --force").run(); |
| 66 | + } |
| 67 | + |
| 68 | + // Check that the command succeeded |
| 69 | + if !output.status.success() { |
| 70 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 71 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 72 | + panic!( |
| 73 | + "Failed to create VM with Ignition config.\nStdout: {}\nStderr: {}", |
| 74 | + stdout, stderr |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + // Verify the VM was created |
| 79 | + let vm_list = cmd!(sh, "{bck} libvirt list").read()?; |
| 80 | + assert!( |
| 81 | + vm_list.contains(&vm_name), |
| 82 | + "VM should be listed after creation" |
| 83 | + ); |
| 84 | + |
| 85 | + println!("Ignition config injection test passed"); |
| 86 | + Ok(()) |
| 87 | +} |
| 88 | +integration_test!(test_libvirt_ignition_works); |
| 89 | + |
| 90 | +/// Test that Ignition config validation rejects nonexistent files |
| 91 | +fn test_libvirt_ignition_invalid_path() -> TestResult { |
| 92 | + let sh = shell()?; |
| 93 | + let bck = get_bck_command()?; |
| 94 | + let label = LIBVIRT_INTEGRATION_TEST_LABEL; |
| 95 | + |
| 96 | + // Pull FCOS image first |
| 97 | + cmd!(sh, "podman pull -q {FCOS_IMAGE}").run()?; |
| 98 | + |
| 99 | + let temp = TempDir::new()?; |
| 100 | + let nonexistent_path = Utf8Path::from_path(temp.path()) |
| 101 | + .expect("temp dir is not utf8") |
| 102 | + .join("nonexistent-config.ign"); |
| 103 | + |
| 104 | + let vm_name = format!("test-ignition-invalid-{}", random_suffix()); |
| 105 | + |
| 106 | + let output = cmd!( |
| 107 | + sh, |
| 108 | + "{bck} libvirt run --name {vm_name} --label {label} --ignition {nonexistent_path} {FCOS_IMAGE}" |
| 109 | + ) |
| 110 | + .ignore_status() |
| 111 | + .output()?; |
| 112 | + |
| 113 | + assert!( |
| 114 | + !output.status.success(), |
| 115 | + "Should fail with nonexistent Ignition config file" |
| 116 | + ); |
| 117 | + |
| 118 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 119 | + assert!( |
| 120 | + stderr.contains("not found"), |
| 121 | + "Error should mention missing file: {}", |
| 122 | + stderr |
| 123 | + ); |
| 124 | + |
| 125 | + println!("Ignition invalid path test passed"); |
| 126 | + Ok(()) |
| 127 | +} |
| 128 | +integration_test!(test_libvirt_ignition_invalid_path); |
| 129 | + |
| 130 | +/// Test that Ignition is rejected for images that don't support it |
| 131 | +fn test_libvirt_ignition_unsupported_image() -> TestResult { |
| 132 | + let sh = shell()?; |
| 133 | + let bck = get_bck_command()?; |
| 134 | + let label = LIBVIRT_INTEGRATION_TEST_LABEL; |
| 135 | + |
| 136 | + // Use standard bootc image that doesn't have Ignition support |
| 137 | + let image = "quay.io/centos-bootc/centos-bootc:stream10"; |
| 138 | + |
| 139 | + let temp_dir = TempDir::new()?; |
| 140 | + let config_path = Utf8Path::from_path(temp_dir.path()) |
| 141 | + .expect("temp dir is not utf8") |
| 142 | + .join("config.ign"); |
| 143 | + |
| 144 | + let ignition_config = r#"{"ignition": {"version": "3.3.0"}}"#; |
| 145 | + fs::write(&config_path, ignition_config)?; |
| 146 | + |
| 147 | + let vm_name = format!("test-ignition-unsupported-{}", random_suffix()); |
| 148 | + |
| 149 | + let output = cmd!( |
| 150 | + sh, |
| 151 | + "{bck} libvirt run --name {vm_name} --label {label} --ignition {config_path} {image}" |
| 152 | + ) |
| 153 | + .ignore_status() |
| 154 | + .output()?; |
| 155 | + |
| 156 | + assert!( |
| 157 | + !output.status.success(), |
| 158 | + "Should fail when using --ignition with non-Ignition image" |
| 159 | + ); |
| 160 | + |
| 161 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 162 | + assert!( |
| 163 | + stderr.contains("does not support Ignition"), |
| 164 | + "Error should mention missing Ignition support: {}", |
| 165 | + stderr |
| 166 | + ); |
| 167 | + |
| 168 | + println!("Ignition unsupported image test passed"); |
| 169 | + Ok(()) |
| 170 | +} |
| 171 | +integration_test!(test_libvirt_ignition_unsupported_image); |
0 commit comments