Skip to content

Commit 6086061

Browse files
committed
libvirt: Support qemu:///system
Closes: #61 Signed-off-by: Colin Walters <[email protected]>
1 parent 9463c5c commit 6086061

File tree

4 files changed

+318
-94
lines changed

4 files changed

+318
-94
lines changed

Justfile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ test-integration *ARGS: build pull-test-images
2525
#!/usr/bin/env bash
2626
set -euo pipefail
2727
export BCVK_PATH=$(pwd)/target/release/bcvk
28-
28+
2929
# Clean up any leftover containers before starting
3030
cargo run --release --bin test-cleanup -p integration-tests 2>/dev/null || true
31-
31+
3232
# Run the tests
3333
if command -v cargo-nextest &> /dev/null; then
3434
cargo nextest run --release -P integration -p integration-tests {{ ARGS }}
@@ -37,12 +37,16 @@ test-integration *ARGS: build pull-test-images
3737
cargo test --release -p integration-tests -- {{ ARGS }}
3838
TEST_EXIT_CODE=$?
3939
fi
40-
40+
4141
# Clean up containers after tests complete
4242
cargo run --release --bin test-cleanup -p integration-tests 2>/dev/null || true
43-
43+
4444
exit $TEST_EXIT_CODE
4545

46+
# Run integration tests with qemu:///system (tests rootless podman + system libvirt)
47+
test-integration-system *ARGS: build pull-test-images
48+
env LIBVIRT_DEFAULT_URI=qemu:///system just test-integration {{ARGS}}
49+
4650
# Clean up integration test containers
4751
test-cleanup:
4852
cargo run --release --bin test-cleanup -p integration-tests

crates/integration-tests/src/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ pub(crate) fn get_alternative_test_image() -> String {
6464
}
6565
}
6666

