Skip to content

Commit d870f45

Browse files
committed
integration-tests: Add macro to reduce test registration boilerplate
The goal is to reduce boilerplate. Assisted-by: Claude Code (Sonnet 4.5) Signed-off-by: Colin Walters <[email protected]>
1 parent b8ce7ee commit d870f45

File tree

10 files changed

+121
-221
lines changed

10 files changed

+121
-221
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/integration-tests/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ uuid = { version = "1.18.1", features = ["v4"] }
3333
camino = "1.1.12"
3434
regex = "1"
3535
linkme = "0.3.30"
36+
paste = "1.0"
3637

3738
[lints]
3839
workspace = true

crates/integration-tests/src/lib.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,56 @@ pub static INTEGRATION_TESTS: [IntegrationTest];
6060
#[distributed_slice]
6161
pub static PARAMETERIZED_INTEGRATION_TESTS: [ParameterizedIntegrationTest];
6262

63+
/// Register an integration test with less boilerplate.
64+
///
65+
/// This macro generates the static registration for an integration test function.
66+
///
67+
/// # Examples
68+
///
69+
/// ```ignore
70+
/// fn test_basic_functionality() -> Result<()> {
71+
/// let output = run_bcvk(&["some", "args"])?;
72+
/// output.assert_success("test");
73+
/// Ok(())
74+
/// }
75+
/// integration_test!(test_basic_functionality);
76+
/// ```
77+
#[macro_export]
78+
macro_rules! integration_test {
79+
($fn_name:ident) => {
80+
::paste::paste! {
81+
#[distributed_slice($crate::INTEGRATION_TESTS)]
82+
static [<$fn_name:upper>]: $crate::IntegrationTest =
83+
$crate::IntegrationTest::new(stringify!($fn_name), $fn_name);
84+
}
85+
};
86+
}
87+
88+
/// Register a parameterized integration test with less boilerplate.
89+
///
90+
/// This macro generates the static registration for a parameterized integration test function.
91+
///
92+
/// # Examples
93+
///
94+
/// ```ignore
95+
/// fn test_with_image(image: &str) -> Result<()> {
96+
/// let output = run_bcvk(&["command", image])?;
97+
/// output.assert_success("test");
98+
/// Ok(())
99+
/// }
100+
/// parameterized_integration_test!(test_with_image);
101+
/// ```
102+
#[macro_export]
103+
macro_rules! parameterized_integration_test {
104+
($fn_name:ident) => {
105+
::paste::paste! {
106+
#[distributed_slice($crate::PARAMETERIZED_INTEGRATION_TESTS)]
107+
static [<$fn_name:upper>]: $crate::ParameterizedIntegrationTest =
108+
$crate::ParameterizedIntegrationTest::new(stringify!($fn_name), $fn_name);
109+
}
110+
};
111+
}
112+
63113
/// Create a test suffix from an image name by replacing invalid characters with underscores
64114
///
65115
/// Replaces all non-alphanumeric characters with `_` to create a predictable, filesystem-safe

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

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@
77
//! - base-disks prune command
88
99
use color_eyre::Result;
10+
use integration_tests::integration_test;
1011
use linkme::distributed_slice;
12+
1113
use regex::Regex;
1214
use std::process::Command;
1315

14-
use crate::{get_bck_command, get_test_image, run_bcvk, IntegrationTest, INTEGRATION_TESTS};
15-
16-
#[distributed_slice(INTEGRATION_TESTS)]
17-
static TEST_BASE_DISK_CREATION_AND_REUSE: IntegrationTest = IntegrationTest::new(
18-
"test_base_disk_creation_and_reuse",
19-
test_base_disk_creation_and_reuse,
20-
);
16+
use crate::{get_bck_command, get_test_image, run_bcvk};
2117

