summaryrefslogtreecommitdiffstats
path: root/nixos/tests/spacecookie.nix
blob: 5e7de52e747e08092e3b3776a5049a7dafe6fd1c (plain)
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
let
  gopherRoot = "/tmp/gopher";
  gopherHost = "gopherd";
  gopherClient = "client";
  fileContent = "Hello Gopher!\n";
  fileName = "file.txt";
in
{ ... }:
{
  name = "spacecookie";
  nodes = {
    ${gopherHost} = {
      systemd.services.spacecookie = {
        preStart = ''
          mkdir -p ${gopherRoot}/directory
          printf "%s" "${fileContent}" > ${gopherRoot}/${fileName}
        '';
      };

      services.spacecookie = {
        enable = true;
        openFirewall = true;
        settings = {
          root = gopherRoot;
          hostname = gopherHost;
        };
      };
    };

    ${gopherClient} = { };
  };

  testScript = ''
    start_all()

    # with daemon type notify, the unit being started
    # should also mean the port is open
    ${gopherHost}.wait_for_unit("spacecookie.service")
    ${gopherClient}.wait_for_unit("network.target")

    fileResponse = ${gopherClient}.succeed("curl -f -s gopher://${gopherHost}/0/${fileName}")

    # the file response should return our created file exactly
    if not (fileResponse == "${builtins.replaceStrings [ "\n" ] [ "\\n" ] fileContent}"):
        raise Exception("Unexpected file response")

    # sanity check on the directory listing: we serve a directory and a file
    # via gopher, so the directory listing should have exactly two entries,
    # one with gopher file type 0 (file) and one with file type 1 (directory).
    # note that the menu is CRLF-terminated and ends with a "." line
    # which is not a directory entry, but the end of the response.
    dirResponse = ${gopherClient}.succeed("curl -f -s gopher://${gopherHost}")
    dirEntries = [l[0] for l in dirResponse.split("\r\n") if len(l) > 0 and l != "."]
    dirEntries.sort()

    if not (["0", "1"] == dirEntries):
        raise Exception("Unexpected directory response")
  '';
}