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] 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')}", + ]