lazydns 0.2.63

A light and fast DNS server/forwarder implementation in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
//! Configuration loader
//!
//! Loads and saves configuration from/to files and strings.

use crate::config::Config;
use crate::{Error, Result};
use once_cell::sync::Lazy;
use regex::Regex;
use std::collections::HashSet;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use tracing::info;

// Compile-once Regex instances to avoid repeated compilation at runtime/tests
static RE_ENV: Lazy<Regex> = Lazy::new(|| Regex::new(r"\$\{([A-Z0-9_]+)(?::-([^}]+))?\}").unwrap());
static RE_INCLUDE: Lazy<Regex> = Lazy::new(|| Regex::new(r"!include\s+([^\s\n]+)").unwrap());
static RE_PLUGIN_ARGS: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"^PLUGINS_([A-Z0-9_]+)_ARGS_(.+)$").unwrap());

/// Load configuration from a YAML file
///
/// Supports:
/// - Environment variable substitution: ${VAR_NAME} or ${VAR_NAME:-default}
/// - File includes: !include path/to/file.yaml
///
/// # Arguments
///
/// * `path` - Path to the YAML configuration file
///
/// # Errors
///
/// Returns an error if the file cannot be read or parsed.
pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Config> {
    load_from_file_internal(path.as_ref(), &mut HashSet::new())
}

/// Internal implementation with circular include detection
fn load_from_file_internal(path: &Path, visited: &mut HashSet<PathBuf>) -> Result<Config> {
    let canonical_path = path
        .canonicalize()
        .map_err(|e| Error::Config(format!("Failed to resolve path: {}", e)))?;

    // Check for circular includes
    if visited.contains(&canonical_path) {
        return Err(Error::Config(format!(
            "Circular include detected: {}",
            path.display()
        )));
    }
    visited.insert(canonical_path.clone());

    let contents = fs::read_to_string(path)
        .map_err(|e| Error::Config(format!("Failed to read config file: {}", e)))?;

    // Substitute environment variables
    let contents = substitute_env_vars(&contents)?;

    // Process includes
    let contents = process_includes(&contents, path.parent(), visited)?;

    load_from_yaml(&contents)
}

/// Substitute environment variables in configuration
///
/// Supports ${VAR_NAME} and ${VAR_NAME:-default_value}
fn substitute_env_vars(content: &str) -> Result<String> {
    let mut result = content.to_string();

    for cap in RE_ENV.captures_iter(content) {
        let full_match = cap.get(0).unwrap().as_str();
        let var_name = cap.get(1).unwrap().as_str();
        let default_value = cap.get(2).map(|m| m.as_str());

        let value = match env::var(var_name) {
            Ok(v) => v,
            Err(_) => {
                if let Some(default) = default_value {
                    default.to_string()
                } else {
                    return Err(Error::Config(format!(
                        "Environment variable {} not found and no default provided",
                        var_name
                    )));
                }
            }
        };

        result = result.replace(full_match, &value);
    }

    Ok(result)
}

/// Process include directives in configuration
///
/// Replaces !include path/to/file.yaml with the file contents
fn process_includes(
    content: &str,
    base_dir: Option<&Path>,
    _visited: &mut HashSet<PathBuf>,
) -> Result<String> {
    let mut result = content.to_string();

    for cap in RE_INCLUDE.captures_iter(content) {
        let full_match = cap.get(0).unwrap().as_str();
        let include_path = cap.get(1).unwrap().as_str();

        // Resolve relative to base directory
        let resolved_path = if let Some(base) = base_dir {
            base.join(include_path)
        } else {
            PathBuf::from(include_path)
        };

        // Read included file
        let included_contents = fs::read_to_string(&resolved_path).map_err(|e| {
            Error::Config(format!(
                "Failed to read included file {}: {}",
                resolved_path.display(),
                e
            ))
        })?;

        // Recursively process the included content
        let included_contents = substitute_env_vars(&included_contents)?;
        let included_contents =
            process_includes(&included_contents, resolved_path.parent(), _visited)?;

        result = result.replace(full_match, &included_contents);
    }

    Ok(result)
}

