about summary refs log tree commit diff
path: root/nixos/tests/caddy.nix
blob: ab9d2fbf4d1d6174db558a96e9d9bdbaff34c941 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import ./make-test.nix ({ pkgs, ... }: {
  name = "caddy";
  meta = with pkgs.stdenv.lib.maintainers; {
    maintainers = [ xfix ];
  };

  nodes = {
    webserver = { pkgs, lib, ... }: {
      services.caddy.enable = true;
      services.caddy.config = ''
        http://localhost {
          gzip

          root ${
            pkgs.runCommand "testdir" {} ''
              mkdir "$out"
              echo hello world > "$out/example.html"
            ''
          }
        }
      '';

      nesting.clone = [
        {
          services.caddy.config = lib.mkForce ''
            http://localhost {
              gzip

              root ${
                pkgs.runCommand "testdir2" {} ''
                  mkdir "$out"
                  echo changed > "$out/example.html"
                ''
              }
            }
          '';
        }

        {
          services.caddy.config = ''
            http://localhost:8080 {
            }
          '';
        }
      ];
    };
  };

  testScript = { nodes, ... }: let
    etagSystem = "${nodes.webserver.config.system.build.toplevel}/fine-tune/child-1";
    justReloadSystem = "${nodes.webserver.config.system.build.toplevel}/fine-tune/child-2";
  in ''
    my $url = 'http://localhost/example.html';
    $webserver->waitForUnit("caddy");
    $webserver->waitForOpenPort("80");

    sub checkEtag {
      my $etag = $webserver->succeed(
        'curl -v '.$url.' 2>&1 | sed -n -e "s/^< [Ee][Tt][Aa][Gg]: *//p"'
      );
      $etag =~ s/\r?\n$//;
      my $httpCode = $webserver->succeed(
        'curl -w "%{http_code}" -X HEAD -H \'If-None-Match: '.$etag.'\' '.$url
      );
      die "HTTP code is not 304" unless $httpCode == 304;
      return $etag;
    }

    subtest "check ETag if serving Nix store paths", sub {
      my $oldEtag = checkEtag;
      $webserver->succeed("${etagSystem}/bin/switch-to-configuration test >&2");
      $webserver->sleep(1); # race condition
      my $newEtag = checkEtag;
      die "Old ETag $oldEtag is the same as $newEtag" if $oldEtag eq $newEtag;
    };

    subtest "config is reloaded on nixos-rebuild switch", sub {
      $webserver->succeed("${justReloadSystem}/bin/switch-to-configuration test >&2");
      $webserver->waitForOpenPort("8080");
    };
  '';
})