summaryrefslogtreecommitdiffstats
path: root/nixos/tests/uwsgi.nix
blob: c79f600faced090fa08d66f34b51a3ba5a296de2 (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
{ pkgs, ... }:
{
  name = "uwsgi";
  meta = {
    maintainers = [ ];
  };

  nodes.machine =
    { pkgs, ... }:
    {
      users.users.hello = {
        isSystemUser = true;
        group = "hello";
      };
      users.groups.hello = { };

      services.uwsgi = {
        enable = true;
        plugins = [
          "python3"
          "php"
        ];
        capabilities = [ "CAP_NET_BIND_SERVICE" ];
        instance.type = "emperor";

        instance.vassals.hello = {
          type = "normal";
          immediate-uid = "hello";
          immediate-gid = "hello";
          module = "wsgi:application";
          http = ":80";
          cap = "net_bind_service";
          pythonPackages = self: [ self.flask ];
          chdir = pkgs.writeTextDir "wsgi.py" ''
            from flask import Flask
            import subprocess
            application = Flask(__name__)

            @application.route("/")
            def hello():
                return "Hello, World!"

            @application.route("/whoami")
            def whoami():
                whoami = "${pkgs.coreutils}/bin/whoami"
                proc = subprocess.run(whoami, capture_output=True)
                return proc.stdout.decode().strip()
          '';
        };

        instance.vassals.php = {
          type = "normal";
          master = true;
          workers = 2;
          http-socket = ":8000";
          http-socket-modifier1 = 14;
          php-index = "index.php";
          php-docroot = pkgs.writeTextDir "index.php" ''
            <?php echo "Hello World\n"; ?>
          '';
        };
      };
    };

  testScript = ''
    machine.wait_for_unit("multi-user.target")
    machine.wait_for_unit("uwsgi.service")

    with subtest("uWSGI has started"):
        machine.wait_for_unit("uwsgi.service")

    with subtest("Vassal can bind on port <1024"):
        machine.wait_for_open_port(80)
        hello = machine.succeed("curl -f http://machine").strip()
        assert "Hello, World!" in hello, f"Excepted 'Hello, World!', got '{hello}'"

    with subtest("Vassal is running as dedicated user"):
        username = machine.succeed("curl -f http://machine/whoami").strip()
        assert username == "hello", f"Excepted 'hello', got '{username}'"

    with subtest("PHP plugin is working"):
        machine.wait_for_open_port(8000)
        assert "Hello World" in machine.succeed("curl -fv http://machine:8000")
  '';
}