Skip to content

Commit

Permalink
Finish timeout implementation
Browse files Browse the repository at this point in the history
This adds a test, sets the default timeout to 120s and fixes the
implementation to actually exit the process.

Signed-off-by: Tim Buchwaldt <tim@buchwaldt.ws>
  • Loading branch information
timbuchwaldt committed Sep 15, 2022
1 parent 3631086 commit 64e985d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
3 changes: 3 additions & 0 deletions core-dump-composer/mocks/crictl-timeout.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

sleep 10
2 changes: 1 addition & 1 deletion core-dump-composer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl CoreConfig {
let pathname = matches.value_of("pathname").unwrap_or("").to_string();
let timeout = matches
.value_of("timeout")
.unwrap_or("60")
.unwrap_or("120")
.parse::<u64>()
.unwrap();

Expand Down
7 changes: 5 additions & 2 deletions core-dump-composer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod logging;
fn main() -> Result<(), anyhow::Error> {
let (send, recv) = channel();
let cc = config::CoreConfig::new()?;
let timeout = cc.params.timeout.clone();
let timeout = cc.params.timeout;

thread::spawn(move || {
let result = handle(cc);
Expand All @@ -32,7 +32,10 @@ fn main() -> Result<(), anyhow::Error> {

match result {
Ok(inner_result) => inner_result,
Err(error) => panic!("Timeout: {}", error),
Err(_error) => {
println!("timeout");
process::exit(1);
}
}
}

Expand Down
73 changes: 73 additions & 0 deletions core-dump-composer/tests/timeout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::env;
use std::process::{Command, Stdio};

#[test]
fn timeout_scenario() -> Result<(), std::io::Error> {
let current_dir = env::current_dir()?;

println!("The current directory is {}", current_dir.display());
// Need to append to path
let key = "PATH";
let mut current_path = String::new();
match env::var(key) {
Ok(val) => current_path = val,
Err(e) => println!("couldn't interpret {}: {}", key, e),
}
let new_path = format!(
"{}/mocks:{}/target/debug:{}",
current_dir.display(),
current_dir.display(),
current_path
);
println!("Running tests using this PATH: {}", new_path);
let output_folder = format!("{}/{}", ".", "output");
// Make a directory to store the generated zip file
let _mkdir = match Command::new("mkdir").arg("-p").arg(&output_folder).spawn() {
Err(why) => panic!("couldn't spawn mkdir: {}", why),
Ok(process) => process,
};
// copy crictl to base_folder
Command::new("cp")
.arg("-f")
.arg("./mocks/crictl-timeout.sh")
.arg("../target/debug/crictl")
.output()
.expect("cp failed");

// cat the test core file to process.
let cat = Command::new("cat")
.env("PATH", &new_path)
.arg("./mocks/test.core")
.stdout(Stdio::piped())
.spawn()?
.stdout
.unwrap();

let cdc = Command::new("../target/debug/core-dump-composer")
.arg("-c")
.arg("1000000000")
.arg("-e")
.arg("node")
.arg("-p")
.arg("4")
.arg("-s")
.arg("10")
.arg("-E")
.arg("!target!debug!core-dump-composer")
.arg("-d")
.arg(&output_folder)
.arg("-t")
.arg("1588462466")
.arg("-h")
.arg("crashing-app-699c49b4ff-86wrh")
.arg("--timeout")
.arg("1")
.stdin(cat)
.output()
.expect("Couldn't execute");

println!("{}", String::from_utf8_lossy(&cdc.stdout));
assert_eq!("timeout\n", String::from_utf8_lossy(&cdc.stdout));
assert_eq!(1, *&cdc.status.code().unwrap());
Ok(())
}

0 comments on commit 64e985d

Please sign in to comment.