vuls: init at 0.27.0 (#348530)
[NixPkgs.git] / nixos / tests / webhook.nix
blobed70514086405ae472a6b5f990129698014c8a74
1 { pkgs, ... }:
2 let
3   forwardedPort = 19000;
4   internalPort = 9000;
5 in
7   name = "webhook";
9   nodes = {
10     webhookMachine = { pkgs, ... }: {
11       virtualisation.forwardPorts = [{
12         host.port = forwardedPort;
13         guest.port = internalPort;
14       }];
15       services.webhook = {
16         enable = true;
17         port = internalPort;
18         openFirewall = true;
19         hooks = {
20           echo = {
21             execute-command = "echo";
22             response-message = "Webhook is reachable!";
23           };
24         };
25         hooksTemplated = {
26           echoTemplate = ''
27             {
28               "id": "echo-template",
29               "execute-command": "echo",
30               "response-message": "{{ getenv "WEBHOOK_MESSAGE" }}"
31             }
32           '';
33         };
34         environment.WEBHOOK_MESSAGE = "Templates are working!";
35       };
36     };
37   };
39   extraPythonPackages = p: [
40     p.requests
41     p.types-requests
42   ];
44   testScript = { nodes, ... }: ''
45     import requests
46     webhookMachine.wait_for_unit("webhook")
47     webhookMachine.wait_for_open_port(${toString internalPort})
49     with subtest("Check that webhooks can be called externally"):
50       response = requests.get("http://localhost:${toString forwardedPort}/hooks/echo")
51       print(f"Response code: {response.status_code}")
52       print("Response: %r" % response.content)
54       assert response.status_code == 200
55       assert response.content == b"Webhook is reachable!"
57     with subtest("Check that templated webhooks can be called externally"):
58       response = requests.get("http://localhost:${toString forwardedPort}/hooks/echo-template")
59       print(f"Response code: {response.status_code}")
60       print("Response: %r" % response.content)
62       assert response.status_code == 200
63       assert response.content == b"Templates are working!"
64   '';