Tests: migrated to the pytest.

This commit is contained in:
Andrei Zeliankou
2020-09-16 21:31:15 +01:00
parent 77ecb6ab49
commit d5e9159340
55 changed files with 4717 additions and 6262 deletions

View File

@@ -2,17 +2,18 @@ import os
import subprocess
from unit.applications.proto import TestApplicationProto
from conftest import option
class TestApplicationGo(TestApplicationProto):
@classmethod
def setUpClass(cls, complete_check=True):
unit = super().setUpClass(complete_check=False)
def setup_class(cls, complete_check=True):
unit = super().setup_class(complete_check=False)
# check go module
go_app = TestApplicationGo()
go_app.testdir = unit.testdir
go_app.temp_dir = unit.temp_dir
proc = go_app.prepare_env('empty', 'app')
if proc and proc.returncode == 0:
cls.available['modules']['go'] = []
@@ -20,8 +21,8 @@ class TestApplicationGo(TestApplicationProto):
return unit if not complete_check else unit.complete()
def prepare_env(self, script, name, static=False):
if not os.path.exists(self.testdir + '/go'):
os.mkdir(self.testdir + '/go')
if not os.path.exists(self.temp_dir + '/go'):
os.mkdir(self.temp_dir + '/go')
env = os.environ.copy()
env['GOPATH'] = self.pardir + '/build/go'
@@ -35,16 +36,16 @@ class TestApplicationGo(TestApplicationProto):
'-ldflags',
'-extldflags "-static"',
'-o',
self.testdir + '/go/' + name,
self.current_dir + '/go/' + script + '/' + name + '.go',
self.temp_dir + '/go/' + name,
option.test_dir + '/go/' + script + '/' + name + '.go',
]
else:
args = [
'go',
'build',
'-o',
self.testdir + '/go/' + name,
self.current_dir + '/go/' + script + '/' + name + '.go',
self.temp_dir + '/go/' + name,
option.test_dir + '/go/' + script + '/' + name + '.go',
]
try:
@@ -59,8 +60,8 @@ class TestApplicationGo(TestApplicationProto):
def load(self, script, name='app', **kwargs):
static_build = False
wdir = self.current_dir + "/go/" + script
executable = self.testdir + "/go/" + name
wdir = option.test_dir + "/go/" + script
executable = self.temp_dir + "/go/" + name
if 'isolation' in kwargs and 'rootfs' in kwargs['isolation']:
wdir = "/go/"

View File

