220 lines
6.5 KiB
Rust
220 lines
6.5 KiB
Rust
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
//! Compiler tests: both compilers are deterministic (byte-identical output for the same model,
|
|
//! whatever the file order), never embed secret values, and match the reviewed golden files.
|
|
//! Regenerate the golden files after an intended change with `IANITOR_UPDATE_GOLDEN=1`.
|
|
|
|
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use ianitor_core::compile::{self, CompileError, CompileOptions};
|
|
use ianitor_core::edge_config::EdgeConfig;
|
|
use ianitor_core::{Model, SourceFile, load};
|
|
|
|
fn fixtures() -> PathBuf {
|
|
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/routes")
|
|
}
|
|
|
|
fn read(p: &Path) -> String {
|
|
std::fs::read_to_string(p).unwrap().replace("\r\n", "\n")
|
|
}
|
|
|
|
/// (file name, contents) for every host file, in directory order or reversed.
|
|
fn host_files(reverse: bool) -> Vec<(String, String)> {
|
|
let mut files: Vec<_> = std::fs::read_dir(fixtures().join("hosts"))
|
|
.unwrap()
|
|
.map(|e| e.unwrap().path())
|
|
.collect();
|
|
files.sort();
|
|
if reverse {
|
|
files.reverse();
|
|
}
|
|
files
|
|
.into_iter()
|
|
.map(|p| {
|
|
(
|
|
format!("hosts/{}", p.file_name().unwrap().to_string_lossy()),
|
|
read(&p),
|
|
)
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn model(reverse: bool) -> Model {
|
|
let site = read(&fixtures().join("ianitor.toml"));
|
|
let files = host_files(reverse);
|
|
let sources: Vec<SourceFile> = files
|
|
.iter()
|
|
.map(|(n, c)| SourceFile {
|
|
name: n,
|
|
contents: c,
|
|
})
|
|
.collect();
|
|
load(
|
|
&SourceFile {
|
|
name: "ianitor.toml",
|
|
contents: &site,
|
|
},
|
|
&sources,
|
|
)
|
|
.expect("fixtures are valid")
|
|
}
|
|
|
|
fn options() -> CompileOptions {
|
|
CompileOptions {
|
|
http_listen: "0.0.0.0:80".parse().unwrap(),
|
|
https_listen: "0.0.0.0:443".parse().unwrap(),
|
|
cert_dir: "/var/lib/ianitor/certs".into(),
|
|
state_dir: "/var/lib/ianitor".into(),
|
|
access_log: "/var/log/ianitor/access.log".into(),
|
|
caddy_credentials_dir: "/run/credentials/caddy-fallback.service".into(),
|
|
caddy_admin: None,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn edge_compiler_is_deterministic() {
|
|
let m = model(false);
|
|
let first = compile::edge(&m, &options()).unwrap();
|
|
for _ in 0..50 {
|
|
assert_eq!(first, compile::edge(&m, &options()).unwrap());
|
|
}
|
|
assert_eq!(
|
|
first,
|
|
compile::edge(&model(true), &options()).unwrap(),
|
|
"file order must not matter"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn caddy_compiler_is_deterministic() {
|
|
let m = model(false);
|
|
let first = compile::caddy(&m, &options()).unwrap();
|
|
for _ in 0..50 {
|
|
assert_eq!(first, compile::caddy(&m, &options()).unwrap());
|
|
}
|
|
assert_eq!(
|
|
first,
|
|
compile::caddy(&model(true), &options()).unwrap(),
|
|
"file order must not matter"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn determinism_holds_across_threads() {
|
|
let outputs: Vec<(String, String)> = std::thread::scope(|s| {
|
|
let handles: Vec<_> = (0..8)
|
|
.map(|i| {
|
|
s.spawn(move || {
|
|
let m = model(i % 2 == 0);
|
|
(
|
|
compile::edge(&m, &options()).unwrap(),
|
|
compile::caddy(&m, &options()).unwrap(),
|
|
)
|
|
})
|
|
})
|
|
.collect();
|
|
handles.into_iter().map(|h| h.join().unwrap()).collect()
|
|
});
|
|
for o in &outputs {
|
|
assert_eq!(o, &outputs[0]);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn edge_output_round_trips_through_the_edge_schema() {
|
|
let text = compile::edge(&model(false), &options()).unwrap();
|
|
let cfg: EdgeConfig = serde_json::from_str(&text).unwrap();
|
|
assert_eq!(cfg.hosts.len(), 3);
|
|
let again = serde_json::to_string_pretty(&cfg).unwrap() + "\n";
|
|
assert_eq!(text, again);
|
|
}
|
|
|
|
#[test]
|
|
fn edge_config_rejects_unknown_fields() {
|
|
let text = compile::edge(&model(false), &options()).unwrap();
|
|
let mut v: serde_json::Value = serde_json::from_str(&text).unwrap();
|
|
v["surprise"] = serde_json::json!(true);
|
|
assert!(serde_json::from_value::<EdgeConfig>(v).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn caddy_output_is_valid_json_with_servers() {
|
|
let text = compile::caddy(&model(false), &options()).unwrap();
|
|
let v: serde_json::Value = serde_json::from_str(&text).unwrap();
|
|
assert!(v["apps"]["http"]["servers"].is_object());
|
|
assert_eq!(v["admin"]["disabled"], serde_json::json!(true));
|
|
}
|
|
|
|
#[test]
|
|
fn outputs_reference_secrets_by_name_only() {
|
|
let edge = compile::edge(&model(false), &options()).unwrap();
|
|
let caddy = compile::caddy(&model(false), &options()).unwrap();
|
|
assert!(
|
|
edge.contains("\"photos_api_key\""),
|
|
"edge config names the secret"
|
|
);
|
|
assert!(caddy.contains("{file./run/credentials/caddy-fallback.service/photos_api_key}"));
|
|
// The compilers never see secret values, so nothing secret-shaped can be in the output.
|
|
for out in [&edge, &caddy] {
|
|
assert!(!out.contains("BEGIN"), "no key material");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_relative_paths_in_options() {
|
|
let mut o = options();
|
|
o.cert_dir = "certs".into();
|
|
assert!(matches!(
|
|
compile::edge(&model(false), &o),
|
|
Err(CompileError::BadOption { .. })
|
|
));
|
|
assert!(matches!(
|
|
compile::caddy(&model(false), &o),
|
|
Err(CompileError::BadOption { .. })
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_path_options_that_would_break_out_of_json_or_placeholders() {
|
|
for bad in ["/run/x}y", "/run/{x", "/run/a\nb"] {
|
|
let mut o = options();
|
|
o.caddy_credentials_dir = bad.into();
|
|
assert!(compile::caddy(&model(false), &o).is_err(), "{bad:?}");
|
|
}
|
|
}
|
|
|
|
fn golden(name: &str, actual: &str) {
|
|
let path = Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
.join("tests/golden")
|
|
.join(name);
|
|
if std::env::var_os("IANITOR_UPDATE_GOLDEN").is_some() {
|
|
std::fs::write(&path, actual).unwrap();
|
|
return;
|
|
}
|
|
let expected = read(&path);
|
|
assert!(
|
|
expected == actual,
|
|
"{name} differs from the golden file; review and rerun with IANITOR_UPDATE_GOLDEN=1.\n--- actual ---\n{actual}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn edge_output_matches_golden_file() {
|
|
golden(
|
|
"edge.json",
|
|
&compile::edge(&model(false), &options()).unwrap(),
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn caddy_output_matches_golden_file() {
|
|
golden(
|
|
"caddy.json",
|
|
&compile::caddy(&model(false), &options()).unwrap(),
|
|
);
|
|
}
|