2218
/// Test that base disk is created and reused for multiple VMs
2319
fn test_base_disk_creation_and_reuse() -> Result<()> {
@@ -99,10 +95,7 @@ fn test_base_disk_creation_and_reuse() -> Result<()> {
9995
println!("✓ Base disk creation and reuse test passed");
10096
Ok(())
10197
}
102-
103-
#[distributed_slice(INTEGRATION_TESTS)]
104-
static TEST_BASE_DISKS_LIST_COMMAND: IntegrationTest =
105-
IntegrationTest::new("test_base_disks_list_command", test_base_disks_list_command);
98+
integration_test!(test_base_disk_creation_and_reuse);
10699

107100
/// Test base-disks list command
108101
fn test_base_disks_list_command() -> Result<()> {
@@ -143,12 +136,7 @@ fn test_base_disks_list_command() -> Result<()> {
143136
}
144137
Ok(())
145138
}
146-
147-
#[distributed_slice(INTEGRATION_TESTS)]
148-
static TEST_BASE_DISKS_LIST_SHOWS_TIMESTAMP: IntegrationTest = IntegrationTest::new(
149-
"test_base_disks_list_shows_timestamp",
150-
test_base_disks_list_shows_timestamp,
151-
);
139+
integration_test!(test_base_disks_list_command);
152140

153141
/// Test base-disks list shows creation timestamp
154142
fn test_base_disks_list_shows_timestamp() -> Result<()> {
@@ -218,12 +206,7 @@ fn test_base_disks_list_shows_timestamp() -> Result<()> {
218206
}
219207
Ok(())
220208
}
221-
222-
#[distributed_slice(INTEGRATION_TESTS)]
223-
static TEST_BASE_DISKS_PRUNE_DRY_RUN: IntegrationTest = IntegrationTest::new(
224-
"test_base_disks_prune_dry_run",
225-
test_base_disks_prune_dry_run,
226-
);
209+
integration_test!(test_base_disks_list_shows_timestamp);
227210

228211
/// Test base-disks prune command with dry-run
229212
fn test_base_disks_prune_dry_run() -> Result<()> {
@@ -260,10 +243,7 @@ fn test_base_disks_prune_dry_run() -> Result<()> {
260243
}
261244
Ok(())
262245
}
263-
264-
#[distributed_slice(INTEGRATION_TESTS)]
265-
static TEST_VM_DISK_REFERENCES_BASE: IntegrationTest =
266-
IntegrationTest::new("test_vm_disk_references_base", test_vm_disk_references_base);
246+
integration_test!(test_base_disks_prune_dry_run);
267247

268248
/// Test that VM disks reference base disks correctly
269249
fn test_vm_disk_references_base() -> Result<()> {
@@ -336,6 +316,7 @@ fn test_vm_disk_references_base() -> Result<()> {
336316
println!("✓ VM disk reference test passed");
337317
Ok(())
338318
}
319+
integration_test!(test_vm_disk_references_base);
339320

340321
/// Helper function to cleanup domain and its disk
341322
fn cleanup_domain(domain_name: &str) {

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

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,12 @@
66
//! - Actual network connectivity through forwarded ports
77
88
use color_eyre::Result;
9+
use integration_tests::integration_test;
910
use linkme::distributed_slice;
10-
use std::process::Command;
1111

12-
use crate::{
13-
get_bck_command, get_test_image, run_bcvk, IntegrationTest, INTEGRATION_TESTS,
14-
LIBVIRT_INTEGRATION_TEST_LABEL,
15-
};
12+
use std::process::Command;
1613

17-
#[distributed_slice(INTEGRATION_TESTS)]
18-
static TEST_LIBVIRT_PORT_FORWARD_PARSING: IntegrationTest = IntegrationTest::new(
19-
"test_libvirt_port_forward_parsing",
20-
test_libvirt_port_forward_parsing,
21-
);
14+
use crate::{get_bck_command, get_test_image, run_bcvk, LIBVIRT_INTEGRATION_TEST_LABEL};
2215

2316
/// Test port forwarding argument parsing
2417
fn test_libvirt_port_forward_parsing() -> Result<()> {
@@ -63,12 +56,7 @@ fn test_libvirt_port_forward_parsing() -> Result<()> {
6356
println!("✓ Port forwarding argument parsing validated");
6457
Ok(())
6558
}
66-
67-
#[distributed_slice(INTEGRATION_TESTS)]
68-
static TEST_LIBVIRT_PORT_FORWARD_INVALID: IntegrationTest = IntegrationTest::new(
69-
"test_libvirt_port_forward_invalid",
70-
test_libvirt_port_forward_invalid,
71-
);
59+
integration_test!(test_libvirt_port_forward_parsing);
7260

7361
/// Test port forwarding error handling for invalid formats
7462
fn test_libvirt_port_forward_invalid() -> Result<()> {
@@ -134,12 +122,7 @@ fn test_libvirt_port_forward_invalid() -> Result<()> {
134122
println!("✓ Port forwarding error handling validated");
135123
Ok(())
136124
}
137-
138-
#[distributed_slice(INTEGRATION_TESTS)]
139-
static TEST_LIBVIRT_PORT_FORWARD_XML: IntegrationTest = IntegrationTest::new(
140-
"test_libvirt_port_forward_xml",
141-
test_libvirt_port_forward_xml,
142-
);
125+
integration_test!(test_libvirt_port_forward_invalid);
143126

144127
/// Test that port forwarding is correctly configured in domain XML
145128
fn test_libvirt_port_forward_xml() -> Result<()> {
@@ -247,12 +230,7 @@ fn test_libvirt_port_forward_xml() -> Result<()> {
247230
println!("✓ Port forwarding XML configuration test passed");
248231
Ok(())
249232
}
250-
251-
#[distributed_slice(INTEGRATION_TESTS)]
252-
static TEST_LIBVIRT_PORT_FORWARD_CONNECTIVITY: IntegrationTest = IntegrationTest::new(
253-
"test_libvirt_port_forward_connectivity",
254-
test_libvirt_port_forward_connectivity,
255-
);
233+
integration_test!(test_libvirt_port_forward_xml);
256234

257235
/// Test actual network connectivity through forwarded ports
258236
fn test_libvirt_port_forward_connectivity() -> Result<()> {
@@ -437,6 +415,7 @@ fn test_libvirt_port_forward_connectivity() -> Result<()> {
437415
println!("✓ Port forwarding connectivity test passed");
438416
Ok(())
439417
}
418+
integration_test!(test_libvirt_port_forward_connectivity);
440419

441420
/// Helper function to cleanup domain
442421
fn cleanup_domain(domain_name: &str) {

0 commit comments

Comments
 (0)