1import ./make-test-python.nix ({ pkgs, ... }:
2{
3 name = "uwsgi";
4 meta = with pkgs.lib.maintainers; {
5 maintainers = [ lnl7 ];
6 };
7
8 nodes.machine = { pkgs, ... }: {
9 users.users.hello =
10 { isSystemUser = true;
11 group = "hello";
12 };
13 users.groups.hello = { };
14
15 services.uwsgi = {
16 enable = true;
17 plugins = [ "python3" "php" ];
18 capabilities = [ "CAP_NET_BIND_SERVICE" ];
19 instance.type = "emperor";
20
21 instance.vassals.hello = {
22 type = "normal";
23 immediate-uid = "hello";
24 immediate-gid = "hello";
25 module = "wsgi:application";
26 http = ":80";
27 cap = "net_bind_service";
28 pythonPackages = self: [ self.flask ];
29 chdir = pkgs.writeTextDir "wsgi.py" ''
30 from flask import Flask
31 import subprocess
32 application = Flask(__name__)
33
34 @application.route("/")
35 def hello():
36 return "Hello, World!"
37
38 @application.route("/whoami")
39 def whoami():
40 whoami = "${pkgs.coreutils}/bin/whoami"
41 proc = subprocess.run(whoami, capture_output=True)
42 return proc.stdout.decode().strip()
43 '';
44 };
45
46 instance.vassals.php = {
47 type = "normal";
48 master = true;
49 workers = 2;
50 http-socket = ":8000";
51 http-socket-modifier1 = 14;
52 php-index = "index.php";
53 php-docroot = pkgs.writeTextDir "index.php" ''
54 <?php echo "Hello World\n"; ?>
55 '';
56 };
57 };
58 };
59
60 testScript =
61 ''
62 machine.wait_for_unit("multi-user.target")
63 machine.wait_for_unit("uwsgi.service")
64
65 with subtest("uWSGI has started"):
66 machine.wait_for_unit("uwsgi.service")
67
68 with subtest("Vassal can bind on port <1024"):
69 machine.wait_for_open_port(80)
70 hello = machine.succeed("curl -f http://machine").strip()
71 assert "Hello, World!" in hello, f"Excepted 'Hello, World!', got '{hello}'"
72
73 with subtest("Vassal is running as dedicated user"):
74 username = machine.succeed("curl -f http://machine/whoami").strip()
75 assert username == "hello", f"Excepted 'hello', got '{username}'"
76
77 with subtest("PHP plugin is working"):
78 machine.wait_for_open_port(8000)
79 assert "Hello World" in machine.succeed("curl -fv http://machine:8000")
80 '';
81})