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 ;
2
10
3
11
const QEMU_ARGS : & [ & str ] = & [
4
12
"-device" ,
@@ -12,37 +20,84 @@ const QEMU_ARGS: &[&str] = &[
12
20
"size=2048" ,
13
21
] ;
14
22
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 > ) {
16
33
let kernel_path = Path :: new ( kernel_binary_path) ;
17
34
let ramdisk_path = match ramdisk_path {
18
35
Some ( rdp) => Some ( Path :: new ( rdp) ) ,
19
36
None => None ,
20
37
} ;
21
38
22
39
// 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" ) ;
24
41
let mut bios_builder = bootloader:: BiosBoot :: new ( kernel_path) ;
25
42
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
+
26
60
// 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" ) ;
28
62
let mut uefi_builder = bootloader:: UefiBoot :: new ( kernel_path) ;
29
63
30
64
// Set ramdisk for test, if supplied.
31
65
if let Some ( rdp) = ramdisk_path {
32
- bios_builder. set_ramdisk ( rdp) ;
33
66
uefi_builder. set_ramdisk ( rdp) ;
34
67
}
35
68
36
- bios_builder. create_disk_image ( & mbr_path) . unwrap ( ) ;
37
69
uefi_builder. create_disk_image ( & gpt_path) . unwrap ( ) ;
38
70
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
+
39
96
// create a TFTP folder with the kernel executable and UEFI bootloader for
40
97
// 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" ) ;
42
99
uefi_builder. create_pxe_tftp_folder ( & tftp_path) . unwrap ( ) ;
43
100
44
- run_test_kernel_on_uefi ( & gpt_path) ;
45
- run_test_kernel_on_bios ( & mbr_path) ;
46
101
run_test_kernel_on_uefi_pxe ( & tftp_path) ;
47
102
}
48
103
@@ -115,3 +170,118 @@ pub fn run_test_kernel_on_uefi_pxe(out_tftp_path: &Path) {
115
170
other => panic ! ( "Test failed with unexpected exit code `{:?}`" , other) ,
116
171
}
117
172
}
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