Tests: unified setup method usage.

To make fixtures accessible inside of setup methods in tests all these methods
are renamed to the "setup_method_fixture" and decorated by autouse flag.

Also all setup methods moved to the top of the files.
This commit is contained in:
Andrei Zeliankou
2023-05-25 16:56:14 +01:00
parent 3e4fa1e202
commit 18fcc07c77
15 changed files with 113 additions and 93 deletions

View File

@@ -1,3 +1,4 @@
import pytest
from unit.applications.lang.python import TestApplicationPython from unit.applications.lang.python import TestApplicationPython
from unit.option import option from unit.option import option
@@ -5,6 +6,10 @@ from unit.option import option
class TestClientIP(TestApplicationPython): class TestClientIP(TestApplicationPython):
prerequisites = {'modules': {'python': 'any'}} prerequisites = {'modules': {'python': 'any'}}
@pytest.fixture(autouse=True)
def setup_method_fixture(self):
self.load('client_ip')
def client_ip(self, options): def client_ip(self, options):
assert 'success' in self.conf( assert 'success' in self.conf(
{ {
@@ -39,9 +44,6 @@ class TestClientIP(TestApplicationPython):
headers={'Connection': 'close', 'X-Forwarded-For': xff}, headers={'Connection': 'close', 'X-Forwarded-For': xff},
)['body'] )['body']
def setup_method(self):
self.load('client_ip')
def test_client_ip_single_ip(self): def test_client_ip_single_ip(self):
self.client_ip( self.client_ip(
{'header': 'X-Forwarded-For', 'source': '123.123.123.123'} {'header': 'X-Forwarded-For', 'source': '123.123.123.123'}

View File

@@ -1,9 +1,14 @@
import pytest
from unit.applications.lang.python import TestApplicationPython from unit.applications.lang.python import TestApplicationPython
class TestForwardedHeader(TestApplicationPython): class TestForwardedHeader(TestApplicationPython):
prerequisites = {'modules': {'python': 'any'}} prerequisites = {'modules': {'python': 'any'}}
@pytest.fixture(autouse=True)
def setup_method_fixture(self):
self.load('forwarded_header')
def forwarded_header(self, forwarded): def forwarded_header(self, forwarded):
assert 'success' in self.conf( assert 'success' in self.conf(
{ {
@@ -40,9 +45,6 @@ class TestForwardedHeader(TestApplicationPython):
def get_scheme(self, *args, **kwargs): def get_scheme(self, *args, **kwargs):
return self.get_fwd(*args, **kwargs)['Url-Scheme'] return self.get_fwd(*args, **kwargs)['Url-Scheme']
def setup_method(self):
self.load('forwarded_header')
def test_forwarded_header_single_ip(self): def test_forwarded_header_single_ip(self):
self.forwarded_header( self.forwarded_header(
{ {

View File

@@ -9,13 +9,14 @@ from unit.option import option
class TestJavaIsolationRootfs(TestApplicationJava): class TestJavaIsolationRootfs(TestApplicationJava):
prerequisites = {'modules': {'java': 'all'}} prerequisites = {'modules': {'java': 'all'}}
def setup_method(self, is_su): @pytest.fixture(autouse=True)
def setup_method_fixture(self, is_su, temp_dir):
if not is_su: if not is_su:
pytest.skip('require root') pytest.skip('require root')
os.makedirs(f'{option.temp_dir}/jars') os.makedirs(f'{temp_dir}/jars')
os.makedirs(f'{option.temp_dir}/tmp') os.makedirs(f'{temp_dir}/tmp')
os.chmod(f'{option.temp_dir}/tmp', 0o777) os.chmod(f'{temp_dir}/tmp', 0o777)
try: try:
subprocess.run( subprocess.run(
@@ -23,7 +24,7 @@ class TestJavaIsolationRootfs(TestApplicationJava):
"mount", "mount",
"--bind", "--bind",
f'{option.current_dir}/build', f'{option.current_dir}/build',
f'{option.temp_dir}/jars', f'{temp_dir}/jars',
], ],
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
) )

View File

@@ -1,5 +1,6 @@
import os import os
import pytest
from unit.applications.proto import TestApplicationProto from unit.applications.proto import TestApplicationProto
from unit.option import option from unit.option import option
from unit.utils import waitforfiles from unit.utils import waitforfiles
@@ -8,12 +9,13 @@ from unit.utils import waitforfiles
class TestNJS(TestApplicationProto): class TestNJS(TestApplicationProto):
prerequisites = {'modules': {'njs': 'any'}} prerequisites = {'modules': {'njs': 'any'}}
def setup_method(self): @pytest.fixture(autouse=True)
def setup_method_fixture(self, temp_dir):
assert 'success' in self.conf( assert 'success' in self.conf(
{ {
"listeners": {"*:7080": {"pass": "routes"}}, "listeners": {"*:7080": {"pass": "routes"}},
"routes": [ "routes": [
{"action": {"share": f"{option.temp_dir}/assets$uri"}} {"action": {"share": f"{temp_dir}/assets$uri"}}
], ],
} }
) )

View File

@@ -14,6 +14,45 @@ class TestProxy(TestApplicationPython):
SERVER_PORT = 7999 SERVER_PORT = 7999
@pytest.fixture(autouse=True)
def setup_method_fixture(self):
run_process(self.run_server, self.SERVER_PORT)
waitforsocket(self.SERVER_PORT)
python_dir = f'{option.test_dir}/python'
assert 'success' in self.conf(
{
"listeners": {
"*:7080": {"pass": "routes"},
"*:7081": {"pass": "applications/mirror"},
},
"routes": [{"action": {"proxy": "http://127.0.0.1:7081"}}],
"applications": {
"mirror": {
"type": self.get_application_type(),
"processes": {"spare": 0},
"path": f'{python_dir}/mirror',
"working_directory": f'{python_dir}/mirror',
"module": "wsgi",
},
"custom_header": {
"type": self.get_application_type(),
"processes": {"spare": 0},
"path": f'{python_dir}/custom_header',
"working_directory": f'{python_dir}/custom_header',
"module": "wsgi",
},
"delayed": {
"type": self.get_application_type(),
"processes": {"spare": 0},
"path": f'{python_dir}/delayed',
"working_directory": f'{python_dir}/delayed',
"module": "wsgi",
},
},
}
), 'proxy initial configuration'
@staticmethod @staticmethod
def run_server(server_port): def run_server(server_port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -59,44 +98,6 @@ Content-Length: 10
def post_http10(self, *args, **kwargs): def post_http10(self, *args, **kwargs):
return self.post(*args, http_10=True, **kwargs) return self.post(*args, http_10=True, **kwargs)
def setup_method(self):
run_process(self.run_server, self.SERVER_PORT)
waitforsocket(self.SERVER_PORT)
python_dir = f'{option.test_dir}/python'
assert 'success' in self.conf(
{
"listeners": {
"*:7080": {"pass": "routes"},
"*:7081": {"pass": "applications/mirror"},
},
"routes": [{"action": {"proxy": "http://127.0.0.1:7081"}}],
"applications": {
"mirror": {
"type": self.get_application_type(),
"processes": {"spare": 0},
"path": f'{python_dir}/mirror',
"working_directory": f'{python_dir}/mirror',
"module": "wsgi",
},
"custom_header": {
"type": self.get_application_type(),
"processes": {"spare": 0},
"path": f'{python_dir}/custom_header',
"working_directory": f'{python_dir}/custom_header',
"module": "wsgi",
},
"delayed": {
"type": self.get_application_type(),
"processes": {"spare": 0},
"path": f'{python_dir}/delayed',
"working_directory": f'{python_dir}/delayed',
"module": "wsgi",
},
},
}
), 'proxy initial configuration'
def test_proxy_http10(self): def test_proxy_http10(self):
for _ in range(10): for _ in range(10):
assert self.get_http10()['status'] == 200, 'status' assert self.get_http10()['status'] == 200, 'status'

View File

@@ -3,6 +3,7 @@ import select
import socket import socket
import time import time
import pytest
from conftest import run_process from conftest import run_process
from unit.applications.lang.python import TestApplicationPython from unit.applications.lang.python import TestApplicationPython
from unit.utils import waitforsocket from unit.utils import waitforsocket
@@ -13,6 +14,26 @@ class TestProxyChunked(TestApplicationPython):
SERVER_PORT = 7999 SERVER_PORT = 7999
@pytest.fixture(autouse=True)
def setup_method_fixture(self):
run_process(self.run_server, self.SERVER_PORT)
waitforsocket(self.SERVER_PORT)
assert 'success' in self.conf(
{
"listeners": {
"*:7080": {"pass": "routes"},
},
"routes": [
{
"action": {
"proxy": f'http://127.0.0.1:{self.SERVER_PORT}'
}
}
],
}
), 'proxy initial configuration'
@staticmethod @staticmethod
def run_server(server_port): def run_server(server_port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -83,25 +104,6 @@ class TestProxyChunked(TestApplicationPython):
def get_http10(self, *args, **kwargs): def get_http10(self, *args, **kwargs):
return self.get(*args, http_10=True, **kwargs) return self.get(*args, http_10=True, **kwargs)
def setup_method(self):
run_process(self.run_server, self.SERVER_PORT)
waitforsocket(self.SERVER_PORT)
assert 'success' in self.conf(
{
"listeners": {
"*:7080": {"pass": "routes"},
},
"routes": [
{
"action": {
"proxy": f'http://127.0.0.1:{self.SERVER_PORT}'
}
}
],
}
), 'proxy initial configuration'
def test_proxy_chunked(self): def test_proxy_chunked(self):
for _ in range(10): for _ in range(10):
assert self.get_http10(body='\r\n\r\n0\r\n\r\n')['status'] == 200 assert self.get_http10(body='\r\n\r\n0\r\n\r\n')['status'] == 200

View File

@@ -11,8 +11,9 @@ from unit.option import option
class TestPythonProcman(TestApplicationPython): class TestPythonProcman(TestApplicationPython):
prerequisites = {'modules': {'python': 'any'}} prerequisites = {'modules': {'python': 'any'}}
def setup_method(self): @pytest.fixture(autouse=True)
self.app_name = f'app-{option.temp_dir.split("/")[-1]}' def setup_method_fixture(self, temp_dir):
self.app_name = f'app-{temp_dir.split("/")[-1]}'
self.app_proc = f'applications/{self.app_name}/processes' self.app_proc = f'applications/{self.app_name}/processes'
self.load('empty', self.app_name) self.load('empty', self.app_name)

View File

@@ -2,8 +2,8 @@ import re
import subprocess import subprocess
import time import time
import pytest
from unit.applications.lang.python import TestApplicationPython from unit.applications.lang.python import TestApplicationPython
from unit.option import option
class TestRespawn(TestApplicationPython): class TestRespawn(TestApplicationPython):
@@ -12,8 +12,9 @@ class TestRespawn(TestApplicationPython):
PATTERN_ROUTER = 'unit: router' PATTERN_ROUTER = 'unit: router'
PATTERN_CONTROLLER = 'unit: controller' PATTERN_CONTROLLER = 'unit: controller'
def setup_method(self): @pytest.fixture(autouse=True)
self.app_name = f'app-{option.temp_dir.split("/")[-1]}' def setup_method_fixture(self, temp_dir):
self.app_name = f'app-{temp_dir.split("/")[-1]}'
self.load('empty', self.app_name) self.load('empty', self.app_name)

View File

@@ -1,12 +1,14 @@
import re import re
import pytest
from unit.applications.proto import TestApplicationProto from unit.applications.proto import TestApplicationProto
class TestReturn(TestApplicationProto): class TestReturn(TestApplicationProto):
prerequisites = {} prerequisites = {}
def setup_method(self): @pytest.fixture(autouse=True)
def setup_method_fixture(self):
self._load_conf( self._load_conf(
{ {
"listeners": {"*:7080": {"pass": "routes"}}, "listeners": {"*:7080": {"pass": "routes"}},

View File

@@ -8,7 +8,8 @@ from unit.option import option
class TestRewrite(TestApplicationProto): class TestRewrite(TestApplicationProto):
prerequisites = {} prerequisites = {}
def setup_method(self): @pytest.fixture(autouse=True)
def setup_method_fixture(self):
assert 'success' in self.conf( assert 'success' in self.conf(
{ {
"listeners": {"*:7080": {"pass": "routes"}}, "listeners": {"*:7080": {"pass": "routes"}},

View File

@@ -7,7 +7,8 @@ from unit.option import option
class TestRouting(TestApplicationPython): class TestRouting(TestApplicationPython):
prerequisites = {'modules': {'python': 'any'}} prerequisites = {'modules': {'python': 'any'}}
def setup_method(self): @pytest.fixture(autouse=True)
def setup_method_fixture(self):
assert 'success' in self.conf( assert 'success' in self.conf(
{ {
"listeners": {"*:7080": {"pass": "routes"}}, "listeners": {"*:7080": {"pass": "routes"}},

View File

@@ -3,21 +3,21 @@ import socket
import pytest import pytest
from unit.applications.proto import TestApplicationProto from unit.applications.proto import TestApplicationProto
from unit.option import option
from unit.utils import waitforfiles from unit.utils import waitforfiles
class TestStatic(TestApplicationProto): class TestStatic(TestApplicationProto):
prerequisites = {} prerequisites = {}
def setup_method(self): @pytest.fixture(autouse=True)
os.makedirs(f'{option.temp_dir}/assets/dir') def setup_method_fixture(self, temp_dir):
with open(f'{option.temp_dir}/assets/index.html', 'w') as index, open( os.makedirs(f'{temp_dir}/assets/dir')
f'{option.temp_dir}/assets/README', 'w' assets_dir = f'{temp_dir}/assets'
) as readme, open(
f'{option.temp_dir}/assets/log.log', 'w' with open(f'{assets_dir}/index.html', 'w') as index, open(
) as log, open( f'{assets_dir}/README', 'w'
f'{option.temp_dir}/assets/dir/file', 'w' ) as readme, open(f'{assets_dir}/log.log', 'w') as log, open(
f'{assets_dir}/dir/file', 'w'
) as file: ) as file:
index.write('0123456789') index.write('0123456789')
readme.write('readme') readme.write('readme')
@@ -27,9 +27,7 @@ class TestStatic(TestApplicationProto):
self._load_conf( self._load_conf(
{ {
"listeners": {"*:7080": {"pass": "routes"}}, "listeners": {"*:7080": {"pass": "routes"}},
"routes": [ "routes": [{"action": {"share": f'{assets_dir}$uri'}}],
{"action": {"share": f'{option.temp_dir}/assets$uri'}}
],
"settings": { "settings": {
"http": { "http": {
"static": { "static": {

View File

@@ -1,6 +1,7 @@
import ssl import ssl
import subprocess import subprocess
import pytest
from unit.applications.tls import TestApplicationTLS from unit.applications.tls import TestApplicationTLS
from unit.option import option from unit.option import option
@@ -8,7 +9,8 @@ from unit.option import option
class TestTLSSNI(TestApplicationTLS): class TestTLSSNI(TestApplicationTLS):
prerequisites = {'modules': {'openssl': 'any'}} prerequisites = {'modules': {'openssl': 'any'}}
def setup_method(self): @pytest.fixture(autouse=True)
def setup_method_fixture(self):
self._load_conf( self._load_conf(
{ {
"listeners": {"*:7080": {"pass": "routes"}}, "listeners": {"*:7080": {"pass": "routes"}},

View File

@@ -1,6 +1,7 @@
import os import os
import re import re
import pytest
from unit.applications.lang.python import TestApplicationPython from unit.applications.lang.python import TestApplicationPython
from unit.option import option from unit.option import option
@@ -8,7 +9,8 @@ from unit.option import option
class TestUpstreamsRR(TestApplicationPython): class TestUpstreamsRR(TestApplicationPython):
prerequisites = {'modules': {'python': 'any'}} prerequisites = {'modules': {'python': 'any'}}
def setup_method(self): @pytest.fixture(autouse=True)
def setup_method_fixture(self):
assert 'success' in self.conf( assert 'success' in self.conf(
{ {
"listeners": { "listeners": {

View File

@@ -1,6 +1,7 @@
import re import re
import time import time
import pytest
from unit.applications.proto import TestApplicationProto from unit.applications.proto import TestApplicationProto
from unit.option import option from unit.option import option
@@ -8,7 +9,8 @@ from unit.option import option
class TestVariables(TestApplicationProto): class TestVariables(TestApplicationProto):
prerequisites = {} prerequisites = {}
def setup_method(self): @pytest.fixture(autouse=True)
def setup_method_fixture(self):
assert 'success' in self.conf( assert 'success' in self.conf(
{ {
"listeners": {"*:7080": {"pass": "routes"}}, "listeners": {"*:7080": {"pass": "routes"}},