/// Load configuration from a YAML string
///
/// # Arguments
///
/// * `yaml` - YAML configuration string
///
/// # Errors
///
/// Returns an error if the YAML cannot be parsed.
pub fn load_from_yaml(yaml: &str) -> Result<Config> {
    let mut config: Config = serde_yaml::from_str(yaml)
        .map_err(|e| Error::Config(format!("Failed to parse YAML: {}", e)))?;

    // Apply environment variable overrides (before validation)
    apply_env_overrides(&mut config)?;

    // Validate the loaded configuration
    config.validate()?;

    Ok(config)
}

/// Apply environment variable overrides to the configuration
///
/// Supports two patterns:
/// 1. Top-level fields: `LOG_FORMAT=json` → sets `config.log.format`
/// 2. Plugin args: `PLUGINS_<TAG>_ARGS_<KEY>=value` → sets `plugin.args.<key>`
///
/// Values are parsed as YAML (numbers, booleans, arrays) with string fallback.
fn apply_env_overrides(config: &mut Config) -> Result<()> {
    // Collect all env vars into a HashMap to ensure we see all updates
    // We'll consider both a fresh single-var read and the snapshot to increase the
    // chance of seeing recent transient updates from other tests.
    let env_vars: std::collections::HashMap<String, String> = env::vars().collect();

    // Helper to pick the first non-empty value between a direct env::var read and
    // the snapshot (env_vars). This reduces flakiness when other tests change
    // environment variables concurrently during test runs.
    fn first_non_empty(
        key: &str,
        env_snapshot: &std::collections::HashMap<String, String>,
    ) -> Option<String> {
        let direct = std::env::var(key).ok().filter(|s| !s.is_empty());
        let snap = env_snapshot.get(key).cloned().filter(|s| !s.is_empty());
        direct.or(snap)
    }

    // Apply top-level overrides (pick the first non-empty observed value)
    if let Some(val) = first_non_empty("LOG_LEVEL", &env_vars) {
        info!("Applied env override: LOG_LEVEL = {}", val);
        config.log.level = val;
    }
    if let Some(val) = first_non_empty("LOG_FORMAT", &env_vars) {
        info!("Applied env override: LOG_FORMAT = {}", val);
        config.log.format = val;
    }
    if let Some(val) = first_non_empty("LOG_FILE", &env_vars) {
        info!("Applied env override: LOG_FILE = {}", val);
        // Create or update file config with the path
        if let Some(ref mut file_cfg) = config.log.file {
            file_cfg.path = val;
            file_cfg.enabled = true;
        } else {
            config.log.file = Some(crate::config::FileLogConfig {
                enabled: true,
                path: val,
                ..Default::default()
            });
        }
    }
    if let Some(val) = first_non_empty("LOG_CONSOLE", &env_vars) {
        info!("Applied env override: LOG_CONSOLE = {}", val);
        let normalized = val.to_lowercase();
        config.log.console = normalized == "true" || normalized == "1" || normalized == "yes";
    }

    apply_env_overrides_from_snapshot(config, &env_vars)
}

