sim: Return exit status from sim run

Instead of just printing a message, return an exit status so that
scripts running the sim can more easily tell if the tests passed.

Jira: MCUB-45
This commit is contained in:
David Brown 2017-03-29 12:28:47 -06:00
parent 274f7874db
commit 361be7ade7
1 changed files with 165 additions and 111 deletions

View File

@ -12,6 +12,7 @@ use docopt::Docopt;
use rand::{Rng, SeedableRng, XorShiftRng};
use rustc_serialize::{Decodable, Decoder};
use std::mem;
use std::process;
use std::slice;
mod area;
@ -79,11 +80,44 @@ fn main() {
return;
}
let mut status = RunStatus::new();
let align = args.flag_align.map(|x| x.0).unwrap_or(1);
let (mut flash, areadesc) = match args.flag_device {
None => panic!("Missing mandatory argument"),
Some(DeviceName::Stm32f4) => {
let device = match args.flag_device {
None => panic!("Missing mandatory device argument"),
Some(dev) => dev,
};
status.run_single(device, align);
if status.failures > 0 {
warn!("{} Tests ran with {} failures", status.failures + status.passes, status.failures);
process::exit(1);
} else {
warn!("{} Tests ran successfully", status.passes);
process::exit(0);
}
}
struct RunStatus {
failures: usize,
passes: usize,
}
impl RunStatus {
fn new() -> RunStatus {
RunStatus {
failures: 0,
passes: 0,
}
}
fn run_single(&mut self, device: DeviceName, align: u8) {
let mut failed = false;
let (mut flash, areadesc) = match device {
DeviceName::Stm32f4 => {
// STM style flash. Large sectors, with a large scratch area.
let flash = Flash::new(vec![16 * 1024, 16 * 1024, 16 * 1024, 16 * 1024,
64 * 1024,
@ -95,7 +129,7 @@ fn main() {
areadesc.add_image(0x060000, 0x020000, FlashId::ImageScratch);
(flash, areadesc)
}
Some(DeviceName::K64f) => {
DeviceName::K64f => {
// NXP style flash. Small sectors, one small sector for scratch.
let flash = Flash::new(vec![4096; 128], align as usize);
@ -105,7 +139,7 @@ fn main() {
areadesc.add_image(0x060000, 0x001000, FlashId::ImageScratch);
(flash, areadesc)
}
Some(DeviceName::K64fBig) => {
DeviceName::K64fBig => {
// Simulating an STM style flash on top of an NXP style flash. Underlying flash device
// uses small sectors, but we tell the bootloader they are large.
let flash = Flash::new(vec![4096; 128], align as usize);
@ -116,7 +150,7 @@ fn main() {
areadesc.add_simple_image(0x060000, 0x020000, FlashId::ImageScratch);
(flash, areadesc)
}
Some(DeviceName::Nrf52840) => {
DeviceName::Nrf52840 => {
// Simulating the flash on the nrf52840 with partitions set up so that the scratch size
// does not divide into the image size.
let flash = Flash::new(vec![4096; 128], align as usize);
@ -157,7 +191,12 @@ fn main() {
let (fl2, total_count) = try_upgrade(&flash, &areadesc, None);
info!("First boot, count={}", total_count);
assert!(verify_image(&fl2, slot0_base, &upgrade));
if !verify_image(&fl2, slot0_base, &upgrade) {
error!("Image mismatch after first boot");
// This isn't really recoverable, and more tests aren't likely to reveal much.
self.failures += 1;
return;
}
let mut bad = 0;
// Let's try an image halfway through.
@ -177,16 +216,25 @@ fn main() {
error!("{} out of {} failed {:.2}%",
bad, total_count,
bad as f32 * 100.0 / total_count as f32);
if bad > 0 {
failed = true;
}
for count in 2 .. 5 {
info!("Try revert: {}", count);
let fl2 = try_revert(&flash, &areadesc, count);
assert!(verify_image(&fl2, slot0_base, &primary));
if !verify_image(&fl2, slot0_base, &primary) {
warn!("Revert failure on count {}", count);
failed = true;
}
}
info!("Try norevert");
let fl2 = try_norevert(&flash, &areadesc);
assert!(verify_image(&fl2, slot0_base, &upgrade));
if !verify_image(&fl2, slot0_base, &upgrade) {
warn!("No revert failed");
failed = true;
}
/*
// show_flash(&flash);
@ -201,6 +249,12 @@ fn main() {
println!("\n------------------\nSecond boot");
c::boot_go(&mut flash, &areadesc);
*/
if failed {
self.failures += 1;
} else {
self.passes += 1;
}
}
}
/// Test a boot, optionally stopping after 'n' flash options. Returns a count of the number of