summaryrefslogtreecommitdiffhomepage
path: root/test/test_python_factory.py
blob: d1752c24864ae2d53866ee8c6ca14bd50ce44f02 (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
from unit.applications.lang.python import ApplicationPython
from unit.option import option

prerequisites = {"modules": {"python": "all"}}

client = ApplicationPython()


def test_python_factory_targets():
    python_dir = f"{option.test_dir}/python"

    assert "success" in client.conf(
        {
            "listeners": {
                "*:8080": {"pass": "applications/targets/1"},
                "*:8081": {"pass": "applications/targets/2"},
                "*:8082": {"pass": "applications/targets/factory-1"},
                "*:8083": {"pass": "applications/targets/factory-2"},
            },
            "applications": {
                "targets": {
                    "type": client.get_application_type(),
                    "working_directory": f"{python_dir}/factory/",
                    "path": f"{python_dir}/factory/",
                    "targets": {
                        "1": {
                            "module": "wsgi",
                            "callable": "wsgi_a",
                            "factory": False,
                        },
                        "2": {
                            "module": "wsgi",
                            "callable": "wsgi_b",
                            "factory": False,
                        },
                        "factory-1": {
                            "module": "wsgi",
                            "callable": "wsgi_a_factory",
                            "factory": True,
                        },
                        "factory-2": {
                            "module": "wsgi",
                            "callable": "wsgi_b_factory",
                            "factory": True,
                        },
                    },
                }
            },
        }
    )

    resp = client.get(port=8080)
    assert resp["status"] == 200
    assert resp["body"] == "1"

    resp = client.get(port=8081)
    assert resp["status"] == 200
    assert resp["body"] == "2"

    resp = client.get(port=8082)
    assert resp["status"] == 200
    assert resp["body"] == "1"

    resp = client.get(port=8083)
    assert resp["status"] == 200
    assert resp["body"] == "2"


def test_python_factory_without_targets():
    python_dir = f"{option.test_dir}/python"

    assert "success" in client.conf(
        {
            "listeners": {
                "*:8080": {"pass": "applications/python-app-factory"},
                "*:8081": {"pass": "applications/python-app"},
            },
            "applications": {
                "python-app-factory": {
                    "type": client.get_application_type(),
                    "working_directory": f"{python_dir}/factory/",
                    "path": f"{python_dir}/factory/",
                    "module": "wsgi",
                    "callable": "wsgi_a_factory",
                    "factory": True,
                },
                "python-app": {
                    "type": client.get_application_type(),
                    "working_directory": f"{python_dir}/factory/",
                    "path": f"{python_dir}/factory/",
                    "module": "wsgi",
                    "callable": "wsgi_b",
                    "factory": False,
                },
            },
        }
    )

    resp = client.get(port=8080)
    assert resp["status"] == 200
    assert resp["body"] == "1"

    resp = client.get(port=8081)
    assert resp["status"] == 200
    assert resp["body"] == "2"


def test_python_factory_invalid_callable_value(skip_alert):
    skip_alert(
        r"failed to apply new conf",
        r"did not return callable object",
        r"can not be called to fetch callable",
    )
    python_dir = f"{option.test_dir}/python"

    invalid_callable_values = [
        "wsgi_factory_returning_invalid_callable",
        "wsgi_invalid_callable",
    ]

    for callable_value in invalid_callable_values:
        assert "error" in client.conf(
            {
                "listeners": {"*:8080": {"pass": "applications/targets/1"}},
                "applications": {
                    "targets": {
                        "type": client.get_application_type(),
                        "working_directory": f"{python_dir}/factory/",
                        "path": f"{python_dir}/factory/",
                        "targets": {
                            "1": {
                                "module": "wsgi",
                                "callable": callable_value,
                                "factory": True,
                            },
                        },
                    }
                },
            }
        )