@@ -1,17 +1,19 @@
import glob
import os
import pytest
import shutil
import subprocess
from unit.applications.proto import TestApplicationProto
from conftest import option
class TestApplicationJava(TestApplicationProto):
def load(self, script, name='app', **kwargs):
app_path = self.testdir + '/java'
app_path = self.temp_dir + '/java'
web_inf_path = app_path + '/WEB-INF/'
classes_path = web_inf_path + 'classes/'
script_path = self.current_dir + '/java/' + script + '/'
script_path = option.test_dir + '/java/' + script + '/'
if not os.path.isdir(app_path):
os.makedirs(app_path)
@@ -54,7 +56,7 @@ class TestApplicationJava(TestApplicationProto):
)
if not ws_jars:
self.fail('websocket api jar not found.')
pytest.fail('websocket api jar not found.')
javac = [
'javac',
@@ -69,7 +71,7 @@ class TestApplicationJava(TestApplicationProto):
process.communicate()
except:
self.fail('Cann\'t run javac process.')
pytest.fail('Cann\'t run javac process.')
self._load_conf(
{

View File

@@ -3,12 +3,13 @@ import shutil
from urllib.parse import quote
from unit.applications.proto import TestApplicationProto
from conftest import option, public_dir
class TestApplicationNode(TestApplicationProto):
@classmethod
def setUpClass(cls, complete_check=True):
unit = super().setUpClass(complete_check=False)
def setup_class(cls, complete_check=True):
unit = super().setup_class(complete_check=False)
# check node module
@@ -21,17 +22,17 @@ class TestApplicationNode(TestApplicationProto):
# copy application
shutil.copytree(
self.current_dir + '/node/' + script, self.testdir + '/node'
option.test_dir + '/node/' + script, self.temp_dir + '/node'
)
# copy modules
shutil.copytree(
self.pardir + '/node/node_modules',
self.testdir + '/node/node_modules',
self.temp_dir + '/node/node_modules',
)
self.public_dir(self.testdir + '/node')
public_dir(self.temp_dir + '/node')
self._load_conf(
{
@@ -42,7 +43,7 @@ class TestApplicationNode(TestApplicationProto):
script: {
"type": "external",
"processes": {"spare": 0},
"working_directory": self.testdir + '/node',
"working_directory": self.temp_dir + '/node',
"executable": name,
}
},

View File

@@ -1,11 +1,12 @@
from unit.applications.proto import TestApplicationProto
from conftest import option
class TestApplicationPerl(TestApplicationProto):
application_type = "perl"
def load(self, script, name='psgi.pl', **kwargs):
script_path = self.current_dir + '/perl/' + script
script_path = option.test_dir + '/perl/' + script
self._load_conf(
{

View File

@@ -1,11 +1,12 @@
from unit.applications.proto import TestApplicationProto
from conftest import option
class TestApplicationPHP(TestApplicationProto):
application_type = "php"
def load(self, script, index='index.php', **kwargs):
script_path = self.current_dir + '/php/' + script
script_path = option.test_dir + '/php/' + script
self._load_conf(
{

View File

@@ -1,20 +1,23 @@
import os
import shutil
import pytest
from unit.applications.proto import TestApplicationProto
from conftest import option
class TestApplicationPython(TestApplicationProto):
application_type = "python"
def load(self, script, name=None, **kwargs):
print()
if name is None:
name = script
if script[0] == '/':
script_path = script
else:
script_path = self.current_dir + '/python/' + script
script_path = option.test_dir + '/python/' + script
if kwargs.get('isolation') and kwargs['isolation'].get('rootfs'):
rootfs = kwargs['isolation']['rootfs']
@@ -27,12 +30,17 @@ class TestApplicationPython(TestApplicationProto):
script_path = '/app/python/' + name
appication_type = self.get_appication_type()
if appication_type is None:
appication_type = self.application_type
self._load_conf(
{
"listeners": {"*:7080": {"pass": "applications/" + name}},
"applications": {
name: {
"type": self.application_type,
"type": appication_type,
"processes": {"spare": 0},
"path": script_path,
"working_directory": script_path,

View File

@@ -1,11 +1,12 @@
from unit.applications.proto import TestApplicationProto
from conftest import option
class TestApplicationRuby(TestApplicationProto):
application_type = "ruby"
def load(self, script, name='config.ru', **kwargs):
script_path = self.current_dir + '/ruby/' + script
script_path = option.test_dir + '/ruby/' + script
self._load_conf(
{

View File

@@ -1,7 +1,9 @@
import os
import re
import time
from unit.control import TestControl
from conftest import option
class TestApplicationProto(TestControl):
@@ -12,7 +14,7 @@ class TestApplicationProto(TestControl):
return time.mktime(time.strptime(date, template))
def search_in_log(self, pattern, name='unit.log'):
with open(self.testdir + '/' + name, 'r', errors='ignore') as f:
with open(self.temp_dir + '/' + name, 'r', errors='ignore') as f:
return re.search(pattern, f.read())
def wait_for_record(self, pattern, name='unit.log'):
@@ -26,6 +28,16 @@ class TestApplicationProto(TestControl):
return found
def get_appication_type(self):
current_test = (
os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]
)
if current_test in option.generated_tests:
return option.generated_tests[current_test]
return None
def _load_conf(self, conf, **kwargs):
if 'applications' in conf:
for app in conf['applications'].keys():
@@ -39,6 +51,4 @@ class TestApplicationProto(TestControl):
if 'isolation' in kwargs:
app_conf['isolation'] = kwargs['isolation']
self.assertIn(
'success', self.conf(conf), 'load application configuration'
)
assert 'success' in self.conf(conf), 'load application configuration'

View File

@@ -4,19 +4,20 @@ import ssl
import subprocess
from unit.applications.proto import TestApplicationProto
from conftest import option
class TestApplicationTLS(TestApplicationProto):
def __init__(self, test):
super().__init__(test)
def setup_method(self):
super().setup_method()
self.context = ssl.create_default_context()
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)
def setup_class(cls, complete_check=True):
unit = super().setup_class(complete_check=False)
# check tls module
@@ -45,9 +46,9 @@ class TestApplicationTLS(TestApplicationProto):
'-x509',
'-new',
'-subj', '/CN=' + name + '/',
'-config', self.testdir + '/openssl.conf',
'-out', self.testdir + '/' + name + '.crt',
'-keyout', self.testdir + '/' + name + '.key',
'-config', self.temp_dir + '/openssl.conf',
'-out', self.temp_dir + '/' + name + '.crt',
'-keyout', self.temp_dir + '/' + name + '.key',
],
stderr=subprocess.STDOUT,
)
@@ -59,8 +60,8 @@ class TestApplicationTLS(TestApplicationProto):
if key is None:
key = crt
key_path = self.testdir + '/' + key + '.key'
crt_path = self.testdir + '/' + crt + '.crt'
key_path = self.temp_dir + '/' + key + '.key'
crt_path = self.temp_dir + '/' + crt + '.crt'
with open(key_path, 'rb') as k, open(crt_path, 'rb') as c:
return self.conf(k.read() + c.read(), '/certificates/' + crt)
@@ -87,7 +88,7 @@ class TestApplicationTLS(TestApplicationProto):
return ssl.get_server_certificate(addr, ssl_version=ssl_version)
def openssl_conf(self):
conf_path = self.testdir + '/openssl.conf'
conf_path = self.temp_dir + '/openssl.conf'
if os.path.exists(conf_path):
return
@@ -105,7 +106,7 @@ distinguished_name = req_distinguished_name
if name is None:
name = script
script_path = self.current_dir + '/python/' + script
script_path = option.test_dir + '/python/' + script
self._load_conf(
{

View File

@@ -1,6 +1,7 @@
import base64
import hashlib
import itertools
import pytest
import random
import re
import select
@@ -21,9 +22,6 @@ class TestApplicationWebsocket(TestApplicationProto):
OP_PONG = 0x0A
CLOSE_CODES = [1000, 1001, 1002, 1003, 1007, 1008, 1009, 1010, 1011]
def __init__(self, preinit=False):
self.preinit = preinit
def key(self):
raw_key = bytes(random.getrandbits(8) for _ in range(16))
return base64.b64encode(raw_key).decode()
@@ -56,7 +54,7 @@ class TestApplicationWebsocket(TestApplicationProto):
while True:
rlist = select.select([sock], [], [], 60)[0]
if not rlist:
self.fail('Can\'t read response from server.')
pytest.fail('Can\'t read response from server.')
resp += sock.recv(4096).decode()
@@ -84,7 +82,7 @@ class TestApplicationWebsocket(TestApplicationProto):
# For all current cases if the "read_timeout" was changed
# than test do not expect to get a response from server.
if read_timeout == 60:
self.fail('Can\'t read response from server.')
pytest.fail('Can\'t read response from server.')
break
data += sock.recv(bytes - len(data))
@@ -130,19 +128,19 @@ class TestApplicationWebsocket(TestApplicationProto):
code, = struct.unpack('!H', data[:2])
reason = data[2:].decode('utf-8')
if not (code in self.CLOSE_CODES or 3000 <= code < 5000):
self.fail('Invalid status code')
pytest.fail('Invalid status code')
frame['code'] = code
frame['reason'] = reason
elif length == 0:
frame['code'] = 1005
frame['reason'] = ''
else:
self.fail('Close frame too short')
pytest.fail('Close frame too short')
frame['data'] = data
if frame['mask']:
self.fail('Received frame with mask')
pytest.fail('Received frame with mask')
return frame