diff options
author | Andrei Zeliankou <zelenkov@nginx.com> | 2023-05-10 13:02:52 +0100 |
---|---|---|
committer | Andrei Zeliankou <zelenkov@nginx.com> | 2023-05-10 13:02:52 +0100 |
commit | e88e16d11e8d197f27d292540a015cf05af6277f (patch) | |
tree | 158f93f7fc50ef72b85de44e879fef0e3c04d6fa | |
parent | dc0391441807c535e1f021263572128459a69d6a (diff) | |
download | unit-e88e16d11e8d197f27d292540a015cf05af6277f.tar.gz unit-e88e16d11e8d197f27d292540a015cf05af6277f.tar.bz2 |
Tests: added tests for NJS loadable modules.
Diffstat (limited to '')
-rw-r--r-- | test/conftest.py | 35 | ||||
-rw-r--r-- | test/njs/global_this/script.js | 3 | ||||
-rw-r--r-- | test/njs/import_from/script.js | 5 | ||||
-rw-r--r-- | test/njs/invalid/script.js | 3 | ||||
-rw-r--r-- | test/njs/next/script.js | 3 | ||||
-rw-r--r-- | test/test_njs_modules.py | 99 |
6 files changed, 133 insertions, 15 deletions
diff --git a/test/conftest.py b/test/conftest.py index 6d8e59fd..926d83f8 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -523,26 +523,31 @@ def _clear_conf(sock, *, log=None): assert 'success' in resp, 'clear conf' - if 'openssl' not in option.available['modules']: - return + def get(url): + return http.get(url=url, sock_type='unix', addr=sock)['body'] - try: - certs = json.loads( - http.get(url='/certificates', sock_type='unix', addr=sock)['body'] - ).keys() + def delete(url): + return http.delete(url=url, sock_type='unix', addr=sock)['body'] + + if 'openssl' in option.available['modules']: + try: + certs = json.loads(get('/certificates')).keys() + + except json.JSONDecodeError: + pytest.fail("Can't parse certificates list.") - except json.JSONDecodeError: - pytest.fail("Can't parse certificates list.") + for cert in certs: + assert 'success' in delete(f'/certificates/{cert}'), 'delete cert' - for cert in certs: - resp = http.delete( - url=f'/certificates/{cert}', - sock_type='unix', - addr=sock, - )['body'] + if 'njs' in option.available['modules']: + try: + scripts = json.loads(get('/js_modules')).keys() - assert 'success' in resp, 'remove certificate' + except json.JSONDecodeError: + pytest.fail("Can't parse njs modules list.") + for script in scripts: + assert 'success' in delete(f'/js_modules/{script}'), 'delete script' def _clear_temp_dir(): temp_dir = unit_instance['temp_dir'] diff --git a/test/njs/global_this/script.js b/test/njs/global_this/script.js new file mode 100644 index 00000000..cf9bc078 --- /dev/null +++ b/test/njs/global_this/script.js @@ -0,0 +1,3 @@ +export default { + "str" : function () {return typeof globalThis.njs.version} +} diff --git a/test/njs/import_from/script.js b/test/njs/import_from/script.js new file mode 100644 index 00000000..99d1b80e --- /dev/null +++ b/test/njs/import_from/script.js @@ -0,0 +1,5 @@ +import cr from 'crypto'; + +export default { + "num" : function () {return typeof cr.createHash('md5').digest().length} +} diff --git a/test/njs/invalid/script.js b/test/njs/invalid/script.js new file mode 100644 index 00000000..a9d79797 --- /dev/null +++ b/test/njs/invalid/script.js @@ -0,0 +1,3 @@ +export default { + "route": function() {blah 'next'} +} diff --git a/test/njs/next/script.js b/test/njs/next/script.js new file mode 100644 index 00000000..86e49e82 --- /dev/null +++ b/test/njs/next/script.js @@ -0,0 +1,3 @@ +export default { + "route": function() {return 'next'} +} diff --git a/test/test_njs_modules.py b/test/test_njs_modules.py new file mode 100644 index 00000000..ce592fe4 --- /dev/null +++ b/test/test_njs_modules.py @@ -0,0 +1,99 @@ +from unit.applications.proto import TestApplicationProto +from unit.option import option + + +class TestNJSModules(TestApplicationProto): + prerequisites = {'modules': {'njs': 'any'}} + + def njs_script_load(self, module, name=None, expect='success'): + if name is None: + name = module + + with open(f'{option.test_dir}/njs/{module}/script.js', 'rb') as s: + assert expect in self.conf(s.read(), f'/js_modules/{name}') + + def test_njs_modules(self): + self.njs_script_load('next') + + assert 'export' in self.conf_get('/js_modules/next') + assert 'error' in self.conf_post('"blah"', '/js_modules/next') + + assert 'success' in self.conf( + { + "settings": {"js_module": "next"}, + "listeners": {"*:7080": {"pass": "routes/first"}}, + "routes": { + "first": [{"action": {"pass": "`routes/${next.route()}`"}}], + "next": [{"action": {"return": 200}}], + }, + } + ) + assert self.get()['status'] == 200, 'string' + + assert 'success' in self.conf({"js_module": ["next"]}, 'settings') + assert self.get()['status'] == 200, 'array' + + # add one more value to array + + assert len(self.conf_get('/js_modules').keys()) == 1 + + self.njs_script_load('next', 'next_2') + + assert len(self.conf_get('/js_modules').keys()) == 2 + + assert 'success' in self.conf_post('"next_2"', 'settings/js_module') + assert self.get()['status'] == 200, 'array len 2' + + assert 'success' in self.conf( + '"`routes/${next_2.route()}`"', 'routes/first/0/action/pass' + ) + assert self.get()['status'] == 200, 'array new' + + # can't update exsisting script + + self.njs_script_load('global_this', 'next', expect='error') + + # delete modules + + assert 'error' in self.conf_delete('/js_modules/next_2') + assert 'success' in self.conf_delete('settings/js_module') + assert 'success' in self.conf_delete('/js_modules/next_2') + + def test_njs_modules_import(self): + self.njs_script_load('import_from') + + assert 'success' in self.conf( + { + "settings": {"js_module": "import_from"}, + "listeners": {"*:7080": {"pass": "routes/first"}}, + "routes": { + "first": [ + {"action": {"pass": "`routes/${import_from.num()}`"}} + ], + "number": [{"action": {"return": 200}}], + }, + } + ) + assert self.get()['status'] == 200 + + def test_njs_modules_this(self): + self.njs_script_load('global_this') + + assert 'success' in self.conf( + { + "settings": {"js_module": "global_this"}, + "listeners": {"*:7080": {"pass": "routes/first"}}, + "routes": { + "first": [ + {"action": {"pass": "`routes/${global_this.str()}`"}} + ], + "string": [{"action": {"return": 200}}], + }, + } + ) + assert self.get()['status'] == 200 + + def test_njs_modules_invalid(self, skip_alert): + skip_alert(r'.*JS compile module.*failed.*') + + self.njs_script_load('invalid', expect='error') |