about summary refs log tree commit diff
path: root/nixpkgs/nixos/tests/webhook.nix
blob: ed70514086405ae472a6b5f990129698014c8a74 (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
{ pkgs, ... }:
let
  forwardedPort = 19000;
  internalPort = 9000;
in
{
  name = "webhook";

  nodes = {
    webhookMachine = { pkgs, ... }: {
      virtualisation.forwardPorts = [{
        host.port = forwardedPort;
        guest.port = internalPort;
      }];
      services.webhook = {
        enable = true;
        port = internalPort;
        openFirewall = true;
        hooks = {
          echo = {
            execute-command = "echo";
            response-message = "Webhook is reachable!";
          };
        };
        hooksTemplated = {
          echoTemplate = ''
            {
              "id": "echo-template",
              "execute-command": "echo",
              "response-message": "{{ getenv "WEBHOOK_MESSAGE" }}"
            }
          '';
        };
        environment.WEBHOOK_MESSAGE = "Templates are working!";
      };
    };
  };

  extraPythonPackages = p: [
    p.requests
    p.types-requests
  ];

  testScript = { nodes, ... }: ''
    import requests
    webhookMachine.wait_for_unit("webhook")
    webhookMachine.wait_for_open_port(${toString internalPort})

    with subtest("Check that webhooks can be called externally"):
      response = requests.get("http://localhost:${toString forwardedPort}/hooks/echo")
      print(f"Response code: {response.status_code}")
      print("Response: %r" % response.content)

      assert response.status_code == 200
      assert response.content == b"Webhook is reachable!"

    with subtest("Check that templated webhooks can be called externally"):
      response = requests.get("http://localhost:${toString forwardedPort}/hooks/echo-template")
      print(f"Response code: {response.status_code}")
      print("Response: %r" % response.content)

      assert response.status_code == 200
      assert response.content == b"Templates are working!"
  '';
}