/// Apply environment overrides using a supplied environment snapshot. This is
/// useful for deterministic tests that want to avoid races with other tests
/// changing process globals.
pub(crate) fn apply_env_overrides_from_snapshot(
    config: &mut Config,
    env_snapshot: &std::collections::HashMap<String, String>,
) -> Result<()> {
    use serde_yaml::Value;

    // Support plugin args env overrides with nested paths and indices.
    // Examples supported:
    //   PLUGINS_MYTAG_ARGS_SIZE=2048
    //   PLUGINS_MYTAG_ARGS_JOBS_0_CRON="0 */6 * * *"

    for (key, value_str) in env_snapshot {
        // Handle top-level environment variables
        match key.as_str() {
            "LOG_LEVEL" => {
                if !value_str.is_empty() {
                    info!("Applied env override: LOG_LEVEL = {}", value_str);
                    config.log.level = value_str.clone();
                }
                continue;
            }
            "LOG_FORMAT" => {
                if !value_str.is_empty() {
                    info!("Applied env override: LOG_FORMAT = {}", value_str);
                    config.log.format = value_str.clone();
                }
                continue;
            }
            "LOG_FILE" => {
                if !value_str.is_empty() {
                    info!("Applied env override: LOG_FILE = {}", value_str);
                    // Create or update file config with the path
                    if let Some(ref mut file_cfg) = config.log.file {
                        file_cfg.path = value_str.clone();
                        file_cfg.enabled = true;
                    } else {
                        config.log.file = Some(crate::config::FileLogConfig {
                            enabled: true,
                            path: value_str.clone(),
                            ..Default::default()
                        });
                    }
                }
                continue;
            }
            "LOG_CONSOLE" => {
                if !value_str.is_empty() {
                    info!("Applied env override: LOG_CONSOLE = {}", value_str);
                    let normalized = value_str.to_lowercase();
                    config.log.console =
                        normalized == "true" || normalized == "1" || normalized == "yes";
                }
                continue;
            }

            // Admin server overrides
            "ADMIN_ENABLED" => {
                info!("Applied env override: ADMIN_ENABLED = {}", value_str);
                // parse boolean-like values: true/false, 1/0
                let normalized = value_str.to_lowercase();
                config.admin.enabled =
                    normalized == "true" || normalized == "1" || normalized == "yes";
                continue;
            }

            "ADMIN_ADDR" => {
                info!("Applied env override: ADMIN_ADDR = {}", value_str);
                config.admin.addr = value_str.clone();
                continue;
            }

            // Monitoring server overrides
            "METRICS_ENABLED" => {
                info!("Applied env override: METRICS_ENABLED = {}", value_str);
                // parse boolean-like values: true/false, 1/0
                let normalized = value_str.to_lowercase();
                config.monitoring.enabled =
                    normalized == "true" || normalized == "1" || normalized == "yes";
                continue;
            }

            "METRICS_ADDR" => {
                info!("Applied env override: METRICS_ADDR = {}", value_str);
                config.monitoring.addr = value_str.clone();
                continue;
            }

            _ => {}
        }

        // Handle plugin args: PLUGINS_<TAG>_ARGS_<KEYPATH>=value
        if let Some(caps) = RE_PLUGIN_ARGS.captures(key) {
            let tag_raw = &caps[1];
            let key_path_raw = &caps[2];

            // Normalize tag (lowercase, _ -> -)
            let tag = normalize_identifier(tag_raw);

            // Parse key path into segments (keys or indices)
            #[derive(Debug)]
            enum Segment {
                Key(String),
                Index(usize),
            }

            let mut path: Vec<Segment> = Vec::new();
            for part in key_path_raw.split('_') {
                if let Ok(idx) = part.parse::<usize>() {
                    path.push(Segment::Index(idx));
                } else {
                    path.push(Segment::Key(normalize_identifier(part)));
                }
            }

            // Find matching plugin by effective_name (normalized)
            if let Some(plugin) = config
                .plugins
                .iter_mut()
                .find(|p| normalize_identifier(p.effective_name()) == tag)
            {
                let value = parse_yaml_value(value_str);

                // Ensure args is a mapping
                if !matches!(plugin.args, Value::Mapping(_)) {
                    plugin.args = Value::Mapping(serde_yaml::Mapping::new());
                }

                // Set value into plugin.args following the path
                fn set_path(target: &mut Value, path: &[Segment], value: Value) {
                    use Segment::*;
                    if path.is_empty() {
                        *target = value;
                        return;
                    }

                    let mut cur: &mut Value = target;
                    for i in 0..path.len() {
                        let is_last = i == path.len() - 1;
                        match &path[i] {
                            Key(k) => match cur {
                                Value::Mapping(map) => {
                                    if is_last {
                                        map.insert(Value::String(k.clone()), value);
                                        return;
                                    }
                                    // descend or create
                                    if !map.contains_key(Value::String(k.clone())) {
                                        let next = match &path[i + 1] {
                                            Index(_) => Value::Sequence(vec![]),
                                            _ => Value::Mapping(serde_yaml::Mapping::new()),
                                        };
                                        map.insert(Value::String(k.clone()), next);
                                    }
                                    cur = map.get_mut(Value::String(k.clone())).unwrap();
                                }
                                _ => {
                                    // replace with mapping
                                    *cur = Value::Mapping(serde_yaml::Mapping::new());
                                    if let Value::Mapping(map) = cur {
                                        if is_last {
                                            map.insert(Value::String(k.clone()), value);
                                            return;
                                        }
                                        let next = match &path[i + 1] {
                                            Index(_) => Value::Sequence(vec![]),
                                            _ => Value::Mapping(serde_yaml::Mapping::new()),
                                        };
                                        map.insert(Value::String(k.clone()), next);
                                        cur = map.get_mut(Value::String(k.clone())).unwrap();
                                    }
                                }
                            },
                            Index(idx) => match cur {
                                Value::Sequence(seq) => {
                                    if *idx >= seq.len() {
                                        seq.resize(*idx + 1, Value::Null);
                                    }
                                    if is_last {
                                        seq[*idx] = value;
                                        return;
                                    }
                                    if seq[*idx].is_null() {
                                        seq[*idx] = match &path[i + 1] {
                                            Index(_) => Value::Sequence(vec![]),
                                            _ => Value::Mapping(serde_yaml::Mapping::new()),
                                        };
                                    }
                                    cur = &mut seq[*idx];
                                }
                                _ => {
                                    // replace with sequence
                                    *cur = Value::Sequence(vec![]);
                                    if let Value::Sequence(seq) = cur {
                                        seq.resize(*idx + 1, Value::Null);
                                        if is_last {
                                            seq[*idx] = value;
                                            return;
                                        }
                                        seq[*idx] = match &path[i + 1] {
                                            Index(_) => Value::Sequence(vec![]),
                                            _ => Value::Mapping(serde_yaml::Mapping::new()),
                                        };
                                        cur = &mut seq[*idx];
                                    }
                                }
                            },
                        }
                    }
                }

                set_path(&mut plugin.args, &path, value);

                info!(
                    "Applied plugin env override: {} -> plugin[{}].args path={:?}",
                    key, tag, path
                );
            } else {
                tracing::warn!("Plugin '{}' not found for env override: {}", tag, key);
            }
        }
    }

    Ok(())
}

