Tests: unit module refactoring.

This commit is contained in:
Andrey Zelenkov
2019-03-28 18:43:13 +03:00
parent 06b9a11494
commit 19eba1730a
33 changed files with 931 additions and 918 deletions

View File

View File

View File

@@ -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,
}
},
}
)

View File

@@ -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,
}
},
}
)

View File

@@ -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,
}
},
}
)

View File

@@ -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,
}
},
}
)

View File

@@ -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,
}
},
}
)

View File

@@ -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",
}
},
}
)

View File

@@ -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,
}
},
}
)

View File

@@ -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())

View File

@@ -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",
}
},
}
)