about summary refs log tree commit diff
path: root/nixos/tests/angie-api.nix
blob: 4c8d6b54247b18a8dc4736c83b20928d4c545082 (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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import ./make-test-python.nix ({lib, pkgs, ...}:
let
  hosts = ''
    192.168.2.101 example.com
    192.168.2.101 api.example.com
    192.168.2.101 backend.example.com
  '';

in
{
  name = "angie-api";
  meta.maintainers = with pkgs.lib.maintainers; [ izorkin ];

  nodes = {
    server = { pkgs, ... }: {
      networking = {
        interfaces.eth1 = {
          ipv4.addresses = [
            { address = "192.168.2.101"; prefixLength = 24; }
          ];
        };
        extraHosts = hosts;
        firewall.allowedTCPPorts = [ 80 ];
      };

      services.nginx = {
        enable = true;
        package = pkgs.angie;

        upstreams = {
          "backend-http" = {
            servers = { "backend.example.com:8080" = { fail_timeout = "0"; }; };
            extraConfig = ''
              zone upstream 256k;
            '';
          };
          "backend-socket" = {
            servers = { "unix:/run/example.sock" = { fail_timeout = "0"; }; };
            extraConfig = ''
              zone upstream 256k;
            '';
          };
        };

        virtualHosts."api.example.com" = {
          locations."/console/" = {
            extraConfig = ''
              api /status/;

              allow 192.168.2.201;
              deny all;
            '';
          };
        };

        virtualHosts."example.com" = {
          locations."/test/" = {
            root = lib.mkForce (pkgs.runCommandLocal "testdir" {} ''
              mkdir -p "$out/test"
              cat > "$out/test/index.html" <<EOF
              <html><body>Hello World!</body></html>
              EOF
            '');
            extraConfig = ''
              status_zone test_zone;

              allow 192.168.2.201;
              deny all;
            '';
          };
          locations."/test/locked/" = {
            extraConfig = ''
              status_zone test_zone;

              deny all;
            '';
          };
          locations."/test/error/" = {
            extraConfig = ''
              status_zone test_zone;

              allow all;
            '';
          };
          locations."/upstream-http/" = {
            proxyPass = "http://backend-http";
          };
          locations."/upstream-socket/" = {
            proxyPass = "http://backend-socket";
          };
        };
      };
    };

    client = { pkgs, ... }: {
      environment.systemPackages = [ pkgs.jq ];
      networking = {
        interfaces.eth1 = {
          ipv4.addresses = [
            { address = "192.168.2.201"; prefixLength = 24; }
          ];
        };
        extraHosts = hosts;
      };
    };
  };

  testScript = ''
    start_all()

    server.wait_for_unit("nginx")
    server.wait_for_open_port(80)

    # Check Angie version
    client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.angie.version' | grep '${pkgs.angie.version}'")

    # Check access
    client.succeed("curl --verbose --head http://api.example.com/console/ | grep 'HTTP/1.1 200'")
    server.succeed("curl --verbose --head http://api.example.com/console/ | grep 'HTTP/1.1 403 Forbidden'")

    # Check responses and requests
    client.succeed("curl --verbose http://example.com/test/")
    client.succeed("curl --verbose http://example.com/test/locked/")
    client.succeed("curl --verbose http://example.com/test/locked/")
    client.succeed("curl --verbose http://example.com/test/error/")
    client.succeed("curl --verbose http://example.com/test/error/")
    client.succeed("curl --verbose http://example.com/test/error/")
    server.succeed("curl --verbose http://example.com/test/")
    client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.location_zones.test_zone.responses.\"200\"' | grep '1'")
    client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.location_zones.test_zone.responses.\"403\"' | grep '3'")
    client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.location_zones.test_zone.responses.\"404\"' | grep '3'")
    client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.location_zones.test_zone.requests.total' | grep '7'")

    # Check upstreams
    client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-http\".peers.\"192.168.2.101:8080\".state' | grep 'up'")
    client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-http\".peers.\"192.168.2.101:8080\".health.fails' | grep '0'")
    client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-socket\".peers.\"unix:/run/example.sock\".state' | grep 'up'")
    client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-socket\".peers.\"unix:/run/example.sock\".health.fails' | grep '0'")
    client.succeed("curl --verbose http://example.com/upstream-http/")
    client.succeed("curl --verbose http://example.com/upstream-socket/")
    client.succeed("curl --verbose http://example.com/upstream-socket/")
    client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-http\".peers.\"192.168.2.101:8080\".health.fails' | grep '1'")
    client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-socket\".peers.\"unix:/run/example.sock\".health.fails' | grep '2'")

    server.shutdown()
    client.shutdown()
  '';
})