67+
/// Get libvirt connection arguments for CLI commands
68+
///
69+
/// Returns ["--connect", "URI"] if LIBVIRT_DEFAULT_URI is set, otherwise empty vec.
70+
/// This uses the standard libvirt environment variable.
71+
pub(crate) fn get_libvirt_connect_args() -> Vec<String> {
72+
if let Some(uri) = std::env::var("LIBVIRT_DEFAULT_URI").ok() {
73+
vec!["--connect".to_string(), uri]
74+
} else {
75+
vec![]
76+
}
77+
}
78+
6779
/// Captured output from a command with decoded stdout/stderr strings
6880
pub(crate) struct CapturedOutput {
6981
pub output: Output,

crates/integration-tests/src/tests/libvirt_base_disks.rs

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
99
use std::process::Command;
1010

11-
use crate::{get_bck_command, get_test_image};
11+
use crate::{get_bck_command, get_libvirt_connect_args, get_test_image};
1212

1313
/// Test that base disk is created and reused for multiple VMs
1414
pub fn test_base_disk_creation_and_reuse() {
1515
let bck = get_bck_command().unwrap();
1616
let test_image = get_test_image();
17+
let connect_args = get_libvirt_connect_args();
1718

1819
// Generate unique names for test VMs
1920
let timestamp = std::time::SystemTime::now()
@@ -33,20 +34,12 @@ pub fn test_base_disk_creation_and_reuse() {
3334

3435
// Create first VM - this should create a new base disk
3536
println!("Creating first VM (should create base disk)...");
36-
let vm1_output = Command::new("timeout")
37-
.args([
38-
"300s",
39-
&bck,
40-
"libvirt",
41-
"run",
42-
"--name",
43-
&vm1_name,
44-
"--filesystem",
45-
"ext4",
46-
&test_image,
47-
])
48-
.output()
49-
.expect("Failed to create first VM");
37+
let mut vm1_cmd = Command::new("timeout");
38+
vm1_cmd.args(["300s", &bck, "libvirt"]);
39+
vm1_cmd.args(&connect_args);
40+
vm1_cmd.arg("run");
41+
vm1_cmd.args(["--name", &vm1_name, "--filesystem", "ext4", &test_image]);
42+
let vm1_output = vm1_cmd.output().expect("Failed to create first VM");
5043

5144
let vm1_stdout = String::from_utf8_lossy(&vm1_output.stdout);
5245
let vm1_stderr = String::from_utf8_lossy(&vm1_output.stderr);
@@ -69,20 +62,12 @@ pub fn test_base_disk_creation_and_reuse() {
6962

7063
// Create second VM - this should reuse the base disk
7164
println!("Creating second VM (should reuse base disk)...");
72-
let vm2_output = Command::new("timeout")
73-
.args([
74-
"300s",
75-
&bck,
76-
"libvirt",
77-
"run",
78-
"--name",
79-
&vm2_name,
80-
"--filesystem",
81-
"ext4",
82-
&test_image,
83-
])
84-
.output()
85-
.expect("Failed to create second VM");
65+
let mut vm2_cmd = Command::new("timeout");
66+
vm2_cmd.args(["300s", &bck, "libvirt"]);
67+
vm2_cmd.args(&connect_args);
68+
vm2_cmd.arg("run");
69+
vm2_cmd.args(["--name", &vm2_name, "--filesystem", "ext4", &test_image]);
70+
let vm2_output = vm2_cmd.output().expect("Failed to create second VM");
8671

8772
let vm2_stdout = String::from_utf8_lossy(&vm2_output.stdout);
8873
let vm2_stderr = String::from_utf8_lossy(&vm2_output.stderr);
@@ -110,13 +95,15 @@ pub fn test_base_disk_creation_and_reuse() {
11095
/// Test base-disks list command
11196
pub fn test_base_disks_list_command() {
11297
let bck = get_bck_command().unwrap();
98+
let connect_args = get_libvirt_connect_args();
11399

114100
println!("Testing base-disks list command");
115101

116-
let output = Command::new(&bck)
117-
.args(["libvirt", "base-disks", "list"])
118-
.output()
119-
.expect("Failed to run base-disks list");
102+
let mut cmd = Command::new(&bck);
103+
cmd.arg("libvirt");
104+
cmd.args(&connect_args);
105+
cmd.args(["base-disks", "list"]);
106+
let output = cmd.output().expect("Failed to run base-disks list");
120107

121108
let stdout = String::from_utf8_lossy(&output.stdout);
122109
let stderr = String::from_utf8_lossy(&output.stderr);
@@ -149,11 +136,15 @@ pub fn test_base_disks_list_command() {
149136
/// Test base-disks prune command with dry-run
150137
pub fn test_base_disks_prune_dry_run() {
151138
let bck = get_bck_command().unwrap();
139+
let connect_args = get_libvirt_connect_args();
152140

153141
println!("Testing base-disks prune --dry-run command");
154142

155-
let output = Command::new(&bck)
156-
.args(["libvirt", "base-disks", "prune", "--dry-run"])
143+
let mut cmd = Command::new(&bck);
144+
cmd.arg("libvirt");
145+
cmd.args(&connect_args);
146+
cmd.args(["base-disks", "prune", "--dry-run"]);
147+
let output = cmd
157148
.output()
158149
.expect("Failed to run base-disks prune --dry-run");
159150

@@ -185,6 +176,7 @@ pub fn test_base_disks_prune_dry_run() {
185176
pub fn test_vm_disk_references_base() {
186177
let bck = get_bck_command().unwrap();
187178
let test_image = get_test_image();
179+
let connect_args = get_libvirt_connect_args();
188180

189181
let timestamp = std::time::SystemTime::now()
190182
.duration_since(std::time::UNIX_EPOCH)
@@ -197,20 +189,12 @@ pub fn test_vm_disk_references_base() {
197189
cleanup_domain(&vm_name);
198190

199191
// Create VM
200-
let output = Command::new("timeout")
201-
.args([
202-
"300s",
203-
&bck,
204-
"libvirt",
205-
"run",
206-
"--name",
207-
&vm_name,
208-
"--filesystem",
209-
"ext4",
210-
&test_image,
211-
])
212-
.output()
213-
.expect("Failed to create VM");
192+
let mut vm_cmd = Command::new("timeout");
193+
vm_cmd.args(["300s", &bck, "libvirt"]);
194+
vm_cmd.args(&connect_args);
195+
vm_cmd.arg("run");
196+
vm_cmd.args(["--name", &vm_name, "--filesystem", "ext4", &test_image]);
197+
let output = vm_cmd.output().expect("Failed to create VM");
214198

215199
if !output.status.success() {
216200
let stderr = String::from_utf8_lossy(&output.stderr);
@@ -220,10 +204,12 @@ pub fn test_vm_disk_references_base() {
220204
}
221205

222206
// Get VM disk path from domain XML
223-
let dumpxml_output = Command::new("virsh")
224-
.args(&["dumpxml", &vm_name])
225-
.output()
226-
.expect("Failed to dump domain XML");
207+
let mut dumpxml_cmd = Command::new("virsh");
208+
for arg in &connect_args {
209+
dumpxml_cmd.arg(arg);
210+
}
211+
dumpxml_cmd.args(&["dumpxml", &vm_name]);
212+
let dumpxml_output = dumpxml_cmd.output().expect("Failed to dump domain XML");
227213

228214
if !dumpxml_output.status.success() {
229215
cleanup_domain(&vm_name);
@@ -261,18 +247,24 @@ pub fn test_vm_disk_references_base() {
261247

262248
/// Helper function to cleanup domain and its disk
263249
fn cleanup_domain(domain_name: &str) {
250+
let connect_args = get_libvirt_connect_args();
264251
println!("Cleaning up domain: {}", domain_name);
265252

266253
// Stop domain if running
267-
let _ = Command::new("virsh")
268-
.args(&["destroy", domain_name])
269-
.output();
254+
let mut destroy_cmd = Command::new("virsh");
255+
for arg in &connect_args {
256+
destroy_cmd.arg(arg);
257+
}
258+
destroy_cmd.args(&["destroy", domain_name]);
259+
let _ = destroy_cmd.output();
270260

271261
// Use bcvk libvirt rm for proper cleanup
272262
let bck = get_bck_command().unwrap();
273-
let cleanup_output = Command::new(&bck)
274-
.args(&["libvirt", "rm", domain_name, "--force", "--stop"])
275-
.output();
263+
let mut cleanup_cmd = Command::new(&bck);
264+
cleanup_cmd.arg("libvirt");
265+
cleanup_cmd.args(&connect_args);
266+
cleanup_cmd.args(&["rm", domain_name, "--force", "--stop"]);
267+
let cleanup_output = cleanup_cmd.output();
276268

277269
if let Ok(output) = cleanup_output {
278270
if output.status.success() {

0 commit comments

Comments
 (0)