nixpkgs/nixos/tests/scrutiny.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

99 lines
3.1 KiB
Nix
Raw Normal View History

{ lib, ... }:
2024-02-19 09:47:18 +00:00
{
name = "scrutiny";
meta.maintainers = with lib.maintainers; [ jnsgruk ];
2024-02-19 09:47:18 +00:00
nodes = {
machine =
{
self,
pkgs,
lib,
...
}:
{
services = {
scrutiny = {
enable = true;
settings = {
notify.urls = [
{
_secret = pkgs.writeText "notify-script" "script://${pkgs.writeShellScript "touch-test-file" ''
echo "HelloWorld" > /run/scrutiny/hello
''}";
}
];
};
2024-02-19 09:47:18 +00:00
};
scrutiny.collector.enable = true;
};
2024-02-19 09:47:18 +00:00
environment.systemPackages =
let
seleniumScript =
pkgs.writers.writePython3Bin "selenium-script"
{
libraries = with pkgs.python3Packages; [ selenium ];
}
''
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
2024-02-19 09:47:18 +00:00
options = Options()
options.add_argument("--headless")
service = webdriver.FirefoxService(executable_path="${lib.getExe pkgs.geckodriver}") # noqa: E501
2024-02-19 09:47:18 +00:00
driver = webdriver.Firefox(options=options, service=service)
driver.implicitly_wait(10)
driver.get("http://localhost:8080/web/dashboard")
2024-02-19 09:47:18 +00:00
wait = WebDriverWait(driver, 10).until(
EC.text_to_be_present_in_element(
(By.TAG_NAME, "body"), "Drive health at a glance")
)
2024-02-19 09:47:18 +00:00
body_text = driver.find_element(By.TAG_NAME, "body").text
assert "Temperature history for each device" in body_text
2024-02-19 09:47:18 +00:00
driver.close()
'';
in
with pkgs;
[
curl
firefox-unwrapped
geckodriver
seleniumScript
];
};
};
# This is the test code that will check if our service is running correctly:
testScript = ''
start_all()
2024-02-19 09:47:18 +00:00
# Wait for Scrutiny to be available
machine.wait_for_unit("scrutiny")
machine.wait_for_open_port(8080)
2024-02-19 09:47:18 +00:00
# Ensure the API responds as we expect
output = machine.succeed("curl localhost:8080/api/health")
assert output == '{"success":true}'
2024-02-19 09:47:18 +00:00
# Start the collector service to send some metrics
collect = machine.succeed("systemctl start scrutiny-collector.service")
2024-02-19 09:47:18 +00:00
# Ensure the application is actually rendered by the Javascript
machine.succeed("PYTHONUNBUFFERED=1 selenium-script")
# Test notification and genJqSecretsReplacementSnippet
machine.succeed("curl -X POST http://localhost:8080/api/health/notify")
machine.wait_for_file("/run/scrutiny/hello")
output = machine.succeed("cat /run/scrutiny/hello")
assert "HelloWorld" in output
'';
}