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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
import time
from unit.applications.lang.python import ApplicationPython
from unit.option import option
from unit.status import Status
prerequisites = {'modules': {'python': 'any'}}
client = ApplicationPython()
def check_connections(accepted, active, idle, closed):
assert Status.get('/connections') == {
'accepted': accepted,
'active': active,
'idle': idle,
'closed': closed,
}
def app_default(name="empty", module="wsgi"):
name_dir = f'{option.test_dir}/python/{name}'
return {
"type": client.get_application_type(),
"processes": {"spare": 0},
"path": name_dir,
"working_directory": name_dir,
"module": module,
}
def test_status():
assert 'error' in client.conf_delete('/status'), 'DELETE method'
def test_status_requests(skip_alert):
skip_alert(r'Python failed to import module "blah"')
assert 'success' in client.conf(
{
"listeners": {
"*:7080": {"pass": "routes"},
"*:7081": {"pass": "applications/empty"},
"*:7082": {"pass": "applications/blah"},
},
"routes": [{"action": {"return": 200}}],
"applications": {
"empty": app_default(),
"blah": {
"type": client.get_application_type(),
"processes": {"spare": 0},
"module": "blah",
},
},
},
)
Status.init()
assert client.get()['status'] == 200
assert Status.get('/requests/total') == 1, '2xx'
assert client.get(port=7081)['status'] == 200
assert Status.get('/requests/total') == 2, '2xx app'
assert (
client.get(headers={'Host': '/', 'Connection': 'close'})['status']
== 400
)
assert Status.get('/requests/total') == 3, '4xx'
assert client.get(port=7082)['status'] == 503
assert Status.get('/requests/total') == 4, '5xx'
client.http(
b"""GET / HTTP/1.1
Host: localhost
GET / HTTP/1.1
Host: localhost
Connection: close
""",
raw=True,
)
assert Status.get('/requests/total') == 6, 'pipeline'
sock = client.get(port=7081, no_recv=True)
time.sleep(1)
assert Status.get('/requests/total') == 7, 'no receive'
sock.close()
def test_status_connections():
assert 'success' in client.conf(
{
"listeners": {
"*:7080": {"pass": "routes"},
"*:7081": {"pass": "applications/delayed"},
},
"routes": [{"action": {"return": 200}}],
"applications": {
"delayed": app_default("delayed"),
},
},
)
Status.init()
# accepted, closed
assert client.get()['status'] == 200
check_connections(1, 0, 0, 1)
# idle
(_, sock) = client.get(
headers={'Host': 'localhost', 'Connection': 'keep-alive'},
start=True,
read_timeout=1,
)
check_connections(2, 0, 1, 1)
client.get(sock=sock)
check_connections(2, 0, 0, 2)
# active
(_, sock) = client.get(
headers={
'Host': 'localhost',
'X-Delay': '2',
'Connection': 'close',
},
port=7081,
start=True,
read_timeout=1,
)
check_connections(3, 1, 0, 2)
client.get(sock=sock)
check_connections(3, 0, 0, 3)
def test_status_applications():
def check_applications(expert):
apps = list(client.conf_get('/status/applications').keys()).sort()
assert apps == expert.sort()
def check_application(name, running, starting, idle, active):
assert Status.get(f'/applications/{name}') == {
'processes': {
'running': running,
'starting': starting,
'idle': idle,
},
'requests': {'active': active},
}
client.load('delayed')
Status.init()
check_applications(['delayed'])
check_application('delayed', 0, 0, 0, 0)
# idle
assert client.get()['status'] == 200
check_application('delayed', 1, 0, 1, 0)
assert 'success' in client.conf('4', 'applications/delayed/processes')
check_application('delayed', 4, 0, 4, 0)
# active
(_, sock) = client.get(
headers={
'Host': 'localhost',
'X-Delay': '2',
'Connection': 'close',
},
start=True,
read_timeout=1,
)
check_application('delayed', 4, 0, 3, 1)
sock.close()
# starting
assert 'success' in client.conf(
{
"listeners": {
"*:7080": {"pass": "applications/restart"},
"*:7081": {"pass": "applications/delayed"},
},
"routes": [],
"applications": {
"restart": app_default("restart", "longstart"),
"delayed": app_default("delayed"),
},
},
)
Status.init()
check_applications(['delayed', 'restart'])
check_application('restart', 0, 0, 0, 0)
check_application('delayed', 0, 0, 0, 0)
client.get(read_timeout=1)
check_application('restart', 0, 1, 0, 1)
check_application('delayed', 0, 0, 0, 0)
def test_status_proxy():
assert 'success' in client.conf(
{
"listeners": {
"*:7080": {"pass": "routes"},
"*:7081": {"pass": "applications/empty"},
},
"routes": [
{
"match": {"uri": "/"},
"action": {"proxy": "http://127.0.0.1:7081"},
}
],
"applications": {
"empty": app_default(),
},
},
)
Status.init()
assert client.get()['status'] == 200
check_connections(2, 0, 0, 2)
assert Status.get('/requests/total') == 2, 'proxy'
|