Skip to content

Commit 3f24dd4

Browse files
committed
Create macro to generate tests
1 parent a0fb1d4 commit 3f24dd4

File tree

3 files changed

+197
-9
lines changed

3 files changed

+197
-9
lines changed

Cargo.lock

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

tests/runner/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ edition = "2021"
1010
bootloader = { path = "../.." }
1111
strip-ansi-escapes = "0.1.1"
1212
ovmf-prebuilt = "0.1.0-alpha.1"
13+
paste = "1.0.11"
14+
lazy_static = "1.4.0"
15+
rand = "0.8.5"

tests/runner/src/lib.rs

Lines changed: 179 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
use std::{io::Write, path::Path, process::Command};
1+
use std::{io::Write, path::{Path, PathBuf}, process::Command};
2+
3+
pub extern crate lazy_static;
4+
pub extern crate paste;
5+
#[doc(hidden)]
6+
pub use paste::paste;
7+
#[doc(hidden)]
8+
pub use lazy_static::lazy_static;
9+
use rand::Rng;
210

311
const QEMU_ARGS: &[&str] = &[
412
"-device",
@@ -12,37 +20,84 @@ const QEMU_ARGS: &[&str] = &[
1220
"size=2048",
1321
];
1422

15-
pub fn run_test_kernel(kernel_binary_path: &str, ramdisk_path: Option<&str>) {
23+
pub fn generate_test_image_filename(path: &Path) -> PathBuf {
24+
let s: String = rand::thread_rng()
25+
.sample_iter(&rand::distributions::Alphanumeric)
26+
.take(8)
27+
.map(char::from)
28+
.collect();
29+
path.with_file_name(s)
30+
}
31+
32+
pub fn run_test_kernel_bios(kernel_binary_path: &str, ramdisk_path: Option<&str>) {
1633
let kernel_path = Path::new(kernel_binary_path);
1734
let ramdisk_path = match ramdisk_path {
1835
Some(rdp) => Some(Path::new(rdp)),
1936
None => None,
2037
};
2138

2239
// create an MBR disk image for legacy BIOS booting
23-
let mbr_path = kernel_path.with_extension("mbr");
40+
let mbr_path = generate_test_image_filename(kernel_path).with_extension("mbr");
2441
let mut bios_builder = bootloader::BiosBoot::new(kernel_path);
2542

43+
// Set ramdisk for test, if supplied.
44+
if let Some(rdp) = ramdisk_path {
45+
bios_builder.set_ramdisk(rdp);
46+
}
47+
48+
bios_builder.create_disk_image(&mbr_path).unwrap();
49+
50+
run_test_kernel_on_bios(&mbr_path);
51+
}
52+
53+
pub fn run_test_kernel_uefi(kernel_binary_path: &str, ramdisk_path: Option<&str>) {
54+
let kernel_path = Path::new(kernel_binary_path);
55+
let ramdisk_path = match ramdisk_path {
56+
Some(rdp) => Some(Path::new(rdp)),
57+
None => None,
58+
};
59+
2660
// create a GPT disk image for UEFI booting
27-
let gpt_path = kernel_path.with_extension("gpt");
61+
let gpt_path = generate_test_image_filename(kernel_path).with_extension("gpt");
2862
let mut uefi_builder = bootloader::UefiBoot::new(kernel_path);
2963

3064
// Set ramdisk for test, if supplied.
3165
if let Some(rdp) = ramdisk_path {
32-
bios_builder.set_ramdisk(rdp);
3366
uefi_builder.set_ramdisk(rdp);
3467
}
3568

36-
bios_builder.create_disk_image(&mbr_path).unwrap();
3769
uefi_builder.create_disk_image(&gpt_path).unwrap();
3870

71+
72+
run_test_kernel_on_uefi(&gpt_path);
73+
}
74+
75+
pub fn run_test_kernel(kernel_binary_path: &str, ramdisk_path: Option<&str>) {
76+
run_test_kernel_uefi(kernel_binary_path, ramdisk_path);
77+
run_test_kernel_bios(kernel_binary_path, ramdisk_path);
78+
run_test_kernel_tftp(kernel_binary_path, ramdisk_path);
79+
}
80+
81+
82+
pub fn run_test_kernel_tftp(kernel_binary_path: &str, ramdisk_path: Option<&str>) {
83+
let kernel_path = Path::new(kernel_binary_path);
84+
let ramdisk_path = match ramdisk_path {
85+
Some(rdp) => Some(Path::new(rdp)),
86+
None => None,
87+
};
88+
89+
let mut uefi_builder = bootloader::UefiBoot::new(kernel_path);
90+
91+
// Set ramdisk for test, if supplied.
92+
if let Some(rdp) = ramdisk_path {
93+
uefi_builder.set_ramdisk(rdp);
94+
}
95+
3996
// create a TFTP folder with the kernel executable and UEFI bootloader for
4097
// UEFI PXE booting
41-
let tftp_path = kernel_path.with_extension(".tftp");
98+
let tftp_path = generate_test_image_filename(kernel_path).with_extension(".tftp");
4299
uefi_builder.create_pxe_tftp_folder(&tftp_path).unwrap();
43100

44-
run_test_kernel_on_uefi(&gpt_path);
45-
run_test_kernel_on_bios(&mbr_path);
46101
run_test_kernel_on_uefi_pxe(&tftp_path);
47102
}
48103

@@ -115,3 +170,118 @@ pub fn run_test_kernel_on_uefi_pxe(out_tftp_path: &Path) {
115170
other => panic!("Test failed with unexpected exit code `{:?}`", other),
116171
}
117172
}
173+
174+
#[macro_export]
175+
/// Creates a series of test functions for a given kernel image to cover bios, uefi, and tftp
176+
///
177+
/// define_test!(name, kernel) will generate all 3 tests, with a ramdisk and no-ramdisk variant.
178+
/// define_test!(name, kernel, ramdisk) will generate all 3 tests, with the specified ramdisk
179+
/// define_test!(name, kernel, without_ramdisk_tests) will generate all 3 tests, with only the no-ramdisk variant
180+
macro_rules! define_test {
181+
($test_name: ident, $bin: tt) => (
182+
$crate::paste! {
183+
#[test]
184+
fn [< $test_name _uefi_without_ramdisk >]() {
185+
$crate::run_test_kernel_uefi(
186+
$bin,
187+
None
188+
);
189+
}
190+
191+
#[test]
192+
fn [< $test_name _tftp_without_ramdisk >]() {
193+
$crate::run_test_kernel_tftp(
194+
$bin,
195+
None
196+
);
197+
}
198+
199+
#[test]
200+
fn [< $test_name _bios_without_ramdisk >]() {
201+
$crate::run_test_kernel_bios(
202+
$bin,
203+
None
204+
);
205+
}
206+
207+
208+
#[test]
209+
fn [< $test_name _uefi_with_ramdisk >]() {
210+
$crate::run_test_kernel_uefi(
211+
$bin,
212+
Some("tests/ramdisk.txt")
213+
);
214+
}
215+
216+
#[test]
217+
fn [< $test_name _tftp_with_ramdisk >]() {
218+
$crate::run_test_kernel_tftp(
219+
$bin,
220+
Some("tests/ramdisk.txt")
221+
);
222+
}
223+
224+
#[test]
225+
fn [< $test_name _bios_with_ramdisk >]() {
226+
$crate::run_test_kernel_bios(
227+
$bin,
228+
Some("tests/ramdisk.txt")
229+
);
230+
}
231+
}
232+
);
233+
($test_name: ident, $bin:tt, without_ramdisk_tests) => (
234+
$crate::paste! {
235+
#[test]
236+
fn [< $test_name _uefi_without_ramdisk >]() {
237+
$crate::run_test_kernel_uefi(
238+
$bin,
239+
None
240+
);
241+
}
242+
243+
#[test]
244+
fn [< $test_name _tftp_without_ramdisk >]() {
245+
$crate::run_test_kernel_tftp(
246+
$bin,
247+
None
248+
);
249+
}
250+
251+
#[test]
252+
fn [< $test_name _bios_without_ramdisk >]() {
253+
$crate::run_test_kernel_bios(
254+
$bin,
255+
None
256+
);
257+
}
258+
}
259+
);
260+
($test_name: ident, $bin: tt, $ramdisk: tt) => (
261+
$crate::paste! {
262+
#[test]
263+
fn [< $test_name _uefi_with_ramdisk >]() {
264+
$crate::run_test_kernel_uefi(
265+
$bin,
266+
Some($ramdisk)
267+
);
268+
}
269+
270+
#[test]
271+
fn [< $test_name _tftp_with_ramdisk >]() {
272+
$crate::run_test_kernel_tftp(
273+
$bin,
274+
Some($ramdisk)
275+
);
276+
}
277+
278+
#[test]
279+
fn [< $test_name _bios_with_ramdisk >]() {
280+
$crate::run_test_kernel_bios(
281+
$bin,
282+
Some($ramdisk)
283+
);
284+
}
285+
}
286+
);
287+
}

0 commit comments

Comments
 (0)