1
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-06-23 09:51:00 +03:00

nixosTests.librenms: migrate to runTest

Part of #386873
This commit is contained in:
Piotr Kwiecinski 2025-04-04 13:44:11 +02:00
parent 2adb1b17c0
commit 8807f45da0
No known key found for this signature in database
GPG key ID: EC0DE1CB9D5258B4
2 changed files with 89 additions and 91 deletions

View file

@ -691,7 +691,7 @@ in
leaps = handleTest ./leaps.nix { }; leaps = handleTest ./leaps.nix { };
lemmy = handleTest ./lemmy.nix { }; lemmy = handleTest ./lemmy.nix { };
libinput = handleTest ./libinput.nix { }; libinput = handleTest ./libinput.nix { };
librenms = handleTest ./librenms.nix { }; librenms = runTest ./librenms.nix;
libresprite = handleTest ./libresprite.nix { }; libresprite = handleTest ./libresprite.nix { };
libreswan = runTest ./libreswan.nix; libreswan = runTest ./libreswan.nix;
libreswan-nat = runTest ./libreswan-nat.nix; libreswan-nat = runTest ./libreswan-nat.nix;

View file

@ -1,106 +1,104 @@
import ./make-test-python.nix ( { pkgs, lib, ... }:
{ pkgs, lib, ... }:
let let
api_token = "f87f42114e44b63ad1b9e3c3d33d6fbe"; # random md5 hash api_token = "f87f42114e44b63ad1b9e3c3d33d6fbe"; # random md5 hash
wrong_api_token = "e68ba041fcf1eab923a7a6de3af5f726"; # another random md5 hash wrong_api_token = "e68ba041fcf1eab923a7a6de3af5f726"; # another random md5 hash
in in
{ {
name = "librenms"; name = "librenms";
meta.maintainers = lib.teams.wdz.members; meta.maintainers = lib.teams.wdz.members;
nodes.librenms = { nodes.librenms = {
time.timeZone = "Europe/Berlin"; time.timeZone = "Europe/Berlin";
environment.systemPackages = with pkgs; [ environment.systemPackages = with pkgs; [
curl curl
jq jq
]; ];
services.librenms = { services.librenms = {
enable = true; enable = true;
hostname = "librenms"; hostname = "librenms";
database = { database = {
createLocally = true; createLocally = true;
host = "localhost"; host = "localhost";
database = "librenms"; database = "librenms";
username = "librenms"; username = "librenms";
passwordFile = pkgs.writeText "librenms-db-pass" "librenmsdbpass"; passwordFile = pkgs.writeText "librenms-db-pass" "librenmsdbpass";
};
nginx = {
default = true;
};
enableOneMinutePolling = true;
settings = {
enable_billing = true;
};
}; };
nginx = {
# systemd oneshot to create a dummy admin user and a API token for testing default = true;
systemd.services.lnms-api-init = { };
description = "LibreNMS API init"; enableOneMinutePolling = true;
after = [ "librenms-setup.service" ]; settings = {
wantedBy = [ "multi-user.target" ]; enable_billing = true;
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
User = "root";
Group = "root";
};
script = ''
API_USER_NAME=api
API_TOKEN=${api_token} # random md5 hash
# seeding database to get the admin roles
${pkgs.librenms}/artisan db:seed --force --no-interaction
# we don't need to know the password, it just has to exist
API_USER_PASS=$(${pkgs.pwgen}/bin/pwgen -s 64 1)
${pkgs.librenms}/artisan user:add $API_USER_NAME -r admin -p $API_USER_PASS
API_USER_ID=$(${pkgs.mariadb}/bin/mysql -D librenms -N -B -e "SELECT user_id FROM users WHERE username = '$API_USER_NAME';")
${pkgs.mariadb}/bin/mysql -D librenms -e "INSERT INTO api_tokens (user_id, token_hash, description) VALUES ($API_USER_ID, '$API_TOKEN', 'API User')"
'';
}; };
}; };
nodes.snmphost = { # systemd oneshot to create a dummy admin user and a API token for testing
systemd.services.lnms-api-init = {
services.snmpd = { description = "LibreNMS API init";
enable = true; after = [ "librenms-setup.service" ];
openFirewall = true; wantedBy = [ "multi-user.target" ];
serviceConfig = {
configText = '' Type = "oneshot";
com2sec readonly default public RemainAfterExit = true;
User = "root";
group MyROGroup v2c readonly Group = "root";
view all included .1 80
access MyROGroup "" any noauth exact all none none
syslocation Testcity, Testcountry
syscontact Testi mc Test <test@example.com>
'';
}; };
script = ''
API_USER_NAME=api
API_TOKEN=${api_token} # random md5 hash
# seeding database to get the admin roles
${pkgs.librenms}/artisan db:seed --force --no-interaction
# we don't need to know the password, it just has to exist
API_USER_PASS=$(${pkgs.pwgen}/bin/pwgen -s 64 1)
${pkgs.librenms}/artisan user:add $API_USER_NAME -r admin -p $API_USER_PASS
API_USER_ID=$(${pkgs.mariadb}/bin/mysql -D librenms -N -B -e "SELECT user_id FROM users WHERE username = '$API_USER_NAME';")
${pkgs.mariadb}/bin/mysql -D librenms -e "INSERT INTO api_tokens (user_id, token_hash, description) VALUES ($API_USER_ID, '$API_TOKEN', 'API User')"
'';
}; };
};
testScript = '' nodes.snmphost = {
start_all()
snmphost.wait_for_unit("snmpd.service") services.snmpd = {
enable = true;
openFirewall = true;
librenms.wait_for_unit("lnms-api-init.service") configText = ''
librenms.wait_for_open_port(80) com2sec readonly default public
# Test that we can authenticate against the API group MyROGroup v2c readonly
librenms.succeed("curl --fail -H 'X-Auth-Token: ${api_token}' http://localhost/api/v0") view all included .1 80
librenms.fail("curl --fail -H 'X-Auth-Token: ${wrong_api_token}' http://localhost/api/v0") access MyROGroup "" any noauth exact all none none
# add snmphost as a device syslocation Testcity, Testcountry
librenms.succeed("curl --fail -X POST -d '{\"hostname\":\"snmphost\",\"version\":\"v2c\",\"community\":\"public\"}' -H 'X-Auth-Token: ${api_token}' http://localhost/api/v0/devices") syscontact Testi mc Test <test@example.com>
'';
# wait until snmphost gets polled };
librenms.wait_until_succeeds("test $(curl -H 'X-Auth-Token: ${api_token}' http://localhost/api/v0/devices/snmphost | jq -Mr .devices[0].last_polled) != 'null'") };
'';
} testScript = ''
) start_all()
snmphost.wait_for_unit("snmpd.service")
librenms.wait_for_unit("lnms-api-init.service")
librenms.wait_for_open_port(80)
# Test that we can authenticate against the API
librenms.succeed("curl --fail -H 'X-Auth-Token: ${api_token}' http://localhost/api/v0")
librenms.fail("curl --fail -H 'X-Auth-Token: ${wrong_api_token}' http://localhost/api/v0")
# add snmphost as a device
librenms.succeed("curl --fail -X POST -d '{\"hostname\":\"snmphost\",\"version\":\"v2c\",\"community\":\"public\"}' -H 'X-Auth-Token: ${api_token}' http://localhost/api/v0/devices")
# wait until snmphost gets polled
librenms.wait_until_succeeds("test $(curl -H 'X-Auth-Token: ${api_token}' http://localhost/api/v0/devices/snmphost | jq -Mr .devices[0].last_polled) != 'null'")
'';
}