Skip to content

Commit f38a2aa

Browse files
authored
Merge pull request #111 from 21studios/main
Implement timeout handling
2 parents 2386e91 + 64e985d commit f38a2aa

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
sleep 10

core-dump-composer/src/config.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub struct CoreParams {
3838
pub directory: String,
3939
pub hostname: String,
4040
pub pathname: String,
41+
pub timeout: u64,
4142
pub namespace: Option<String>,
4243
pub podname: Option<String>,
4344
pub uuid: Uuid,
@@ -56,6 +57,12 @@ impl CoreConfig {
5657
let directory = matches.value_of("directory").unwrap_or("").to_string();
5758
let hostname = matches.value_of("hostname").unwrap_or("").to_string();
5859
let pathname = matches.value_of("pathname").unwrap_or("").to_string();
60+
let timeout = matches
61+
.value_of("timeout")
62+
.unwrap_or("120")
63+
.parse::<u64>()
64+
.unwrap();
65+
5966
let uuid = Uuid::new_v4();
6067

6168
let params = CoreParams {
@@ -67,6 +74,7 @@ impl CoreConfig {
6774
directory,
6875
hostname,
6976
pathname,
77+
timeout,
7078
namespace: None,
7179
podname: None,
7280
uuid,
@@ -292,6 +300,14 @@ pub fn try_get_matches() -> clap::Result<ArgMatches> {
292300
.takes_value(true)
293301
.help("Hostname (same as nodename returned by uname(2))"),
294302
)
303+
.arg(
304+
Arg::new("timeout")
305+
.short('T')
306+
.long("timeout")
307+
.required(false)
308+
.takes_value(true)
309+
.help("Timeout in seconds to wait for processing of the Coredump"),
310+
)
295311
.arg(
296312
Arg::new("test-threads")
297313
.long("test-threads")

core-dump-composer/src/main.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,37 @@ use std::fs::File;
99
use std::io;
1010
use std::io::prelude::*;
1111
use std::process;
12+
use std::sync::mpsc::channel;
13+
use std::thread;
14+
use std::time::Duration;
1215
use zip::write::FileOptions;
1316
use zip::ZipWriter;
1417

1518
mod config;
1619
mod logging;
1720

1821
fn main() -> Result<(), anyhow::Error> {
19-
let mut cc = config::CoreConfig::new()?;
22+
let (send, recv) = channel();
23+
let cc = config::CoreConfig::new()?;
24+
let timeout = cc.params.timeout;
25+
26+
thread::spawn(move || {
27+
let result = handle(cc);
28+
send.send(result).unwrap();
29+
});
30+
31+
let result = recv.recv_timeout(Duration::from_secs(timeout));
32+
33+
match result {
34+
Ok(inner_result) => inner_result,
35+
Err(_error) => {
36+
println!("timeout");
37+
process::exit(1);
38+
}
39+
}
40+
}
41+
42+
fn handle(mut cc: config::CoreConfig) -> Result<(), anyhow::Error> {
2043
cc.set_namespace("default".to_string());
2144
let l_log_level = cc.log_level.clone();
2245
let log_path = logging::init_logger(l_log_level)?;

core-dump-composer/tests/timeout.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use std::env;
2+
use std::process::{Command, Stdio};
3+
4+
#[test]
5+
fn timeout_scenario() -> Result<(), std::io::Error> {
6+
let current_dir = env::current_dir()?;
7+
8+
println!("The current directory is {}", current_dir.display());
9+
// Need to append to path
10+
let key = "PATH";
11+
let mut current_path = String::new();
12+
match env::var(key) {
13+
Ok(val) => current_path = val,
14+
Err(e) => println!("couldn't interpret {}: {}", key, e),
15+
}
16+
let new_path = format!(
17+
"{}/mocks:{}/target/debug:{}",
18+
current_dir.display(),
19+
current_dir.display(),
20+
current_path
21+
);
22+
println!("Running tests using this PATH: {}", new_path);
23+
let output_folder = format!("{}/{}", ".", "output");
24+
// Make a directory to store the generated zip file
25+
let _mkdir = match Command::new("mkdir").arg("-p").arg(&output_folder).spawn() {
26+
Err(why) => panic!("couldn't spawn mkdir: {}", why),
27+
Ok(process) => process,
28+
};
29+
// copy crictl to base_folder
30+
Command::new("cp")
31+
.arg("-f")
32+
.arg("./mocks/crictl-timeout.sh")
33+
.arg("../target/debug/crictl")
34+
.output()
35+
.expect("cp failed");
36+
37+
// cat the test core file to process.
38+
let cat = Command::new("cat")
39+
.env("PATH", &new_path)
40+
.arg("./mocks/test.core")
41+
.stdout(Stdio::piped())
42+
.spawn()?
43+
.stdout
44+
.unwrap();
45+
46+
let cdc = Command::new("../target/debug/core-dump-composer")
47+
.arg("-c")
48+
.arg("1000000000")
49+
.arg("-e")
50+
.arg("node")
51+
.arg("-p")
52+
.arg("4")
53+
.arg("-s")
54+
.arg("10")
55+
.arg("-E")
56+
.arg("!target!debug!core-dump-composer")
57+
.arg("-d")
58+
.arg(&output_folder)
59+
.arg("-t")
60+
.arg("1588462466")
61+
.arg("-h")
62+
.arg("crashing-app-699c49b4ff-86wrh")
63+
.arg("--timeout")
64+
.arg("1")
65+
.stdin(cat)
66+
.output()
67+
.expect("Couldn't execute");
68+
69+
println!("{}", String::from_utf8_lossy(&cdc.stdout));
70+
assert_eq!("timeout\n", String::from_utf8_lossy(&cdc.stdout));
71+
assert_eq!(1, *&cdc.status.code().unwrap());
72+
Ok(())
73+
}

0 commit comments

Comments
 (0)