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

Add logging of applied adjust rules #2230

Merged
merged 1 commit into from
Oct 4, 2023
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ classifiers = [
]
dependencies = [ # F39 / PyPI
"click>=8.0.3,!=8.1.4", # 8.1.3 / 8.1.6 TODO type annotations tmt.cli.Context -> click.core.Context click/issues/2558
"fmf>=1.2.1",
"fmf>=1.3.0",
"jinja2>=2.11.3", # 3.1.2 / 3.1.2
"pint>=0.16.1", # 0.16.1 / 0.22
"requests>=2.25.1", # 2.28.2 / 2.31.0
Expand Down
50 changes: 48 additions & 2 deletions tmt/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,46 @@ def _export(
)


def create_adjust_callback(logger: tmt.log.Logger) -> fmf.base.AdjustCallback:
"""
Create a custom callback for fmf's adjust.

Given the ``adjust`` rules are applied on many places, for
proper logging they need their own specific logger. Create
a callback closure with the given logger.
"""

def callback(
node: fmf.Tree,
rule: _RawAdjustRule,
applied: Optional[bool]) -> None:
if applied is None:
logger.verbose(
f"Adjust rule skipped on '{node.name}'",
tmt.utils.format_value(rule, key_color='cyan'),
color='blue',
level=3,
topic=tmt.log.Topic.ADJUST_DECISIONS)

elif applied is False:
logger.verbose(
f"Adjust rule not applied to '{node.name}'",
tmt.utils.format_value(rule, key_color='cyan'),
color='red',
level=3,
topic=tmt.log.Topic.ADJUST_DECISIONS)

else:
logger.verbose(
f"Adjust rule applied to '{node.name}'",
tmt.utils.format_value(rule, key_color='cyan'),
color='green',
level=3,
topic=tmt.log.Topic.ADJUST_DECISIONS)

return callback


# Types describing content accepted by various require-like keys: strings, fmf ids,
# paths, or lists mixing various types.
#
Expand Down Expand Up @@ -2628,7 +2668,10 @@ def tree(self) -> fmf.Tree:
except fmf.utils.FileError as error:
raise tmt.utils.GeneralError(f"Invalid yaml syntax: {error}")
# Adjust metadata for current fmf context
self._tree.adjust(fmf.context.Context(**self._fmf_context), case_sensitive=False)
self._tree.adjust(
fmf.context.Context(**self._fmf_context),
case_sensitive=False,
decision_callback=create_adjust_callback(self._logger))
return self._tree

@tree.setter
Expand Down Expand Up @@ -3931,7 +3974,10 @@ def resolve_dynamic_ref(
reference_tree = fmf.Tree(data=data)
if not plan:
raise tmt.utils.FileError("Cannot get plan fmf context to evaluate dynamic ref.")
reference_tree.adjust(fmf.context.Context(**plan._fmf_context), case_sensitive=False)
reference_tree.adjust(
fmf.context.Context(**plan._fmf_context),
case_sensitive=False,
decision_callback=create_adjust_callback(logger))
# Also temporarily build a plan so that env and context variables are expanded
Plan(logger=logger, node=reference_tree, run=plan.my_run, skip_validation=True)
ref = reference_tree.get("ref")
Expand Down
1 change: 1 addition & 0 deletions tmt/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Topic(enum.Enum):
KEY_NORMALIZATION = 'key-normalization'
CLI_INVOCATIONS = 'cli-invocations'
COMMAND_EVENTS = 'command-events'
ADJUST_DECISIONS = 'adjust-decisions'


DEFAULT_TOPICS: Set[Topic] = set()
Expand Down
Loading