From 058ae5a3188dca29b76ef153b1cb1acdf22b8705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20R=C3=B6hrich?= Date: Tue, 4 Jun 2024 15:56:57 +0200 Subject: [PATCH 1/2] config: allow user-config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allow a user to maintain a config file in their home directory, which overrides the default settings. This makes it possible to execute the test suite from a clean git repo state and reduces the probability that config changes are accidentally committed to the repo. Signed-off-by: Moritz Röhrich --- harvester_e2e_tests/conftest.py | 53 +++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/harvester_e2e_tests/conftest.py b/harvester_e2e_tests/conftest.py index 3f4bec056..3eea0ecdc 100644 --- a/harvester_e2e_tests/conftest.py +++ b/harvester_e2e_tests/conftest.py @@ -15,6 +15,7 @@ # To contact SUSE about this file by physical or electronic mail, # you may find current contact information at www.suse.com +import os import pytest import yaml from datetime import datetime @@ -49,9 +50,42 @@ def check_depends(self, depends, item): DepMgr.checkDepend, DepMgr._check_depend = check_depends, DepMgr.checkDepend +def merge_config(user_config_path, default_config_path): + """ + Merge two config files. The user config takes precedence over the default + config. + """ + + try: + with open(user_config_path, 'r') as ucnf: + user_data = yaml.safe_load(ucnf) + except FileNotFoundError: + user_data = {} + + try: + with open(default_config_path, 'r') as dcnf: + default_data = yaml.safe_load(dcnf) + except FileNotFoundError: + default_data = {} + + config = {} + for key in default_data: + config[key] = user_data.get(key, default_data[key]) + + for key in user_data: + if not key in default_data.keys(): + config[key] = user_data[key] + + return config + + def pytest_addoption(parser): - with open('config.yml') as f: - config_data = yaml.safe_load(f) + user_home = os.getenv('HOME') + user_config_path= os.path.join(user_home, '.config', 'harvester', 'tests', + 'config.yml') + default_config_path = os.path.join(os.getcwd(), 'config.yml') + config_data = merge_config(user_config_path, default_config_path) + parser.addoption( '--endpoint', action='store', @@ -421,3 +455,18 @@ def pytest_html_results_table_header(cells): def pytest_html_results_table_row(report, cells): cells.insert(1, f'{datetime.utcnow()}') + + +def pytest_report_header(config): + if config.getoption("verbose") > 0: + return [ + f"Harvester Endpoint: {config.getoption('endpoint')}", + f"Harvester Username: {config.getoption('username')}", + f"Harvester Password: {config.getoption('password')}", + f"Host Password: {config.getoption('host_password')}", + f"Host Private Key: {config.getoption('host_private_key')}", + f"VLAN ID: {config.getoption('vlan_id')}", + f"VLAN NIC: {config.getoption('vlan_nic')}", + f"Rancher Endpoint: {config.getoption('rancher_endpoint')}", + f"Rancher Password: {config.getoption('rancher_admin_password')}", + ] From 4b64980062dc0dee7055d8c5cd96cc4071acce34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20R=C3=B6hrich?= Date: Thu, 13 Jun 2024 14:28:03 +0200 Subject: [PATCH 2/2] user config: read path from variable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Read user config path from environment variable `HARVESTER_TESTS_CONFIG` Simplified config data loading. Signed-off-by: Moritz Röhrich --- README.md | 6 ++++++ harvester_e2e_tests/conftest.py | 38 ++++++++++++++------------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index c69306ec5..1180b0f6a 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,12 @@ The section is targeting to explain configure options and test cases depends on. Notice that configuration options in [config.yml](config.yml) can be overwritten by command line parameters. For example, to overwrite the `endpoint` option, we can use the `--endpoint` parameter while running the tests. +To avoid dirtying the repository, place the customized version of the +`config.yml` in any one of the places: + +- `$HARVESTER_TESTS_CONFIG` +- `$HOME/.config/harvester/tests/config.yml` + ### Deprecated Config Options - `do-not-cleanup` - `harvester_cluster_nodes` diff --git a/harvester_e2e_tests/conftest.py b/harvester_e2e_tests/conftest.py index 3eea0ecdc..85e0cde4e 100644 --- a/harvester_e2e_tests/conftest.py +++ b/harvester_e2e_tests/conftest.py @@ -50,41 +50,35 @@ def check_depends(self, depends, item): DepMgr.checkDepend, DepMgr._check_depend = check_depends, DepMgr.checkDepend -def merge_config(user_config_path, default_config_path): +def merge_config(): """ Merge two config files. The user config takes precedence over the default config. """ - try: - with open(user_config_path, 'r') as ucnf: - user_data = yaml.safe_load(ucnf) - except FileNotFoundError: - user_data = {} - - try: - with open(default_config_path, 'r') as dcnf: - default_data = yaml.safe_load(dcnf) - except FileNotFoundError: - default_data = {} + paths = [ + os.path.join(os.getcwd(), 'config.yml'), + os.getenv('HARVESTER_TESTS_CONFIG', None), + os.path.join(os.getenv('HOME'), '.config', 'harvester', 'tests', + 'config.yml') + ] config = {} - for key in default_data: - config[key] = user_data.get(key, default_data[key]) + for path in paths: + if path: + try: + with open(path, 'r') as cnf: + data = yaml.safe_load(cnf) + except FileNotFoundError: + data = {} - for key in user_data: - if not key in default_data.keys(): - config[key] = user_data[key] + config.update(data) return config def pytest_addoption(parser): - user_home = os.getenv('HOME') - user_config_path= os.path.join(user_home, '.config', 'harvester', 'tests', - 'config.yml') - default_config_path = os.path.join(os.getcwd(), 'config.yml') - config_data = merge_config(user_config_path, default_config_path) + config_data = merge_config() parser.addoption( '--endpoint',