Skip to content

Commit

Permalink
user config: read path from variable
Browse files Browse the repository at this point in the history
Read user config path from environment variable `HARVESTER_TESTS_CONFIG`

Simplified config data loading.

Signed-off-by: Moritz Röhrich <[email protected]>
  • Loading branch information
m-ildefons committed Jun 13, 2024
1 parent 058ae5a commit 4b64980
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
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
38 changes: 16 additions & 22 deletions harvester_e2e_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 4b64980

Please sign in to comment.