ahoy: init at 2.2.0 (#366784)
[NixPkgs.git] / nixos / tests / soft-serve.nix
blob1fa365c93ef1474f66f6dd294f6efbe8382133d8
1 import ./make-test-python.nix (
2   { pkgs, lib, ... }:
3   let
4     inherit (import ./ssh-keys.nix pkgs) snakeOilPrivateKey snakeOilPublicKey;
5     sshPort = 8231;
6     httpPort = 8232;
7     statsPort = 8233;
8     gitPort = 8418;
9   in
10   {
11     name = "soft-serve";
12     meta.maintainers = with lib.maintainers; [ dadada ];
13     nodes = {
14       client =
15         { pkgs, ... }:
16         {
17           environment.systemPackages = with pkgs; [
18             curl
19             git
20             openssh
21           ];
22           environment.etc.sshKey = {
23             source = snakeOilPrivateKey;
24             mode = "0600";
25           };
26         };
28       server =
29         { config, ... }:
30         {
31           services.soft-serve = {
32             enable = true;
33             settings = {
34               name = "TestServer";
35               ssh.listen_addr = ":${toString sshPort}";
36               git.listen_addr = ":${toString gitPort}";
37               http.listen_addr = ":${toString httpPort}";
38               stats.listen_addr = ":${toString statsPort}";
39               initial_admin_keys = [ snakeOilPublicKey ];
40             };
41           };
42           networking.firewall.allowedTCPPorts = [
43             sshPort
44             httpPort
45             statsPort
46           ];
47         };
48     };
50     testScript =
51       { ... }:
52       ''
53         SSH_PORT = ${toString sshPort}
54         HTTP_PORT = ${toString httpPort}
55         STATS_PORT = ${toString statsPort}
56         KEY = "${snakeOilPublicKey}"
57         SSH_KEY = "/etc/sshKey"
58         SSH_COMMAND = f"ssh -p {SSH_PORT} -i {SSH_KEY} -o StrictHostKeyChecking=no"
59         TEST_DIR = "/tmp/test"
60         GIT = f"git -C {TEST_DIR}"
62         for machine in client, server:
63             machine.wait_for_unit("network.target")
65         server.wait_for_unit("soft-serve.service")
66         server.wait_for_open_port(SSH_PORT)
68         with subtest("Get info"):
69             status, test = client.execute(f"{SSH_COMMAND} server info")
70             if status != 0:
71                 raise Exception("Failed to get SSH info")
72             key = " ".join(KEY.split(" ")[0:2])
73             if not key in test:
74                 raise Exception("Admin key must be configured correctly")
76         with subtest("Create user"):
77             client.succeed(f"{SSH_COMMAND} server user create beatrice")
78             client.succeed(f"{SSH_COMMAND} server user info beatrice")
80         with subtest("Create repo"):
81             client.succeed(f"git init {TEST_DIR}")
82             client.succeed(f"{GIT} config --global user.email you@example.com")
83             client.succeed(f"touch {TEST_DIR}/foo")
84             client.succeed(f"{GIT} add foo")
85             client.succeed(f"{GIT} commit --allow-empty -m test")
86             client.succeed(f"{GIT} remote add origin git@server:test")
87             client.succeed(f"GIT_SSH_COMMAND='{SSH_COMMAND}' {GIT} push -u origin master")
88             client.execute("rm -r /tmp/test")
90         server.wait_for_open_port(HTTP_PORT)
92         with subtest("Clone over HTTP"):
93             client.succeed(f"curl --connect-timeout 10 http://server:{HTTP_PORT}/")
94             client.succeed(f"git clone http://server:{HTTP_PORT}/test /tmp/test")
95             client.execute("rm -r /tmp/test")
97         with subtest("Clone over SSH"):
98             client.succeed(f"GIT_SSH_COMMAND='{SSH_COMMAND}' git clone git@server:test /tmp/test")
99             client.execute("rm -r /tmp/test")
101         with subtest("Get stats over HTTP"):
102             server.wait_for_open_port(STATS_PORT)
103             status, test = client.execute(f"curl --connect-timeout 10 http://server:{STATS_PORT}/metrics")
104             if status != 0:
105                 raise Exception("Failed to get metrics from status port")
106             if not "go_gc_duration_seconds_count" in test:
107                 raise Exception("Metrics did not contain key 'go_gc_duration_seconds_count'")
108       '';
109   }