Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
5.8 kB · 161 lines
Rust
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162use std::collections::BTreeMap;
use super::*;
fn fixture_manifest(body: &[u8]) -> GenerationManifest { let body_hash = Digest::of_bytes(body).hex(); let repo_did = "did:plc:fixture"; let mut manifest = GenerationManifest { generation_id: String::new(), generator_profile: GENERATOR_PROFILE_VERSION.to_string(), conf_template: crate::manifest::NGINX_TEMPLATE_VERSION.to_string(), knot_did: "did:web:knot.example".to_string(), hostname: "knot.example".to_string(), owner: "did:plc:owner".to_string(), static_version: "fixture".to_string(), capabilities: Vec::new(), profile: serde_json::json!({}), repos: vec![RepoEntry { repo_did: repo_did.to_string(), owner: "did:plc:owner".to_string(), rkey: "fixture".to_string(), name: None, head: None, public_refs: Vec::new(), object_format: "sha1".to_string(), fingerprint: "repo-fingerprint".to_string(), git_export_hash: Some("export-fingerprint".to_string()), }], routes: BTreeMap::from([( "health".to_string(), StaticResponse { body_hash, status: 200, content_type: "application/json".to_string(), headers: Vec::new(), cas_key: "health".to_string(), }, )]), sections: BTreeMap::new(), section_routes: BTreeMap::new(), section_objects: BTreeMap::new(), repo_aliases: BTreeMap::from([(repo_did.to_string(), "r_000".to_string())]), ref_aliases: BTreeMap::new(), stats: BuildStats::default(), }; manifest.finalize_id(); manifest}
fn fill_private_generation( generation: &PrivateGeneration, manifest: &GenerationManifest, body: &[u8], nginx: &[u8],) -> anyhow::Result<()> { let root = generation.path(); let route = manifest.routes.values().next().expect("fixture response"); let response_file = response_path(&root.join("www/_responses"), &route.body_hash)?; std::fs::create_dir_all(response_file.parent().expect("response parent"))?; std::fs::write(response_file, body)?;
let export = root.join("www/_git/r_000"); std::fs::create_dir_all(&export)?; std::fs::write(export.join("export.fingerprint"), "export-fingerprint\n")?;
std::fs::create_dir_all(root.join("nginx/maps"))?; std::fs::write(root.join("nginx/knot-static.conf"), nginx)?; std::fs::write(root.join("nginx/maps/routes.conf"), b"map {}\n")?; let cas = Cas::open(root.join("cas")); manifest.write(root, &cas)}
#[test]fn repeated_publication_reuses_generation_without_touching_files() -> anyhow::Result<()> { let root = tempfile::tempdir()?; let generations = root.path().join("generations"); std::fs::create_dir(&generations)?; let body = br#"{"status":"ok"}"#; let manifest = fixture_manifest(body); let destination = generations.join(&manifest.generation_id);
let first = PrivateGeneration::new(&generations)?; fill_private_generation(&first, &manifest, body, b"first config\n")?; assert_eq!( first.publish(&destination, &manifest)?, Publication::Published );
let config = destination.join("nginx/knot-static.conf"); let original = std::fs::read(&config)?; #[cfg(unix)] let original_inode = { use std::os::unix::fs::MetadataExt as _; std::fs::metadata(&config)?.ino() };
let second = PrivateGeneration::new(&generations)?; fill_private_generation(&second, &manifest, body, b"replacement config\n")?; assert_eq!( second.publish(&destination, &manifest)?, Publication::Reused );
assert_eq!(std::fs::read(&config)?, original); #[cfg(unix)] { use std::os::unix::fs::MetadataExt as _; assert_eq!(std::fs::metadata(&config)?.ino(), original_inode); } Ok(())}
#[test]fn corrupt_existing_generation_fails_without_changing_it() -> anyhow::Result<()> { let root = tempfile::tempdir()?; let generations = root.path().join("generations"); std::fs::create_dir(&generations)?; let body = br#"{"status":"ok"}"#; let manifest = fixture_manifest(body); let destination = generations.join(&manifest.generation_id);
let first = PrivateGeneration::new(&generations)?; fill_private_generation(&first, &manifest, body, b"first config\n")?; first.publish(&destination, &manifest)?;
let config = destination.join("nginx/knot-static.conf"); std::fs::write(&config, b"corrupt but preserved\n")?; let before = std::fs::read(&config)?; #[cfg(unix)] let before_inode = { use std::os::unix::fs::MetadataExt as _; std::fs::metadata(&config)?.ino() };
let replacement = PrivateGeneration::new(&generations)?; fill_private_generation(&replacement, &manifest, body, b"replacement config\n")?; let error = replacement .publish(&destination, &manifest) .expect_err("corrupt existing generation must not be replaced"); assert!(error.to_string().contains("incomplete or corrupt")); assert_eq!(std::fs::read(&config)?, before); #[cfg(unix)] { use std::os::unix::fs::MetadataExt as _; assert_eq!(std::fs::metadata(&config)?.ino(), before_inode); } Ok(())}
#[test]fn prior_export_uses_prior_manifest_token() { let mut manifest = fixture_manifest(b"body"); manifest .repo_aliases .insert("did:plc:fixture".to_string(), "r_007".to_string());
assert_eq!( prior_export(&manifest, "did:plc:fixture", "repo-fingerprint"), Some(("r_007", "export-fingerprint")) );}