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