-
Notifications
You must be signed in to change notification settings - Fork 32
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
m-ildefons
wants to merge
2
commits into
harvester:main
Choose a base branch
from
m-ildefons:wip/user-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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) | ||
|
||
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', | ||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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')}", | ||
] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be more pythonic?
Ref. https://stackoverflow.com/questions/38987/how-do-i-merge-two-dictionaries-in-a-single-expression-in-python
There was a problem hiding this comment.
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.