From 18fcc07c7796889fe1cc4bc86564459ccd387ae7 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Thu, 25 May 2023 16:56:14 +0100 Subject: 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. --- test/test_java_isolation_rootfs.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'test/test_java_isolation_rootfs.py') 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, ) -- 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_java_isolation_rootfs.py | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) (limited to 'test/test_java_isolation_rootfs.py') diff --git a/test/test_java_isolation_rootfs.py b/test/test_java_isolation_rootfs.py index 28bc4a0d..bbd2c915 100644 --- a/test/test_java_isolation_rootfs.py +++ b/test/test_java_isolation_rootfs.py @@ -5,15 +5,12 @@ import pytest from unit.applications.lang.java import TestApplicationJava from unit.option import option +prerequisites = {'modules': {'java': 'all'}, 'privileged_user': True} -class TestJavaIsolationRootfs(TestApplicationJava): - prerequisites = {'modules': {'java': 'all'}} +class TestJavaIsolationRootfs(TestApplicationJava): @pytest.fixture(autouse=True) - def setup_method_fixture(self, is_su, temp_dir): - if not is_su: - pytest.skip('require root') - + def setup_method_fixture(self, temp_dir): os.makedirs(f'{temp_dir}/jars') os.makedirs(f'{temp_dir}/tmp') os.chmod(f'{temp_dir}/tmp', 0o777) @@ -35,10 +32,7 @@ class TestJavaIsolationRootfs(TestApplicationJava): except subprocess.CalledProcessError: pytest.fail("Can't run mount process.") - def teardown_method(self, is_su): - if not is_su: - return - + def teardown_method(self): try: subprocess.run( ["umount", "--lazy", f"{option.temp_dir}/jars"], @@ -51,13 +45,8 @@ class TestJavaIsolationRootfs(TestApplicationJava): except subprocess.CalledProcessError: pytest.fail("Can't run umount process.") - def test_java_isolation_rootfs_chroot_war(self, is_su, temp_dir): - if not is_su: - pytest.skip('require root') - - isolation = { - 'rootfs': temp_dir, - } + def test_java_isolation_rootfs_chroot_war(self, temp_dir): + isolation = {'rootfs': temp_dir} self.load('empty_war', isolation=isolation) -- 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_java_isolation_rootfs.py | 91 +++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 45 deletions(-) (limited to 'test/test_java_isolation_rootfs.py') diff --git a/test/test_java_isolation_rootfs.py b/test/test_java_isolation_rootfs.py index bbd2c915..66b2a81e 100644 --- a/test/test_java_isolation_rootfs.py +++ b/test/test_java_isolation_rootfs.py @@ -2,64 +2,65 @@ import os import subprocess import pytest -from unit.applications.lang.java import TestApplicationJava +from unit.applications.lang.java import ApplicationJava from unit.option import option prerequisites = {'modules': {'java': 'all'}, 'privileged_user': True} +client = ApplicationJava() -class TestJavaIsolationRootfs(TestApplicationJava): - @pytest.fixture(autouse=True) - def setup_method_fixture(self, temp_dir): - os.makedirs(f'{temp_dir}/jars') - os.makedirs(f'{temp_dir}/tmp') - os.chmod(f'{temp_dir}/tmp', 0o777) - try: - subprocess.run( - [ - "mount", - "--bind", - f'{option.current_dir}/build', - f'{temp_dir}/jars', - ], - stderr=subprocess.STDOUT, - ) +@pytest.fixture(autouse=True) +def setup_method_fixture(temp_dir): + os.makedirs(f'{temp_dir}/jars') + os.makedirs(f'{temp_dir}/tmp') + os.chmod(f'{temp_dir}/tmp', 0o777) - except KeyboardInterrupt: - raise + try: + subprocess.run( + [ + "mount", + "--bind", + f'{option.current_dir}/build', + f'{temp_dir}/jars', + ], + stderr=subprocess.STDOUT, + ) - except subprocess.CalledProcessError: - pytest.fail("Can't run mount process.") + except KeyboardInterrupt: + raise - def teardown_method(self): - try: - subprocess.run( - ["umount", "--lazy", f"{option.temp_dir}/jars"], - stderr=subprocess.STDOUT, - ) + except subprocess.CalledProcessError: + pytest.fail("Can't run mount process.") - except KeyboardInterrupt: - raise + yield - except subprocess.CalledProcessError: - pytest.fail("Can't run umount process.") + try: + subprocess.run( + ["umount", "--lazy", f"{option.temp_dir}/jars"], + stderr=subprocess.STDOUT, + ) - def test_java_isolation_rootfs_chroot_war(self, temp_dir): - isolation = {'rootfs': temp_dir} + except KeyboardInterrupt: + raise - self.load('empty_war', isolation=isolation) + except subprocess.CalledProcessError: + pytest.fail("Can't run umount process.") - assert 'success' in self.conf( - '"/"', - '/config/applications/empty_war/working_directory', - ) - assert 'success' in self.conf( - '"/jars"', 'applications/empty_war/unit_jars' - ) - assert 'success' in self.conf( - '"/java/empty.war"', 'applications/empty_war/webapp' - ) +def test_java_isolation_rootfs_chroot_war(temp_dir): + client.load('empty_war', isolation={'rootfs': temp_dir}) + + assert 'success' in client.conf( + '"/"', + '/config/applications/empty_war/working_directory', + ) + + assert 'success' in client.conf( + '"/jars"', 'applications/empty_war/unit_jars' + ) + assert 'success' in client.conf( + '"/java/empty.war"', 'applications/empty_war/webapp' + ) - assert self.get()['status'] == 200, 'war' + assert client.get()['status'] == 200, 'war' -- cgit