summaryrefslogtreecommitdiffstats
path: root/nixos/tests/nginx-etag.nix
blob: a6ced2eba537e0c4231661f6a5cb71e5357e0737 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
{ ... }:
{
  name = "nginx-etag";

  nodes = {
    server =
      { pkgs, lib, ... }:
      {
        networking.firewall.enable = false;
        services.nginx.enable = true;
        services.nginx.virtualHosts.server = {
          root = pkgs.runCommandLocal "testdir" { } ''
            mkdir "$out"
            cat > "$out/test.js" <<EOF
            document.getElementById('foobar').setAttribute('foo', 'bar');
            EOF
            cat > "$out/index.html" <<EOF
            <!DOCTYPE html>
            <div id="foobar">test</div>
            <script src="test.js"></script>
            EOF
          '';
        };

        specialisation.pass-checks.configuration = {
          services.nginx.virtualHosts.server = {
            root = lib.mkForce (
              pkgs.runCommandLocal "testdir2" { } ''
                mkdir "$out"
                cat > "$out/test.js" <<EOF
                document.getElementById('foobar').setAttribute('foo', 'yay');
                EOF
                cat > "$out/index.html" <<EOF
                <!DOCTYPE html>
                <div id="foobar">test</div>
                <script src="test.js"></script>
                EOF
              ''
            );
          };
        };
      };

    client =
      { pkgs, lib, ... }:
      {
        environment.systemPackages =
          let
            testRunner =
              pkgs.writers.writePython3Bin "test-runner"
                {
                  libraries = [ pkgs.python3Packages.selenium ];
                }
                ''
                  import os
                  import shutil
                  import time

                  from selenium.webdriver import Firefox
                  from selenium.webdriver.firefox.options import Options
                  from selenium.webdriver.firefox.service import Service

                  service = Service(shutil.which("geckodriver"))

                  options = Options()
                  options.add_argument('--headless')
                  driver = Firefox(options=options, service=service)

                  driver.implicitly_wait(20)
                  driver.get('http://server/')
                  driver.find_element('xpath', '//div[@foo="bar"]')
                  open('/tmp/passed_stage1', 'w')

                  while not os.path.exists('/tmp/proceed'):
                      time.sleep(0.5)

                  driver.get('http://server/')
                  driver.find_element('xpath', '//div[@foo="yay"]')
                  open('/tmp/passed', 'w')
                '';
          in
          [
            pkgs.firefox-unwrapped
            pkgs.geckodriver
            testRunner
          ];
      };
  };

  testScript =
    { nodes, ... }:
    let
      inherit (nodes.server.system.build) toplevel;
      newSystem = "${toplevel}/specialisation/pass-checks";
    in
    ''
      start_all()

      server.wait_for_unit("nginx.service")
      client.wait_for_unit("multi-user.target")
      client.execute("test-runner >&2 &")
      client.wait_for_file("/tmp/passed_stage1")

      server.succeed(
          "${newSystem}/bin/switch-to-configuration test >&2"
      )
      client.succeed("touch /tmp/proceed")

      client.wait_for_file("/tmp/passed")
    '';
}