Create test runner
diff --git a/.travis.yml b/.travis.yml index 7b6bf87..162f863 100644 --- a/.travis.yml +++ b/.travis.yml
@@ -16,10 +16,10 @@ # Once Travis supports a version of Ubuntu Disco or newer we can apt install QEMU for RISC-V # Until then we need to build it ourselves before_install: -# - sudo apt-get -y install qemu-system-misc -# addons: -# apt: -# update: true + # - sudo apt-get -y install qemu-system-misc + # addons: + # apt: + # update: true - wget https://download.qemu.org/qemu-4.2.0.tar.xz - tar xJf qemu-4.2.0.tar.xz - pushd qemu-4.2.0 @@ -42,6 +42,7 @@ script: - make test # Run a QEMU instance of the HiFive1 app - - make flash-hifive1 EXAMPLE=hello_world - - timeout --foreground 10s qemu-system-riscv32 -M sifive_e -kernel ../tock/boards/hifive1/target/riscv32imac-unknown-none-elf/release/hifive1 -device loader,file=./target/riscv32imac-unknown-none-elf/tab/hifive1/hello_world/rv32imac.tbf,addr=0x20430000 -nographic | tee serial - - grep "Hello Tock World" serial + - PLATFORM=hifive1 cargo rrv32imac --example libtock_test --features=alloc + - pushd test-runner + - cargo run + - popd
diff --git a/Cargo.toml b/Cargo.toml index 5641d3c..c1b9714 100644 --- a/Cargo.toml +++ b/Cargo.toml
@@ -59,5 +59,6 @@ members = [ "async-support", "codegen", - "core" + "core", + "test-runner" ]
diff --git a/test-runner/Cargo.toml b/test-runner/Cargo.toml new file mode 100644 index 0000000..22d3e07 --- /dev/null +++ b/test-runner/Cargo.toml
@@ -0,0 +1,19 @@ +[package] +name = "test-runner" +version = "0.1.0" +authors = ["torfmaster <briefe@kebes.de>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +structopt = { version = "0.3", default-features = false } +futures = "0.3.4" + +[dependencies.async-std] +version = "1.5.0" +features = ["attributes"] + +[dependencies.tokio] +version = "0.2.12" +features = ["process", "rt-threaded", "macros", "io-util", "time"] \ No newline at end of file
diff --git a/test-runner/src/main.rs b/test-runner/src/main.rs new file mode 100644 index 0000000..e8c7dcb --- /dev/null +++ b/test-runner/src/main.rs
@@ -0,0 +1,105 @@ +use std::collections::HashMap; +use std::process::Stdio; +use std::time::Duration; +use structopt::StructOpt; +use tokio::io::AsyncBufReadExt; +use tokio::io::BufReader; +use tokio::process::Command; +use tokio::time::timeout; + +#[derive(Debug, StructOpt)] +#[structopt(name = "libtock test runner", about = "run libtock tests")] +struct Arguments { + #[structopt(short, long)] + timeout_ms: usize, +} + +#[tokio::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + timeout(Duration::from_secs(10), perform_tests()).await? +} + +async fn perform_tests() -> Result<(), Box<dyn std::error::Error>> { + let tests = Command::new("qemu-system-riscv32") + .arg("-M") + .arg("sifive_e") + .arg("-kernel") + .arg("../../tock/boards/hifive1/target/riscv32imac-unknown-none-elf/release/hifive1") + .arg("-device") + .arg("loader,file=./../target/riscv32imac-unknown-none-elf/tab/hifive1/libtock_test/rv32imac.tbf,addr=0x20430000") + .arg("-nographic") + .stdout(Stdio::piped()) + .kill_on_drop(true) + .spawn()?; + + let stdout = tests.stdout.unwrap(); + + let stdout_reader = BufReader::new(stdout); + let mut stdout_lines = stdout_reader.lines(); + let mut result_map = HashMap::<TockTest, bool>::new(); + + while let Some(line) = stdout_lines.next_line().await? { + println!("UART: {}", line); + if let Some(entry) = parse_line(line) { + result_map.insert(entry.0, entry.1); + } + if result_map.keys().len() >= NUMBER_OF_TESTS { + if result_map.values().all(|x| *x) { + return Ok(()); + } else { + panic!(); + } + } + } + panic!("qemu exited unexpectedly") +} + +fn parse_line(input: String) -> Option<(TockTest, bool)> { + let result = input.find("[ OK ]").is_some(); + let input = input.replace("[ OK ]", ""); + let input = input.replace("[ FAILURE ]", ""); + let input = input.trim(); + let tock_test = label_to_tocktest(input.to_string()); + tock_test.map(|test| (test, result)) +} + +const NUMBER_OF_TESTS: usize = 6; +#[derive(PartialEq, Debug, Hash, Eq)] +enum TockTest { + Console, + StaticMut, + DynamicDispatch, + Formatting, + Heap, + DriversOnlyInstantiableOnce, +} + +fn label_to_tocktest(label: String) -> Option<TockTest> { + match label.as_str() { + "Console" => Some(TockTest::Console), + "static mut" => Some(TockTest::StaticMut), + "Dynamic dispatch" => Some(TockTest::DynamicDispatch), + "Formatting" => Some(TockTest::Formatting), + "Heap" => Some(TockTest::Heap), + "Drivers only instantiable once" => Some(TockTest::DriversOnlyInstantiableOnce), + _ => None, + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + pub fn works() { + assert_eq!( + parse_line("[ OK ] Console".into()), + Some((TockTest::Console, true)) + ); + } + + #[test] + pub fn fails() { + assert_eq!(parse_line("[ OK ] No Console".into()), None); + } +}