Skip to content

Commit d67d350

Browse files
gourav-kandoriaac000
authored andcommitted
tests: Add tests for python application factories
Add the following tests cases: 1. When "factory" key is used inside the "targets" option. 2. When "factory" key is used at the root level of python application config. 3. When factory returns invalid callable or When factory is invalid callable Link: <#1336> [ Commit subject & message formatting tweaks - Andrew ] Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
1 parent a9aa9e7 commit d67d350

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed

test/python/factory/wsgi.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def wsgi_a(env, start_response):
2+
start_response("200", [("Content-Length", "1")])
3+
return [b"1"]
4+
5+
6+
def wsgi_b(env, start_response):
7+
start_response("200", [("Content-Length", "1")])
8+
return [b"2"]
9+
10+
11+
def wsgi_a_factory():
12+
return wsgi_a
13+
14+
15+
def wsgi_b_factory():
16+
return wsgi_b
17+
18+
19+
wsgi_invalid_callable = None
20+
21+
22+
def wsgi_factory_returning_invalid_callable():
23+
return wsgi_invalid_callable

test/test_python_factory.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
from unit.applications.lang.python import ApplicationPython
2+
from unit.option import option
3+
4+
prerequisites = {"modules": {"python": "all"}}
5+
6+
client = ApplicationPython()
7+
8+
9+
def test_python_factory_targets():
10+
python_dir = f"{option.test_dir}/python"
11+
12+
assert "success" in client.conf(
13+
{
14+
"listeners": {
15+
"*:8080": {"pass": "applications/targets/1"},
16+
"*:8081": {"pass": "applications/targets/2"},
17+
"*:8082": {"pass": "applications/targets/factory-1"},
18+
"*:8083": {"pass": "applications/targets/factory-2"},
19+
},
20+
"applications": {
21+
"targets": {
22+
"type": client.get_application_type(),
23+
"working_directory": f"{python_dir}/factory/",
24+
"path": f"{python_dir}/factory/",
25+
"targets": {
26+
"1": {
27+
"module": "wsgi",
28+
"callable": "wsgi_a",
29+
"factory": False,
30+
},
31+
"2": {
32+
"module": "wsgi",
33+
"callable": "wsgi_b",
34+
"factory": False,
35+
},
36+
"factory-1": {
37+
"module": "wsgi",
38+
"callable": "wsgi_a_factory",
39+
"factory": True,
40+
},
41+
"factory-2": {
42+
"module": "wsgi",
43+
"callable": "wsgi_b_factory",
44+
"factory": True,
45+
},
46+
},
47+
}
48+
},
49+
}
50+
)
51+
52+
resp = client.get(port=8080)
53+
assert resp["status"] == 200
54+
assert resp["body"] == "1"
55+
56+
resp = client.get(port=8081)
57+
assert resp["status"] == 200
58+
assert resp["body"] == "2"
59+
60+
resp = client.get(port=8082)
61+
assert resp["status"] == 200
62+
assert resp["body"] == "1"
63+
64+
resp = client.get(port=8083)
65+
assert resp["status"] == 200
66+
assert resp["body"] == "2"
67+
68+
69+
def test_python_factory_without_targets():
70+
python_dir = f"{option.test_dir}/python"
71+
72+
assert "success" in client.conf(
73+
{
74+
"listeners": {
75+
"*:8080": {"pass": "applications/python-app-factory"},
76+
"*:8081": {"pass": "applications/python-app"},
77+
},
78+
"applications": {
79+
"python-app-factory": {
80+
"type": client.get_application_type(),
81+
"working_directory": f"{python_dir}/factory/",
82+
"path": f"{python_dir}/factory/",
83+
"module": "wsgi",
84+
"callable": "wsgi_a_factory",
85+
"factory": True,
86+
},
87+
"python-app": {
88+
"type": client.get_application_type(),
89+
"working_directory": f"{python_dir}/factory/",
90+
"path": f"{python_dir}/factory/",
91+
"module": "wsgi",
92+
"callable": "wsgi_b",
93+
"factory": False,
94+
},
95+
},
96+
}
97+
)
98+
99+
resp = client.get(port=8080)
100+
assert resp["status"] == 200
101+
assert resp["body"] == "1"
102+
103+
resp = client.get(port=8081)
104+
assert resp["status"] == 200
105+
assert resp["body"] == "2"
106+
107+
108+
def test_python_factory_invalid_callable_value(skip_alert):
109+
skip_alert(
110+
r"failed to apply new conf",
111+
r"did not return callable object",
112+
r"can not be called to fetch callable",
113+
)
114+
python_dir = f"{option.test_dir}/python"
115+
116+
invalid_callable_values = [
117+
"wsgi_factory_returning_invalid_callable",
118+
"wsgi_invalid_callable",
119+
]
120+
121+
for callable_value in invalid_callable_values:
122+
assert "error" in client.conf(
123+
{
124+
"listeners": {"*:8080": {"pass": "applications/targets/1"}},
125+
"applications": {
126+
"targets": {
127+
"type": client.get_application_type(),
128+
"working_directory": f"{python_dir}/factory/",
129+
"path": f"{python_dir}/factory/",
130+
"targets": {
131+
"1": {
132+
"module": "wsgi",
133+
"callable": callable_value,
134+
"factory": True,
135+
},
136+
},
137+
}
138+
},
139+
}
140+
)

0 commit comments

Comments
 (0)