Skip to content

Commit 897915f

Browse files
committed
tests/integration: Use linkme
This keeps tests defined in one place, same as main Rust `#[test]`, reducing conflicts and context switching. Signed-off-by: Colin Walters <walters@verbum.org>
1 parent 02f25b7 commit 897915f

10 files changed

Lines changed: 360 additions & 244 deletions

File tree

Cargo.lock

Lines changed: 21 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
@@ -32,3 +32,4 @@ tempfile = "3"
3232
uuid = { version = "1.18.1", features = ["v4"] }
3333
camino = "1.1.12"
3434
regex = "1"
35+
linkme = "0.3.30"

crates/integration-tests/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,33 @@
33
//! This module contains constants and utilities that are shared between
44
//! the main test binary and helper binaries like cleanup.
55
6+
// Unfortunately needed here to work with linkme
7+
#![allow(unsafe_code)]
8+
9+
use linkme::distributed_slice;
10+
611
/// Label used to identify containers created by integration tests
712
pub const INTEGRATION_TEST_LABEL: &str = "bcvk.integration-test=1";
813

914
/// Label used to identify libvirt VMs created by integration tests
1015
pub const LIBVIRT_INTEGRATION_TEST_LABEL: &str = "bcvk-integration";
16+
17+
/// A test function that returns a Result
18+
pub type TestFn = fn() -> color_eyre::Result<()>;
19+
20+
/// Metadata for a registered integration test
21+
#[derive(Debug)]
22+
pub struct IntegrationTest {
23+
pub name: &'static str,
24+
pub f: TestFn,
25+
}
26+
27+
impl IntegrationTest {
28+
pub const fn new(name: &'static str, f: TestFn) -> Self {
29+
Self { name, f }
30+
}
31+
}
32+
33+
/// Distributed slice holding all registered integration tests
34+
#[distributed_slice]
35+
pub static INTEGRATION_TESTS: [IntegrationTest];

crates/integration-tests/src/main.rs

Lines changed: 16 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use serde_json::Value;
88
use xshell::{cmd, Shell};
99

1010
// Re-export constants from lib for internal use
11-
pub(crate) use integration_tests::{INTEGRATION_TEST_LABEL, LIBVIRT_INTEGRATION_TEST_LABEL};
11+
pub(crate) use integration_tests::{
12+
IntegrationTest, INTEGRATION_TESTS, INTEGRATION_TEST_LABEL, LIBVIRT_INTEGRATION_TEST_LABEL,
13+
};
14+
use linkme::distributed_slice;
1215

