From 31ff94add9c4043a753683d9e8b68733c69aa1ac Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Mon, 29 May 2023 16:45:49 +0100 Subject: Tests: more fixtures. Common methods from applications/proto.py converted to the fixtures. sysctl check moved to the specific file where it is using. Some options moved to the constructor to have early access. --- test/test_usr1.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'test/test_usr1.py') diff --git a/test/test_usr1.py b/test/test_usr1.py index 4bff0242..30160a88 100644 --- a/test/test_usr1.py +++ b/test/test_usr1.py @@ -9,7 +9,9 @@ from unit.utils import waitforfiles class TestUSR1(TestApplicationPython): prerequisites = {'modules': {'python': 'any'}} - def test_usr1_access_log(self, temp_dir, unit_pid): + def test_usr1_access_log( + self, search_in_file, temp_dir, unit_pid, wait_for_record + ): self.load('empty') log = 'access.log' @@ -27,7 +29,7 @@ class TestUSR1(TestApplicationPython): assert self.get()['status'] == 200 assert ( - self.wait_for_record(r'"GET / HTTP/1.1" 200 0 "-" "-"', log_new) + wait_for_record(r'"GET / HTTP/1.1" 200 0 "-" "-"', log_new) is not None ), 'rename new' assert not os.path.isfile(log_path), 'rename old' @@ -39,12 +41,14 @@ class TestUSR1(TestApplicationPython): assert self.get(url='/usr1')['status'] == 200 assert ( - self.wait_for_record(r'"GET /usr1 HTTP/1.1" 200 0 "-" "-"', log) + wait_for_record(r'"GET /usr1 HTTP/1.1" 200 0 "-" "-"', log) is not None ), 'reopen 2' - assert self.search_in_log(r'/usr1', log_new) is None, 'rename new 2' + assert search_in_file(r'/usr1', log_new) is None, 'rename new 2' - def test_usr1_unit_log(self, temp_dir, unit_pid): + def test_usr1_unit_log( + self, search_in_file, temp_dir, unit_pid, wait_for_record + ): self.load('log_body') log_new = 'new.log' @@ -59,7 +63,7 @@ class TestUSR1(TestApplicationPython): body = 'body_for_a_log_new\n' assert self.post(body=body)['status'] == 200 - assert self.wait_for_record(body, log_new) is not None, 'rename new' + assert wait_for_record(body, log_new) is not None, 'rename new' assert not os.path.isfile(log_path), 'rename old' os.kill(unit_pid, signal.SIGUSR1) @@ -69,8 +73,8 @@ class TestUSR1(TestApplicationPython): body = 'body_for_a_log_unit\n' assert self.post(body=body)['status'] == 200 - assert self.wait_for_record(body) is not None, 'rename new' - assert self.search_in_log(body, log_new) is None, 'rename new 2' + assert wait_for_record(body) is not None, 'rename new' + assert search_in_file(body, log_new) is None, 'rename new 2' finally: # merge two log files into unit.log to check alerts -- cgit From ce2405ec3dd97e8bdf8f63312e3c6ce59ef562d4 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Mon, 12 Jun 2023 14:16:59 +0100 Subject: Tests: prerequisites checking reworked. Prerequisites check moved to the module level to simplify class structure. Discovery and prerequisites checks functions moved to the separate files. Introduced "require" fixture to provide per-test requirements check. --- test/test_usr1.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/test_usr1.py') diff --git a/test/test_usr1.py b/test/test_usr1.py index 30160a88..160cef77 100644 --- a/test/test_usr1.py +++ b/test/test_usr1.py @@ -5,10 +5,10 @@ from unit.applications.lang.python import TestApplicationPython from unit.log import Log from unit.utils import waitforfiles +prerequisites = {'modules': {'python': 'any'}} -class TestUSR1(TestApplicationPython): - prerequisites = {'modules': {'python': 'any'}} +class TestUSR1(TestApplicationPython): def test_usr1_access_log( self, search_in_file, temp_dir, unit_pid, wait_for_record ): -- cgit From c183bd8749a19477390f8cb77efe5f6d223f0905 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Wed, 14 Jun 2023 18:20:09 +0100 Subject: Tests: get rid of classes in test files. Class usage came from the unittest framework and it was always redundant after migration to the pytest. This commit removes classes from files containing tests to make them more readable and understandable. --- test/test_usr1.py | 112 ++++++++++++++++++++++++++---------------------------- 1 file changed, 54 insertions(+), 58 deletions(-) (limited to 'test/test_usr1.py') diff --git a/test/test_usr1.py b/test/test_usr1.py index 160cef77..ce756fc0 100644 --- a/test/test_usr1.py +++ b/test/test_usr1.py @@ -1,91 +1,87 @@ import os import signal -from unit.applications.lang.python import TestApplicationPython +from unit.applications.lang.python import ApplicationPython from unit.log import Log from unit.utils import waitforfiles prerequisites = {'modules': {'python': 'any'}} +client = ApplicationPython() -class TestUSR1(TestApplicationPython): - def test_usr1_access_log( - self, search_in_file, temp_dir, unit_pid, wait_for_record - ): - self.load('empty') - log = 'access.log' - log_new = 'new.log' - log_path = f'{temp_dir}/{log}' +def test_usr1_access_log(search_in_file, temp_dir, unit_pid, wait_for_record): + client.load('empty') - assert 'success' in self.conf( - f'"{log_path}"', 'access_log' - ), 'access log configure' + log = 'access.log' + log_new = 'new.log' + log_path = f'{temp_dir}/{log}' - assert waitforfiles(log_path), 'open' + assert 'success' in client.conf( + f'"{log_path}"', 'access_log' + ), 'access log configure' - os.rename(log_path, f'{temp_dir}/{log_new}') + assert waitforfiles(log_path), 'open' - assert self.get()['status'] == 200 + os.rename(log_path, f'{temp_dir}/{log_new}') - assert ( - wait_for_record(r'"GET / HTTP/1.1" 200 0 "-" "-"', log_new) - is not None - ), 'rename new' - assert not os.path.isfile(log_path), 'rename old' + assert client.get()['status'] == 200 - os.kill(unit_pid, signal.SIGUSR1) + assert ( + wait_for_record(r'"GET / HTTP/1.1" 200 0 "-" "-"', log_new) is not None + ), 'rename new' + assert not os.path.isfile(log_path), 'rename old' - assert waitforfiles(log_path), 'reopen' + os.kill(unit_pid, signal.SIGUSR1) - assert self.get(url='/usr1')['status'] == 200 + assert waitforfiles(log_path), 'reopen' - assert ( - wait_for_record(r'"GET /usr1 HTTP/1.1" 200 0 "-" "-"', log) - is not None - ), 'reopen 2' - assert search_in_file(r'/usr1', log_new) is None, 'rename new 2' + assert client.get(url='/usr1')['status'] == 200 - def test_usr1_unit_log( - self, search_in_file, temp_dir, unit_pid, wait_for_record - ): - self.load('log_body') + assert ( + wait_for_record(r'"GET /usr1 HTTP/1.1" 200 0 "-" "-"', log) is not None + ), 'reopen 2' + assert search_in_file(r'/usr1', log_new) is None, 'rename new 2' - log_new = 'new.log' - log_path = f'{temp_dir}/unit.log' - log_path_new = f'{temp_dir}/{log_new}' - os.rename(log_path, log_path_new) +def test_usr1_unit_log(search_in_file, temp_dir, unit_pid, wait_for_record): + client.load('log_body') - Log.swap(log_new) + log_new = 'new.log' + log_path = f'{temp_dir}/unit.log' + log_path_new = f'{temp_dir}/{log_new}' + + os.rename(log_path, log_path_new) - try: - body = 'body_for_a_log_new\n' - assert self.post(body=body)['status'] == 200 + Log.swap(log_new) - assert wait_for_record(body, log_new) is not None, 'rename new' - assert not os.path.isfile(log_path), 'rename old' + try: + body = 'body_for_a_log_new\n' + assert client.post(body=body)['status'] == 200 - os.kill(unit_pid, signal.SIGUSR1) + assert wait_for_record(body, log_new) is not None, 'rename new' + assert not os.path.isfile(log_path), 'rename old' - assert waitforfiles(log_path), 'reopen' + os.kill(unit_pid, signal.SIGUSR1) - body = 'body_for_a_log_unit\n' - assert self.post(body=body)['status'] == 200 + assert waitforfiles(log_path), 'reopen' - assert wait_for_record(body) is not None, 'rename new' - assert search_in_file(body, log_new) is None, 'rename new 2' + body = 'body_for_a_log_unit\n' + assert client.post(body=body)['status'] == 200 - finally: - # merge two log files into unit.log to check alerts + assert wait_for_record(body) is not None, 'rename new' + assert search_in_file(body, log_new) is None, 'rename new 2' - with open(log_path, 'r', errors='ignore') as unit_log: - log = unit_log.read() + finally: + # merge two log files into unit.log to check alerts - with open(log_path, 'w') as unit_log, open( - log_path_new, 'r', errors='ignore' - ) as unit_log_new: - unit_log.write(unit_log_new.read()) - unit_log.write(log) + with open(log_path, 'r', errors='ignore') as unit_log: + log = unit_log.read() - Log.swap(log_new) + with open(log_path, 'w') as unit_log, open( + log_path_new, 'r', errors='ignore' + ) as unit_log_new: + unit_log.write(unit_log_new.read()) + unit_log.write(log) + + Log.swap(log_new) -- cgit