2022-09-05 22:06:16 +00:00
|
|
|
import time
|
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
from unit.applications.lang.python import ApplicationPython
|
2022-09-05 22:06:16 +00:00
|
|
|
from unit.option import option
|
|
|
|
from unit.status import Status
|
|
|
|
|
2023-06-12 13:16:59 +00:00
|
|
|
prerequisites = {'modules': {'python': 'any'}}
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
client = ApplicationPython()
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2022-09-27 11:08:36 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
def check_connections(accepted, active, idle, closed):
|
|
|
|
assert Status.get('/connections') == {
|
|
|
|
'accepted': accepted,
|
|
|
|
'active': active,
|
|
|
|
'idle': idle,
|
|
|
|
'closed': closed,
|
|
|
|
}
|
2022-09-27 11:08:36 +00:00
|
|
|
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
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,
|
|
|
|
}
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
|
|
|
|
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": {
|
2023-11-08 18:37:02 +00:00
|
|
|
"*:8080": {"pass": "routes"},
|
|
|
|
"*:8081": {"pass": "applications/empty"},
|
|
|
|
"*:8082": {"pass": "applications/blah"},
|
2023-06-14 17:20:09 +00:00
|
|
|
},
|
|
|
|
"routes": [{"action": {"return": 200}}],
|
|
|
|
"applications": {
|
|
|
|
"empty": app_default(),
|
|
|
|
"blah": {
|
|
|
|
"type": client.get_application_type(),
|
|
|
|
"processes": {"spare": 0},
|
|
|
|
"module": "blah",
|
2022-09-05 22:06:16 +00:00
|
|
|
},
|
|
|
|
},
|
2023-06-14 17:20:09 +00:00
|
|
|
},
|
|
|
|
)
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
Status.init()
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
assert client.get()['status'] == 200
|
|
|
|
assert Status.get('/requests/total') == 1, '2xx'
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-11-08 18:37:02 +00:00
|
|
|
assert client.get(port=8081)['status'] == 200
|
2023-06-14 17:20:09 +00:00
|
|
|
assert Status.get('/requests/total') == 2, '2xx app'
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
assert (
|
|
|
|
client.get(headers={'Host': '/', 'Connection': 'close'})['status']
|
|
|
|
== 400
|
|
|
|
)
|
|
|
|
assert Status.get('/requests/total') == 3, '4xx'
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-11-08 18:37:02 +00:00
|
|
|
assert client.get(port=8082)['status'] == 503
|
2023-06-14 17:20:09 +00:00
|
|
|
assert Status.get('/requests/total') == 4, '5xx'
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
client.http(
|
|
|
|
b"""GET / HTTP/1.1
|
2022-09-05 22:06:16 +00:00
|
|
|
Host: localhost
|
|
|
|
|
|
|
|
GET / HTTP/1.1
|
|
|
|
Host: localhost
|
|
|
|
Connection: close
|
|
|
|
|
|
|
|
""",
|
2023-06-14 17:20:09 +00:00
|
|
|
raw=True,
|
|
|
|
)
|
|
|
|
assert Status.get('/requests/total') == 6, 'pipeline'
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-11-08 18:37:02 +00:00
|
|
|
sock = client.get(port=8081, no_recv=True)
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
time.sleep(1)
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
assert Status.get('/requests/total') == 7, 'no receive'
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
sock.close()
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
|
|
|
|
def test_status_connections():
|
|
|
|
assert 'success' in client.conf(
|
|
|
|
{
|
|
|
|
"listeners": {
|
2023-11-08 18:37:02 +00:00
|
|
|
"*:8080": {"pass": "routes"},
|
|
|
|
"*:8081": {"pass": "applications/delayed"},
|
2023-06-14 17:20:09 +00:00
|
|
|
},
|
|
|
|
"routes": [{"action": {"return": 200}}],
|
|
|
|
"applications": {
|
|
|
|
"delayed": app_default("delayed"),
|
2022-09-05 22:06:16 +00:00
|
|
|
},
|
2023-06-14 17:20:09 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
Status.init()
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
# accepted, closed
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
assert client.get()['status'] == 200
|
|
|
|
check_connections(1, 0, 0, 1)
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
# idle
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
(_, sock) = client.get(
|
|
|
|
headers={'Host': 'localhost', 'Connection': 'keep-alive'},
|
|
|
|
start=True,
|
|
|
|
read_timeout=1,
|
|
|
|
)
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
check_connections(2, 0, 1, 1)
|
2023-05-25 15:56:14 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
client.get(sock=sock)
|
|
|
|
check_connections(2, 0, 0, 2)
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
# active
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
(_, sock) = client.get(
|
|
|
|
headers={
|
|
|
|
'Host': 'localhost',
|
|
|
|
'X-Delay': '2',
|
|
|
|
'Connection': 'close',
|
|
|
|
},
|
2023-11-08 18:37:02 +00:00
|
|
|
port=8081,
|
2023-06-14 17:20:09 +00:00
|
|
|
start=True,
|
|
|
|
read_timeout=1,
|
|
|
|
)
|
|
|
|
check_connections(3, 1, 0, 2)
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
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,
|
2022-09-05 22:06:16 +00:00
|
|
|
},
|
2023-06-14 17:20:09 +00:00
|
|
|
'requests': {'active': active},
|
|
|
|
}
|
|
|
|
|
|
|
|
client.load('delayed')
|
|
|
|
Status.init()
|
|
|
|
|
|
|
|
check_applications(['delayed'])
|
|
|
|
check_application('delayed', 0, 0, 0, 0)
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
# idle
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
assert client.get()['status'] == 200
|
|
|
|
check_application('delayed', 1, 0, 1, 0)
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
assert 'success' in client.conf('4', 'applications/delayed/processes')
|
|
|
|
check_application('delayed', 4, 0, 4, 0)
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
# active
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
(_, 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()
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
# starting
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
assert 'success' in client.conf(
|
|
|
|
{
|
|
|
|
"listeners": {
|
2023-11-08 18:37:02 +00:00
|
|
|
"*:8080": {"pass": "applications/restart"},
|
|
|
|
"*:8081": {"pass": "applications/delayed"},
|
2022-09-05 22:06:16 +00:00
|
|
|
},
|
2023-06-14 17:20:09 +00:00
|
|
|
"routes": [],
|
|
|
|
"applications": {
|
|
|
|
"restart": app_default("restart", "longstart"),
|
|
|
|
"delayed": app_default("delayed"),
|
2022-09-05 22:06:16 +00:00
|
|
|
},
|
2023-06-14 17:20:09 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
Status.init()
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
check_applications(['delayed', 'restart'])
|
|
|
|
check_application('restart', 0, 0, 0, 0)
|
|
|
|
check_application('delayed', 0, 0, 0, 0)
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
client.get(read_timeout=1)
|
2022-09-05 22:06:16 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
check_application('restart', 0, 1, 0, 1)
|
|
|
|
check_application('delayed', 0, 0, 0, 0)
|
2022-09-27 11:08:36 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
|
|
|
|
def test_status_proxy():
|
|
|
|
assert 'success' in client.conf(
|
|
|
|
{
|
|
|
|
"listeners": {
|
2023-11-08 18:37:02 +00:00
|
|
|
"*:8080": {"pass": "routes"},
|
|
|
|
"*:8081": {"pass": "applications/empty"},
|
2023-06-14 17:20:09 +00:00
|
|
|
},
|
|
|
|
"routes": [
|
|
|
|
{
|
|
|
|
"match": {"uri": "/"},
|
2023-11-08 18:37:02 +00:00
|
|
|
"action": {"proxy": "http://127.0.0.1:8081"},
|
2023-06-14 17:20:09 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"applications": {
|
|
|
|
"empty": app_default(),
|
2022-09-27 11:08:36 +00:00
|
|
|
},
|
2023-06-14 17:20:09 +00:00
|
|
|
},
|
|
|
|
)
|
2022-09-27 11:08:36 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
Status.init()
|
2022-09-27 11:08:36 +00:00
|
|
|
|
2023-06-14 17:20:09 +00:00
|
|
|
assert client.get()['status'] == 200
|
|
|
|
check_connections(2, 0, 0, 2)
|
|
|
|
assert Status.get('/requests/total') == 2, 'proxy'
|