Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: allow user-config #1318

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions harvester_e2e_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be more pythonic?

>>> dcnf = {'A': 1, 'B': 2}
>>> ucnf = {'a': 10, 'B': 20, 'c': 30}
>>> ucnf | dcnf                         # python 3.9+
{'a': 10, 'B': 2, 'c': 30, 'A': 1}
>>> 
>>> {**ucnf, **dcnf}                    # python 3.5+
{'a': 10, 'B': 2, 'c': 30, 'A': 1}

Ref. https://stackoverflow.com/questions/38987/how-do-i-merge-two-dictionaries-in-a-single-expression-in-python

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out. Yes, it would be better to use a more pythoninc way to express the dict update.
I opted to use the dict.update method, since this allows me to loop over a list of potential config sources and reuse the file-reading logic.
It also makes it very clear what's happening and doesn't depend on a python version.

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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it's better to use an env. vairable like HVST_TESTS_CONFIG_PATH to give user the flexibility instead of fixed path.
e.g. user_config_path = os.getenv('HVST_TESTS_CONFIG_PATH')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I've added the variable HARVESTER_TESTS_CONFIG as possible location for the config file.
I still want it to look in a default location outside of the repository, since this way dropping a file somewhere is enough and the user doesn't need to set an environment variable as well.
I've also put a short line in the readme to explain all this.

'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',
Expand Down Expand Up @@ -421,3 +455,18 @@ def pytest_html_results_table_header(cells):

def pytest_html_results_table_row(report, cells):
cells.insert(1, f'<td class="col-time">{datetime.utcnow()}</td>')


def pytest_report_header(config):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is a good idea to expose sensitive data, it is not necessary for the report.

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