Skip to content

Commit

Permalink
ci: test status now in json
Browse files Browse the repository at this point in the history
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
  • Loading branch information
Kent Overstreet committed Feb 1, 2024
1 parent aab9f00 commit 8ee3955
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 70 deletions.
36 changes: 19 additions & 17 deletions ci-web/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions ci-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@ build = "src/build.rs"
cgi = "0.6"
git2 = "0.16"
querystring = "1.1.0"
multimap = "0.8.3"
die = "0.2.0"
libc = "0.2"
toml = "0.5.9"
serde = "1.0.145"
serde_derive = "1.0.145"
regex = "1"
memoize = "0.3.1"
glob = "0.3.0"
clap = { version = "4.0.32", features = ["derive"] }
file-lock = "2.1.6"
capnp = "0.19.*"
anyhow = "1.0.71"
chrono = "0.4.26"
chrono = { version = "0.4.26", features = ["serde"] }
serde_json = "1.0.113"

[build-dependencies]
capnpc = "0.19.*"
7 changes: 4 additions & 3 deletions ci-web/src/bin/cgi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const STYLESHEET: &str = "bootstrap.min.css";
fn filter_results(r: TestResultsMap, tests_matching: &Regex) -> TestResultsMap {
r.iter()
.filter(|i| tests_matching.is_match(&i.0) )
.map(|(k, v)| (k.clone(), *v))
.map(|(k, v)| (k.clone(), v.clone()))
.collect()
}

Expand Down Expand Up @@ -45,9 +45,9 @@ fn commit_get_results(ci: &Ci, commit: &git2::Commit) -> CommitResults {
let tests = commitdir_get_results_filtered(ci, &id);

CommitResults {
id: id,
id,
message: commit.message().unwrap().to_string(),
tests: tests,
tests,
}
}

Expand Down Expand Up @@ -254,6 +254,7 @@ fn ci_commit(ci: &Ci) -> cgi::Response {
writeln!(&mut out, "<tr class={}>", result.status.table_class()).unwrap();
writeln!(&mut out, "<td> {} </td>", name).unwrap();
writeln!(&mut out, "<td> {} </td>", result.status.to_str()).unwrap();
writeln!(&mut out, "<td> {} </td>", result.msg.unwrap_or("".to_string())).unwrap();
writeln!(&mut out, "<td> {}s </td>", result.duration).unwrap();
writeln!(&mut out, "<td> <a href=c/{}/{}/log.br> log </a> </td>", &commit_id, name).unwrap();
writeln!(&mut out, "<td> <a href=c/{}/{}/full_log.br> full log </a> </td>", &commit_id, name).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion ci-web/src/bin/gen-commit-summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ fn main() {
}
let ktestrc = ktestrc.unwrap();

commit_update_results_from_fs(&ktestrc, &args.commit);
let commit_dir = ktestrc.output_dir.join(&args.commit);
commit_update_results_from_fs(&commit_dir);
}
76 changes: 49 additions & 27 deletions ci-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::BTreeMap;
use std::fs::read_to_string;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use serde_derive::Deserialize;
use toml;
use anyhow;
Expand Down Expand Up @@ -58,17 +58,19 @@ pub use testresult_capnp::test_result::Status as TestStatus;

