Skip to content
This repository was archived by the owner on Mar 7, 2021. It is now read-only.

Commit c866fb9

Browse files
committed
Factor out test utilities from the printk tests
1 parent 9036b65 commit c866fb9

File tree

3 files changed

+43
-35
lines changed

3 files changed

+43
-35
lines changed

tests/printk/tests.rs

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,8 @@
1-
use std::env;
2-
use std::process::Command;
3-
4-
struct LoadedModule {
5-
name: String,
6-
}
7-
8-
impl LoadedModule {
9-
fn load(name: String) -> LoadedModule {
10-
Command::new("sudo")
11-
.arg("insmod")
12-
.arg(&name)
13-
.status()
14-
.unwrap();
15-
return LoadedModule { name };
16-
}
17-
}
1+
extern crate kernel_module_tests;
182

19-
impl Drop for LoadedModule {
20-
fn drop(&mut self) {
21-
Command::new("sudo")
22-
.arg("rmmod")
23-
.arg(&self.name)
24-
.status()
25-
.unwrap();
26-
}
27-
}
3+
use std::process::Command;
284

29-
fn with_kernel_module<F: Fn()>(f: F) {
30-
Command::new("sudo")
31-
.arg("dmesg")
32-
.arg("-C")
33-
.status()
34-
.unwrap();
35-
let _m = LoadedModule::load(env::var("KERNEL_MODULE").unwrap());
36-
f();
37-
}
5+
use kernel_module_tests::with_kernel_module;
386

397
fn assert_dmesg_contains(msgs: &[&[u8]]) {
408
let output = Command::new("dmesg").output().unwrap();

tests/run_tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def run(*args, **kwargs):
1717

1818

1919
def main():
20+
run("rustc", "--crate-type=rlib", os.path.join(BASE_DIR, "testlib.rs"))
21+
2022
for path in os.listdir(BASE_DIR):
2123
if (
2224
not os.path.isdir(os.path.join(BASE_DIR, path)) or
@@ -57,6 +59,7 @@ def main():
5759
"--test",
5860
"--out-dir", os.path.join(BASE_DIR, path),
5961
os.path.join(BASE_DIR, path, "tests.rs"),
62+
"--extern", "kernel_module_tests=libtestlib.rlib"
6063
)
6164
# TODO: qemu
6265
run(

tests/testlib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::env;
2+
use std::process::Command;
3+
4+
struct LoadedModule {
5+
name: String,
6+
}
7+
8+
impl LoadedModule {
9+
fn load(name: String) -> LoadedModule {
10+
Command::new("sudo")
11+
.arg("insmod")
12+
.arg(&name)
13+
.status()
14+
.unwrap();
15+
return LoadedModule { name };
16+
}
17+
}
18+
19+
impl Drop for LoadedModule {
20+
fn drop(&mut self) {
21+
Command::new("sudo")
22+
.arg("rmmod")
23+
.arg(&self.name)
24+
.status()
25+
.unwrap();
26+
}
27+
}
28+
29+
pub fn with_kernel_module<F: Fn()>(f: F) {
30+
Command::new("sudo")
31+
.arg("dmesg")
32+
.arg("-C")
33+
.status()
34+
.unwrap();
35+
let _m = LoadedModule::load(env::var("KERNEL_MODULE").unwrap());
36+
f();
37+
}

0 commit comments

Comments
 (0)