-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable beeflow core pull-deps to use a Dockerfile to build the needed neo4j container and add the apoc file * create AlterConfig class and config_uitls * Add path of newly built/pulled containers to config * enable pull-deps to run when there isn't an existing config * remove some of the excess ouptput when creating a config * update tests to use the AlterConfig code. * add to pull-deps documentation and add explanation for how to use AlterConfig. * add config utils tests * change name of config sections to names from the bee config * add a config pytest fixture and change validator class name --------- Co-authored-by: leahh <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
16f917c
commit 414bcab
Showing
10 changed files
with
366 additions
and
83 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""Functions used by the config classes.""" | ||
|
||
import os | ||
import shutil | ||
|
||
|
||
def filter_and_validate(config, validator): | ||
"""Filter and validate the configuration file.""" | ||
default_keys = list(config['DEFAULT']) | ||
config = {sec_name: {key: config[sec_name][key] for key in config[sec_name] | ||
if sec_name == 'DEFAULT' or key not in default_keys} # noqa | ||
for sec_name in config} | ||
# Validate the config | ||
return validator.validate(config) | ||
|
||
|
||
def write_config(file_name, sections): | ||
"""Write the configuration file.""" | ||
try: | ||
with open(file_name, 'w', encoding='utf-8') as fp: | ||
print('# BEE Configuration File', file=fp) | ||
for sec_name, section in sections.items(): | ||
if not section: | ||
continue | ||
print(file=fp) | ||
print(f'[{sec_name}]', file=fp) | ||
for opt_name, value in section.items(): | ||
print(f'{opt_name} = {value}', file=fp) | ||
except FileNotFoundError: | ||
print('Configuration file does not exist!') | ||
|
||
|
||
def backup(fname): | ||
"""Backup the configuration file.""" | ||
i = 1 | ||
backup_path = f'{fname}.{i}' | ||
while os.path.exists(backup_path): | ||
i += 1 | ||
backup_path = f'{fname}.{i}' | ||
shutil.copy(fname, backup_path) | ||
print(f'Saved old config to "{backup_path}".') | ||
print() |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
"""Unit tests for the config driver.""" | ||
|
||
import pytest | ||
from beeflow.common.config_driver import AlterConfig | ||
|
||
|
||
# AlterConfig tests | ||
def test_initialization_without_changes(mocker): | ||
"""Test initialization without any changes.""" | ||
mocked_load = mocker.patch('beeflow.common.config_driver.AlterConfig._load_config') | ||
|
||
alter_config = AlterConfig() | ||
mocked_load.assert_called_once() | ||
assert alter_config.changes == {} | ||
|
||
|
||
def test_initialization_with_changes(mocker): | ||
"""Test initialization with some predefined changes.""" | ||
mocked_load = mocker.patch('beeflow.common.config_driver.AlterConfig._load_config') | ||
mocked_change = mocker.patch('beeflow.common.config_driver.AlterConfig.change_value') | ||
|
||
changes = {"DEFAULT": {"bee_workdir": "/new/path"}} | ||
alter_config = AlterConfig(changes=changes) | ||
mocked_load.assert_called_once() | ||
mocked_change.assert_called_once() | ||
assert alter_config.changes == changes | ||
|
||
|
||
def test_change_value_success(mocker): | ||
"""Test changing an existing config value.""" | ||
# Define the config | ||
sample_config = { | ||
'DEFAULT': {'bee_workdir': '$BEE_WORKDIR', 'workload_scheduler': '$WORKLOAD_SCHEDULER'}, | ||
'task_manager': {'container_runtime': 'Charliecloud', 'runner_opts': ''} | ||
} | ||
mocker.patch('beeflow.common.config_driver.AlterConfig._load_config') | ||
alter_config = AlterConfig() | ||
alter_config.config = sample_config | ||
|
||
mocker.patch("pathlib.Path.mkdir") | ||
alter_config.change_value("DEFAULT", "bee_workdir", "/new/path") | ||
assert alter_config.config["DEFAULT"]["bee_workdir"] == "/new/path" | ||
|
||
|
||
def test_change_value_nonexistent_section(mocker): | ||
"""Test changing a value in a nonexistent section raises an error.""" | ||
# Define the config | ||
sample_config = { | ||
'DEFAULT': {'bee_workdir': '$BEE_WORKDIR', 'workload_scheduler': '$WORKLOAD_SCHEDULER'}, | ||
'task_manager': {'container_runtime': 'Charliecloud', 'runner_opts': ''} | ||
} | ||
mocker.patch('beeflow.common.config_driver.AlterConfig._load_config') | ||
alter_config = AlterConfig() | ||
alter_config.config = sample_config | ||
|
||
mocker.patch("pathlib.Path.mkdir") | ||
with pytest.raises(ValueError, match="Section NON_EXISTENT not found in the config."): | ||
alter_config.change_value("NON_EXISTENT", "some_option", "new_value") | ||
|
||
|
||
def test_change_value_nonexistent_option(mocker): | ||
"""Test changing a nonexistent option raises an error.""" | ||
# Define the config | ||
sample_config = { | ||
'DEFAULT': {'bee_workdir': '$BEE_WORKDIR', 'workload_scheduler': '$WORKLOAD_SCHEDULER'}, | ||
'task_manager': {'container_runtime': 'Charliecloud', 'runner_opts': ''} | ||
} | ||
mocker.patch('beeflow.common.config_driver.AlterConfig._load_config') | ||
alter_config = AlterConfig() | ||
alter_config.config = sample_config | ||
|
||
mocker.patch("pathlib.Path.mkdir") | ||
with pytest.raises( | ||
ValueError, | ||
match="Option non_existent_option not found in section DEFAULT." | ||
): | ||
alter_config.change_value("DEFAULT", "non_existent_option", "new_value") | ||
|
||
|
||
def save(mocker): | ||
"""Test the save function.""" | ||
mocker.patch('beeflow.common.config_driver.AlterConfig._load_config') | ||
alter_config = AlterConfig() | ||
|
||
mocked_save = mocker.patch('beeflow.common.config_driver.AlterConfig.save') | ||
alter_config.save() | ||
mocked_save.assert_called_once() | ||
|
||
|
||
def test_change_value_multiple_times(mocker): | ||
"""Test changing a config value multiple times and tracking changes.""" | ||
# Define the config | ||
sample_config = { | ||
'DEFAULT': {'bee_workdir': '$BEE_WORKDIR', 'workload_scheduler': '$WORKLOAD_SCHEDULER'}, | ||
'task_manager': {'container_runtime': 'Charliecloud', 'runner_opts': ''} | ||
} | ||
mocker.patch('beeflow.common.config_driver.AlterConfig._load_config') | ||
alter_config = AlterConfig() | ||
alter_config.config = sample_config | ||
|
||
mocker.patch("pathlib.Path.mkdir") | ||
alter_config.change_value("DEFAULT", "bee_workdir", "/path/one") | ||
alter_config.change_value("DEFAULT", "bee_workdir", "/path/two") | ||
assert alter_config.config["DEFAULT"]["bee_workdir"] == "/path/two" | ||
assert alter_config.changes == {"DEFAULT": {"bee_workdir": "/path/two"}} |
Oops, something went wrong.