1316
mod tests {
1417
pub mod libvirt_base_disks;
@@ -129,6 +132,9 @@ pub(crate) fn run_bcvk_nocapture(args: &[&str]) -> std::io::Result<()> {
129132
Ok(())
130133
}
131134

135+
#[distributed_slice(INTEGRATION_TESTS)]
136+
static TEST_IMAGES_LIST: IntegrationTest = IntegrationTest::new("images_list", test_images_list);
137+
132138
fn test_images_list() -> Result<()> {
133139
println!("Running test: bcvk images list --json");
134140

@@ -174,144 +180,15 @@ fn test_images_list() -> Result<()> {
174180
fn main() {
175181
let args = Arguments::from_args();
176182

177-
let tests = vec![
178-
Trial::test("images_list", || {
179-
test_images_list()?;
180-
Ok(())
181-
}),
182-
Trial::test("run_ephemeral_correct_kernel", || {
183-
tests::run_ephemeral::test_run_ephemeral_correct_kernel();
184-
Ok(())
185-
}),
186-
Trial::test("run_ephemeral_poweroff", || {
187-
tests::run_ephemeral::test_run_ephemeral_poweroff();
188-
Ok(())
189-
}),
190-
Trial::test("run_ephemeral_with_memory_limit", || {
191-
tests::run_ephemeral::test_run_ephemeral_with_memory_limit();
192-
Ok(())
193-
}),
194-
Trial::test("run_ephemeral_with_vcpus", || {
195-
tests::run_ephemeral::test_run_ephemeral_with_vcpus();
196-
Ok(())
197-
}),
198-
Trial::test("run_ephemeral_execute", || {
199-
tests::run_ephemeral::test_run_ephemeral_execute();
200-
Ok(())
201-
}),
202-
Trial::test("run_ephemeral_container_ssh_access", || {
203-
tests::run_ephemeral::test_run_ephemeral_container_ssh_access();
204-
Ok(())
205-
}),
206-
Trial::test("run_ephemeral_ssh_command", || {
207-
tests::run_ephemeral_ssh::test_run_ephemeral_ssh_command();
208-
Ok(())
209-
}),
210-
Trial::test("run_ephemeral_ssh_cleanup", || {
211-
tests::run_ephemeral_ssh::test_run_ephemeral_ssh_cleanup();
212-
Ok(())
213-
}),
214-
Trial::test("run_ephemeral_ssh_system_command", || {
215-
tests::run_ephemeral_ssh::test_run_ephemeral_ssh_system_command();
216-
Ok(())
217-
}),
218-
Trial::test("run_ephemeral_ssh_exit_code", || {
219-
tests::run_ephemeral_ssh::test_run_ephemeral_ssh_exit_code();
220-
Ok(())
221-
}),
222-
Trial::test("run_ephemeral_ssh_cross_distro_compatibility", || {
223-
tests::run_ephemeral_ssh::test_run_ephemeral_ssh_cross_distro_compatibility();
224-
Ok(())
225-
}),
226-
Trial::test("mount_feature_bind", || {
227-
tests::mount_feature::test_mount_feature_bind();
228-
Ok(())
229-
}),
230-
Trial::test("mount_feature_ro_bind", || {
231-
tests::mount_feature::test_mount_feature_ro_bind();
232-
Ok(())
233-
}),
234-
Trial::test("to_disk", || {
235-
tests::to_disk::test_to_disk();
236-
Ok(())
237-
}),
238-
Trial::test("to_disk_qcow2", || {
239-
tests::to_disk::test_to_disk_qcow2();
240-
Ok(())
241-
}),
242-
Trial::test("to_disk_caching", || {
243-
tests::to_disk::test_to_disk_caching();
244-
Ok(())
245-
}),
246-
Trial::test("libvirt_list_functionality", || {
247-
tests::libvirt_verb::test_libvirt_list_functionality();
248-
Ok(())
249-
}),
250-
Trial::test("libvirt_list_json_output", || {
251-
tests::libvirt_verb::test_libvirt_list_json_output();
252-
Ok(())
253-
}),
254-
Trial::test("libvirt_list_json_ssh_metadata", || {
255-
tests::libvirt_verb::test_libvirt_list_json_ssh_metadata();
256-
Ok(())
257-
}),
258-
Trial::test("libvirt_run_resource_options", || {
259-
tests::libvirt_verb::test_libvirt_run_resource_options();
260-
Ok(())
261-
}),
262-
Trial::test("libvirt_run_networking", || {
263-
tests::libvirt_verb::test_libvirt_run_networking();
264-
Ok(())
265-
}),
266-
Trial::test("libvirt_ssh_integration", || {
267-
tests::libvirt_verb::test_libvirt_ssh_integration();
268-
Ok(())
269-
}),
270-
Trial::test("libvirt_run_ssh_full_workflow", || {
271-
tests::libvirt_verb::test_libvirt_run_ssh_full_workflow();
272-
Ok(())
273-
}),
274-
Trial::test("libvirt_vm_lifecycle", || {
275-
tests::libvirt_verb::test_libvirt_vm_lifecycle();
276-
Ok(())
277-
}),
278-
Trial::test("libvirt_label_functionality", || {
279-
tests::libvirt_verb::test_libvirt_label_functionality();
280-
Ok(())
281-
}),
282-
Trial::test("libvirt_error_handling", || {
283-
tests::libvirt_verb::test_libvirt_error_handling();
284-
Ok(())
285-
}),
286-
Trial::test("libvirt_bind_storage_ro", || {
287-
tests::libvirt_verb::test_libvirt_bind_storage_ro();
288-
Ok(())
289-
}),
290-
Trial::test("libvirt_transient_vm", || {
291-
tests::libvirt_verb::test_libvirt_transient_vm();
292-
Ok(())
293-
}),
294-
Trial::test("libvirt_base_disk_creation_and_reuse", || {
295-
tests::libvirt_base_disks::test_base_disk_creation_and_reuse();
296-
Ok(())
297-
}),
298-
Trial::test("libvirt_base_disks_list_command", || {
299-
tests::libvirt_base_disks::test_base_disks_list_command();
300-
Ok(())
301-
}),
302-
Trial::test("libvirt_base_disks_list_shows_timestamp", || {
303-
tests::libvirt_base_disks::test_base_disks_list_shows_timestamp();
304-
Ok(())
305-
}),
306-
Trial::test("libvirt_base_disks_prune_dry_run", || {
307-
tests::libvirt_base_disks::test_base_disks_prune_dry_run();
308-
Ok(())
309-
}),
310-
Trial::test("libvirt_vm_disk_references_base", || {
311-
tests::libvirt_base_disks::test_vm_disk_references_base();
312-
Ok(())
313-
}),
314-
];
183+
// Collect tests from the distributed slice
184+
let tests: Vec<Trial> = INTEGRATION_TESTS
185+
.iter()
186+
.map(|test| {
187+
let name = test.name;
188+
let f = test.f;
189+
Trial::test(name, move || f().map_err(|e| format!("{:?}", e).into()))
190+
})
191+
.collect();
315192

316193
// Run the tests and exit with the result
317194
libtest_mimic::run(&args, tests).exit();

0 commit comments

Comments
 (0)