epazote 3.5.0

Automated HTTP (microservices) supervisor 🌿
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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
use crate::cli::{
    actions::{
        Action, FallbackContext, FallbackServiceType, FallbackState,
        client::build_client,
        execute_command, execute_fallback_command, execute_fallback_http, get_fallback_state,
        metrics::{ServiceMetrics, metrics_server},
        request::{build_http_request, handle_http_response},
        reset_fallback_state, should_continue_fallback,
        ssl::{SslCheckCache, check_ssl_certificate, new_ssl_check_cache},
    },
    config::{Config, ServiceDetails},
};
use anyhow::{Result, anyhow};
use reqwest::Client;
use rustls::crypto::CryptoProvider;
use std::{collections::HashMap, sync::Arc, time::Duration};
use tokio::{
    sync::Mutex,
    time::{Instant, MissedTickBehavior, interval},
};
use tracing::{debug, error, info, instrument};

enum ServiceAction {
    Url(Client),
    Command(String),
}

fn expected_command_status(service_details: &ServiceDetails) -> Result<i32> {
    service_details
        .expect
        .expected_status_i32()
        .ok_or_else(|| anyhow!("Command checks require expect.status"))
}

/// Handle the create action
///
/// # Errors
///
/// Returns an error if the configuration is invalid or the metrics server fails to start.
#[instrument(skip(action))]
pub async fn handle(action: Action) -> Result<()> {
    // rustls requires a cryptographic provider
    CryptoProvider::install_default(rustls::crypto::ring::default_provider())
        .map_err(|e| anyhow!("Failed to install default crypto provider: {e:?}"))?;

    let Action::Run { config, port } = action;

    let config_path = config;

    let config = Config::new(config_path)?;

    // Create service metrics
    let service_metrics = Arc::new(ServiceMetrics::new()?);
    let ssl_check_cache = new_ssl_check_cache();

    let mut service_handles = Vec::new();

    for (service_name, service) in &config.services {
        let service_counters: Arc<Mutex<HashMap<String, FallbackState>>> =
            Arc::new(Mutex::new(HashMap::new()));

        let service_name = service_name.clone();
        let service_details = service.clone();
        let counters = service_counters;
        let ssl_cache = ssl_check_cache.clone();

        let action = if let Some(ref command) = service_details.test {
            ServiceAction::Command(command.clone())
        } else {
            let (builder, _client_config) = build_client(&service_details)?;
            let client = builder.build()?;

            ServiceAction::Url(client)
        };

        // Clone the metrics for this task
        let metrics = service_metrics.clone();

        // Spawn a task for each service
        let handle = tokio::spawn(async move {
            let every = service_details.every;
            run_service(
                service_name,
                service_details,
                action,
                metrics,
                every,
                counters,
                ssl_cache,
            )
            .await;
        });

        service_handles.push(handle);
    }

    // Spawn metrics server
    let metrics_server_handle = tokio::spawn(async move {
        if let Err(e) = metrics_server(service_metrics, port).await {
            error!("Metrics server error: {}", e);
        }
    });

    info!("Epazote 🌿 is running");

    // Wait for all tasks to complete
    tokio::select! {
        (result, _, _) = futures::future::select_all(service_handles) => {
            match result {
                Ok(()) => error!("A service monitoring task completed unexpectedly"),
                Err(e) => error!("A service monitoring task panicked: {}", e),
            }
        },
        _ = metrics_server_handle => {
            error!("Metrics server stopped unexpectedly");
        }
    }

    Ok(())
}

