Skip to content

Commit

Permalink
Fix subject type matching
Browse files Browse the repository at this point in the history
JIRA: RHELWF-9659
  • Loading branch information
hluk committed Aug 25, 2023
1 parent 6afad9f commit bc6aade
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 19 deletions.
2 changes: 1 addition & 1 deletion greenwave/policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ def matches(self, **attributes):

def matches_subject_type(self, **attributes):
subject = attributes.get('subject')
return not subject or subject.type == self.subject_type
return not subject or subject.subject_type.matches(self.subject_type)

def matches_sub_policy(self, sub_policy):
return set(sub_policy.all_decision_contexts).intersection(self.all_decision_contexts)
Expand Down
34 changes: 17 additions & 17 deletions greenwave/subjects/subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ class Subject:
Item or identifier should uniquely identify the artefact (test subject).
"""
_type: Union[GenericSubjectType, SubjectType]
subject_type: Union[GenericSubjectType, SubjectType]
item: str

def __init__(self, type_: Union[GenericSubjectType, SubjectType], item: str):
self._type = type_
self.subject_type = type_
self.item = item

def product_versions_from_koji_build_target(self, target):
return sorted(self._matching_product_versions(
target, self._type.product_version_from_koji_build_target))
target, self.subject_type.product_version_from_koji_build_target))

@property
def type(self):
"""Subject type string."""
return self._type.id
return self.subject_type.id

@property
def identifier(self):
Expand All @@ -52,15 +52,15 @@ def identifier(self):
@property
def package_name(self):
"""Package name of the subject or None."""
if self._type.is_nvr:
if self.subject_type.is_nvr:
return self.item.rsplit("-", 2)[0]

return None

@property
def short_product_version(self):
"""Get short product version of the subject (guess from identifier) or None."""
if self._type.is_nvr:
if self.subject_type.is_nvr:
try:
_, _, release = self.identifier.rsplit("-", 2)
_, short_prod_version = release.rsplit(".", 1)
Expand All @@ -75,31 +75,31 @@ def product_versions(self):
pvs = sorted({
pv.lower()
for pv in self._matching_product_versions(
self.item, self._type.product_version_match)
self.item, self.subject_type.product_version_match)
})
if pvs:
return pvs

if self._type.product_version:
return [self._type.product_version]
if self.subject_type.product_version:
return [self.subject_type.product_version]

return []

@property
def is_koji_build(self):
return self._type.is_koji_build
return self.subject_type.is_koji_build

@property
def supports_remote_rule(self):
return self._type.supports_remote_rule
return self.subject_type.supports_remote_rule

@property
def ignore_missing_policy(self):
return self._type.ignore_missing_policy
return self.subject_type.ignore_missing_policy

def to_dict(self):
if self._type.item_dict:
return _to_dict(self._type.item_dict, self.item)
if self.subject_type.item_dict:
return _to_dict(self.subject_type.item_dict, self.item)

return {"type": self.type, "item": self.item}

Expand All @@ -110,8 +110,8 @@ def result_queries(self):
For example, one set of parameters could have "type=koji_build" for one
query and "type=brew-build" for another.
"""
if self._type.result_queries:
for query_dict in self._type.result_queries:
if self.subject_type.result_queries:
for query_dict in self.subject_type.result_queries:
yield _to_dict(query_dict, self.item)
else:
yield self.to_dict()
Expand All @@ -130,5 +130,5 @@ def __str__(self):

def __repr__(self):
return "Subject({!r}, {!r})".format(
self._type, self.item
self.subject_type, self.item
)
3 changes: 3 additions & 0 deletions greenwave/subjects/subject_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ def _set_default_attributes(self):
for name, attr in SubjectType.safe_yaml_attributes.items():
self.__setattr__(name, attr.default_value)

def matches(self, id_):
return id_ == self.id or id_ in self.aliases

def __repr__(self):
return '<GenericSubjectType {!r}>'.format(self.id)

Expand Down
38 changes: 38 additions & 0 deletions greenwave/tests/test_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from greenwave.app_factory import create_app
from greenwave.decision import Decision
from greenwave.policies import (
applicable_decision_context_product_version_pairs,
load_policies,
summarize_answers,
Policy,
Expand Down Expand Up @@ -1591,3 +1592,40 @@ def test_cache_passing_results():
cached = results4.retrieve(subject, testcase='sometest')
assert results4.retrieve_data_called == 0
assert cached == retrieved2


def test_applicable_policies():
policies = Policy.safe_load_all(dedent("""
--- !Policy
id: test_policy
product_versions: [fedora-rawhide]
decision_context: test_context
subject_type: koji_build
rules:
- !RemoteRule {required: false}
- !PassingTestCaseRule {test_case_name: common.test.case}
"""))
remote_fragment = dedent("""
--- !Policy
product_versions:
- fedora-rawhide
decision_context: test_context
subject_type: brew-build
rules:
- !PassingTestCaseRule {test_case_name: remote.test.case}
""")

subject = create_subject('koji_build', 'nethack-1.2.3-1.el9000')

with mock.patch("greenwave.resources.retrieve_scm_from_koji") as scm:
scm.return_value = (
"rpms", "nethack", "c3c47a08a66451cb9686c49f040776ed35a0d1bb")
with mock.patch("greenwave.resources.retrieve_yaml_remote_rule") as f:
f.return_value = remote_fragment
result = applicable_decision_context_product_version_pairs(
policies,
subject=subject,
testcase="remote.test.case",
)

assert len(result) == 1
2 changes: 1 addition & 1 deletion greenwave/tests/test_subjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ def test_subject_to_repr_generic(app):
))
def test_subject_product_version_match(compose, expected_product_version, app):
subject = create_subject('compose', compose)
assert subject._type.product_version_match
assert subject.subject_type.product_version_match
assert subject.product_versions == expected_product_version, compose

0 comments on commit bc6aade

Please sign in to comment.