//! Configuration files (`config::file`), read by the node's options: TOML (`.toml`) for //! people, JSON (`.json`) for generated configuration, with the same keys. use std::path::{Path, PathBuf}; use anyhow::Result; use super::{Layer, SCHEMA}; /// The settings `path` makes, its includes resolved. fn load(path: &Path) -> Result { ::config::file::load(path, &SCHEMA) } mod tests { use super::*; use crate::files::scratch_dir; use serde_json::json; fn write(dir: &Path, name: &str, text: &str) -> PathBuf { let path = dir.join(name); std::fs::write(&path, text).unwrap(); path } #[test] fn includes_tables_and_importers() { let dir = scratch_dir("config-includes"); write(&dir, "key-path", "/run/secrets/swarm-key\n"); write( &dir, "public.toml", "neighbours = 8\nalias = \"public\"\n[mycelium]\npeers = [\"tls://a:9651\"]\n", ); write( &dir, "secrets.json", r#"{ "swarm-key-file": { "file": "key-path" } }"#, ); let main = write( &dir, "main.toml", "include = [\"public.toml\", \"secrets.json\", { path = \"missing.toml\", optional = true }]\nalias = \"main\"\n", ); let layer = load(&main).unwrap(); assert_eq!(layer["neighbours"].value, json!(8)); assert_eq!(layer["alias"].value, json!("main")); assert_eq!(layer["mycelium-peers"].value, json!(["tls://a:9651"])); assert_eq!( layer["swarm-key-file"].value, json!("/run/secrets/swarm-key") ); assert!(layer["swarm-key-file"].origin.is_imported()); assert!(layer["alias"].origin.to_string().ends_with("main.toml")); } #[test] fn unset() { let dir = scratch_dir("config-unset"); write( &dir, "base.toml", "alias = \"base\"\nneighbours = 8\nkeepalive = \"30s\"\n", ); let toml = write( &dir, "local.toml", "include = \"base.toml\"\nunset = [\"alias\"]\n", ); let layer = load(&toml).unwrap(); assert!(layer["alias"].value.is_null() && layer["alias"].origin.unset); assert_eq!(layer["neighbours"].value, json!(8)); let json = write( &dir, "local.json", r#"{ "include": "base.toml", "neighbours": null }"#, ); let layer = load(&json).unwrap(); assert!(layer["neighbours"].origin.unset); assert_eq!(layer["keepalive"].value, json!("30s")); let unknown = write(&dir, "bad.toml", "unset = [\"nope\"]\n"); assert!(load(&unknown).is_err()); } #[test] fn errors() { let dir = scratch_dir("config-errors"); let unknown = write(&dir, "unknown.toml", "nieghbours = 3\n"); assert!(load(&unknown).is_err()); let cycle = write(&dir, "cycle.toml", "include = [\"cycle.toml\"]\n"); assert!(load(&cycle).is_err()); let missing = write(&dir, "missing.toml", "include = [\"nope.toml\"]\n"); assert!(load(&missing).is_err()); let format = write(&dir, "config.yaml", "alias: x\n"); assert!(load(&format).is_err()); let gone = write(&dir, "gone.toml", "include-optional = [\"x.toml\"]\n"); assert!(load(&gone).is_err()); } /// Includes are loaded in the order listed, optional ones too: the later wins. #[test] fn includes_in_order() { let dir = scratch_dir("config-include-order"); write(&dir, "local.toml", "alias = \"local\"\n"); write(&dir, "secrets.toml", "alias = \"secrets\"\n"); let local_last = write( &dir, "a.toml", "include = [\"secrets.toml\", { path = \"local.toml\", optional = true }]\n", ); assert_eq!(load(&local_last).unwrap()["alias"].value, json!("local")); let secrets_last = write( &dir, "b.toml", "include = [{ path = \"local.toml\", optional = true }, \"secrets.toml\"]\n", ); assert_eq!( load(&secrets_last).unwrap()["alias"].value, json!("secrets") ); } }