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
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a name="deprecated_config" />
- `do-not-cleanup`
- `harvester_cluster_nodes`
Expand Down
47 changes: 45 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,36 @@ def check_depends(self, depends, item):
DepMgr.checkDepend, DepMgr._check_depend = check_depends, DepMgr.checkDepend


def merge_config():
"""
Merge two config files. The user config takes precedence over the default
config.
"""

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 path in paths:
if path:
try:
with open(path, 'r') as cnf:
data = yaml.safe_load(cnf)
except FileNotFoundError:
data = {}

config.update(data)

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)
config_data = merge_config()

parser.addoption(
'--endpoint',
action='store',
Expand Down Expand Up @@ -421,3 +449,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')}",
]