impl TestStatus {
fn from_str(status: &str) -> TestStatus {
let status = status.to_lowercase();

if status.is_empty() {
TestStatus::Inprogress
} else if status.contains("IN PROGRESS") {
} else if status.contains("in progress") {
TestStatus::Inprogress
} else if status.contains("PASSED") {
} else if status.contains("passed") {
TestStatus::Passed
} else if status.contains("FAILED") {
} else if status.contains("failed") {
TestStatus::Failed
} else if status.contains("NOTRUN") {
} else if status.contains("notrun") {
TestStatus::Notrun
} else if status.contains("NOT STARTED") {
} else if status.contains("not started") {
TestStatus::Notstarted
} else {
TestStatus::Unknown
Expand Down Expand Up @@ -98,35 +100,54 @@ impl TestStatus {
}
}

#[derive(Copy, Clone, Debug)]
#[derive(Clone, Debug, Deserialize)]
pub struct TestResultJson {
pub status: String,
pub msg: Option<String>,
pub duration: u64,
}

#[derive(Clone, Debug)]
pub struct TestResult {
pub status: TestStatus,
pub starttime: DateTime<Utc>,
pub duration: u64,
pub status: TestStatus,
pub msg: Option<String>,
pub starttime: DateTime<Utc>,
pub duration: u64,
}

pub type TestResultsMap = BTreeMap<String, TestResult>;

fn commitdir_get_results_fs(ktestrc: &Ktestrc, commit_id: &String) -> TestResultsMap {
fn commitdir_get_results_fs(commit_dir: &Path) -> TestResultsMap {
fn read_test_result(testdir: &std::fs::DirEntry) -> Option<TestResult> {
let mut f = File::open(&testdir.path().join("status")).ok()?;
let mut status = String::new();
f.read_to_string(&mut status).ok()?;
let mut status_str = String::new();
f.read_to_string(&mut status_str).ok()?;

let status_str = status_str.trim();

println!("status_str: {:?}", status_str);

let status_json: TestResultJson = serde_json::from_str(&status_str).unwrap();

println!("status_json: {:?}", status_json);

Some(TestResult {
status: TestStatus::from_str(&status),
starttime: f.metadata().ok()?.modified().ok()?.into(),
duration: read_to_string(&testdir.path().join("duration")).unwrap_or("0".to_string()).parse().unwrap_or(0),
status: TestStatus::from_str(&status_json.status),
msg: status_json.msg,
starttime: f.metadata().ok()?.modified().ok()?.into(),
duration: status_json.duration,
})
}

let mut results = BTreeMap::new();

let results_dir = ktestrc.output_dir.join(commit_id).read_dir();
let results_dir = commit_dir.read_dir();

if let Ok(results_dir) = results_dir {
for d in results_dir.filter_map(|i| i.ok()) {
if let Some(r) = read_test_result(&d) {
let r = read_test_result(&d);
println!("{:?}", r);
if let Some(r) = r {
results.insert(d.file_name().into_string().unwrap(), r);
}
}
Expand All @@ -138,7 +159,7 @@ fn commitdir_get_results_fs(ktestrc: &Ktestrc, commit_id: &String) -> TestResult
use testresult_capnp::test_results;
use capnp::serialize;

fn results_to_capnp(ktestrc: &Ktestrc, commit_id: &String, results_in: &TestResultsMap) -> anyhow::Result<()> {
fn results_to_capnp(commit_dir: &Path, results_in: &TestResultsMap) -> anyhow::Result<()> {
let mut message = capnp::message::Builder::new_default();
let results = message.init_root::<test_results::Builder>();
let mut result_list = results.init_entries(results_in.len().try_into().unwrap());
Expand All @@ -151,8 +172,8 @@ fn results_to_capnp(ktestrc: &Ktestrc, commit_id: &String, results_in: &TestResu
result.set_status(result_in.status);
}

let fname = ktestrc.output_dir.join(commit_id.clone() + ".capnp");
let fname_new = ktestrc.output_dir.join(commit_id.clone() + ".capnp.new");
let fname = commit_dir.with_extension("capnp");
let fname_new = commit_dir.with_extension("capnp.new");

let mut out = File::create(&fname_new)?;

Expand All @@ -163,10 +184,10 @@ fn results_to_capnp(ktestrc: &Ktestrc, commit_id: &String, results_in: &TestResu
Ok(())
}

pub fn commit_update_results_from_fs(ktestrc: &Ktestrc, commit_id: &String) {
let results = commitdir_get_results_fs(&ktestrc, commit_id);
pub fn commit_update_results_from_fs(commit_dir: &Path) {
let results = commitdir_get_results_fs(commit_dir);

results_to_capnp(ktestrc, commit_id, &results)
results_to_capnp(commit_dir, &results)
.map_err(|e| eprintln!("error generating capnp: {}", e)).ok();
}

Expand All @@ -180,9 +201,10 @@ pub fn commitdir_get_results(ktestrc: &Ktestrc, commit_id: &String) -> anyhow::R
let mut results = BTreeMap::new();
for e in entries {
let r = TestResult {
status: e.get_status()?,
starttime: Utc.timestamp_opt(e.get_starttime(), 0).unwrap(),
duration: e.get_duration()
status: e.get_status()?,
msg: e.get_msg()?.to_string().ok(),
starttime: Utc.timestamp_opt(e.get_starttime(), 0).unwrap(),
duration: e.get_duration()
};

results.insert(e.get_name()?.to_string()?, r);
Expand Down
1 change: 1 addition & 0 deletions ci-web/src/testresult.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct TestResult {
notstarted @4;
unknown @5;
}
msg @4: Text;
}

struct TestResults {
Expand Down
29 changes: 12 additions & 17 deletions lib/supervisor.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,9 @@ static char *test_is_starting(const char *line)
return ret;
}

static bool test_is_ending(char *line)
static const char *test_is_ending(const char *line)
{
return str_starts_with(line, "========= PASSED ") ||
str_starts_with(line, "========= FAILED ") ||
str_starts_with(line, "========= NOTRUN");
return str_starts_with(line, "========= FINISH ");
}

static FILE *popen_with_pid(char *argv[], pid_t *child)
Expand Down Expand Up @@ -224,13 +222,10 @@ static void test_start(char *new_test, struct timespec now)
set_timeout(default_timeout);
}

static void test_end(struct timespec now)
static void test_end(void)
{
write_test_file("duration", "%li", now.tv_sec - current_test_start.tv_sec);

fclose(current_test_log);
current_test_log = NULL;

set_timeout(default_timeout);
}

Expand Down Expand Up @@ -327,19 +322,17 @@ int main(int argc, char *argv[])
set_timeout(default_timeout);
again:
while ((len = getline(&line, &n, childf)) >= 0) {
struct timespec now = xclock_gettime(CLOCK_MONOTONIC);

strim(line);

char *output = mprintf("%.5lu %s\n", now.tv_sec - start.tv_sec, line);

read_watchdog(line);

char *new_test = test_is_starting(line);
struct timespec now = xclock_gettime(CLOCK_MONOTONIC);
char *output = mprintf("%.5lu %s\n", now.tv_sec - start.tv_sec, line);

/* If a test is starting, close logfile for previous test: */
char *new_test = test_is_starting(line);
if (current_test_log && new_test)
test_end(now);
test_end();

if (new_test)
test_start(new_test, now);
Expand All @@ -349,9 +342,11 @@ int main(int argc, char *argv[])
fputs(output, logfile);
fputs(output, stdout);

if (current_test_log && test_is_ending(line)) {
write_test_file("status", "%s\n", line);
test_end(now);
const char *statusline;
if (current_test_log &&
(statusline = test_is_ending(line))) {
write_test_file("status", "%s\n", statusline);
test_end();
}

if (exit_on_failure && str_starts_with(line, "TEST FAILED"))
Expand Down
Loading

0 comments on commit 8ee3955

Please sign in to comment.