about summary refs log tree commit diff
path: root/nixos/tests/grafana
diff options
context:
space:
mode:
authorKFears <kfearsoff@gmail.com>2022-09-18 13:35:07 +0400
committerKFears <kfearsoff@gmail.com>2022-10-21 16:42:30 +0400
commit89e30315e0d141ca8fa1c0ab90608bb599c58d55 (patch)
tree6bd2df549d19d6123dfe53853b7056338769b78b /nixos/tests/grafana
parent2b94d18320866f98e6a5030545a35c93d274abe6 (diff)
nixos/grafana: refactor dashboards for RFC42
This commit refactors `services.grafana.provision.dashboards` towards
the RFC42 style. To preserve backwards compatibility, we have to jump
through a ton of hoops, introducing esoteric type signatures and bizarre
structs. The Grafana module definition should hopefully become a lot
cleaner after a release cycle or two once the old configuration style is
completely deprecated.
Diffstat (limited to 'nixos/tests/grafana')
-rw-r--r--nixos/tests/grafana/basic.nix114
-rw-r--r--nixos/tests/grafana/default.nix9
-rw-r--r--nixos/tests/grafana/provision-dashboards/default.nix92
-rw-r--r--nixos/tests/grafana/provision-dashboards/provision-dashboards.yaml6
-rw-r--r--nixos/tests/grafana/provision-dashboards/test_dashboard.json47
5 files changed, 268 insertions, 0 deletions
diff --git a/nixos/tests/grafana/basic.nix b/nixos/tests/grafana/basic.nix
new file mode 100644
index 0000000000000..f674026b821e2
--- /dev/null
+++ b/nixos/tests/grafana/basic.nix
@@ -0,0 +1,114 @@
+args@{ pkgs, ... }:
+
+(import ../make-test-python.nix ({ lib, pkgs, ... }:
+
+let
+  inherit (lib) mkMerge nameValuePair maintainers;
+
+  baseGrafanaConf = {
+    services.grafana = {
+      enable = true;
+      addr = "localhost";
+      analytics.reporting.enable = false;
+      domain = "localhost";
+      security = {
+        adminUser = "testadmin";
+        adminPassword = "snakeoilpwd";
+      };
+    };
+  };
+
+  extraNodeConfs = {
+    declarativePlugins = {
+      services.grafana.declarativePlugins = [ pkgs.grafanaPlugins.grafana-clock-panel ];
+    };
+
+    postgresql = {
+      services.grafana.database = {
+        host = "127.0.0.1:5432";
+        user = "grafana";
+      };
+      services.postgresql = {
+        enable = true;
+        ensureDatabases = [ "grafana" ];
+        ensureUsers = [{
+          name = "grafana";
+          ensurePermissions."DATABASE grafana" = "ALL PRIVILEGES";
+        }];
+      };
+      systemd.services.grafana.after = [ "postgresql.service" ];
+    };
+
+    mysql = {
+      services.grafana.database.user = "grafana";
+      services.mysql = {
+        enable = true;
+        ensureDatabases = [ "grafana" ];
+        ensureUsers = [{
+          name = "grafana";
+          ensurePermissions."grafana.*" = "ALL PRIVILEGES";
+        }];
+        package = pkgs.mariadb;
+      };
+      systemd.services.grafana.after = [ "mysql.service" ];
+    };
+  };
+
+  nodes = builtins.listToAttrs (map (dbName:
+    nameValuePair dbName (mkMerge [
+    baseGrafanaConf
+    (extraNodeConfs.${dbName} or {})
+  ])) [ "sqlite" "declarativePlugins" "postgresql" "mysql" ]);
+
+in {
+  name = "grafana-basic";
+
+  meta = with maintainers; {
+    maintainers = [ willibutz ];
+  };
+
+  inherit nodes;
+
+  testScript = ''
+    start_all()
+
+    with subtest("Declarative plugins installed"):
+        declarativePlugins.wait_for_unit("grafana.service")
+        declarativePlugins.wait_for_open_port(3000)
+        declarativePlugins.succeed(
+            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/plugins | grep grafana-clock-panel"
+        )
+        declarativePlugins.shutdown()
+
+    with subtest("Successful API query as admin user with sqlite db"):
+        sqlite.wait_for_unit("grafana.service")
+        sqlite.wait_for_open_port(3000)
+        print(sqlite.succeed(
+            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/org/users -i"
+        ))
+        sqlite.succeed(
+            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/org/users | grep admin\@localhost"
+        )
+        sqlite.shutdown()
+
+    with subtest("Successful API query as admin user with postgresql db"):
+        postgresql.wait_for_unit("grafana.service")
+        postgresql.wait_for_unit("postgresql.service")
+        postgresql.wait_for_open_port(3000)
+        postgresql.wait_for_open_port(5432)
+        postgresql.succeed(
+            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/org/users | grep admin\@localhost"
+        )
+        postgresql.shutdown()
+
+    with subtest("Successful API query as admin user with mysql db"):
+        mysql.wait_for_unit("grafana.service")
+        mysql.wait_for_unit("mysql.service")
+        mysql.wait_for_open_port(3000)
+        mysql.wait_for_open_port(3306)
+        mysql.succeed(
+            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/org/users | grep admin\@localhost"
+        )
+        mysql.shutdown()
+  '';
+})) args
diff --git a/nixos/tests/grafana/default.nix b/nixos/tests/grafana/default.nix
new file mode 100644
index 0000000000000..d72fbe4e3f7d9
--- /dev/null
+++ b/nixos/tests/grafana/default.nix
@@ -0,0 +1,9 @@
+{ system ? builtins.currentSystem
+, config ? { }
+, pkgs ? import ../../.. { inherit system config; }
+}:
+
+{
+  basic = import ./basic.nix { inherit system pkgs; };
+  provision-dashboards = import ./provision-dashboards { inherit system pkgs; };
+}
diff --git a/nixos/tests/grafana/provision-dashboards/default.nix b/nixos/tests/grafana/provision-dashboards/default.nix
new file mode 100644
index 0000000000000..a0c64c8810d32
--- /dev/null
+++ b/nixos/tests/grafana/provision-dashboards/default.nix
@@ -0,0 +1,92 @@
+args@{ pkgs, ... }:
+
+(import ../../make-test-python.nix ({ lib, pkgs, ... }:
+
+let
+  inherit (lib) mkMerge nameValuePair maintainers;
+
+  baseGrafanaConf = {
+    services.grafana = {
+      enable = true;
+      addr = "localhost";
+      analytics.reporting.enable = false;
+      domain = "localhost";
+      security = {
+        adminUser = "testadmin";
+        adminPassword = "snakeoilpwd";
+      };
+      provision.enable = true;
+    };
+
+    systemd.tmpfiles.rules = [
+      "L /var/lib/grafana/dashboards/test.json 0700 grafana grafana - ${pkgs.writeText "test.json" (builtins.readFile ./test_dashboard.json)}"
+    ];
+  };
+
+  extraNodeConfs = {
+    provisionDashboardOld = {
+      services.grafana.provision = {
+        dashboards = [{ options.path = "/var/lib/grafana/dashboards"; }];
+      };
+    };
+
+    provisionDashboardNix = {
+      services.grafana.provision = {
+        dashboards.settings = {
+          apiVersion = 1;
+          providers = [{
+            name = "default";
+            options.path = "/var/lib/grafana/dashboards";
+          }];
+        };
+      };
+    };
+
+    provisionDashboardYaml = {
+      services.grafana.provision.dashboards.path = ./provision-dashboards.yaml;
+    };
+  };
+
+  nodes = builtins.listToAttrs (map (provisionType:
+    nameValuePair provisionType (mkMerge [
+    baseGrafanaConf
+    (extraNodeConfs.${provisionType} or {})
+  ])) [ "provisionDashboardOld" "provisionDashboardNix" "provisionDashboardYaml" ]);
+
+in {
+  name = "grafana-provision-dashboards";
+
+  meta = with maintainers; {
+    maintainers = [ kfears willibutz ];
+  };
+
+  inherit nodes;
+
+  testScript = ''
+    start_all()
+
+    with subtest("Successful dashboard provision with Nix (old format)"):
+        provisionDashboardOld.wait_for_unit("grafana.service")
+        provisionDashboardOld.wait_for_open_port(3000)
+        provisionDashboardOld.succeed(
+            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/dashboards/uid/test_dashboard | grep Test\ Dashboard"
+        )
+        provisionDashboardOld.shutdown()
+
+    with subtest("Successful dashboard provision with Nix (new format)"):
+        provisionDashboardNix.wait_for_unit("grafana.service")
+        provisionDashboardNix.wait_for_open_port(3000)
+        provisionDashboardNix.succeed(
+            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/dashboards/uid/test_dashboard | grep Test\ Dashboard"
+        )
+        provisionDashboardNix.shutdown()
+
+    with subtest("Successful dashboard provision with YAML"):
+        provisionDashboardYaml.wait_for_unit("grafana.service")
+        provisionDashboardYaml.wait_for_open_port(3000)
+        provisionDashboardYaml.succeed(
+            "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/dashboards/uid/test_dashboard | grep Test\ Dashboard"
+        )
+        provisionDashboardYaml.shutdown()
+  '';
+})) args
diff --git a/nixos/tests/grafana/provision-dashboards/provision-dashboards.yaml b/nixos/tests/grafana/provision-dashboards/provision-dashboards.yaml
new file mode 100644
index 0000000000000..dc83fe6b892dc
--- /dev/null
+++ b/nixos/tests/grafana/provision-dashboards/provision-dashboards.yaml
@@ -0,0 +1,6 @@
+apiVersion: 1
+
+providers:
+  - name: 'default'
+    options:
+      path: /var/lib/grafana/dashboards
diff --git a/nixos/tests/grafana/provision-dashboards/test_dashboard.json b/nixos/tests/grafana/provision-dashboards/test_dashboard.json
new file mode 100644
index 0000000000000..6e7a5b37f22b8
--- /dev/null
+++ b/nixos/tests/grafana/provision-dashboards/test_dashboard.json
@@ -0,0 +1,47 @@
+{
+  "annotations": {
+    "list": [
+      {
+        "builtIn": 1,
+        "datasource": {
+          "type": "grafana",
+          "uid": "-- Grafana --"
+        },
+        "enable": true,
+        "hide": true,
+        "iconColor": "rgba(0, 211, 255, 1)",
+        "name": "Annotations & Alerts",
+        "target": {
+          "limit": 100,
+          "matchAny": false,
+          "tags": [],
+          "type": "dashboard"
+        },
+        "type": "dashboard"
+      }
+    ]
+  },
+  "editable": true,
+  "fiscalYearStartMonth": 0,
+  "graphTooltip": 0,
+  "id": 28,
+  "links": [],
+  "liveNow": false,
+  "panels": [],
+  "schemaVersion": 37,
+  "style": "dark",
+  "tags": [],
+  "templating": {
+    "list": []
+  },
+  "time": {
+    "from": "now-6h",
+    "to": "now"
+  },
+  "timepicker": {},
+  "timezone": "",
+  "title": "Test Dashboard",
+  "uid": "test_dashboard",
+  "version": 1,
+  "weekStart": ""
+}