1 import ./make-test-python.nix ({ pkgs, lib, ... }:
4 api_token = "f87f42114e44b63ad1b9e3c3d33d6fbe"; # random md5 hash
5 wrong_api_token = "e68ba041fcf1eab923a7a6de3af5f726"; # another random md5 hash
8 meta.maintainers = lib.teams.wdz.members;
11 time.timeZone = "Europe/Berlin";
13 environment.systemPackages = with pkgs; [
20 hostname = "librenms";
24 database = "librenms";
25 username = "librenms";
26 passwordFile = pkgs.writeText "librenms-db-pass" "librenmsdbpass";
31 enableOneMinutePolling = true;
33 enable_billing = true;
37 # systemd oneshot to create a dummy admin user and a API token for testing
38 systemd.services.lnms-api-init = {
39 description = "LibreNMS API init";
40 after = [ "librenms-setup.service" ];
41 wantedBy = [ "multi-user.target" ];
44 RemainAfterExit = true;
50 API_TOKEN=${api_token} # random md5 hash
52 # we don't need to know the password, it just has to exist
53 API_USER_PASS=$(${pkgs.pwgen}/bin/pwgen -s 64 1)
54 ${pkgs.librenms}/artisan user:add $API_USER_NAME -r admin -p $API_USER_PASS
55 API_USER_ID=$(${pkgs.mariadb}/bin/mysql -D librenms -N -B -e "SELECT user_id FROM users WHERE username = '$API_USER_NAME';")
57 ${pkgs.mariadb}/bin/mysql -D librenms -e "INSERT INTO api_tokens (user_id, token_hash, description) VALUES ($API_USER_ID, '$API_TOKEN', 'API User')"
63 networking.firewall.allowedUDPPorts = [ 161 ];
65 systemd.services.snmpd = {
66 description = "snmpd";
67 after = [ "network-online.target" ];
68 wants = [ "network-online.target" ];
69 wantedBy = [ "multi-user.target" ];
75 snmpd-config = pkgs.writeText "snmpd-config" ''
76 com2sec readonly default public
78 group MyROGroup v2c readonly
79 view all included .1 80
80 access MyROGroup "" any noauth exact all none none
82 syslocation Testcity, Testcountry
83 syscontact Testi mc Test <test@example.com>
85 in "${pkgs.net-snmp}/bin/snmpd -c ${snmpd-config} -C";
93 snmphost.wait_until_succeeds("pgrep snmpd")
95 librenms.wait_for_unit("lnms-api-init.service")
96 librenms.wait_for_open_port(80)
98 # Test that we can authenticate against the API
99 librenms.succeed("curl --fail -H 'X-Auth-Token: ${api_token}' http://localhost/api/v0")
100 librenms.fail("curl --fail -H 'X-Auth-Token: ${wrong_api_token}' http://localhost/api/v0")
102 # add snmphost as a device
103 librenms.succeed("curl --fail -X POST -d '{\"hostname\":\"snmphost\",\"version\":\"v2c\",\"community\":\"public\"}' -H 'X-Auth-Token: ${api_token}' http://localhost/api/v0/devices")
105 # wait until snmphost gets polled
106 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'")