[core] Compilers: Pingora runtime config and Caddy JSON, golden files
This commit is contained in:
@@ -12,7 +12,7 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
use ianitor_core::compile::{self, CompileError, CompileOptions};
|
||||
use ianitor_core::edge_config::EdgeConfig;
|
||||
use ianitor_core::{load, Model, SourceFile};
|
||||
use ianitor_core::{Model, SourceFile, load};
|
||||
|
||||
fn fixtures() -> PathBuf {
|
||||
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/routes")
|
||||
@@ -34,15 +34,33 @@ fn host_files(reverse: bool) -> Vec<(String, String)> {
|
||||
}
|
||||
files
|
||||
.into_iter()
|
||||
.map(|p| (format!("hosts/{}", p.file_name().unwrap().to_string_lossy()), read(&p)))
|
||||
.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")
|
||||
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 {
|
||||
@@ -64,7 +82,11 @@ fn edge_compiler_is_deterministic() {
|
||||
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");
|
||||
assert_eq!(
|
||||
first,
|
||||
compile::edge(&model(true), &options()).unwrap(),
|
||||
"file order must not matter"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -74,7 +96,11 @@ fn caddy_compiler_is_deterministic() {
|
||||
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");
|
||||
assert_eq!(
|
||||
first,
|
||||
compile::caddy(&model(true), &options()).unwrap(),
|
||||
"file order must not matter"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -84,7 +110,10 @@ fn determinism_holds_across_threads() {
|
||||
.map(|i| {
|
||||
s.spawn(move || {
|
||||
let m = model(i % 2 == 0);
|
||||
(compile::edge(&m, &options()).unwrap(), compile::caddy(&m, &options()).unwrap())
|
||||
(
|
||||
compile::edge(&m, &options()).unwrap(),
|
||||
compile::caddy(&m, &options()).unwrap(),
|
||||
)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
@@ -124,7 +153,10 @@ fn caddy_output_is_valid_json_with_servers() {
|
||||
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!(
|
||||
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] {
|
||||
@@ -136,8 +168,14 @@ fn outputs_reference_secrets_by_name_only() {
|
||||
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 { .. })));
|
||||
assert!(matches!(
|
||||
compile::edge(&model(false), &o),
|
||||
Err(CompileError::BadOption { .. })
|
||||
));
|
||||
assert!(matches!(
|
||||
compile::caddy(&model(false), &o),
|
||||
Err(CompileError::BadOption { .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -150,7 +188,9 @@ fn rejects_path_options_that_would_break_out_of_json_or_placeholders() {
|
||||
}
|
||||
|
||||
fn golden(name: &str, actual: &str) {
|
||||
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/golden").join(name);
|
||||
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;
|
||||
@@ -164,10 +204,16 @@ fn golden(name: &str, actual: &str) {
|
||||
|
||||
#[test]
|
||||
fn edge_output_matches_golden_file() {
|
||||
golden("edge.json", &compile::edge(&model(false), &options()).unwrap());
|
||||
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());
|
||||
golden(
|
||||
"caddy.json",
|
||||
&compile::caddy(&model(false), &options()).unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user