diff options
Diffstat (limited to 'tests/receptor.rs')
| -rw-r--r-- | tests/receptor.rs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/tests/receptor.rs b/tests/receptor.rs index 170fbae..061e23f 100644 --- a/tests/receptor.rs +++ b/tests/receptor.rs @@ -742,3 +742,95 @@ fn pack_from_locations() { "{said}" ); } + +/// `--pack --dry-run`: every problem, by address, with location and variables, as text +/// or JSON; nothing written; and --pack refuses with all of them. +#[test] +fn pack_dry_run() { + let host = Host::new("dry-run"); + let _ = host.schema(secrets_schema(&host)); + // A store that has a.pub, and nothing else. + let secrets = host.program( + "secrets", + r#"if [ "$2" = a.pub ]; then echo pub-of-K; else echo "Error: $2 is not in the password store." >&2; exit 1; fi"#, + ); + let sources = host.root.join("sources.json"); + std::fs::write( + &sources, + json!({ + "channel": "secrets", + "complete": true, + "locations": { "secrets": { "source": { "command": [secrets, "show", "$(var:key)"] } } }, + "items": { + "a.key": { "location": "secrets", "key": "a.key" }, + "a.pub": { "location": "secrets", "key": "a.pub" }, + "nope": { "source": "env:PATH" } + } + }) + .to_string(), + ) + .unwrap(); + let enstate = |arguments: &[&str]| { + let output = Command::new(env!("CARGO_BIN_EXE_enstate")) + .args(arguments) + .arg(format!( + "--schema={}", + host.root.join("schema.json").display() + )) + .output() + .unwrap(); + ( + output.status.code(), + String::from_utf8_lossy(&output.stdout).into_owned(), + String::from_utf8_lossy(&output.stderr).into_owned(), + ) + }; + let path = sources.to_str().unwrap(); + let (code, stdout, stderr) = enstate(&["--pack", path, "--dry-run"]); + assert_eq!(code, Some(1), "{stderr}"); + assert!( + stdout.contains("secrets/a.key (location secrets, key a.key): can't be read:") + && stdout.contains("is not in the password store") + && stdout.contains("secrets/nope: the schema has no slot secrets/nope") + && stdout.contains("channel secrets: 3 items, 2 problems"), + "{stdout}" + ); + let (code, stdout, _) = enstate(&["--pack", path, "--dry-run", "--json"]); + assert_eq!(code, Some(1)); + let report: Value = serde_json::from_str(&stdout).unwrap(); + let problems = report["problems"].as_array().unwrap(); + assert_eq!(problems.len(), 2, "{report}"); + let key = problems + .iter() + .find(|p| p["address"] == "secrets/a.key") + .unwrap(); + assert_eq!(key["problem"], "unreadable"); + assert_eq!(key["location"], "secrets"); + assert_eq!(key["variables"], json!({ "key": "a.key" })); + assert!( + key["source"].as_str().unwrap().contains("show $(var:key)"), + "{key}" + ); + assert!(problems.iter().any(|p| p["problem"] == "not-in-schema")); + // Nothing written; and the real thing refuses with both. + let out = host.root.join("packed"); + let (code, _, stderr) = enstate(&["--pack", path, "--out", out.to_str().unwrap()]); + assert_eq!(code, Some(2)); + assert!( + stderr.contains("can't make a submission") + && stderr.contains("secrets/a.key") + && stderr.contains("secrets/nope"), + "{stderr}" + ); + assert!(!out.exists()); + // Options not for a dry run. + let (code, _, stderr) = enstate(&["--pack", path, "--dry-run", "--out", "x"]); + assert_eq!(code, Some(2)); + assert!(stderr.contains("--out is not for a dry run"), "{stderr}"); + let (code, _, stderr) = enstate(&["--pack", path, "--json", "--out", "x"]); + assert_eq!(code, Some(2)); + assert!( + stderr.contains("--json is for --pack only with --dry-run"), + "{stderr}" + ); +} |
