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

Added a way to specify test filters for custom attributes. #443

Merged
merged 3 commits into from
Feb 5, 2024
Merged
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
7 changes: 6 additions & 1 deletion testing.conf
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,15 @@ project_name =
#
# If a site needs specific job attributes to be specified, such as billing
# accounts, they can be added here. The value is a comma separated list
# of "executor.name": "value" pairs.
# of "executor.attr_name": "value" pairs. A regular expression filter enclosed
# in square brackets can be used after "custom_attributes" to specify that the
# attributes should only apply to certain tests. The specified custom_attributes
# directives are processed in the order in which they appear and a lack of a
# filter is equivalent to a ".*" filter.
#
# custom_attributes = "slurm.account": "xyz", \
# "slurm.constraint": "knl"
# custom_attributes[test_nodefile\[slurm:.*\]] = "slurm.qos": "debug"

custom_attributes =

Expand Down
21 changes: 19 additions & 2 deletions tests/ci_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@


def read_conf(fname: str) -> Dict[str, str]:
conf = {}
conf: Dict[str, str] = {}

Check warning on line 38 in tests/ci_runner.py

View check run for this annotation

Codecov / codecov/patch

tests/ci_runner.py#L38

Added line #L38 was not covered by tests
with open(fname, 'r') as f:
line = read_line(f)
while line is not None:
Expand All @@ -47,11 +47,28 @@
kv = line.split('=', 2)
if len(kv) != 2:
raise ValueError('Invalid line in configuration file: "%s"' % line)
conf[kv[0].strip()] = kv[1].strip()
add_conf(conf, kv[0].strip(), kv[1].strip())

Check warning on line 50 in tests/ci_runner.py

View check run for this annotation

Codecov / codecov/patch

tests/ci_runner.py#L50

Added line #L50 was not covered by tests
line = read_line(f)
return conf


def add_conf(conf: Dict[str, str], key: str, value: str) -> None:
if key.startswith('custom_attributes'):
if not key.startswith('custom_attributes['):
key = key + '[.*]'
if not key.endswith(']'):
raise ValueError('Invalid custom_attributes entry. Missing closing bracket.')
filter = key[len('custom_attributes['):-1]
key = 'custom_attributes'
value = '{"filter": "%s", "value": {%s}}' % (filter.replace('\\', '\\\\'), value)
if key in conf:
conf[key] = conf[key] + ', ' + value

Check warning on line 65 in tests/ci_runner.py

View check run for this annotation

Codecov / codecov/patch

tests/ci_runner.py#L56-L65

Added lines #L56 - L65 were not covered by tests
else:
conf[key] = value

Check warning on line 67 in tests/ci_runner.py

View check run for this annotation

Codecov / codecov/patch

tests/ci_runner.py#L67

Added line #L67 was not covered by tests
else:
conf[key] = value

Check warning on line 69 in tests/ci_runner.py

View check run for this annotation

Codecov / codecov/patch

tests/ci_runner.py#L69

Added line #L69 was not covered by tests


def run(*args: str, cwd: Optional[str] = None) -> str:
p = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=False,
cwd=cwd, text=True)
Expand Down
35 changes: 33 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
for x in _get_executors((metafunc.config)):
etp = ExecutorTestParams(x, queue_name=metafunc.config.option.queue_name,
project_name=metafunc.config.option.project_name,
custom_attributes=metafunc.config.option.custom_attributes)
custom_attributes_raw=metafunc.config.option.custom_attributes)
etps.append(etp)

metafunc.parametrize('execparams', etps, ids=lambda x: str(x))
Expand Down Expand Up @@ -361,7 +361,13 @@
if s is None:
return None
else:
return json.loads('{' + s + '}')
attrspec = json.loads('[' + s + ']')
d = {}
for item in attrspec:
if item['filter'] not in d:
d[item['filter']] = {}
d[item['filter']].update(item['value'])
return d

Check warning on line 370 in tests/conftest.py

View check run for this annotation

Codecov / codecov/patch

tests/conftest.py#L364-L370

Added lines #L364 - L370 were not covered by tests


def _strip_home(path: List[str]) -> List[str]:
Expand Down Expand Up @@ -434,8 +440,31 @@
return datetime.datetime.now(tz=datetime.timezone.utc).isoformat(' ')


def _process_custom_attributes(item):
if not hasattr(item, 'callspec'):
return
if 'execparams' not in item.callspec.params:
return
ep: Optional[List[Dict[str, Dict[str, object]]]] = item.callspec.params['execparams']
if ep.custom_attributes_raw is None:
return
if ep is None:
return
test_name = item.name
for filter, attrs in ep.custom_attributes_raw.items():
if re.match(filter, test_name):
ep.custom_attributes.update(attrs)

Check warning on line 456 in tests/conftest.py

View check run for this annotation

Codecov / codecov/patch

tests/conftest.py#L451-L456

Added lines #L451 - L456 were not covered by tests


def _set_attrs(execparams, attrs):
for k, v in attrs.items():
execparams.customattributes[k] = v

Check warning on line 461 in tests/conftest.py

View check run for this annotation

Codecov / codecov/patch

tests/conftest.py#L460-L461

Added lines #L460 - L461 were not covered by tests


@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
_process_custom_attributes(item)

outcome = yield
report = outcome.get_result()

Expand Down Expand Up @@ -588,6 +617,8 @@
env = config.option.environment

url = config.getoption('server_url')
if not url:
return

Check warning on line 621 in tests/conftest.py

View check run for this annotation

Codecov / codecov/patch

tests/conftest.py#L620-L621

Added lines #L620 - L621 were not covered by tests
minimal = config.getoption('minimal_uploads')
if minimal:
data = _sanitize(data)
Expand Down
6 changes: 4 additions & 2 deletions tests/executor_test_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class ExecutorTestParams:

def __init__(self, spec: str, queue_name: Optional[str] = None,
project_name: Optional[str] = None,
custom_attributes: Optional[Dict[str, object]] = None) -> None:
custom_attributes_raw: Optional[Dict[str, Dict[str, object]]] = None) \
-> None:
"""
Construct a new instance.

Expand Down Expand Up @@ -35,7 +36,8 @@ def __init__(self, spec: str, queue_name: Optional[str] = None,

self.queue_name = queue_name
self.project_name = project_name
self.custom_attributes = custom_attributes
self.custom_attributes_raw = custom_attributes_raw
self.custom_attributes: Dict[str, object] = {}

def __repr__(self) -> str:
"""Returns a string representation of this object."""
Expand Down
Loading