Skip to content
Open
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
14 changes: 12 additions & 2 deletions crates/youki/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ fn main() -> Result<()> {
std::env::args_os()
);

let log_to_file = opts.global.log.is_some();
let root_path = rootpath::determine(opts.global.root, &*syscall)?;
let systemd_cgroup = opts.global.systemd_cgroup;

Expand All @@ -116,7 +117,7 @@ fn main() -> Result<()> {
CommonCmd::Exec(exec) => match commands::exec::exec(exec, root_path) {
Ok(exit_code) => std::process::exit(exit_code),
Err(e) => {
tracing::error!("error in executing command: {:?}", e);
report_error(&e, log_to_file);
std::process::exit(-1);
}
},
Expand All @@ -128,7 +129,7 @@ fn main() -> Result<()> {
CommonCmd::Run(run) => match commands::run::run(run, root_path, systemd_cgroup) {
Ok(exit_code) => std::process::exit(exit_code),
Err(e) => {
tracing::error!("error in executing command: {:?}", e);
report_error(&e, log_to_file);
std::process::exit(-1);
}
},
Expand All @@ -150,3 +151,12 @@ fn main() -> Result<()> {
}
cmd_result
}

/// Logs the error, and also prints it to stderr when the log goes to a file
/// (`--log`), as runc and crun do. Without `--log` the log is already stderr.
fn report_error(e: &anyhow::Error, log_to_file: bool) {
tracing::error!("error in executing command: {:?}", e);
if log_to_file {
eprintln!("Error: {e:?}");
}
}
65 changes: 65 additions & 0 deletions crates/youki/tests/error_output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//! A failed `exec` or `run` must report the error on stderr when the log goes to a
//! file, as runc and crun do: callers such as conmon read the reason from stderr.
//! Without `--log` the log is stderr itself, so the error must appear only once.
//!
//! An invalid container id makes both commands fail before they touch any
//! container, so these tests need no privileges.

use std::fs;
use std::path::Path;
use std::process::{Command, Output};

const INVALID_ID: &str = "invalid$id";
const EXPECTED: &str = "container id contains invalid characters";

fn youki(root: &Path, log: Option<&Path>, args: &[&str]) -> Output {
let mut command = Command::new(env!("CARGO_BIN_EXE_youki"));
command.arg("--root").arg(root);
if let Some(log) = log {
command.arg("--log").arg(log).args(["--log-format", "json"]);
}
command.args(args).output().unwrap()
}

fn check_with_log_file(args: &[&str]) {
let dir = tempfile::tempdir().unwrap();
let log = dir.path().join("youki.log");

let output = youki(&dir.path().join("root"), Some(&log), args);

assert_eq!(output.status.code(), Some(255));
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains(EXPECTED), "stderr: {stderr:?}");
let log_content = fs::read_to_string(&log).unwrap();
assert!(log_content.contains(EXPECTED), "log: {log_content:?}");
}

fn check_without_log_file(args: &[&str]) {
let dir = tempfile::tempdir().unwrap();

let output = youki(&dir.path().join("root"), None, args);

assert_eq!(output.status.code(), Some(255));
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(stderr.matches(EXPECTED).count(), 1, "stderr: {stderr:?}");
}

#[test]
fn exec_error_with_log_file() {
check_with_log_file(&["exec", INVALID_ID, "true"]);
}

#[test]
fn exec_error_without_log_file() {
check_without_log_file(&["exec", INVALID_ID, "true"]);
}

#[test]
fn run_error_with_log_file() {
check_with_log_file(&["run", INVALID_ID]);
}

#[test]
fn run_error_without_log_file() {
check_without_log_file(&["run", INVALID_ID]);
}
Loading