diff options
author | Andrey Zelenkov <zelenkov@nginx.com> | 2019-03-28 18:43:13 +0300 |
---|---|---|
committer | Andrey Zelenkov <zelenkov@nginx.com> | 2019-03-28 18:43:13 +0300 |
commit | 19eba1730a1ca839ed62a37f34c204f580d1b653 (patch) | |
tree | e9f54ca64fc7db66e33350826c76ef3814cfa4a0 /test/unit/applications | |
parent | 06b9a11494561e309114266bfe3bb001352b596c (diff) | |
download | unit-19eba1730a1ca839ed62a37f34c204f580d1b653.tar.gz unit-19eba1730a1ca839ed62a37f34c204f580d1b653.tar.bz2 |
Tests: unit module refactoring.
Diffstat (limited to 'test/unit/applications')
-rw-r--r-- | test/unit/applications/__init__.py | 0 | ||||
-rw-r--r-- | test/unit/applications/lang/__init__.py | 0 | ||||
-rw-r--r-- | test/unit/applications/lang/go.py | 40 | ||||
-rw-r--r-- | test/unit/applications/lang/java.py | 74 | ||||
-rw-r--r-- | test/unit/applications/lang/node.py | 34 | ||||
-rw-r--r-- | test/unit/applications/lang/perl.py | 20 | ||||
-rw-r--r-- | test/unit/applications/lang/php.py | 21 | ||||
-rw-r--r-- | test/unit/applications/lang/python.py | 24 | ||||
-rw-r--r-- | test/unit/applications/lang/ruby.py | 20 | ||||
-rw-r--r-- | test/unit/applications/proto.py | 15 | ||||
-rw-r--r-- | test/unit/applications/tls.py | 92 |
11 files changed, 340 insertions, 0 deletions
diff --git a/test/unit/applications/__init__.py b/test/unit/applications/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/test/unit/applications/__init__.py diff --git a/test/unit/applications/lang/__init__.py b/test/unit/applications/lang/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/test/unit/applications/lang/__init__.py diff --git a/test/unit/applications/lang/go.py b/test/unit/applications/lang/go.py new file mode 100644 index 00000000..4852459c --- /dev/null +++ b/test/unit/applications/lang/go.py @@ -0,0 +1,40 @@ +import os +from subprocess import Popen +from unit.applications.proto import TestApplicationProto + + +class TestApplicationGo(TestApplicationProto): + def load(self, script, name='app'): + + if not os.path.isdir(self.testdir + '/go'): + os.mkdir(self.testdir + '/go') + + go_app_path = self.current_dir + '/go/' + + env = os.environ.copy() + env['GOPATH'] = self.pardir + '/go' + process = Popen( + [ + 'go', + 'build', + '-o', + self.testdir + '/go/' + name, + go_app_path + script + '/' + name + '.go', + ], + env=env, + ) + process.communicate() + + self.conf( + { + "listeners": {"*:7080": {"application": script}}, + "applications": { + script: { + "type": "external", + "processes": {"spare": 0}, + "working_directory": go_app_path + script, + "executable": self.testdir + '/go/' + name, + } + }, + } + ) diff --git a/test/unit/applications/lang/java.py b/test/unit/applications/lang/java.py new file mode 100644 index 00000000..91bfb9ec --- /dev/null +++ b/test/unit/applications/lang/java.py @@ -0,0 +1,74 @@ +import os +import shutil +from subprocess import Popen +from unit.applications.proto import TestApplicationProto + + +class TestApplicationJava(TestApplicationProto): + def load(self, script, name='app'): + + app_path = self.testdir + '/java' + web_inf_path = app_path + '/WEB-INF/' + classes_path = web_inf_path + 'classes/' + + script_path = self.current_dir + '/java/' + script + '/' + + if not os.path.isdir(app_path): + os.makedirs(app_path) + + src = [] + + for f in os.listdir(script_path): + if f.endswith('.java'): + src.append(script_path + f) + continue + + if f.startswith('.') or f == 'Makefile': + continue + + if os.path.isdir(script_path + f): + if f == 'WEB-INF': + continue + + shutil.copytree(script_path + f, app_path + '/' + f) + continue + + if f == 'web.xml': + if not os.path.isdir(web_inf_path): + os.makedirs(web_inf_path) + + shutil.copy2(script_path + f, web_inf_path) + else: + shutil.copy2(script_path + f, app_path) + + if src: + if not os.path.isdir(classes_path): + os.makedirs(classes_path) + + tomcat_jar = self.pardir + '/build/tomcat-servlet-api-9.0.13.jar' + + javac = [ + 'javac', + '-encoding', 'utf-8', + '-d', classes_path, + '-classpath', tomcat_jar, + ] + javac.extend(src) + + process = Popen(javac) + process.communicate() + + self.conf( + { + "listeners": {"*:7080": {"application": script}}, + "applications": { + script: { + "unit_jars": self.pardir + '/build', + "type": "java", + "processes": {"spare": 0}, + "working_directory": script_path, + "webapp": app_path, + } + }, + } + ) diff --git a/test/unit/applications/lang/node.py b/test/unit/applications/lang/node.py new file mode 100644 index 00000000..f1b99cc7 --- /dev/null +++ b/test/unit/applications/lang/node.py @@ -0,0 +1,34 @@ +import os +import shutil +from unit.applications.proto import TestApplicationProto + + +class TestApplicationNode(TestApplicationProto): + def load(self, script, name='app.js'): + + # copy application + + shutil.copytree( + self.current_dir + '/node/' + script, self.testdir + '/node' + ) + + # link modules + + os.symlink( + self.pardir + '/node/node_modules', + self.testdir + '/node/node_modules', + ) + + self.conf( + { + "listeners": {"*:7080": {"application": script}}, + "applications": { + script: { + "type": "external", + "processes": {"spare": 0}, + "working_directory": self.testdir + '/node', + "executable": name, + } + }, + } + ) diff --git a/test/unit/applications/lang/perl.py b/test/unit/applications/lang/perl.py new file mode 100644 index 00000000..6970873d --- /dev/null +++ b/test/unit/applications/lang/perl.py @@ -0,0 +1,20 @@ +from unit.applications.proto import TestApplicationProto + + +class TestApplicationPerl(TestApplicationProto): + def load(self, script, name='psgi.pl'): + script_path = self.current_dir + '/perl/' + script + + self.conf( + { + "listeners": {"*:7080": {"application": script}}, + "applications": { + script: { + "type": "perl", + "processes": {"spare": 0}, + "working_directory": script_path, + "script": script_path + '/' + name, + } + }, + } + ) diff --git a/test/unit/applications/lang/php.py b/test/unit/applications/lang/php.py new file mode 100644 index 00000000..c4043764 --- /dev/null +++ b/test/unit/applications/lang/php.py @@ -0,0 +1,21 @@ +from unit.applications.proto import TestApplicationProto + + +class TestApplicationPHP(TestApplicationProto): + def load(self, script, name='index.php'): + script_path = self.current_dir + '/php/' + script + + self.conf( + { + "listeners": {"*:7080": {"application": script}}, + "applications": { + script: { + "type": "php", + "processes": {"spare": 0}, + "root": script_path, + "working_directory": script_path, + "index": name, + } + }, + } + ) diff --git a/test/unit/applications/lang/python.py b/test/unit/applications/lang/python.py new file mode 100644 index 00000000..8c2c8707 --- /dev/null +++ b/test/unit/applications/lang/python.py @@ -0,0 +1,24 @@ +from unit.applications.proto import TestApplicationProto + + +class TestApplicationPython(TestApplicationProto): + def load(self, script, name=None): + if name is None: + name = script + + script_path = self.current_dir + '/python/' + script + + self.conf( + { + "listeners": {"*:7080": {"application": name}}, + "applications": { + name: { + "type": "python", + "processes": {"spare": 0}, + "path": script_path, + "working_directory": script_path, + "module": "wsgi", + } + }, + } + ) diff --git a/test/unit/applications/lang/ruby.py b/test/unit/applications/lang/ruby.py new file mode 100644 index 00000000..94086d26 --- /dev/null +++ b/test/unit/applications/lang/ruby.py @@ -0,0 +1,20 @@ +from unit.applications.proto import TestApplicationProto + + +class TestApplicationRuby(TestApplicationProto): + def load(self, script, name='config.ru'): + script_path = self.current_dir + '/ruby/' + script + + self.conf( + { + "listeners": {"*:7080": {"application": script}}, + "applications": { + script: { + "type": "ruby", + "processes": {"spare": 0}, + "working_directory": script_path, + "script": script_path + '/' + name, + } + }, + } + ) diff --git a/test/unit/applications/proto.py b/test/unit/applications/proto.py new file mode 100644 index 00000000..17dfed35 --- /dev/null +++ b/test/unit/applications/proto.py @@ -0,0 +1,15 @@ +import re +import time +from unit.control import TestControl + + +class TestApplicationProto(TestControl): + def sec_epoch(self): + return time.mktime(time.gmtime()) + + def date_to_sec_epoch(self, date, template='%a, %d %b %Y %H:%M:%S %Z'): + return time.mktime(time.strptime(date, template)) + + def search_in_log(self, pattern): + with open(self.testdir + '/unit.log', 'r', errors='ignore') as f: + return re.search(pattern, f.read()) diff --git a/test/unit/applications/tls.py b/test/unit/applications/tls.py new file mode 100644 index 00000000..1e1f3675 --- /dev/null +++ b/test/unit/applications/tls.py @@ -0,0 +1,92 @@ +import ssl +import subprocess +from unit.applications.proto import TestApplicationProto + + +class TestApplicationTLS(TestApplicationProto): + def __init__(self, test): + super().__init__(test) + + self.context = ssl.create_default_context() + self.context.check_hostname = False + self.context.verify_mode = ssl.CERT_NONE + + def certificate(self, name='default', load=True): + subprocess.call( + [ + 'openssl', + 'req', + '-x509', + '-new', + '-subj', '/CN=' + name + '/', + '-config', self.testdir + '/openssl.conf', + '-out', self.testdir + '/' + name + '.crt', + '-keyout', self.testdir + '/' + name + '.key', + ] + ) + + if load: + self.certificate_load(name) + + def certificate_load(self, crt, key=None): + if key is None: + key = crt + + key_path = self.testdir + '/' + key + '.key' + crt_path = self.testdir + '/' + crt + '.crt' + + with open(key_path, 'rb') as k, open(crt_path, 'rb') as c: + return self.conf(k.read() + c.read(), '/certificates/' + crt) + + def get_ssl(self, **kwargs): + return self.get(wrapper=self.context.wrap_socket, **kwargs) + + def post_ssl(self, **kwargs): + return self.post(wrapper=self.context.wrap_socket, **kwargs) + + def get_server_certificate(self, addr=('127.0.0.1', 7080)): + + ssl_list = dir(ssl) + + if 'PROTOCOL_TLS' in ssl_list: + ssl_version = ssl.PROTOCOL_TLS + + elif 'PROTOCOL_TLSv1_2' in ssl_list: + ssl_version = ssl.PROTOCOL_TLSv1_2 + + else: + ssl_version = ssl.PROTOCOL_TLSv1_1 + + return ssl.get_server_certificate(addr, ssl_version=ssl_version) + + def load(self, script, name=None): + if name is None: + name = script + + # create default openssl configuration + + with open(self.testdir + '/openssl.conf', 'w') as f: + f.write( + """[ req ] +default_bits = 1024 +encrypt_key = no +distinguished_name = req_distinguished_name +[ req_distinguished_name ]""" + ) + + script_path = self.current_dir + '/python/' + script + + self.conf( + { + "listeners": {"*:7080": {"application": name}}, + "applications": { + name: { + "type": "python", + "processes": {"spare": 0}, + "path": script_path, + "working_directory": script_path, + "module": "wsgi", + } + }, + } + ) |