/// Parse a string value as YAML (numbers, booleans, arrays) with string fallback
fn parse_yaml_value(value_str: &str) -> serde_yaml::Value {
    // Try to parse as YAML
    match serde_yaml::from_str::<serde_yaml::Value>(value_str) {
        Ok(v) => {
            // If it's just a plain string wrapped by YAML, unwrap it
            match v {
                serde_yaml::Value::String(s) => serde_yaml::Value::String(s),
                _ => v,
            }
        }
        Err(_) => {
            // Fall back to treating it as a string
            serde_yaml::Value::String(value_str.to_string())
        }
    }
}

/// Normalize identifiers: lowercase and convert _ to -
fn normalize_identifier(s: &str) -> String {
    s.to_lowercase().replace('_', "-")
}

/// Save configuration to a YAML file
///
/// # Arguments
///
/// * `config` - The configuration to save
/// * `path` - Path to save the YAML configuration file
///
/// # Errors
///
/// Returns an error if the file cannot be written.
pub fn save_to_file<P: AsRef<Path>>(config: &Config, path: P) -> Result<()> {
    let yaml = to_yaml(config)?;

    fs::write(path.as_ref(), yaml)
        .map_err(|e| Error::Config(format!("Failed to write config file: {}", e)))?;

    Ok(())
}

