1{ lib, ... }:
2
3{
4 name = "scrutiny";
5 meta.maintainers = with lib.maintainers; [ ];
6
7 nodes = {
8 machine =
9 {
10 self,
11 pkgs,
12 lib,
13 ...
14 }:
15 {
16 services = {
17 scrutiny = {
18 enable = true;
19 settings = {
20 notify.urls = [
21 {
22 _secret = pkgs.writeText "notify-script" "script://${pkgs.writeShellScript "touch-test-file" ''
23 echo "HelloWorld" > /run/scrutiny/hello
24 ''}";
25 }
26 ];
27 };
28 };
29 scrutiny.collector.enable = true;
30 };
31
32 environment.systemPackages =
33 let
34 seleniumScript =
35 pkgs.writers.writePython3Bin "selenium-script"
36 {
37 libraries = with pkgs.python3Packages; [ selenium ];
38 }
39 ''
40 from selenium import webdriver
41 from selenium.webdriver.common.by import By
42 from selenium.webdriver.firefox.options import Options
43 from selenium.webdriver.support.ui import WebDriverWait
44 from selenium.webdriver.support import expected_conditions as EC
45
46 options = Options()
47 options.add_argument("--headless")
48 service = webdriver.FirefoxService(executable_path="${lib.getExe pkgs.geckodriver}") # noqa: E501
49
50 driver = webdriver.Firefox(options=options, service=service)
51 driver.implicitly_wait(10)
52 driver.get("http://localhost:8080/web/dashboard")
53
54 wait = WebDriverWait(driver, 10).until(
55 EC.text_to_be_present_in_element(
56 (By.TAG_NAME, "body"), "Drive health at a glance")
57 )
58
59 body_text = driver.find_element(By.TAG_NAME, "body").text
60 assert "Temperature history for each device" in body_text
61
62 driver.close()
63 '';
64 in
65 with pkgs;
66 [
67 curl
68 firefox-unwrapped
69 geckodriver
70 seleniumScript
71 ];
72 };
73 };
74 # This is the test code that will check if our service is running correctly:
75 testScript = ''
76 start_all()
77
78 # Wait for Scrutiny to be available
79 machine.wait_for_unit("scrutiny")
80 machine.wait_for_open_port(8080)
81
82 # Ensure the API responds as we expect
83 output = machine.succeed("curl localhost:8080/api/health")
84 assert output == '{"success":true}'
85
86 # Start the collector service to send some metrics
87 collect = machine.succeed("systemctl start scrutiny-collector.service")
88
89 # Ensure the application is actually rendered by the Javascript
90 machine.succeed("PYTHONUNBUFFERED=1 selenium-script")
91
92 # Test notification and genJqSecretsReplacementSnippet
93 machine.succeed("curl -X POST http://localhost:8080/api/health/notify")
94 machine.wait_for_file("/run/scrutiny/hello")
95 output = machine.succeed("cat /run/scrutiny/hello")
96 assert "HelloWorld" in output
97 '';
98}