|
| 1 | +use std::fmt::Write as _; |
| 2 | +use std::path::PathBuf; |
| 3 | + |
| 4 | +use datadog_agent_config_overlay_model::schema_gen::{self, FieldInfo, FieldType}; |
| 5 | +use datadog_agent_config_overlay_model::smoke_test_support::ConfigurationStruct; |
| 6 | +use datadog_agent_config_overlay_model::{Files, KnownEntry, SchemaOverlay, TestSupport}; |
| 7 | +use indexmap::IndexMap; |
| 8 | + |
| 9 | +fn main() { |
| 10 | + let files = Files::default(); |
| 11 | + |
| 12 | + let schema_dir = files.schema.parent().expect("schema file must have a parent directory"); |
| 13 | + println!("cargo:rerun-if-changed={}", schema_dir.display()); |
| 14 | + println!("cargo:rerun-if-changed={}", files.overlay.display()); |
| 15 | + println!("cargo:rerun-if-changed=build.rs"); |
| 16 | + |
| 17 | + let schema_path = files.schema.clone(); |
| 18 | + let overlay = SchemaOverlay::load(files).unwrap_or_else(|e| panic!("{e}")); |
| 19 | + let schema_map = schema_gen::load_schema(&schema_path); |
| 20 | + |
| 21 | + let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap()); |
| 22 | + generate_smoke_tests(&overlay, &schema_map, &out_dir); |
| 23 | +} |
| 24 | + |
| 25 | +fn generate_smoke_tests(overlay: &SchemaOverlay, schema_map: &IndexMap<String, FieldInfo>, out_dir: &std::path::Path) { |
| 26 | + let mut keys: Vec<(&str, &TestSupport, &FieldInfo)> = Vec::new(); |
| 27 | + |
| 28 | + for (yaml_path, entry) in &overlay.inventory { |
| 29 | + let ts = match entry { |
| 30 | + KnownEntry::Full(f) => &f.test_support, |
| 31 | + KnownEntry::Partial(p) => &p.test_support, |
| 32 | + _ => continue, |
| 33 | + }; |
| 34 | + if !ts.used_by.contains(&ConfigurationStruct::MigratedToConfigSystem) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + let field_info = schema_map |
| 38 | + .get(yaml_path.as_str()) |
| 39 | + .unwrap_or_else(|| panic!("MigratedToConfigSystem key '{}' not found in schema", yaml_path)); |
| 40 | + keys.push((yaml_path.as_str(), ts, field_info)); |
| 41 | + } |
| 42 | + |
| 43 | + keys.sort_by_key(|(path, _, _)| *path); |
| 44 | + |
| 45 | + let mut out = String::new(); |
| 46 | + writeln!(out, "// @generated by build.rs from schema_overlay.yaml — DO NOT EDIT").unwrap(); |
| 47 | + writeln!(out).unwrap(); |
| 48 | + |
| 49 | + for (yaml_path, ts, field_info) in &keys { |
| 50 | + let fn_name = yaml_path.replace('.', "_"); |
| 51 | + let test_value = resolve_test_value(yaml_path, ts, field_info); |
| 52 | + |
| 53 | + writeln!(out, "#[tokio::test]").unwrap(); |
| 54 | + writeln!(out, "async fn smoke_{fn_name}() {{").unwrap(); |
| 55 | + writeln!(out, " assert_key_propagates(\"{yaml_path}\", {test_value}).await;").unwrap(); |
| 56 | + writeln!(out, "}}").unwrap(); |
| 57 | + writeln!(out).unwrap(); |
| 58 | + } |
| 59 | + |
| 60 | + let path = out_dir.join("translation_smoke_tests.rs"); |
| 61 | + std::fs::write(&path, out).unwrap_or_else(|e| panic!("cannot write {}: {}", path.display(), e)); |
| 62 | +} |
| 63 | + |
| 64 | +fn resolve_test_value(yaml_path: &str, ts: &TestSupport, field_info: &FieldInfo) -> String { |
| 65 | + if let Some(ref test_json) = ts.test_json { |
| 66 | + return format_json_literal(test_json); |
| 67 | + } |
| 68 | + |
| 69 | + let effective_type = ts |
| 70 | + .value_type_override |
| 71 | + .as_ref() |
| 72 | + .map_or(&field_info.value_type, |vt| match vt { |
| 73 | + datadog_agent_config_overlay_model::ValueType::Boolean => &FieldType::Bool, |
| 74 | + datadog_agent_config_overlay_model::ValueType::Integer => &FieldType::Integer, |
| 75 | + datadog_agent_config_overlay_model::ValueType::Float => &FieldType::Float, |
| 76 | + datadog_agent_config_overlay_model::ValueType::String => &FieldType::String, |
| 77 | + datadog_agent_config_overlay_model::ValueType::StringList => &FieldType::StringList, |
| 78 | + }); |
| 79 | + |
| 80 | + match effective_type { |
| 81 | + FieldType::Bool => { |
| 82 | + let default_is_true = field_info |
| 83 | + .default |
| 84 | + .as_deref() |
| 85 | + .map(|d| d.trim() == "true") |
| 86 | + .unwrap_or(false); |
| 87 | + if default_is_true { |
| 88 | + "serde_json::json!(false)".to_string() |
| 89 | + } else { |
| 90 | + "serde_json::json!(true)".to_string() |
| 91 | + } |
| 92 | + } |
| 93 | + FieldType::Integer => "serde_json::json!(42i64)".to_string(), |
| 94 | + FieldType::Float => "serde_json::json!(1.5f64)".to_string(), |
| 95 | + FieldType::String => "serde_json::json!(\"http://smoke-test.example.com:3128\")".to_string(), |
| 96 | + FieldType::StringList => { |
| 97 | + "serde_json::json!([\"smoke-host-1.example.com\", \"smoke-host-2.example.com\"])".to_string() |
| 98 | + } |
| 99 | + FieldType::Unknown => { |
| 100 | + panic!("MigratedToConfigSystem key '{yaml_path}' has unknown type and no test_json override"); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +fn format_json_literal(raw: &str) -> String { |
| 106 | + if raw.contains('"') { |
| 107 | + format!("serde_json::from_str::<serde_json::Value>(r#\"{raw}\"#).unwrap()") |
| 108 | + } else { |
| 109 | + format!("serde_json::json!({raw})") |
| 110 | + } |
| 111 | +} |
0 commit comments