Skip to content

Commit 1f1f827

Browse files
committed
Add functionality to validate project groups in config
1 parent 80ed34b commit 1f1f827

3 files changed

Lines changed: 39 additions & 8 deletions

File tree

benchcab/config.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ def read_optional_key(config: dict):
9191
raise ValueError(msg)
9292
config["project"] = os.environ["PROJECT"]
9393

94+
# Directory List is obtained from Gadi Resources - https://opus.nci.org.au/display/Help/0.+Welcome+to+Gadi
95+
data_dirs = ["/g/data", "/scratch"]
96+
groups = list(
97+
set([group for data_dir in data_dirs for group in os.listdir(data_dir)])
98+
)
99+
100+
if config["project"] not in groups:
101+
msg = f"User is not a member of project [{config['project']}]: Check if project key is correct"
102+
103+
raise ValueError(msg)
104+
94105
if "realisations" in config:
95106
for r in config["realisations"]:
96107
r["name"] = r.get("name")
@@ -152,8 +163,8 @@ def read_config(config_path: str) -> dict:
152163
"""
153164
# Read configuration file
154165
config = read_config_file(config_path)
155-
# Populate configuration dict with optional keys
156-
read_optional_key(config)
157166
# Validate and return.
158167
validate_config(config)
168+
# Populate configuration dict with optional keys
169+
read_optional_key(config)
159170
return config

benchcab/data/test/config-optional.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Config with optional data
2-
project: optional
2+
project: hh5
33

44
fluxsite:
55
experiment: AU-Tum

tests/test_config.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import benchcab.utils as bu
1414

1515
# Temporarily set $PROJECT for testing module
16-
OPTIONAL_CONFIG_PROJECT = "tt1"
16+
OPTIONAL_CONFIG_PROJECT = "ks32"
1717

1818

1919
@pytest.fixture(autouse=True)
@@ -24,6 +24,13 @@ def _set_project_env_variable(monkeypatch):
2424
yield
2525

2626

27+
@pytest.fixture(autouse=True)
28+
def _set_project_validation_dirs():
29+
with mock.patch("os.listdir") as mocked_listdir:
30+
mocked_listdir.return_value = ["hh5", OPTIONAL_CONFIG_PROJECT]
31+
yield
32+
33+
2734
@pytest.fixture()
2835
def config_str(request) -> str:
2936
"""Provide relative YAML path string of data files."""
@@ -89,7 +96,7 @@ def all_optional_custom_config(default_only_config) -> dict:
8996
Reads from config-optional.yml
9097
"""
9198
config = default_only_config | {
92-
"project": "optional",
99+
"project": "hh5",
93100
"fluxsite": {
94101
"experiment": "AU-Tum",
95102
"multiprocess": False,
@@ -163,15 +170,28 @@ def test_read_optional_key_add_data(input_config, output_config, request):
163170
assert pformat(config) == pformat(request.getfixturevalue(output_config))
164171

165172

166-
def test_no_project(default_only_config, monkeypatch):
173+
def test_no_project_name(default_only_config, monkeypatch):
167174
"""If project key and $PROJECT are not provided, then raise error."""
168175
monkeypatch.delenv("PROJECT")
169-
error_msg = re.escape(
176+
err_msg = re.escape(
170177
"""Couldn't resolve project: check 'project' in config.yaml
171178
and/or $PROJECT set in ~/.config/gadi-login.conf
172179
"""
173180
)
174-
with pytest.raises(ValueError, match=error_msg):
181+
with pytest.raises(ValueError, match=err_msg):
182+
bc.read_optional_key(default_only_config)
183+
184+
185+
def test_user_not_in_project(default_only_config):
186+
"""If user is not in viewable NCI projects, raise error."""
187+
default_only_config["project"] = "non_existing"
188+
err_msg = re.escape(
189+
"User is not a member of project [non_existing]: Check if project key is correct"
190+
)
191+
with pytest.raises(
192+
ValueError,
193+
match=err_msg,
194+
):
175195
bc.read_optional_key(default_only_config)
176196

177197

0 commit comments

Comments
 (0)