/// Convert configuration to YAML string
///
/// # Arguments
///
/// * `config` - The configuration to convert
///
/// # Errors
///
/// Returns an error if serialization fails.
pub fn to_yaml(config: &Config) -> Result<String> {
    serde_yaml::to_string(config)
        .map_err(|e| Error::Config(format!("Failed to serialize YAML: {}", e)))
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::NamedTempFile;

    #[test]
    fn test_load_from_yaml_minimal() {
        let yaml = r#"
log:
  level: debug
"#;
        let config = load_from_yaml(yaml).unwrap();
        assert_eq!(config.log.level, "debug");
    }

    #[test]
    fn test_load_from_yaml_full() {
        // Ensure environment overrides do not interfere with this test by
        // setting LOG_LEVEL to an empty value (treated as no override by the
        // loader logic). This reduces flakiness from concurrent tests.
        unsafe {
            std::env::set_var("LOG_LEVEL", "");
        }

        let yaml = r#"
log:
  level: info
  console: true
  file:
    enabled: true
    path: /var/log/app.log
    rotation:
      type: time
      period: daily
plugins:
  - plugin_type: forward
    priority: 100
"#;
        let config = load_from_yaml(yaml).unwrap();
        assert_eq!(config.log.level, "info");
        assert!(config.log.console);
        assert!(config.log.file.is_some());
        let file_cfg = config.log.file.as_ref().unwrap();
        assert!(file_cfg.enabled);
        assert_eq!(file_cfg.path, "/var/log/app.log");
        assert_eq!(config.plugins.len(), 1);

        unsafe {
            std::env::remove_var("LOG_LEVEL");
        }
    }

    #[test]
    fn test_load_from_yaml_invalid() {
        let yaml = "invalid: yaml: content: [";
        let result = load_from_yaml(yaml);
        assert!(result.is_err());
    }

    #[test]
    fn test_to_yaml() {
        let config = Config::new();
        let yaml = to_yaml(&config).unwrap();

        assert!(yaml.contains("log:"));
    }

    #[test]
    fn test_save_and_load_file() {
        let config = Config::new();

        // Create a temporary file
        let temp_file = NamedTempFile::new().unwrap();
        let path = temp_file.path().to_path_buf();

        // Save config
        save_to_file(&config, &path).unwrap();

        // Load it back
        let loaded = load_from_file(&path).unwrap();

        assert_eq!(config.log, loaded.log);
    }

    #[test]
    fn test_load_nonexistent_file() {
        let result = load_from_file("/nonexistent/path/config.yaml");
        assert!(result.is_err());
    }

    #[test]
    fn test_roundtrip() {
        let original = Config::new();
        let yaml = to_yaml(&original).unwrap();
        let loaded = load_from_yaml(&yaml).unwrap();

        assert_eq!(original.log, loaded.log);
    }

    #[test]
    fn test_substitute_env_vars() {
        // Set a test environment variable
        unsafe {
            env::set_var("TEST_VAR", "test_value");
            env::set_var("DNS_PORT", "5353");
        }

        let content = "server: ${TEST_VAR}\nport: ${DNS_PORT}";
        let result = substitute_env_vars(content).unwrap();

        assert_eq!(result, "server: test_value\nport: 5353");

        unsafe {
            env::remove_var("TEST_VAR");
            env::remove_var("DNS_PORT");
        }
    }

    #[test]
    fn test_substitute_env_vars_with_default() {
        // Don't set the variable
        unsafe {
            env::remove_var("MISSING_VAR");
        }

        let content = "value: ${MISSING_VAR:-default_value}";
        let result = substitute_env_vars(content).unwrap();

        assert_eq!(result, "value: default_value");
    }

    #[test]
    fn test_substitute_env_vars_missing_no_default() {
        unsafe {
            env::remove_var("MISSING_VAR");
        }

        let content = "value: ${MISSING_VAR}";
        let result = substitute_env_vars(content);

        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("MISSING_VAR not found")
        );
    }

    // NOTE: These env override tests must run single-threaded due to environment variable interference
    // Run with: cargo test -- --test-threads=1

    #[tokio::test(flavor = "current_thread")]
    async fn test_apply_env_overrides_top_level_log_level() {
        // Use a deterministic snapshot-based approach to avoid races with other
        // tests that may modify process environment variables concurrently.
        let yaml = r#"
log:
  level: info
  format: text
plugins: []
"#;

        let mut config: Config = serde_yaml::from_str(yaml).unwrap();
        let mut snapshot = std::collections::HashMap::new();
        snapshot.insert("LOG_LEVEL".to_string(), "debug".to_string());

        apply_env_overrides_from_snapshot(&mut config, &snapshot).unwrap();
        assert_eq!(
            config.log.level, "debug",
            "LOG_LEVEL should override config"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_apply_env_overrides_top_level_log_format() {
        unsafe {
            env::set_var("LOG_FORMAT", "json");
        }

        let yaml = r#"
log:
  level: info
  format: text
plugins: []
"#;
        let config = load_from_yaml(yaml).unwrap();
        assert_eq!(
            config.log.format, "json",
            "LOG_FORMAT should override config"
        );

        unsafe {
            env::remove_var("LOG_FORMAT");
        }
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_apply_env_overrides_admin_config() {
        unsafe {
            env::set_var("ADMIN_ENABLED", "false");
            env::set_var("ADMIN_ADDR", "127.0.0.1:9999");
        }

        // minimal config with no admin section: env should override defaults
        let yaml = r#"
log:
  level: info
plugins: []
"#;
        let config = load_from_yaml(yaml).unwrap();
        assert!(!config.admin.enabled);
        assert_eq!(config.admin.addr, "127.0.0.1:9999");

        unsafe {
            env::remove_var("ADMIN_ENABLED");
            env::remove_var("ADMIN_ADDR");
        }
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_apply_env_overrides_monitoring_config() {
        unsafe {
            env::set_var("METRICS_ENABLED", "true");
            env::set_var("METRICS_ADDR", "127.0.0.1:9999");
        }

        // minimal config with no metrics section: env should override defaults
        let yaml = r#"
log:
  level: info
plugins: []
"#;
        let config = load_from_yaml(yaml).unwrap();
        assert!(config.monitoring.enabled);
        assert_eq!(config.monitoring.addr, "127.0.0.1:9999");

        unsafe {
            env::remove_var("METRICS_ENABLED");
            env::remove_var("METRICS_ADDR");
        }
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_apply_env_overrides_plugin_args() {
        unsafe {
            env::set_var("PLUGINS_CACHE_ARGS_SIZE", "2048");
        }

        let yaml = r#"
log:
  level: info
  format: text
plugins:
  - plugin_type: cache
    args:
      size: 1024
"#;
        let config = load_from_yaml(yaml).unwrap();

        // Find the cache plugin and verify args
        let cache_plugin = config
            .plugins
            .iter()
            .find(|p| p.plugin_type == "cache")
            .unwrap();
        if let serde_yaml::Value::Mapping(args) = &cache_plugin.args {
            let size_value = args.get(serde_yaml::Value::String("size".to_string()));
            assert!(size_value.is_some());
            // The value should be 2048 (from env override)
            if let Some(serde_yaml::Value::Number(n)) = size_value {
                assert_eq!(
                    n.as_u64(),
                    Some(2048),
                    "PLUGINS_CACHE_ARGS_SIZE should override config"
                );
            }
        }

        unsafe {
            env::remove_var("PLUGINS_CACHE_ARGS_SIZE");
        }
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_apply_env_overrides_plugin_args_string_value() {
        unsafe {
            env::set_var("PLUGINS_ADD_GFWLIST_ARGS_SERVER", "http://10.100.100.1");
        }

        let yaml = r#"
log:
  level: info
  format: text
plugins:
  - plugin_type: add-gfwlist
    args:
      server: http://default.com
"#;
        let config = load_from_yaml(yaml).unwrap();

        let plugin = config
            .plugins
            .iter()
            .find(|p| p.plugin_type == "add-gfwlist")
            .unwrap();
        if let serde_yaml::Value::Mapping(args) = &plugin.args {
            let server_value = args.get(serde_yaml::Value::String("server".to_string()));
            if let Some(serde_yaml::Value::String(s)) = server_value {
                assert_eq!(
                    s, "http://10.100.100.1",
                    "PLUGINS_ADD_GFWLIST_ARGS_SERVER should override config"
                );
            }
        }

        unsafe {
            env::remove_var("PLUGINS_ADD_GFWLIST_ARGS_SERVER");
        }
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_apply_env_overrides_jobs_index_cron() {
        // Override jobs[0].cron via env var
        unsafe {
            env::set_var(
                "PLUGINS_AUTO_UPDATE_SCHEDULER_ARGS_JOBS_0_CRON",
                "0 */6 * * *",
            );
        }

        let yaml = r#"
plugins:
  - tag: auto_update_scheduler
    plugin_type: cron
    args:
      jobs: []
"#;
        let config = load_from_yaml(yaml).unwrap();

        let plugin = config
            .plugins
            .iter()
            .find(|p| p.effective_name() == "auto_update_scheduler")
            .unwrap();

        if let serde_yaml::Value::Mapping(args_map) = &plugin.args {
            let jobs_val = args_map.get(serde_yaml::Value::String("jobs".to_string()));
            assert!(jobs_val.is_some());
            if let serde_yaml::Value::Sequence(seq) = jobs_val.unwrap() {
                assert!(!seq.is_empty());
                if let serde_yaml::Value::Mapping(job0) = &seq[0] {
                    let cron_val = job0.get(serde_yaml::Value::String("cron".to_string()));
                    assert!(cron_val.is_some());
                    assert_eq!(cron_val.unwrap().as_str().unwrap(), "0 */6 * * *");
                } else {
                    panic!("jobs[0] is not a mapping");
                }
            } else {
                panic!("jobs is not a sequence");
            }
        } else {
            panic!("plugin.args is not a mapping");
        }

        unsafe {
            env::remove_var("PLUGINS_AUTO_UPDATE_SCHEDULER_ARGS_JOBS_0_CRON");
        }
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_apply_env_overrides_numeric_string_parsing() {
        let value_str = "2048";
        let result = parse_yaml_value(value_str);

        // Should parse as a number, not a string
        match result {
            serde_yaml::Value::Number(n) => {
                assert_eq!(n.as_u64(), Some(2048));
            }
            _ => panic!("Expected number, got {:?}", result),
        }
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_apply_env_overrides_boolean_parsing() {
        let value_true = parse_yaml_value("true");
        let value_false = parse_yaml_value("false");

        assert_eq!(value_true, serde_yaml::Value::Bool(true));
        assert_eq!(value_false, serde_yaml::Value::Bool(false));
    }

    #[test]
    fn test_apply_env_overrides_array_parsing() {
        let value_array = parse_yaml_value("[8.8.8.8, 1.1.1.1]");

        match value_array {
            serde_yaml::Value::Sequence(seq) => {
                assert_eq!(seq.len(), 2);
            }
            _ => panic!("Expected sequence, got {:?}", value_array),
        }
    }

    #[test]
    fn test_normalize_identifier() {
        assert_eq!(normalize_identifier("ADD_GFWLIST"), "add-gfwlist");
        assert_eq!(normalize_identifier("CACHE_SIZE"), "cache-size");
        assert_eq!(
            normalize_identifier("ENABLE_LAZY_CACHE"),
            "enable-lazy-cache"
        );
    }

    // Cleanup test that runs last and clears all env overrides
    #[test]
    fn test_zzz_cleanup_env_overrides() {
        unsafe {
            env::remove_var("LOG_LEVEL");
            env::remove_var("LOG_FORMAT");
            env::remove_var("LOG_FILE");
            env::remove_var("LOG_ROTATE");
            env::remove_var("PLUGINS_CACHE_ARGS_SIZE");
            env::remove_var("PLUGINS_ADD_GFWLIST_ARGS_SERVER");
        }
    }
}