From cd8fcdb6ef12de946eab382e22b470c90b099956 Mon Sep 17 00:00:00 2001 From: Andras Mitzki Date: Fri, 7 Jun 2019 13:55:10 +0200 Subject: [PATCH 1/6] LightOperations: Move calculate_testcase_name() into a new file - start using a seperate file for pytest related operations - with this we can avoid future cyclic import problem between operations.py and testcase_parameters.py Signed-off-by: Andras Mitzki --- .../functional_tests/conftest.py | 2 +- .../src/common/operations.py | 6 ---- .../src/common/pytest_operations.py | 28 +++++++++++++++++++ .../testcase_parameters.py | 2 +- 4 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 tests/python_functional/src/common/pytest_operations.py diff --git a/tests/python_functional/functional_tests/conftest.py b/tests/python_functional/functional_tests/conftest.py index c6849a8910..18f451433c 100644 --- a/tests/python_functional/functional_tests/conftest.py +++ b/tests/python_functional/functional_tests/conftest.py @@ -24,8 +24,8 @@ from pathlib2 import Path -from src.common.operations import calculate_testcase_name from src.common.operations import copy_file +from src.common.pytest_operations import calculate_testcase_name logger = logging.getLogger(__name__) diff --git a/tests/python_functional/src/common/operations.py b/tests/python_functional/src/common/operations.py index 982ac1bac1..eb97852baa 100644 --- a/tests/python_functional/src/common/operations.py +++ b/tests/python_functional/src/common/operations.py @@ -51,9 +51,3 @@ def delete_session_file(shared_file_name, syslog_ng_testcase): working_dir = syslog_ng_testcase.testcase_parameters.get_working_dir() shared_file_name = Path(working_dir, shared_file_name) shared_file_name.unlink() - - -def calculate_testcase_name(pytest_request): - # In case of parametrized tests we need to replace "[" and "]" because - # testcase name will appear in directory name - return pytest_request.node.name.replace("[", "_").replace("]", "_") diff --git a/tests/python_functional/src/common/pytest_operations.py b/tests/python_functional/src/common/pytest_operations.py new file mode 100644 index 0000000000..89ff6e193e --- /dev/null +++ b/tests/python_functional/src/common/pytest_operations.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +############################################################################# +# Copyright (c) 2015-2019 Balabit +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 2 as published +# by the Free Software Foundation, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# +# As an additional exemption you are allowed to compile & link against the +# OpenSSL libraries as published by the OpenSSL project. See the file +# COPYING for details. +# +############################################################################# + + +def calculate_testcase_name(pytest_request): + # In case of parametrized tests we need to replace "[" and "]" because + # testcase name will appear in directory name + return pytest_request.node.name.replace("[", "_").replace("]", "_") diff --git a/tests/python_functional/src/testcase_parameters/testcase_parameters.py b/tests/python_functional/src/testcase_parameters/testcase_parameters.py index 2a6be6014f..ca53e775e0 100644 --- a/tests/python_functional/src/testcase_parameters/testcase_parameters.py +++ b/tests/python_functional/src/testcase_parameters/testcase_parameters.py @@ -22,7 +22,7 @@ ############################################################################# from pathlib2 import Path -from src.common.operations import calculate_testcase_name +from src.common.pytest_operations import calculate_testcase_name class TestcaseParameters(object): From 65178be4252e252aa21c7f03f42aa0dcac7837e9 Mon Sep 17 00:00:00 2001 From: Andras Mitzki Date: Fri, 7 Jun 2019 13:55:35 +0200 Subject: [PATCH 2/6] LightTestcaseParameters: Change WorkingDir to a global variable - this variable is used on multiple places around in Light, and it was bothering to pass this variable through multiple layers Signed-off-by: Andras Mitzki --- tests/python_functional/functional_tests/conftest.py | 3 ++- .../src/testcase_parameters/testcase_parameters.py | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/python_functional/functional_tests/conftest.py b/tests/python_functional/functional_tests/conftest.py index 18f451433c..e31174f5bd 100644 --- a/tests/python_functional/functional_tests/conftest.py +++ b/tests/python_functional/functional_tests/conftest.py @@ -24,6 +24,7 @@ from pathlib2 import Path +import src.testcase_parameters.testcase_parameters as tc_parameters from src.common.operations import copy_file from src.common.pytest_operations import calculate_testcase_name @@ -33,7 +34,7 @@ def pytest_runtest_setup(item): def prepare_testcase_working_dir(pytest_request): testcase_parameters = pytest_request.getfixturevalue("testcase_parameters") - working_directory = testcase_parameters.get_working_dir() + tc_parameters.WORKING_DIR = working_directory = testcase_parameters.get_working_dir() if not working_directory.exists(): working_directory.mkdir(parents=True) testcase_file_path = testcase_parameters.get_testcase_file() diff --git a/tests/python_functional/src/testcase_parameters/testcase_parameters.py b/tests/python_functional/src/testcase_parameters/testcase_parameters.py index ca53e775e0..ede7c3a1ba 100644 --- a/tests/python_functional/src/testcase_parameters/testcase_parameters.py +++ b/tests/python_functional/src/testcase_parameters/testcase_parameters.py @@ -24,6 +24,8 @@ from src.common.pytest_operations import calculate_testcase_name +WORKING_DIR = None + class TestcaseParameters(object): def __init__(self, pytest_request): From 4ae434778424e46a6c42da364404117002260d4c Mon Sep 17 00:00:00 2001 From: Andras Mitzki Date: Fri, 7 Jun 2019 13:55:54 +0200 Subject: [PATCH 3/6] LightOperations: Start using WorkingDir as a global variable Signed-off-by: Andras Mitzki --- tests/python_functional/src/common/operations.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/python_functional/src/common/operations.py b/tests/python_functional/src/common/operations.py index eb97852baa..ca54c6602f 100644 --- a/tests/python_functional/src/common/operations.py +++ b/tests/python_functional/src/common/operations.py @@ -24,6 +24,8 @@ from pathlib2 import Path +import src.testcase_parameters.testcase_parameters as tc_parameters + def open_file(file_path, mode): # Python 2 compatibility note: open() can work only with string representation of path @@ -43,11 +45,9 @@ def cast_to_list(items): def copy_shared_file(shared_file_name, syslog_ng_testcase): shared_dir = syslog_ng_testcase.testcase_parameters.get_shared_dir() - working_dir = syslog_ng_testcase.testcase_parameters.get_working_dir() - copy_file(Path(shared_dir, shared_file_name), working_dir) + copy_file(Path(shared_dir, shared_file_name), tc_parameters.WORKING_DIR) -def delete_session_file(shared_file_name, syslog_ng_testcase): - working_dir = syslog_ng_testcase.testcase_parameters.get_working_dir() - shared_file_name = Path(working_dir, shared_file_name) +def delete_session_file(shared_file_name): + shared_file_name = Path(tc_parameters.WORKING_DIR, shared_file_name) shared_file_name.unlink() From 81a0b2dfb108967d2c4795b717949b0c4b8146ed Mon Sep 17 00:00:00 2001 From: Andras Mitzki Date: Fri, 7 Jun 2019 13:56:09 +0200 Subject: [PATCH 4/6] LightSyslogNgPath: Start using WorkingDir as a global variable - start using global WorkingDir instead of copied in SyslogNgPath - remove get_working_dir() from SyslogNgPath Signed-off-by: Andras Mitzki --- tests/python_functional/src/syslog_ng/syslog_ng_cli.py | 3 ++- tests/python_functional/src/syslog_ng/syslog_ng_paths.py | 8 +++----- .../src/syslog_ng/tests/test_syslog_ng_paths.py | 2 +- .../src/syslog_ng_ctl/syslog_ng_ctl_executor.py | 4 ++-- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/tests/python_functional/src/syslog_ng/syslog_ng_cli.py b/tests/python_functional/src/syslog_ng/syslog_ng_cli.py index 2809edddc1..3b823fdb2d 100644 --- a/tests/python_functional/src/syslog_ng/syslog_ng_cli.py +++ b/tests/python_functional/src/syslog_ng/syslog_ng_cli.py @@ -24,6 +24,7 @@ from pathlib2 import Path +import src.testcase_parameters.testcase_parameters as tc_parameters from src.common.blocking import wait_until_false from src.common.blocking import wait_until_true from src.syslog_ng.console_log_reader import ConsoleLogReader @@ -152,6 +153,6 @@ def __handle_core_file(self): core_file_found = True self.__process = None self.__syslog_ng_executor.get_backtrace_from_core(core_file=str(core_file)) - core_file.replace(Path(self.__instance_paths.get_working_dir(), core_file)) + core_file.replace(Path(tc_parameters.WORKING_DIR, core_file)) if core_file_found: raise Exception("syslog-ng core file was found and processed") diff --git a/tests/python_functional/src/syslog_ng/syslog_ng_paths.py b/tests/python_functional/src/syslog_ng/syslog_ng_paths.py index 8f8454ce10..cf4052dacf 100644 --- a/tests/python_functional/src/syslog_ng/syslog_ng_paths.py +++ b/tests/python_functional/src/syslog_ng/syslog_ng_paths.py @@ -22,6 +22,7 @@ ############################################################################# from pathlib2 import Path +import src.testcase_parameters.testcase_parameters as tc_parameters from src.common.random_id import get_unique_id @@ -35,14 +36,14 @@ def set_syslog_ng_paths(self, instance_name): if self.__instance_name is not None: raise Exception("Instance already configured") self.__instance_name = instance_name - working_dir = self.__testcase_parameters.get_working_dir() + working_dir = tc_parameters.WORKING_DIR relative_working_dir = self.__testcase_parameters.get_relative_working_dir() install_dir = self.__testcase_parameters.get_install_dir() if not install_dir: raise ValueError("Missing --installdir start parameter") self.__syslog_ng_paths = { - "dirs": {"working_dir": working_dir, "install_dir": Path(install_dir)}, + "dirs": {"install_dir": Path(install_dir)}, "file_paths": { "config_path": Path(working_dir, "syslog_ng_{}.conf".format(instance_name)), "persist_path": Path(working_dir, "syslog_ng_{}.persist".format(instance_name)), @@ -64,9 +65,6 @@ def set_syslog_ng_paths(self, instance_name): def get_instance_name(self): return self.__instance_name - def get_working_dir(self): - return self.__syslog_ng_paths["dirs"]["working_dir"] - def get_install_dir(self): return self.__syslog_ng_paths["dirs"]["install_dir"] diff --git a/tests/python_functional/src/syslog_ng/tests/test_syslog_ng_paths.py b/tests/python_functional/src/syslog_ng/tests/test_syslog_ng_paths.py index 654138a051..f4aa67e85c 100644 --- a/tests/python_functional/src/syslog_ng/tests/test_syslog_ng_paths.py +++ b/tests/python_functional/src/syslog_ng/tests/test_syslog_ng_paths.py @@ -30,7 +30,7 @@ def test_syslog_ng_paths(fake_testcase_parameters): syslog_ng_paths = SyslogNgPaths(fake_testcase_parameters) syslog_ng_paths.set_syslog_ng_paths(instance_name="server") assert set(list(syslog_ng_paths._SyslogNgPaths__syslog_ng_paths)) == {"dirs", "file_paths", "binary_file_paths"} - assert set(list(syslog_ng_paths._SyslogNgPaths__syslog_ng_paths["dirs"])) == {"working_dir", "install_dir"} + assert set(list(syslog_ng_paths._SyslogNgPaths__syslog_ng_paths["dirs"])) == {"install_dir"} assert set(list(syslog_ng_paths._SyslogNgPaths__syslog_ng_paths["file_paths"])) == { "config_path", "persist_path", diff --git a/tests/python_functional/src/syslog_ng_ctl/syslog_ng_ctl_executor.py b/tests/python_functional/src/syslog_ng_ctl/syslog_ng_ctl_executor.py index a02606233c..42eccf2346 100644 --- a/tests/python_functional/src/syslog_ng_ctl/syslog_ng_ctl_executor.py +++ b/tests/python_functional/src/syslog_ng_ctl/syslog_ng_ctl_executor.py @@ -22,13 +22,13 @@ ############################################################################# from pathlib2 import Path +import src.testcase_parameters.testcase_parameters as tc_parameters from src.executors.command_executor import CommandExecutor class SyslogNgCtlExecutor(object): def __init__(self, instance_paths): self.__instance_paths = instance_paths - self.__working_dir = instance_paths.get_working_dir() self.__syslog_ng_control_tool_path = instance_paths.get_syslog_ng_ctl_bin() self.__syslog_ng_control_socket_path = instance_paths.get_control_socket_path() self.__command_executor = CommandExecutor() @@ -42,7 +42,7 @@ def run_command(self, command_short_name, command): def __construct_std_file_path(self, command_short_name, std_type): instance_name = self.__instance_paths.get_instance_name() - return Path(self.__working_dir, "syslog_ng_ctl_{}_{}_{}".format(instance_name, command_short_name, std_type)) + return Path(tc_parameters.WORKING_DIR, "syslog_ng_ctl_{}_{}_{}".format(instance_name, command_short_name, std_type)) def __construct_ctl_command(self, command): ctl_command = [self.__syslog_ng_control_tool_path] From 684b274a9ed657ebd062a773e0a6c7aafab28313 Mon Sep 17 00:00:00 2001 From: Andras Mitzki Date: Fri, 7 Jun 2019 13:57:13 +0200 Subject: [PATCH 5/6] LightSyslogNgConfig: Start using WorkingDir as a global variable - remove working_dir dependency from SyslogNgConfig Signed-off-by: Andras Mitzki --- tests/python_functional/conftest.py | 4 ++-- tests/python_functional/src/syslog_ng_config/renderer.py | 3 +-- .../statements/destinations/file_destination.py | 5 +++-- .../syslog_ng_config/statements/sources/file_source.py | 8 ++++---- .../src/syslog_ng_config/syslog_ng_config.py | 9 ++++----- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/tests/python_functional/conftest.py b/tests/python_functional/conftest.py index 8cd131e44f..5b139c6ef2 100644 --- a/tests/python_functional/conftest.py +++ b/tests/python_functional/conftest.py @@ -97,8 +97,8 @@ def testcase_parameters(request): @pytest.fixture -def config(request, testcase_parameters): - return SyslogNgConfig(testcase_parameters.get_working_dir(), request.getfixturevalue("version")) +def config(request): + return SyslogNgConfig(request.getfixturevalue("version")) @pytest.fixture diff --git a/tests/python_functional/src/syslog_ng_config/renderer.py b/tests/python_functional/src/syslog_ng_config/renderer.py index 001810e7f1..d1fb437c72 100644 --- a/tests/python_functional/src/syslog_ng_config/renderer.py +++ b/tests/python_functional/src/syslog_ng_config/renderer.py @@ -23,9 +23,8 @@ class ConfigRenderer(object): - def __init__(self, syslog_ng_config, working_dir): + def __init__(self, syslog_ng_config): self.__syslog_ng_config = syslog_ng_config - self.__working_dir = working_dir self.__syslog_ng_config_content = "" self.__render() diff --git a/tests/python_functional/src/syslog_ng_config/statements/destinations/file_destination.py b/tests/python_functional/src/syslog_ng_config/statements/destinations/file_destination.py index 30001b322c..3ebf16e6e6 100644 --- a/tests/python_functional/src/syslog_ng_config/statements/destinations/file_destination.py +++ b/tests/python_functional/src/syslog_ng_config/statements/destinations/file_destination.py @@ -22,14 +22,15 @@ ############################################################################# from pathlib2 import Path +import src.testcase_parameters.testcase_parameters as tc_parameters from src.driver_io.file.file_io import FileIO from src.syslog_ng_config.statements.destinations.destination_driver import DestinationDriver class FileDestination(DestinationDriver): - def __init__(self, working_dir, file_name, **options): + def __init__(self, file_name, **options): self.driver_name = "file" - self.path = Path(working_dir, file_name) + self.path = Path(tc_parameters.WORKING_DIR, file_name) super(FileDestination, self).__init__(FileIO, [self.path], options) def get_path(self): diff --git a/tests/python_functional/src/syslog_ng_config/statements/sources/file_source.py b/tests/python_functional/src/syslog_ng_config/statements/sources/file_source.py index ea55a29206..94c5175e8e 100644 --- a/tests/python_functional/src/syslog_ng_config/statements/sources/file_source.py +++ b/tests/python_functional/src/syslog_ng_config/statements/sources/file_source.py @@ -22,22 +22,22 @@ ############################################################################# from pathlib2 import Path +import src.testcase_parameters.testcase_parameters as tc_parameters from src.driver_io.file.file_io import FileIO from src.syslog_ng_config.statements.sources.source_driver import SourceDriver class FileSource(SourceDriver): - def __init__(self, working_dir, file_name, **options): - self.working_dir = working_dir + def __init__(self, file_name, **options): self.driver_name = "file" - self.path = Path(working_dir, file_name) + self.path = Path(tc_parameters.WORKING_DIR, file_name) super(FileSource, self).__init__(FileIO, [self.path], options) def get_path(self): return self.path def set_path(self, pathname): - self.path = Path(self.working_dir, pathname) + self.path = Path(tc_parameters.WORKING_DIR, pathname) def write_log(self, formatted_log, counter=1): self.sd_write_log(self.get_path(), formatted_log, counter=counter) diff --git a/tests/python_functional/src/syslog_ng_config/syslog_ng_config.py b/tests/python_functional/src/syslog_ng_config/syslog_ng_config.py index 0c755ff3b6..d6d4bbe4b0 100644 --- a/tests/python_functional/src/syslog_ng_config/syslog_ng_config.py +++ b/tests/python_functional/src/syslog_ng_config/syslog_ng_config.py @@ -37,8 +37,7 @@ class SyslogNgConfig(object): - def __init__(self, working_dir, version): - self.__working_dir = working_dir + def __init__(self, version): self.__raw_config = None self.__syslog_ng_config = { "version": version, @@ -61,7 +60,7 @@ def write_content(self, config_path): if self.__raw_config: rendered_config = self.__raw_config else: - rendered_config = ConfigRenderer(self.__syslog_ng_config, self.__working_dir).get_rendered_config() + rendered_config = ConfigRenderer(self.__syslog_ng_config).get_rendered_config() logger.info("Generated syslog-ng config\n{}\n".format(rendered_config)) FileIO(config_path).rewrite(rendered_config) @@ -90,10 +89,10 @@ def create_global_options(self, **kwargs): self.__syslog_ng_config["global_options"].update(kwargs) def create_file_source(self, **kwargs): - return FileSource(self.__working_dir, **kwargs) + return FileSource(**kwargs) def create_file_destination(self, **kwargs): - return FileDestination(self.__working_dir, **kwargs) + return FileDestination(**kwargs) def create_filter(self, **kwargs): return Filter(**kwargs) From c1e3a52f82d32515ccd5d5a7991b48480f9c1340 Mon Sep 17 00:00:00 2001 From: Andras Mitzki Date: Fri, 7 Jun 2019 13:57:48 +0200 Subject: [PATCH 6/6] LightConftest: Start using WorkingDir as a global variable in unit-tests Signed-off-by: Andras Mitzki --- tests/python_functional/src/conftest.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/python_functional/src/conftest.py b/tests/python_functional/src/conftest.py index f2421473a3..811c970389 100644 --- a/tests/python_functional/src/conftest.py +++ b/tests/python_functional/src/conftest.py @@ -23,6 +23,7 @@ ############################################################################# import pytest +import src.testcase_parameters.testcase_parameters as tc_parameters from src.testcase_parameters.testcase_parameters import TestcaseParameters @@ -35,6 +36,7 @@ def test_message(): def fake_testcase_parameters(request, tmpdir): orig_installdir = request.config.option.installdir orig_reportdir = request.config.option.reports + tc_parameters.WORKING_DIR = tmpdir.join("workingdir") request.config.option.installdir = tmpdir.join("installdir") request.config.option.reports = tmpdir.join("reports") request.config.option.runwithvalgrind = False