1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//! Per-sh CRI log files for CI runs.
//!
//! Each `(sh ...)` call within a job produces a file at
//! `jobs/<job-id>/sh-<n>.log` in k8s CRI log format:
//!
//! ```text
//! <RFC3339 ts> <stream> <tag> <content>
//! ```
//!
//! Stream is `stdout` or `stderr`. Tag is `F` (full line).
use std::path::Path;
use super::runtime::ShOutput;
/// Write a sh output to a CRI log file.
///
/// Each line of stdout/stderr becomes one CRI-format line with the
/// given base timestamp, stream tag, and `F` (full) tag.
pub fn write_cri_log(path: &Path, output: &ShOutput, ts: &str) -> std::io::Result<()> {
use std::io::Write;
let mut f = fs_err::File::create(path)?;
for line in output.stdout.lines() {
writeln!(f, "{ts} stdout F {line}")?;
}
for line in output.stderr.lines() {
writeln!(f, "{ts} stderr F {line}")?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cri_log_splits_stdout_into_lines() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join("sh-1.log");
let output = ShOutput {
exit: 0,
stdout: "line one\nline two\n".to_string(),
stderr: String::new(),
cmd: "[\"echo\"]".to_string(),
};
write_cri_log(&path, &output, "2026-05-06T12:00:00Z").expect("write");
let contents = std::fs::read_to_string(&path).expect("read");
let lines: Vec<&str> = contents.lines().collect();
assert_eq!(lines.len(), 2);
assert!(lines[0].contains("stdout F line one"));
assert!(lines[1].contains("stdout F line two"));
}
#[test]
fn cri_log_handles_stderr() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join("sh-1.log");
let output = ShOutput {
exit: 1,
stdout: String::new(),
stderr: "an error\n".to_string(),
cmd: "[\"false\"]".to_string(),
};
write_cri_log(&path, &output, "2026-05-06T12:00:00Z").expect("write");
let contents = std::fs::read_to_string(&path).expect("read");
assert!(contents.contains("stderr F an error"));
}
#[test]
fn cri_log_handles_empty_output() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join("sh-1.log");
let output = ShOutput {
exit: 0,
stdout: String::new(),
stderr: String::new(),
cmd: "true".to_string(),
};
write_cri_log(&path, &output, "2026-05-06T12:00:00Z").expect("write");
let contents = std::fs::read_to_string(&path).expect("read");
assert!(contents.is_empty());
}
}