/// Runs the task for a single service
async fn run_service(
    service_name: String,
    service_details: ServiceDetails,
    action: ServiceAction,
    metrics: Arc<ServiceMetrics>,
    interval_duration: Duration,
    counters: Arc<Mutex<HashMap<String, FallbackState>>>,
    ssl_cache: SslCheckCache,
) {
    let mut interval_timer = interval(interval_duration);
    interval_timer.set_missed_tick_behavior(MissedTickBehavior::Skip);

    loop {
        interval_timer.tick().await; // Wait for the next interval

        debug!("Running scan for service: {}", service_name);

        // Perform the service scan
        match scan_service(
            &service_name,
            &service_details,
            &action,
            &metrics,
            counters.clone(),
            &ssl_cache,
        )
        .await
        {
            Ok(()) => (),
            Err(e) => {
                // Increment failure counter
                metrics
                    .epazote_failures_total
                    .with_label_values(&[&service_name])
                    .inc();

                metrics
                    .epazote_status
                    .with_label_values(&[&service_name])
                    .set(0);

                error!("Error scanning service '{}': {}", &service_name, e);
            }
        }
    }
}

/// `scan_service` performs the actual scan of the service
async fn scan_service(
    service_name: &str,
    service_details: &ServiceDetails,
    action: &ServiceAction,
    metrics: &ServiceMetrics,
    counters: Arc<Mutex<HashMap<String, FallbackState>>>,
    ssl_cache: &SslCheckCache,
) -> Result<()> {
    let start_time = Instant::now();

    match action {
        ServiceAction::Url(client) => {
            let request_builder = build_http_request(client, service_details)?;

            let request = request_builder.build()?;

            let url = request.url().to_string();

            if url.starts_with("https://") {
                check_ssl_certificate(&url, service_name, metrics, ssl_cache).await?;
            }

            debug!("HTTP request: {:?}", request);

            // Make the request
            let response = match client.execute(request).await {
                Ok(response) => response,
                Err(error) => {
                    if let Some(action) = &service_details.expect.if_not
                        && should_continue_fallback(service_name, &counters, action).await
                    {
                        let state = get_fallback_state(service_name, &counters)
                            .await
                            .unwrap_or_default();
                        let context = FallbackContext {
                            service_name,
                            service_type: FallbackServiceType::Http,
                            expected_status: service_details.expect.expected_status_i32(),
                            actual_status: None,
                            error: "request_error",
                            failure_count: state.consecutive_failures,
                            threshold: action.threshold.unwrap_or(1),
                            url: Some(&url),
                            test: None,
                        };

                        if let Some(cmd) = &action.cmd {
                            let exit_code = execute_fallback_command(cmd, &context).await?;
                            info!(
                                "Executed fallback command for {} with exit code {}",
                                service_name, exit_code
                            );
                        }

                        if let Some(http) = &action.http {
                            let status = execute_fallback_http(http).await?;
                            info!(
                                "Executed fallback HTTP request for {} with status code {}",
                                service_name, status
                            );
                        }
                    }

                    return Err(error.into());
                }
            };

            // Record response time
            let response_time = start_time.elapsed().as_secs_f64();
            metrics
                .epazote_response_time
                .with_label_values(&[service_name])
                .observe(response_time);

            // Handle the response
            handle_http_response(service_name, service_details, response, metrics, counters)
                .await?;
        }

        ServiceAction::Command(command) => {
            debug!("Executing command: {}", command);

            let exit_status = execute_command(command).await.unwrap_or(1);
            let expected_status = expected_command_status(service_details)?;

            if exit_status == expected_status {
                reset_fallback_state(service_name, &counters).await;
            } else if let Some(action) = &service_details.expect.if_not
                && should_continue_fallback(service_name, &counters, action).await
            {
                let state = get_fallback_state(service_name, &counters)
                    .await
                    .unwrap_or_default();
                let context = FallbackContext {
                    service_name,
                    service_type: FallbackServiceType::Command,
                    expected_status: Some(expected_status),
                    actual_status: Some(exit_status),
                    error: "command_failed",
                    failure_count: state.consecutive_failures,
                    threshold: action.threshold.unwrap_or(1),
                    url: None,
                    test: Some(command),
                };

                if let Some(cmd) = &action.cmd {
                    let exit_code = execute_fallback_command(cmd, &context).await?;
                    info!(
                        "Executed fallback command for {} with exit code {}",
                        service_name, exit_code
                    );
                }

                if let Some(http) = &action.http {
                    let status = execute_fallback_http(http).await?;
                    info!(
                        "Executed fallback HTTP request for {} with status code {}",
                        service_name, status
                    );
                }
            }
        }
    }

    Ok(())
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::cli::config::{Action, Expect, HttpMethod};
    use mockito::Server;
    use reqwest::StatusCode;
    use std::{fs, net::TcpListener, os::unix::fs::PermissionsExt, path::PathBuf, sync::Arc};
    use tokio::process::Command;
    use tokio::runtime::Runtime;
    use tokio::time::Duration;

    /// Helper Function: Create Mock `ServiceDetails`
    fn mock_service_details(
        test_cmd: Option<&str>,
        expect_status: u16,
        if_not: Option<&str>,
    ) -> ServiceDetails {
        ServiceDetails {
            every: Duration::from_secs(1),
            expect: Expect {
                status: Some(expect_status),
                header: None,
                body: None,
                body_not: None,
                json: None,
                if_not: if_not.map(|cmd| Action {
                    cmd: Some(cmd.to_string()),
                    ..Default::default()
                }),
            },
            follow_redirects: Some(true),
            headers: None,
            max_bytes: None,
            test: test_cmd.map(std::string::ToString::to_string),
            timeout: Duration::from_secs(5),
            url: None,
            method: HttpMethod::Get,
            body: None,
        }
    }

    /// Helper Function: Create Mock Action
    fn mock_action(test_cmd: &str) -> ServiceAction {
        ServiceAction::Command(test_cmd.to_string())
    }

    fn create_env_capture_script(env_vars: &[&str]) -> (tempfile::TempDir, String, PathBuf) {
        let tempdir = tempfile::Builder::new()
            .prefix("epazote-run-env-")
            .tempdir_in(".")
            .expect("Failed to create temp dir");
        let script_path = tempdir.path().join("capture.sh");
        let output_path = tempdir.path().join("output.txt");
        let body = env_vars
            .iter()
            .map(|key| format!("printenv {key}"))
            .collect::<Vec<_>>()
            .join("\n");

        fs::write(
            &script_path,
            format!("#!/bin/sh\n{{\n{body}\n}} > {}\n", output_path.display()),
        )
        .expect("Failed to write capture script");

        let mut permissions = fs::metadata(&script_path)
            .expect("Failed to stat script")
            .permissions();
        permissions.set_mode(0o755);
        fs::set_permissions(&script_path, permissions).expect("Failed to chmod script");

        (
            tempdir,
            script_path
                .to_str()
                .expect("Invalid script path")
                .to_string(),
            output_path,
        )
    }

    /// Test: Verify Shell Command Exit Codes
    async fn run_command(cmd: &str) -> i32 {
        let shell = std::env::var("SHELL").unwrap_or_else(|_| "sh".to_string());
        let output = Command::new(shell)
            .arg("-c")
            .arg(cmd)
            .output()
            .await
            .expect("Failed to execute command");

        output.status.code().unwrap_or(1) // Default to 1 if no exit code
    }

    #[test]
    // this test is only for the test run_command function, not the actual code
    fn test_command_exit_status() {
        let rt = Runtime::new().expect("Failed to create runtime");

        let exit_code_0 = rt.block_on(run_command("exit 0"));
        assert_eq!(exit_code_0, 0, "Command `exit 0` should return exit code 0");

        let exit_code_1 = rt.block_on(run_command("exit 1"));
        assert_eq!(exit_code_1, 1, "Command `exit 1` should return exit code 1");
    }

    /// Test: Successful HTTP Service with Expected Status
    #[tokio::test]
    async fn test_http_service_expect_status() {
        let mut server = Server::new_async().await;
        let _m = server
            .mock("GET", "/test")
            .with_status(200)
            .create_async()
            .await;

        let url = format!("{}/test", server.url());
        let client = Client::new();
        let response = client
            .get(&url)
            .send()
            .await
            .expect("Failed to send request");
        let status = response.status();

        assert_eq!(status, StatusCode::OK, "Expected status 200 OK");
    }

    /// Test: Scan Service Command - Success
    #[tokio::test]
    async fn test_scan_service_command_success() {
        let service_details = mock_service_details(Some("exit 0"), 0, None);
        let action = mock_action("exit 0");
        let metrics = Arc::new(ServiceMetrics::new().expect("Failed to create metrics"));
        let counters: Arc<Mutex<HashMap<String, FallbackState>>> =
            Arc::new(Mutex::new(HashMap::new()));
        let ssl_cache = new_ssl_check_cache();

        let result = scan_service(
            "test-service",
            &service_details,
            &action,
            &metrics,
            counters,
            &ssl_cache,
        )
        .await;

        assert!(
            result.is_ok(),
            "Scan service should succeed for a successful command"
        );
    }

    #[tokio::test]
    async fn test_scan_service_command_if_not_cmd_sets_env_vars() {
        let (_tempdir, script_path, output_path) = create_env_capture_script(&[
            "EPAZOTE_SERVICE_NAME",
            "EPAZOTE_SERVICE_TYPE",
            "EPAZOTE_EXPECTED_STATUS",
            "EPAZOTE_ACTUAL_STATUS",
            "EPAZOTE_ERROR",
            "EPAZOTE_FAILURE_COUNT",
            "EPAZOTE_THRESHOLD",
            "EPAZOTE_TEST",
        ]);

        let mut service_details = mock_service_details(Some("exit 1"), 0, Some(&script_path));
        service_details
            .expect
            .if_not
            .as_mut()
            .expect("if_not should be present")
            .threshold = Some(2);

        let action = mock_action("exit 1");
        let metrics = Arc::new(ServiceMetrics::new().expect("Failed to create metrics"));
        let counters: Arc<Mutex<HashMap<String, FallbackState>>> =
            Arc::new(Mutex::new(HashMap::new()));
        let ssl_cache = new_ssl_check_cache();

        for _ in 0..2 {
            let result = scan_service(
                "test-service",
                &service_details,
                &action,
                &metrics,
                Arc::clone(&counters),
                &ssl_cache,
            )
            .await;

            assert!(result.is_ok(), "Scan service should complete");
        }

        let output = fs::read_to_string(output_path).expect("Failed to read env capture");
        assert_eq!(
            output.lines().collect::<Vec<_>>(),
            vec![
                "test-service",
                "command",
                "0",
                "1",
                "command_failed",
                "2",
                "2",
                "exit 1",
            ]
        );
    }

    /// Test: Scan Service Command - Failure with Fallback
    #[tokio::test]
    async fn test_scan_service_command_failure_with_fallback() {
        let service_details = mock_service_details(Some("exit 1"), 0, Some("echo 'Fallback'"));
        let action = mock_action("exit 1");
        let metrics = Arc::new(ServiceMetrics::new().expect("Failed to create metrics"));
        let counters: Arc<Mutex<HashMap<String, FallbackState>>> =
            Arc::new(Mutex::new(HashMap::new()));
        let ssl_cache = new_ssl_check_cache();

        let result = scan_service(
            "test-service",
            &service_details,
            &action,
            &metrics,
            Arc::clone(&counters),
            &ssl_cache,
        )
        .await;

        assert!(
            result.is_ok(),
            "Scan service should execute fallback for failed command"
        );

        let counters_locked = counters.lock().await;
        let count = counters_locked
            .get("test-service")
            .map_or(0, |state| state.fallback_executions);

        assert_eq!(count, 1, "Counter should have been incremented");
    }

    /// Test: Scan Service Command - Stops after 2 failures
    #[tokio::test]
    async fn test_scan_service_command_failure_with_stop_after_2_attempts() {
        let mut service_details = mock_service_details(Some("exit 1"), 0, Some("echo 'Fallback'"));
        let action = mock_action("exit 1");

        // Set stop condition to 2
        service_details
            .expect
            .if_not
            .as_mut()
            .expect("if_not should be present")
            .stop = Some(2);

        let metrics = Arc::new(ServiceMetrics::new().expect("Failed to create metrics"));
        let counters: Arc<Mutex<HashMap<String, FallbackState>>> =
            Arc::new(Mutex::new(HashMap::new()));
        let ssl_cache = new_ssl_check_cache();

        // First attempt
        let result1 = scan_service(
            "test-service",
            &service_details,
            &action,
            &metrics,
            Arc::clone(&counters),
            &ssl_cache,
        )
        .await;

        assert!(result1.is_ok(), "First attempt should allow fallback");

        // Check counter after first attempt
        let count1 = {
            let counters_locked = counters.lock().await;
            counters_locked
                .get("test-service")
                .map_or(0, |state| state.fallback_executions)
        };
        assert_eq!(count1, 1, "Counter should be 1 after first attempt");

        // Second attempt
        let result2 = scan_service(
            "test-service",
            &service_details,
            &action,
            &metrics,
            Arc::clone(&counters),
            &ssl_cache,
        )
        .await;

        assert!(result2.is_ok(), "Second attempt should allow fallback");

        // Check counter after second attempt
        let count2 = {
            let counters_locked = counters.lock().await;
            counters_locked
                .get("test-service")
                .map_or(0, |state| state.fallback_executions)
        };
        assert_eq!(count2, 2, "Counter should be 2 after second attempt");

        // Third attempt (should NOT execute fallback)
        let result3 = scan_service(
            "test-service",
            &service_details,
            &action,
            &metrics,
            Arc::clone(&counters),
            &ssl_cache,
        )
        .await;

        assert!(
            result3.is_ok(),
            "Third attempt should skip fallback due to stop limit"
        );

        // Check counter after third attempt (should remain at 2)
        let count3 = {
            let counters_locked = counters.lock().await;
            counters_locked
                .get("test-service")
                .map_or(0, |state| state.fallback_executions)
        };
        assert_eq!(count3, 2, "Counter should remain at 2 after third attempt");
    }

    #[tokio::test]
    async fn test_scan_service_command_threshold_delays_fallback() {
        let mut service_details = mock_service_details(Some("exit 1"), 0, Some("echo 'Fallback'"));
        let action = mock_action("exit 1");

        service_details
            .expect
            .if_not
            .as_mut()
            .expect("if_not should be present")
            .threshold = Some(3);

        let metrics = Arc::new(ServiceMetrics::new().expect("Failed to create metrics"));
        let counters: Arc<Mutex<HashMap<String, FallbackState>>> =
            Arc::new(Mutex::new(HashMap::new()));
        let ssl_cache = new_ssl_check_cache();

        for expected_executions in [0, 0, 1] {
            let result = scan_service(
                "test-service",
                &service_details,
                &action,
                &metrics,
                Arc::clone(&counters),
                &ssl_cache,
            )
            .await;

            assert!(result.is_ok(), "Scan service should complete");

            let counters_locked = counters.lock().await;
            let state = counters_locked
                .get("test-service")
                .expect("State not found");
            assert_eq!(state.fallback_executions, expected_executions);
            drop(counters_locked);
        }
    }

    #[tokio::test]
    async fn test_scan_service_command_success_resets_threshold_counter() {
        let mut service_details = mock_service_details(Some("exit 1"), 0, Some("echo 'Fallback'"));
        service_details
            .expect
            .if_not
            .as_mut()
            .expect("if_not should be present")
            .threshold = Some(2);

        let failing_action = mock_action("exit 1");
        let success_action = mock_action("exit 0");
        let metrics = Arc::new(ServiceMetrics::new().expect("Failed to create metrics"));
        let counters: Arc<Mutex<HashMap<String, FallbackState>>> =
            Arc::new(Mutex::new(HashMap::new()));
        let ssl_cache = new_ssl_check_cache();

        let first_failure = scan_service(
            "test-service",
            &service_details,
            &failing_action,
            &metrics,
            Arc::clone(&counters),
            &ssl_cache,
        )
        .await;
        assert!(first_failure.is_ok());

        let success = scan_service(
            "test-service",
            &service_details,
            &success_action,
            &metrics,
            Arc::clone(&counters),
            &ssl_cache,
        )
        .await;
        assert!(success.is_ok());

        let second_failure = scan_service(
            "test-service",
            &service_details,
            &failing_action,
            &metrics,
            Arc::clone(&counters),
            &ssl_cache,
        )
        .await;
        assert!(second_failure.is_ok());

        let counters_locked = counters.lock().await;
        let state = counters_locked
            .get("test-service")
            .expect("State not found");
        assert_eq!(state.consecutive_failures, 1);
        assert_eq!(state.fallback_executions, 0);
    }

    #[tokio::test]
    async fn test_scan_service_http_request_error_sets_env_vars() {
        let (_tempdir, script_path, output_path) = create_env_capture_script(&[
            "EPAZOTE_SERVICE_NAME",
            "EPAZOTE_SERVICE_TYPE",
            "EPAZOTE_EXPECTED_STATUS",
            "EPAZOTE_ERROR",
            "EPAZOTE_FAILURE_COUNT",
            "EPAZOTE_THRESHOLD",
            "EPAZOTE_URL",
        ]);

        let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind test listener");
        let url = format!(
            "http://{}/health",
            listener.local_addr().expect("Failed to get local addr")
        );
        drop(listener);

        let service_details = ServiceDetails {
            every: Duration::from_secs(1),
            expect: Expect {
                status: Some(200),
                header: None,
                body: None,
                body_not: None,
                json: None,
                if_not: Some(Action {
                    cmd: Some(script_path),
                    http: None,
                    stop: None,
                    threshold: Some(1),
                }),
            },
            follow_redirects: Some(true),
            headers: None,
            max_bytes: None,
            test: None,
            timeout: Duration::from_millis(100),
            url: Some(url.clone()),
            method: HttpMethod::Get,
            body: None,
        };

        let action = ServiceAction::Url(Client::new());
        let metrics = Arc::new(ServiceMetrics::new().expect("Failed to create metrics"));
        let counters: Arc<Mutex<HashMap<String, FallbackState>>> =
            Arc::new(Mutex::new(HashMap::new()));
        let ssl_cache = new_ssl_check_cache();

        let result = scan_service(
            "http-error-service",
            &service_details,
            &action,
            &metrics,
            Arc::clone(&counters),
            &ssl_cache,
        )
        .await;

        assert!(
            result.is_err(),
            "Request error should still return an error"
        );

        let output = fs::read_to_string(output_path).expect("Failed to read env capture");
        assert_eq!(
            output.lines().collect::<Vec<_>>(),
            vec![
                "http-error-service",
                "http",
                "200",
                "request_error",
                "1",
                "1",
                &url,
            ]
        );
    }

    #[tokio::test]
    async fn test_scan_service_command_stop_does_not_reset_after_success() {
        let mut service_details = mock_service_details(Some("exit 1"), 0, Some("echo 'Fallback'"));
        let if_not = service_details
            .expect
            .if_not
            .as_mut()
            .expect("if_not should be present");
        if_not.threshold = Some(1);
        if_not.stop = Some(1);

        let failing_action = mock_action("exit 1");
        let success_action = mock_action("exit 0");
        let metrics = Arc::new(ServiceMetrics::new().expect("Failed to create metrics"));
        let counters: Arc<Mutex<HashMap<String, FallbackState>>> =
            Arc::new(Mutex::new(HashMap::new()));
        let ssl_cache = new_ssl_check_cache();

        let first_failure = scan_service(
            "test-service",
            &service_details,
            &failing_action,
            &metrics,
            Arc::clone(&counters),
            &ssl_cache,
        )
        .await;
        assert!(first_failure.is_ok());

        let success = scan_service(
            "test-service",
            &service_details,
            &success_action,
            &metrics,
            Arc::clone(&counters),
            &ssl_cache,
        )
        .await;
        assert!(success.is_ok());

        let second_failure = scan_service(
            "test-service",
            &service_details,
            &failing_action,
            &metrics,
            Arc::clone(&counters),
            &ssl_cache,
        )
        .await;
        assert!(second_failure.is_ok());

        let counters_locked = counters.lock().await;
        let state = counters_locked
            .get("test-service")
            .expect("State not found");
        assert_eq!(state.fallback_executions, 1);
        assert_eq!(state.consecutive_failures, 1);
    }

    /// Test: Scan Service Command - Ensure counter can reach 1000 when no stop condition is set
    #[tokio::test]
    async fn test_scan_service_command_runs_1000_times_without_stop() {
        let mut service_details = mock_service_details(Some("exit 1"), 0, Some("echo 'Fallback'"));
        let action = mock_action("exit 1");

        // Ensure no stop limit is set
        service_details
            .expect
            .if_not
            .as_mut()
            .expect("if_not should be present")
            .stop = None;

        let metrics = Arc::new(ServiceMetrics::new().expect("Failed to create metrics"));
        let counters: Arc<Mutex<HashMap<String, FallbackState>>> =
            Arc::new(Mutex::new(HashMap::new()));
        let ssl_cache = new_ssl_check_cache();

        // Run scan_service 100 times
        for _ in 0..100 {
            let _ = scan_service(
                "test-service",
                &service_details,
                &action,
                &metrics,
                Arc::clone(&counters),
                &ssl_cache,
            )
            .await;
        }

        // Check that counter reached 1000
        let final_count = {
            let counters_locked = counters.lock().await;
            counters_locked
                .get("test-service")
                .map_or(0, |state| state.fallback_executions)
        };

        assert_eq!(
            final_count, 100,
            "Counter should reach 100 when no stop is set"
        );
    }

    /// Test: Scan Service Command - Failure with Fallback and Stop
    #[tokio::test]
    async fn test_scan_service_command_failure_with_fallback_and_stop() {
        let service_details = mock_service_details(Some("exit 1"), 0, Some("echo 'Fallback'"));
        let action = mock_action("exit 1");
        let metrics = Arc::new(ServiceMetrics::new().expect("Failed to create metrics"));
        let counters: Arc<Mutex<HashMap<String, FallbackState>>> =
            Arc::new(Mutex::new(HashMap::new()));
        let ssl_cache = new_ssl_check_cache();

        let result = scan_service(
            "test-service",
            &service_details,
            &action,
            &metrics,
            counters,
            &ssl_cache,
        )
        .await;
        assert!(
            result.is_ok(),
            "Scan service should execute fallback for failed command"
        );
    }

    /// Test: Run Service - URL Success
    #[tokio::test]
    async fn test_run_service_http_success() {
        let mut server = Server::new_async().await;
        let _m = server
            .mock("GET", "/health")
            .with_status(200)
            .create_async()
            .await;

        let service_details = ServiceDetails {
            every: Duration::from_secs(1),
            expect: Expect {
                status: Some(200),
                header: None,
                body: None,
                body_not: None,
                json: None,
                if_not: None,
            },
            follow_redirects: Some(true),
            headers: None,
            max_bytes: None,
            test: None,
            timeout: Duration::from_secs(5),
            url: Some(format!("{}/health", server.url())),
            method: HttpMethod::Get,
            body: None,
        };

        let action = ServiceAction::Url(Client::new());
        let metrics = Arc::new(ServiceMetrics::new().expect("Failed to create metrics"));
        let counters: Arc<Mutex<HashMap<String, FallbackState>>> =
            Arc::new(Mutex::new(HashMap::new()));
        let ssl_cache = new_ssl_check_cache();

        tokio::spawn(async move {
            run_service(
                "http-service".to_string(),
                service_details,
                action,
                metrics,
                Duration::from_millis(100),
                counters,
                ssl_cache,
            )
            .await;
        });

        tokio::time::sleep(Duration::from_millis(500)).await;
    }
}