hive-forge: ci-rerun takes repeatable --input key=value for workflow dispatch
The dispatch goes out as a raw API POST so a forge rejection of an input reaches the caller with its response body.
This commit is contained in:
parent
96cb25a040
commit
439cb28c8c
1 changed files with 378 additions and 19 deletions
179
hive-forge/tests/ci_rerun_inputs.rs
Normal file
179
hive-forge/tests/ci_rerun_inputs.rs
Normal file
|
|
@ -0,0 +1,179 @@
|
|||
//! `ci-rerun --branch … --input k=v` against a mock forge: the dispatch POST
|
||||
//! must carry the `inputs` map and `ref`, and a forge rejection must reach
|
||||
//! the caller verbatim.
|
||||
|
||||
use std::io::{BufRead as _, BufReader, Read as _, Write as _};
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
use std::sync::mpsc;
|
||||
|
||||
/// A unique scratch `HYPERHIVE_STATE_DIR` holding a `forge-token` file,
|
||||
/// same manual-tmpdir pattern as `pr_edit.rs`.
|
||||
fn scratch_state_dir(tag: &str) -> PathBuf {
|
||||
let ts = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0, |d| d.as_nanos());
|
||||
let dir = std::env::temp_dir().join(format!("hive-forge-ci-rerun-test-{tag}-{ts}"));
|
||||
std::fs::create_dir_all(&dir).expect("create scratch state dir");
|
||||
std::fs::write(dir.join("forge-token"), "test-token\n").expect("seed forge-token");
|
||||
dir
|
||||
}
|
||||
|
||||
/// Read one request, send `(request line, body)` down `tx`, answer with
|
||||
/// `status_line` and a JSON `body`.
|
||||
fn serve(
|
||||
mut stream: TcpStream,
|
||||
tx: &mpsc::Sender<(String, String)>,
|
||||
status_line: &str,
|
||||
reply: &str,
|
||||
) {
|
||||
let mut reader = BufReader::new(stream.try_clone().expect("clone stream"));
|
||||
let mut request_line = String::new();
|
||||
reader
|
||||
.read_line(&mut request_line)
|
||||
.expect("read request line");
|
||||
let mut content_length = 0usize;
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
if reader.read_line(&mut line).expect("read header") == 0 || line == "\r\n" {
|
||||
break;
|
||||
}
|
||||
if let Some((name, value)) = line.split_once(':')
|
||||
&& name.eq_ignore_ascii_case("content-length")
|
||||
{
|
||||
content_length = value.trim().parse().expect("numeric content-length");
|
||||
}
|
||||
}
|
||||
let mut raw = vec![0u8; content_length];
|
||||
reader.read_exact(&mut raw).expect("read body");
|
||||
tx.send((
|
||||
request_line.trim().to_owned(),
|
||||
String::from_utf8_lossy(&raw).into_owned(),
|
||||
))
|
||||
.expect("send captured request");
|
||||
let response = format!(
|
||||
"HTTP/1.1 {status_line}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{reply}",
|
||||
reply.len()
|
||||
);
|
||||
stream
|
||||
.write_all(response.as_bytes())
|
||||
.expect("write response");
|
||||
}
|
||||
|
||||
struct Outcome {
|
||||
ok: bool,
|
||||
stdout: String,
|
||||
stderr: String,
|
||||
request_line: String,
|
||||
body: serde_json::Value,
|
||||
}
|
||||
|
||||
/// Run `hive-forge -r o/r ci-rerun <args>` against a forge answering the
|
||||
/// dispatch with `status_line` / `reply`.
|
||||
fn dispatch(tag: &str, args: &[&str], status_line: &'static str, reply: &'static str) -> Outcome {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").expect("bind mock forge");
|
||||
let addr = listener.local_addr().expect("mock forge addr");
|
||||
let (tx, rx) = mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
for stream in listener.incoming() {
|
||||
serve(stream.expect("accept"), &tx, status_line, reply);
|
||||
}
|
||||
});
|
||||
let out = Command::new(env!("CARGO_BIN_EXE_hive-forge"))
|
||||
.args(["-r", "o/r", "ci-rerun"])
|
||||
.args(args)
|
||||
.env("HIVE_FORGE_URL", format!("http://{addr}"))
|
||||
.env("HYPERHIVE_STATE_DIR", scratch_state_dir(tag))
|
||||
.env_remove("HIVE_FORGE_TOKEN_FILE")
|
||||
.env_remove("HIVE_LABEL")
|
||||
.output()
|
||||
.expect("run hive-forge");
|
||||
let (request_line, body) = rx.try_recv().unwrap_or_default();
|
||||
Outcome {
|
||||
ok: out.status.success(),
|
||||
stdout: String::from_utf8_lossy(&out.stdout).into_owned(),
|
||||
stderr: String::from_utf8_lossy(&out.stderr).into_owned(),
|
||||
request_line,
|
||||
body: serde_json::from_str(&body).unwrap_or(serde_json::Value::Null),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inputs_travel_in_the_dispatch_body() {
|
||||
let o = dispatch(
|
||||
"inputs",
|
||||
&[
|
||||
"--branch",
|
||||
"k8s/v1",
|
||||
"--workflow",
|
||||
"vm-nightly.yml",
|
||||
"--input",
|
||||
"test=k8s-swarm-images",
|
||||
"--input",
|
||||
"extra=a=b",
|
||||
],
|
||||
"204 No Content",
|
||||
"",
|
||||
);
|
||||
assert!(o.ok, "{}", o.stderr);
|
||||
assert_eq!(
|
||||
o.request_line,
|
||||
"POST /api/v1/repos/o/r/actions/workflows/vm-nightly.yml/dispatches HTTP/1.1"
|
||||
);
|
||||
assert_eq!(o.body["ref"], "k8s/v1");
|
||||
assert_eq!(
|
||||
o.body["inputs"],
|
||||
serde_json::json!({ "test": "k8s-swarm-images", "extra": "a=b" })
|
||||
);
|
||||
assert!(
|
||||
o.stdout
|
||||
.contains("with inputs: test=k8s-swarm-images extra=a=b"),
|
||||
"{}",
|
||||
o.stdout
|
||||
);
|
||||
assert!(
|
||||
o.stdout
|
||||
.contains("ci-runs --workflow vm-nightly.yml --branch k8s/v1"),
|
||||
"{}",
|
||||
o.stdout
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_input_sends_no_inputs_key() {
|
||||
let o = dispatch("none", &["--branch", "main"], "204 No Content", "");
|
||||
assert!(o.ok, "{}", o.stderr);
|
||||
assert_eq!(o.body["ref"], "main");
|
||||
assert!(o.body["inputs"].is_null(), "{}", o.body);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forge_rejection_of_an_input_is_printed_verbatim() {
|
||||
let o = dispatch(
|
||||
"reject",
|
||||
&["--branch", "main", "--input", "bogus=1"],
|
||||
"400 Bad Request",
|
||||
r#"{"message":"input bogus is not defined in the workflow"}"#,
|
||||
);
|
||||
assert!(!o.ok, "{}", o.stdout);
|
||||
assert!(
|
||||
o.stderr
|
||||
.contains("input bogus is not defined in the workflow"),
|
||||
"{}",
|
||||
o.stderr
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_input_is_refused_before_any_request() {
|
||||
let o = dispatch(
|
||||
"malformed",
|
||||
&["--branch", "main", "--input", "nokey"],
|
||||
"204 No Content",
|
||||
"",
|
||||
);
|
||||
assert!(!o.ok);
|
||||
assert!(o.stderr.contains("not key=value"), "{}", o.stderr);
|
||||
assert!(o.request_line.is_empty(), "{}", o.request_line);
|
||||
}
|
||||
Loading…
Reference in a new issue