Skip to content

Commit e88e16d

Browse files
Tests: added tests for NJS loadable modules.
1 parent dc03914 commit e88e16d

File tree

6 files changed

+133
-15
lines changed

6 files changed

+133
-15
lines changed

test/conftest.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -523,26 +523,31 @@ def _clear_conf(sock, *, log=None):
523523

524524
assert 'success' in resp, 'clear conf'
525525

526-
if 'openssl' not in option.available['modules']:
527-
return
526+
def get(url):
527+
return http.get(url=url, sock_type='unix', addr=sock)['body']
528528

529-
try:
530-
certs = json.loads(
531-
http.get(url='/certificates', sock_type='unix', addr=sock)['body']
532-
).keys()
529+
def delete(url):
530+
return http.delete(url=url, sock_type='unix', addr=sock)['body']
531+
532+
if 'openssl' in option.available['modules']:
533+
try:
534+
certs = json.loads(get('/certificates')).keys()
535+
536+
except json.JSONDecodeError:
537+
pytest.fail("Can't parse certificates list.")
533538

534-
except json.JSONDecodeError:
535-
pytest.fail("Can't parse certificates list.")
539+
for cert in certs:
540+
assert 'success' in delete(f'/certificates/{cert}'), 'delete cert'
536541

537-
for cert in certs:
538-
resp = http.delete(
539-
url=f'/certificates/{cert}',
540-
sock_type='unix',
541-
addr=sock,
542-
)['body']
542+
if 'njs' in option.available['modules']:
543+
try:
544+
scripts = json.loads(get('/js_modules')).keys()
543545

544-
assert 'success' in resp, 'remove certificate'
546+
except json.JSONDecodeError:
547+
pytest.fail("Can't parse njs modules list.")
545548

549+
for script in scripts:
550+
assert 'success' in delete(f'/js_modules/{script}'), 'delete script'
546551

547552
def _clear_temp_dir():
548553
temp_dir = unit_instance['temp_dir']

test/njs/global_this/script.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
"str" : function () {return typeof globalThis.njs.version}
3+
}

test/njs/import_from/script.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import cr from 'crypto';
2+
3+
export default {
4+
"num" : function () {return typeof cr.createHash('md5').digest().length}
5+
}

test/njs/invalid/script.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
"route": function() {blah 'next'}
3+
}

test/njs/next/script.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
"route": function() {return 'next'}
3+
}

test/test_njs_modules.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
from unit.applications.proto import TestApplicationProto
2+
from unit.option import option
3+
4+
5+
class TestNJSModules(TestApplicationProto):
6+
prerequisites = {'modules': {'njs': 'any'}}
7+
8+
def njs_script_load(self, module, name=None, expect='success'):
9+
if name is None:
10+
name = module
11+
12+
with open(f'{option.test_dir}/njs/{module}/script.js', 'rb') as s:
13+
assert expect in self.conf(s.read(), f'/js_modules/{name}')
14+
15+
def test_njs_modules(self):
16+
self.njs_script_load('next')
17+
18+
assert 'export' in self.conf_get('/js_modules/next')
19+
assert 'error' in self.conf_post('"blah"', '/js_modules/next')
20+
21+
assert 'success' in self.conf(
22+
{
23+
"settings": {"js_module": "next"},
24+
"listeners": {"*:7080": {"pass": "routes/first"}},
25+
"routes": {
26+
"first": [{"action": {"pass": "`routes/${next.route()}`"}}],
27+
"next": [{"action": {"return": 200}}],
28+
},
29+
}
30+
)
31+
assert self.get()['status'] == 200, 'string'
32+
33+
assert 'success' in self.conf({"js_module": ["next"]}, 'settings')
34+
assert self.get()['status'] == 200, 'array'
35+
36+
# add one more value to array
37+
38+
assert len(self.conf_get('/js_modules').keys()) == 1
39+
40+
self.njs_script_load('next', 'next_2')
41+
42+
assert len(self.conf_get('/js_modules').keys()) == 2
43+
44+
assert 'success' in self.conf_post('"next_2"', 'settings/js_module')
45+
assert self.get()['status'] == 200, 'array len 2'
46+
47+
assert 'success' in self.conf(
48+
'"`routes/${next_2.route()}`"', 'routes/first/0/action/pass'
49+
)
50+
assert self.get()['status'] == 200, 'array new'
51+
52+
# can't update exsisting script
53+
54+
self.njs_script_load('global_this', 'next', expect='error')
55+
56+
# delete modules
57+
58+
assert 'error' in self.conf_delete('/js_modules/next_2')
59+
assert 'success' in self.conf_delete('settings/js_module')
60+
assert 'success' in self.conf_delete('/js_modules/next_2')
61+
62+
def test_njs_modules_import(self):
63+
self.njs_script_load('import_from')
64+
65+
assert 'success' in self.conf(
66+
{
67+
"settings": {"js_module": "import_from"},
68+
"listeners": {"*:7080": {"pass": "routes/first"}},
69+
"routes": {
70+
"first": [
71+
{"action": {"pass": "`routes/${import_from.num()}`"}}
72+
],
73+
"number": [{"action": {"return": 200}}],
74+
},
75+
}
76+
)
77+
assert self.get()['status'] == 200
78+
79+
def test_njs_modules_this(self):
80+
self.njs_script_load('global_this')
81+
82+
assert 'success' in self.conf(
83+
{
84+
"settings": {"js_module": "global_this"},
85+
"listeners": {"*:7080": {"pass": "routes/first"}},
86+
"routes": {
87+
"first": [
88+
{"action": {"pass": "`routes/${global_this.str()}`"}}
89+
],
90+
"string": [{"action": {"return": 200}}],
91+
},
92+
}
93+
)
94+
assert self.get()['status'] == 200
95+
96+
def test_njs_modules_invalid(self, skip_alert):
97+
skip_alert(r'.*JS compile module.*failed.*')
98+
99+
self.njs_script_load('invalid', expect='error')

0 commit comments

Comments
 (0)