diff options
author | Andrey Zelenkov <zelenkov@nginx.com> | 2019-09-14 14:44:35 +0300 |
---|---|---|
committer | Andrey Zelenkov <zelenkov@nginx.com> | 2019-09-14 14:44:35 +0300 |
commit | 65ca2d7b198bf573fe3caf5e75bf6bd3a172820b (patch) | |
tree | 4964d0f5f4bd8b68fe06d8f975c0287b8a240951 | |
parent | 962cdb66590b9f9ec57e0a6aa5d0cb359bb8f145 (diff) | |
download | unit-65ca2d7b198bf573fe3caf5e75bf6bd3a172820b.tar.gz unit-65ca2d7b198bf573fe3caf5e75bf6bd3a172820b.tar.bz2 |
Tests: refactored prerequisites model.
Diffstat (limited to '')
-rw-r--r-- | test/test_access_log.py | 2 | ||||
-rw-r--r-- | test/test_configuration.py | 2 | ||||
-rw-r--r-- | test/test_go_application.py | 2 | ||||
-rw-r--r-- | test/test_http_header.py | 2 | ||||
-rw-r--r-- | test/test_java_application.py | 2 | ||||
-rw-r--r-- | test/test_java_websockets.py | 2 | ||||
-rw-r--r-- | test/test_node_application.py | 2 | ||||
-rw-r--r-- | test/test_node_websockets.py | 2 | ||||
-rw-r--r-- | test/test_perl_application.py | 2 | ||||
-rw-r--r-- | test/test_php_application.py | 2 | ||||
-rw-r--r-- | test/test_php_basic.py | 2 | ||||
-rw-r--r-- | test/test_python_application.py | 2 | ||||
-rw-r--r-- | test/test_python_basic.py | 2 | ||||
-rw-r--r-- | test/test_python_environment.py | 2 | ||||
-rw-r--r-- | test/test_python_procman.py | 2 | ||||
-rw-r--r-- | test/test_routing.py | 2 | ||||
-rw-r--r-- | test/test_routing_tls.py | 2 | ||||
-rw-r--r-- | test/test_ruby_application.py | 2 | ||||
-rw-r--r-- | test/test_settings.py | 2 | ||||
-rw-r--r-- | test/test_tls.py | 2 | ||||
-rw-r--r-- | test/unit/applications/lang/go.py | 13 | ||||
-rw-r--r-- | test/unit/applications/lang/node.py | 11 | ||||
-rw-r--r-- | test/unit/applications/tls.py | 22 | ||||
-rw-r--r-- | test/unit/main.py | 173 |
24 files changed, 146 insertions, 113 deletions
diff --git a/test/test_access_log.py b/test/test_access_log.py index fbcc131f..8dc87524 100644 --- a/test/test_access_log.py +++ b/test/test_access_log.py @@ -7,7 +7,7 @@ from unit.applications.lang.python import TestApplicationPython class TestAccessLog(TestApplicationPython): - prerequisites = ['python'] + prerequisites = {'modules': ['python']} def load(self, script): super().load(script) diff --git a/test/test_configuration.py b/test/test_configuration.py index 6e59c0a7..69647858 100644 --- a/test/test_configuration.py +++ b/test/test_configuration.py @@ -3,7 +3,7 @@ from unit.control import TestControl class TestConfiguration(TestControl): - prerequisites = ['python'] + prerequisites = {'modules': ['python']} def test_json_empty(self): self.assertIn('error', self.conf(''), 'empty') diff --git a/test/test_go_application.py b/test/test_go_application.py index 488bfdd5..42429be7 100644 --- a/test/test_go_application.py +++ b/test/test_go_application.py @@ -2,7 +2,7 @@ from unit.applications.lang.go import TestApplicationGo class TestGoApplication(TestApplicationGo): - prerequisites = ['go'] + prerequisites = {'modules': ['go']} def test_go_application_variables(self): self.load('variables') diff --git a/test/test_http_header.py b/test/test_http_header.py index 603f6f0f..b773bd68 100644 --- a/test/test_http_header.py +++ b/test/test_http_header.py @@ -3,7 +3,7 @@ from unit.applications.lang.python import TestApplicationPython class TestHTTPHeader(TestApplicationPython): - prerequisites = ['python'] + prerequisites = {'modules': ['python']} def test_http_header_value_leading_sp(self): self.load('custom_header') diff --git a/test/test_java_application.py b/test/test_java_application.py index 526be565..2e937718 100644 --- a/test/test_java_application.py +++ b/test/test_java_application.py @@ -4,7 +4,7 @@ from unit.applications.lang.java import TestApplicationJava class TestJavaApplication(TestApplicationJava): - prerequisites = ['java'] + prerequisites = {'modules': ['java']} def test_java_conf_error(self): self.skip_alerts.extend( diff --git a/test/test_java_websockets.py b/test/test_java_websockets.py index 58460353..3f2c0a8a 100644 --- a/test/test_java_websockets.py +++ b/test/test_java_websockets.py @@ -6,7 +6,7 @@ from unit.applications.websockets import TestApplicationWebsocket class TestJavaWebsockets(TestApplicationJava): - prerequisites = ['java'] + prerequisites = {'modules': ['java']} ws = TestApplicationWebsocket(True) diff --git a/test/test_node_application.py b/test/test_node_application.py index 0354c978..a5b4a108 100644 --- a/test/test_node_application.py +++ b/test/test_node_application.py @@ -3,7 +3,7 @@ from unit.applications.lang.node import TestApplicationNode class TestNodeApplication(TestApplicationNode): - prerequisites = ['node'] + prerequisites = {'modules': ['node']} def test_node_application_basic(self): self.load('basic') diff --git a/test/test_node_websockets.py b/test/test_node_websockets.py index 86964ce0..b24bee75 100644 --- a/test/test_node_websockets.py +++ b/test/test_node_websockets.py @@ -6,7 +6,7 @@ from unit.applications.websockets import TestApplicationWebsocket class TestNodeWebsockets(TestApplicationNode): - prerequisites = ['node'] + prerequisites = {'modules': ['node']} ws = TestApplicationWebsocket() diff --git a/test/test_perl_application.py b/test/test_perl_application.py index bc26b000..bf3c65d5 100644 --- a/test/test_perl_application.py +++ b/test/test_perl_application.py @@ -3,7 +3,7 @@ from unit.applications.lang.perl import TestApplicationPerl class TestPerlApplication(TestApplicationPerl): - prerequisites = ['perl'] + prerequisites = {'modules': ['perl']} def test_perl_application(self): self.load('variables') diff --git a/test/test_php_application.py b/test/test_php_application.py index ee2048b5..d614885c 100644 --- a/test/test_php_application.py +++ b/test/test_php_application.py @@ -3,7 +3,7 @@ import unittest from unit.applications.lang.php import TestApplicationPHP class TestPHPApplication(TestApplicationPHP): - prerequisites = ['php'] + prerequisites = {'modules': ['php']} def before_disable_functions(self): body = self.get()['body'] diff --git a/test/test_php_basic.py b/test/test_php_basic.py index 0c84f206..7ecff1b2 100644 --- a/test/test_php_basic.py +++ b/test/test_php_basic.py @@ -2,7 +2,7 @@ from unit.control import TestControl class TestPHPBasic(TestControl): - prerequisites = ['php'] + prerequisites = {'modules': ['php']} conf_app = { "app": { diff --git a/test/test_python_application.py b/test/test_python_application.py index 3484b25e..1f366570 100644 --- a/test/test_python_application.py +++ b/test/test_python_application.py @@ -4,7 +4,7 @@ from unit.applications.lang.python import TestApplicationPython class TestPythonApplication(TestApplicationPython): - prerequisites = ['python'] + prerequisites = {'modules': ['python']} def test_python_application_variables(self): self.load('variables') diff --git a/test/test_python_basic.py b/test/test_python_basic.py index e63158e5..67a5f548 100644 --- a/test/test_python_basic.py +++ b/test/test_python_basic.py @@ -2,7 +2,7 @@ from unit.control import TestControl class TestPythonBasic(TestControl): - prerequisites = ['python'] + prerequisites = {'modules': ['python']} conf_app = { "app": { diff --git a/test/test_python_environment.py b/test/test_python_environment.py index 744f4947..fe0baa13 100644 --- a/test/test_python_environment.py +++ b/test/test_python_environment.py @@ -2,7 +2,7 @@ from unit.applications.lang.python import TestApplicationPython class TestPythonEnvironment(TestApplicationPython): - prerequisites = ['python'] + prerequisites = {'modules': ['python']} def test_python_environment_name_null(self): self.load('environment') diff --git a/test/test_python_procman.py b/test/test_python_procman.py index b0c70e53..52d8cacb 100644 --- a/test/test_python_procman.py +++ b/test/test_python_procman.py @@ -6,7 +6,7 @@ from unit.applications.lang.python import TestApplicationPython class TestPythonProcman(TestApplicationPython): - prerequisites = ['python'] + prerequisites = {'modules': ['python']} def pids_for_process(self): time.sleep(0.2) diff --git a/test/test_routing.py b/test/test_routing.py index 6073877d..20e3a1c4 100644 --- a/test/test_routing.py +++ b/test/test_routing.py @@ -3,7 +3,7 @@ from unit.applications.proto import TestApplicationProto class TestRouting(TestApplicationProto): - prerequisites = ['python'] + prerequisites = {'modules': ['python']} def setUp(self): super().setUp() diff --git a/test/test_routing_tls.py b/test/test_routing_tls.py index 433a303e..3df2bc82 100644 --- a/test/test_routing_tls.py +++ b/test/test_routing_tls.py @@ -2,7 +2,7 @@ from unit.applications.tls import TestApplicationTLS class TestRoutingTLS(TestApplicationTLS): - prerequisites = ['python', 'openssl'] + prerequisites = {'modules': ['python', 'openssl']} def test_routes_match_scheme(self): self.certificate() diff --git a/test/test_ruby_application.py b/test/test_ruby_application.py index 67db8a8e..6f82ae81 100644 --- a/test/test_ruby_application.py +++ b/test/test_ruby_application.py @@ -3,7 +3,7 @@ from unit.applications.lang.ruby import TestApplicationRuby class TestRubyApplication(TestApplicationRuby): - prerequisites = ['ruby'] + prerequisites = {'modules': ['ruby']} def test_ruby_application(self): self.load('variables') diff --git a/test/test_settings.py b/test/test_settings.py index 98063440..6b849558 100644 --- a/test/test_settings.py +++ b/test/test_settings.py @@ -5,7 +5,7 @@ from unit.applications.lang.python import TestApplicationPython class TestSettings(TestApplicationPython): - prerequisites = ['python'] + prerequisites = {'modules': ['python']} def test_settings_header_read_timeout(self): self.load('empty') diff --git a/test/test_tls.py b/test/test_tls.py index 076a2c38..3514bbcb 100644 --- a/test/test_tls.py +++ b/test/test_tls.py @@ -6,7 +6,7 @@ from unit.applications.tls import TestApplicationTLS class TestTLS(TestApplicationTLS): - prerequisites = ['python', 'openssl'] + prerequisites = {'modules': ['python', 'openssl']} def findall(self, pattern): with open(self.testdir + '/unit.log', 'r', errors='ignore') as f: diff --git a/test/unit/applications/lang/go.py b/test/unit/applications/lang/go.py index 35ceefa7..15ac1cd9 100644 --- a/test/unit/applications/lang/go.py +++ b/test/unit/applications/lang/go.py @@ -4,6 +4,19 @@ from unit.applications.proto import TestApplicationProto class TestApplicationGo(TestApplicationProto): + @classmethod + def setUpClass(cls, complete_check=True): + unit = super().setUpClass(complete_check=False) + + # check go module + + go_app = TestApplicationGo() + go_app.testdir = unit.testdir + if go_app.prepare_env('empty', 'app').returncode == 0: + cls.available['modules']['go'] = [] + + return unit if not complete_check else unit.complete() + def prepare_env(self, script, name): if not os.path.exists(self.testdir + '/go'): os.mkdir(self.testdir + '/go') diff --git a/test/unit/applications/lang/node.py b/test/unit/applications/lang/node.py index 1be16458..3cc72669 100644 --- a/test/unit/applications/lang/node.py +++ b/test/unit/applications/lang/node.py @@ -4,6 +4,17 @@ from unit.applications.proto import TestApplicationProto class TestApplicationNode(TestApplicationProto): + @classmethod + def setUpClass(cls, complete_check=True): + unit = super().setUpClass(complete_check=False) + + # check node module + + if os.path.exists(unit.pardir + '/node/node_modules'): + cls.available['modules']['node'] = [] + + return unit if not complete_check else unit.complete() + def load(self, script, name='app.js'): # copy application diff --git a/test/unit/applications/tls.py b/test/unit/applications/tls.py index 6e8deefb..1290279d 100644 --- a/test/unit/applications/tls.py +++ b/test/unit/applications/tls.py @@ -1,4 +1,5 @@ import os +import re import ssl import subprocess from unit.applications.proto import TestApplicationProto @@ -12,6 +13,27 @@ class TestApplicationTLS(TestApplicationProto): self.context.check_hostname = False self.context.verify_mode = ssl.CERT_NONE + @classmethod + def setUpClass(cls, complete_check=True): + unit = super().setUpClass(complete_check=False) + + # check tls module + + try: + subprocess.check_output(['which', 'openssl']) + + output = subprocess.check_output( + [unit.unitd, '--version'], stderr=subprocess.STDOUT + ) + + if re.search('--openssl', output.decode()): + cls.available['modules']['openssl'] = [] + + except: + pass + + return unit if not complete_check else unit.complete() + def certificate(self, name='default', load=True): self.openssl_conf() diff --git a/test/unit/main.py b/test/unit/main.py index 2ee43251..d6a14c0e 100644 --- a/test/unit/main.py +++ b/test/unit/main.py @@ -12,8 +12,6 @@ import subprocess from multiprocessing import Process -available_modules = {} - class TestUnit(unittest.TestCase): current_dir = os.path.abspath( @@ -45,9 +43,9 @@ class TestUnit(unittest.TestCase): # rerun test for each available module version type = self.application_type - for prerequisite in self.prerequisites: - if prerequisite in available_modules: - for version in available_modules[prerequisite]: + for module in self.prerequisites['modules']: + if module in self.available['modules']: + for version in self.available['modules'][module]: self.application_type = type + ' ' + version super().run(result) @@ -66,8 +64,83 @@ class TestUnit(unittest.TestCase): unittest.main() @classmethod - def setUpClass(cls): - TestUnit().check_modules(*cls.prerequisites) + def setUpClass(cls, complete_check=True): + cls.available = {'modules': {}, 'features': {}} + unit = TestUnit() + + unit._run() + + # read unit.log + + for i in range(50): + with open(unit.testdir + '/unit.log', 'r') as f: + log = f.read() + m = re.search('controller started', log) + + if m is None: + time.sleep(0.1) + else: + break + + if m is None: + unit.stop() + exit("Unit is writing log too long") + + # discover available modules from unit.log + + for module in re.findall(r'module: ([a-zA-Z]+) (.*) ".*"$', log, re.M): + if module[0] not in cls.available['modules']: + cls.available['modules'][module[0]] = [module[1]] + else: + cls.available['modules'][module[0]].append(module[1]) + + def check(available, prerequisites): + missed = [] + + # check modules + + if 'modules' in prerequisites: + available_modules = list(available['modules'].keys()) + + for module in prerequisites['modules']: + if module in available_modules: + continue + + missed.append(module) + + if missed: + print('Unit has no ' + ', '.join(missed) + ' module(s)') + raise unittest.SkipTest() + + # check features + + if 'features' in prerequisites: + available_features = list(available['modules'].keys()) + + for feature in prerequisites['features']: + if feature in available_features: + continue + + missed.append(feature) + + if missed: + print(', '.join(missed) + ' feature(s) not supported') + raise unittest.SkipTest() + + def destroy(): + unit.stop() + unit._check_alerts(log) + shutil.rmtree(unit.testdir) + + def complete(): + destroy() + check(cls.available, cls.prerequisites) + + if complete_check: + complete() + else: + unit.complete = complete + return unit def setUp(self): self._run() @@ -108,92 +181,6 @@ class TestUnit(unittest.TestCase): else: self._print_path_to_log() - def check_modules(self, *modules): - self._run() - - for i in range(50): - with open(self.testdir + '/unit.log', 'r') as f: - log = f.read() - m = re.search('controller started', log) - - if m is None: - time.sleep(0.1) - else: - break - - if m is None: - self.stop() - exit("Unit is writing log too long") - - # discover all available modules - - global available_modules - available_modules = {} - for module in re.findall(r'module: ([a-zA-Z]+) (.*) ".*"$', log, re.M): - if module[0] not in available_modules: - available_modules[module[0]] = [module[1]] - else: - available_modules[module[0]].append(module[1]) - - missed_module = '' - for module in modules: - if module == 'go': - env = os.environ.copy() - env['GOPATH'] = self.pardir + '/go' - - try: - process = subprocess.Popen( - [ - 'go', - 'build', - '-o', - self.testdir + '/go/check_module', - self.current_dir + '/go/empty/app.go', - ], - env=env, - ) - process.communicate() - - m = module if process.returncode == 0 else None - - except: - m = None - - elif module == 'node': - if os.path.isdir(self.pardir + '/node/node_modules'): - m = module - else: - m = None - - elif module == 'openssl': - try: - subprocess.check_output(['which', 'openssl']) - - output = subprocess.check_output( - [self.unitd, '--version'], - stderr=subprocess.STDOUT, - ) - - m = re.search('--openssl', output.decode()) - - except: - m = None - - else: - if module not in available_modules: - m = None - - if m is None: - missed_module = module - break - - self.stop() - self._check_alerts(log) - shutil.rmtree(self.testdir) - - if missed_module: - raise unittest.SkipTest('Unit has no ' + missed_module + ' module') - def stop(self): if self._started: self._stop() |