Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement timeout handling #111

Merged
merged 2 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 16 additions & 0 deletions core-dump-composer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct CoreParams {
pub directory: String,
pub hostname: String,
pub pathname: String,
pub timeout: u64,
pub namespace: Option<String>,
pub podname: Option<String>,
pub uuid: Uuid,
Expand All @@ -56,6 +57,12 @@ impl CoreConfig {
let directory = matches.value_of("directory").unwrap_or("").to_string();
let hostname = matches.value_of("hostname").unwrap_or("").to_string();
let pathname = matches.value_of("pathname").unwrap_or("").to_string();
let timeout = matches
.value_of("timeout")
.unwrap_or("120")
.parse::<u64>()
.unwrap();

let uuid = Uuid::new_v4();

let params = CoreParams {
Expand All @@ -67,6 +74,7 @@ impl CoreConfig {
directory,
hostname,
pathname,
timeout,
namespace: None,
podname: None,
uuid,
Expand Down Expand Up @@ -292,6 +300,14 @@ pub fn try_get_matches() -> clap::Result<ArgMatches> {
.takes_value(true)
.help("Hostname (same as nodename returned by uname(2))"),
)
.arg(
Arg::new("timeout")
.short('T')
.long("timeout")
.required(false)
.takes_value(true)
.help("Timeout in seconds to wait for processing of the Coredump"),
)
.arg(
Arg::new("test-threads")
.long("test-threads")
Expand Down
25 changes: 24 additions & 1 deletion core-dump-composer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,37 @@ use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::process;
use std::sync::mpsc::channel;
use std::thread;
use std::time::Duration;
use zip::write::FileOptions;
use zip::ZipWriter;

mod config;
mod logging;

fn main() -> Result<(), anyhow::Error> {
let mut cc = config::CoreConfig::new()?;
let (send, recv) = channel();
let cc = config::CoreConfig::new()?;
let timeout = cc.params.timeout;

thread::spawn(move || {
let result = handle(cc);
send.send(result).unwrap();
});

let result = recv.recv_timeout(Duration::from_secs(timeout));

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

fn handle(mut cc: config::CoreConfig) -> Result<(), anyhow::Error> {
cc.set_namespace("default".to_string());
let l_log_level = cc.log_level.clone();
let log_path = logging::init_logger(l_log_level)?;
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(())
}