diff options
author | Andrei Zeliankou <zelenkov@nginx.com> | 2023-05-25 16:56:14 +0100 |
---|---|---|
committer | Andrei Zeliankou <zelenkov@nginx.com> | 2023-05-25 16:56:14 +0100 |
commit | 18fcc07c7796889fe1cc4bc86564459ccd387ae7 (patch) | |
tree | b38ea8171bb88ccdee8317747389789944198fc4 /test | |
parent | 3e4fa1e2022970dee003bea0932ea0c10f8744ba (diff) | |
download | unit-18fcc07c7796889fe1cc4bc86564459ccd387ae7.tar.gz unit-18fcc07c7796889fe1cc4bc86564459ccd387ae7.tar.bz2 |
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.
Diffstat (limited to '')
-rw-r--r-- | test/test_client_ip.py | 8 | ||||
-rw-r--r-- | test/test_forwarded_header.py | 8 | ||||
-rw-r--r-- | test/test_java_isolation_rootfs.py | 11 | ||||
-rw-r--r-- | test/test_njs.py | 6 | ||||
-rw-r--r-- | test/test_proxy.py | 77 | ||||
-rw-r--r-- | test/test_proxy_chunked.py | 40 | ||||
-rw-r--r-- | test/test_python_procman.py | 5 | ||||
-rw-r--r-- | test/test_respawn.py | 7 | ||||
-rw-r--r-- | test/test_return.py | 4 | ||||
-rw-r--r-- | test/test_rewrite.py | 3 | ||||
-rw-r--r-- | test/test_routing.py | 3 | ||||
-rw-r--r-- | test/test_static.py | 22 | ||||
-rw-r--r-- | test/test_tls_sni.py | 4 | ||||
-rw-r--r-- | test/test_upstreams_rr.py | 4 | ||||
-rw-r--r-- | test/test_variables.py | 4 |
15 files changed, 113 insertions, 93 deletions
diff --git a/test/test_client_ip.py b/test/test_client_ip.py index d0e7b793..59e170f1 100644 --- a/test/test_client_ip.py +++ b/test/test_client_ip.py @@ -1,3 +1,4 @@ +import pytest from unit.applications.lang.python import TestApplicationPython from unit.option import option @@ -5,6 +6,10 @@ from unit.option import option class TestClientIP(TestApplicationPython): prerequisites = {'modules': {'python': 'any'}} + @pytest.fixture(autouse=True) + def setup_method_fixture(self): + self.load('client_ip') + def client_ip(self, options): assert 'success' in self.conf( { @@ -39,9 +44,6 @@ class TestClientIP(TestApplicationPython): headers={'Connection': 'close', 'X-Forwarded-For': xff}, )['body'] - def setup_method(self): - self.load('client_ip') - def test_client_ip_single_ip(self): self.client_ip( {'header': 'X-Forwarded-For', 'source': '123.123.123.123'} diff --git a/test/test_forwarded_header.py b/test/test_forwarded_header.py index eb2f25f8..9c6e0395 100644 --- a/test/test_forwarded_header.py +++ b/test/test_forwarded_header.py @@ -1,9 +1,14 @@ +import pytest from unit.applications.lang.python import TestApplicationPython class TestForwardedHeader(TestApplicationPython): prerequisites = {'modules': {'python': 'any'}} + @pytest.fixture(autouse=True) + def setup_method_fixture(self): + self.load('forwarded_header') + def forwarded_header(self, forwarded): assert 'success' in self.conf( { @@ -40,9 +45,6 @@ class TestForwardedHeader(TestApplicationPython): def get_scheme(self, *args, **kwargs): return self.get_fwd(*args, **kwargs)['Url-Scheme'] - def setup_method(self): - self.load('forwarded_header') - def test_forwarded_header_single_ip(self): self.forwarded_header( { diff --git a/test/test_java_isolation_rootfs.py b/test/test_java_isolation_rootfs.py index 28668997..28bc4a0d 100644 --- a/test/test_java_isolation_rootfs.py +++ b/test/test_java_isolation_rootfs.py @@ -9,13 +9,14 @@ from unit.option import option class TestJavaIsolationRootfs(TestApplicationJava): 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: pytest.skip('require root') - os.makedirs(f'{option.temp_dir}/jars') - os.makedirs(f'{option.temp_dir}/tmp') - os.chmod(f'{option.temp_dir}/tmp', 0o777) + os.makedirs(f'{temp_dir}/jars') + os.makedirs(f'{temp_dir}/tmp') + os.chmod(f'{temp_dir}/tmp', 0o777) try: subprocess.run( @@ -23,7 +24,7 @@ class TestJavaIsolationRootfs(TestApplicationJava): "mount", "--bind", f'{option.current_dir}/build', - f'{option.temp_dir}/jars', + f'{temp_dir}/jars', ], stderr=subprocess.STDOUT, ) diff --git a/test/test_njs.py b/test/test_njs.py index bd89e16d..86a8a4ba 100644 --- a/test/test_njs.py +++ b/test/test_njs.py @@ -1,5 +1,6 @@ import os +import pytest from unit.applications.proto import TestApplicationProto from unit.option import option from unit.utils import waitforfiles @@ -8,12 +9,13 @@ from unit.utils import waitforfiles class TestNJS(TestApplicationProto): prerequisites = {'modules': {'njs': 'any'}} - def setup_method(self): + @pytest.fixture(autouse=True) + def setup_method_fixture(self, temp_dir): assert 'success' in self.conf( { "listeners": {"*:7080": {"pass": "routes"}}, "routes": [ - {"action": {"share": f"{option.temp_dir}/assets$uri"}} + {"action": {"share": f"{temp_dir}/assets$uri"}} ], } ) diff --git a/test/test_proxy.py b/test/test_proxy.py index 2f1775cb..23b4a6a4 100644 --- a/test/test_proxy.py +++ b/test/test_proxy.py @@ -14,6 +14,45 @@ class TestProxy(TestApplicationPython): 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 def run_server(server_port): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -59,44 +98,6 @@ Content-Length: 10 def post_http10(self, *args, **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): for _ in range(10): assert self.get_http10()['status'] == 200, 'status' diff --git a/test/test_proxy_chunked.py b/test/test_proxy_chunked.py index 66aab67f..b5198e9c 100644 --- a/test/test_proxy_chunked.py +++ b/test/test_proxy_chunked.py @@ -3,6 +3,7 @@ import select import socket import time +import pytest from conftest import run_process from unit.applications.lang.python import TestApplicationPython from unit.utils import waitforsocket @@ -13,6 +14,26 @@ class TestProxyChunked(TestApplicationPython): 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 def run_server(server_port): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -83,25 +104,6 @@ class TestProxyChunked(TestApplicationPython): def get_http10(self, *args, **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): for _ in range(10): assert self.get_http10(body='\r\n\r\n0\r\n\r\n')['status'] == 200 diff --git a/test/test_python_procman.py b/test/test_python_procman.py index d3bcaa53..82766d65 100644 --- a/test/test_python_procman.py +++ b/test/test_python_procman.py @@ -11,8 +11,9 @@ from unit.option import option class TestPythonProcman(TestApplicationPython): prerequisites = {'modules': {'python': 'any'}} - def setup_method(self): - self.app_name = f'app-{option.temp_dir.split("/")[-1]}' + @pytest.fixture(autouse=True) + 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.load('empty', self.app_name) diff --git a/test/test_respawn.py b/test/test_respawn.py index 00271503..2d01cf3b 100644 --- a/test/test_respawn.py +++ b/test/test_respawn.py @@ -2,8 +2,8 @@ import re import subprocess import time +import pytest from unit.applications.lang.python import TestApplicationPython -from unit.option import option class TestRespawn(TestApplicationPython): @@ -12,8 +12,9 @@ class TestRespawn(TestApplicationPython): PATTERN_ROUTER = 'unit: router' PATTERN_CONTROLLER = 'unit: controller' - def setup_method(self): - self.app_name = f'app-{option.temp_dir.split("/")[-1]}' + @pytest.fixture(autouse=True) + def setup_method_fixture(self, temp_dir): + self.app_name = f'app-{temp_dir.split("/")[-1]}' self.load('empty', self.app_name) diff --git a/test/test_return.py b/test/test_return.py index e65a7a31..71b1242c 100644 --- a/test/test_return.py +++ b/test/test_return.py @@ -1,12 +1,14 @@ import re +import pytest from unit.applications.proto import TestApplicationProto class TestReturn(TestApplicationProto): prerequisites = {} - def setup_method(self): + @pytest.fixture(autouse=True) + def setup_method_fixture(self): self._load_conf( { "listeners": {"*:7080": {"pass": "routes"}}, diff --git a/test/test_rewrite.py b/test/test_rewrite.py index 3bc7df19..ba4ca577 100644 --- a/test/test_rewrite.py +++ b/test/test_rewrite.py @@ -8,7 +8,8 @@ from unit.option import option class TestRewrite(TestApplicationProto): prerequisites = {} - def setup_method(self): + @pytest.fixture(autouse=True) + def setup_method_fixture(self): assert 'success' in self.conf( { "listeners": {"*:7080": {"pass": "routes"}}, diff --git a/test/test_routing.py b/test/test_routing.py index 4909a698..41de6f51 100644 --- a/test/test_routing.py +++ b/test/test_routing.py @@ -7,7 +7,8 @@ from unit.option import option class TestRouting(TestApplicationPython): prerequisites = {'modules': {'python': 'any'}} - def setup_method(self): + @pytest.fixture(autouse=True) + def setup_method_fixture(self): assert 'success' in self.conf( { "listeners": {"*:7080": {"pass": "routes"}}, diff --git a/test/test_static.py b/test/test_static.py index f7eade7c..48004661 100644 --- a/test/test_static.py +++ b/test/test_static.py @@ -3,21 +3,21 @@ import socket import pytest from unit.applications.proto import TestApplicationProto -from unit.option import option from unit.utils import waitforfiles class TestStatic(TestApplicationProto): prerequisites = {} - def setup_method(self): - os.makedirs(f'{option.temp_dir}/assets/dir') - with open(f'{option.temp_dir}/assets/index.html', 'w') as index, open( - f'{option.temp_dir}/assets/README', 'w' - ) as readme, open( - f'{option.temp_dir}/assets/log.log', 'w' - ) as log, open( - f'{option.temp_dir}/assets/dir/file', 'w' + @pytest.fixture(autouse=True) + def setup_method_fixture(self, temp_dir): + os.makedirs(f'{temp_dir}/assets/dir') + assets_dir = f'{temp_dir}/assets' + + with open(f'{assets_dir}/index.html', 'w') as index, open( + f'{assets_dir}/README', 'w' + ) as readme, open(f'{assets_dir}/log.log', 'w') as log, open( + f'{assets_dir}/dir/file', 'w' ) as file: index.write('0123456789') readme.write('readme') @@ -27,9 +27,7 @@ class TestStatic(TestApplicationProto): self._load_conf( { "listeners": {"*:7080": {"pass": "routes"}}, - "routes": [ - {"action": {"share": f'{option.temp_dir}/assets$uri'}} - ], + "routes": [{"action": {"share": f'{assets_dir}$uri'}}], "settings": { "http": { "static": { diff --git a/test/test_tls_sni.py b/test/test_tls_sni.py index e918bb20..18b85ed8 100644 --- a/test/test_tls_sni.py +++ b/test/test_tls_sni.py @@ -1,6 +1,7 @@ import ssl import subprocess +import pytest from unit.applications.tls import TestApplicationTLS from unit.option import option @@ -8,7 +9,8 @@ from unit.option import option class TestTLSSNI(TestApplicationTLS): prerequisites = {'modules': {'openssl': 'any'}} - def setup_method(self): + @pytest.fixture(autouse=True) + def setup_method_fixture(self): self._load_conf( { "listeners": {"*:7080": {"pass": "routes"}}, diff --git a/test/test_upstreams_rr.py b/test/test_upstreams_rr.py index 324c93cb..2bb23be0 100644 --- a/test/test_upstreams_rr.py +++ b/test/test_upstreams_rr.py @@ -1,6 +1,7 @@ import os import re +import pytest from unit.applications.lang.python import TestApplicationPython from unit.option import option @@ -8,7 +9,8 @@ from unit.option import option class TestUpstreamsRR(TestApplicationPython): prerequisites = {'modules': {'python': 'any'}} - def setup_method(self): + @pytest.fixture(autouse=True) + def setup_method_fixture(self): assert 'success' in self.conf( { "listeners": { diff --git a/test/test_variables.py b/test/test_variables.py index 545d61e9..4b924147 100644 --- a/test/test_variables.py +++ b/test/test_variables.py @@ -1,6 +1,7 @@ import re import time +import pytest from unit.applications.proto import TestApplicationProto from unit.option import option @@ -8,7 +9,8 @@ from unit.option import option class TestVariables(TestApplicationProto): prerequisites = {} - def setup_method(self): + @pytest.fixture(autouse=True) + def setup_method_fixture(self): assert 'success' in self.conf( { "listeners": {"*:7080": {"pass": "routes"}}, |