about summary refs log tree commit diff
path: root/nixos/tests/prometheus/pushgateway.nix
blob: 7904c8bf45b040c99f8335373519272a39af4df8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import ../make-test-python.nix ({ lib, pkgs, ... }:

{
  name = "prometheus-pushgateway";

  nodes = {
    prometheus = { config, pkgs, ... }: {
      environment.systemPackages = [ pkgs.jq ];

      networking.firewall.allowedTCPPorts = [ config.services.prometheus.port ];

      services.prometheus = {
        enable = true;
        globalConfig.scrape_interval = "2s";

        scrapeConfigs = [
          {
            job_name = "pushgateway";
            static_configs = [
              {
                targets = [
                  "pushgateway:9091"
                ];
              }
            ];
          }
        ];
      };
    };

    pushgateway = { config, pkgs, ... }: {
      networking.firewall.allowedTCPPorts = [ 9091 ];

      services.prometheus.pushgateway = {
        enable = true;
      };
    };

    client = { config, pkgs, ... }: {
    };
  };

  testScript = ''
    pushgateway.wait_for_unit("pushgateway")
    pushgateway.wait_for_open_port(9091)
    pushgateway.wait_until_succeeds("curl -s http://127.0.0.1:9091/-/ready")
    pushgateway.wait_until_succeeds("journalctl -o cat -u pushgateway.service | grep 'version=${pkgs.prometheus-pushgateway.version}'")

    prometheus.wait_for_unit("prometheus")
    prometheus.wait_for_open_port(9090)

    prometheus.wait_until_succeeds(
      "curl -sf 'http://127.0.0.1:9090/api/v1/query?query=count(up\{job=\"pushgateway\"\})' | "
      + "jq '.data.result[0].value[1]' | grep '\"1\"'"
    )

    prometheus.wait_until_succeeds(
      "curl -sf 'http://127.0.0.1:9090/api/v1/query?query=sum(pushgateway_build_info)%20by%20(version)' | "
      + "jq '.data.result[0].metric.version' | grep '\"${pkgs.prometheus-pushgateway.version}\"'"
    )

    client.wait_for_unit("network-online.target")

    # Add a metric and check in Prometheus
    client.wait_until_succeeds(
      "echo 'some_metric 3.14' | curl --data-binary @- http://pushgateway:9091/metrics/job/some_job"
    )

    prometheus.wait_until_succeeds(
      "curl -sf 'http://127.0.0.1:9090/api/v1/query?query=some_metric' | "
      + "jq '.data.result[0].value[1]' | grep '\"3.14\"'"
    )

    prometheus.wait_until_succeeds(
      "curl -sf 'http://127.0.0.1:9090/api/v1/query?query=absent(some_metric)' | "
      + "jq '.data.result[0].value[1]' | grep 'null'"
    )

    # Delete the metric, check not in Prometheus
    client.wait_until_succeeds(
      "curl -X DELETE http://pushgateway:9091/metrics/job/some_job"
    )

    prometheus.wait_until_fails(
      "curl -sf 'http://127.0.0.1:9090/api/v1/query?query=some_metric' | "
      + "jq '.data.result[0].value[1]' | grep '\"3.14\"'"
    )

    prometheus.wait_until_succeeds(
      "curl -sf 'http://127.0.0.1:9090/api/v1/query?query=absent(some_metric)' | "
      + "jq '.data.result[0].value[1]' | grep '\"1\"'"
    )
  '';
})