Skip to content
Open
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
16 changes: 16 additions & 0 deletions tests/policy/test-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ rlJournalStart
run_test "Verify that custom value which is the same as the default is recognized" /value-source/same-as-default "5m +10m +50m"
rlPhaseEnd

rlPhaseStartTest "Multiple policies"
function run_test () {
rlRun "tmt -vv test export \
--policy-root ../../policies/test \
--policy-name test --policy-name contact \
/basic"
rlRun -s "tmt -vv test export \
--policy-root ../../policies/test \
--policy-name test --policy-name contact \
/basic 2> /dev/null"
}
run_test
rlRun "yq '.[0] | .test' $rlRun_LOG | grep -- 'bash -c'" 0 "Test policy was applied"
rlRun "yq '.[0] | .contact == \"xyzzy\"' $rlRun_LOG" 0 "Test policy was applied"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The assertion always passes because yq exits with 0 even if the expression evaluates to false. Use grep to verify the output value, similar to line 77.

Suggested change
rlRun "yq '.[0] | .contact == \"xyzzy\"' $rlRun_LOG" 0 "Test policy was applied"
rlRun "yq '.[0] | .contact[0]' $rlRun_LOG | grep -q 'xyzzy'" 0 "Contact policy was applied"

rlPhaseEnd

rlPhaseStartTest "Test whether tmt run accepts a policy"
function run_test () {
local option="$1"
Expand Down
28 changes: 15 additions & 13 deletions tmt/cli/_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@


def _load_policies(
policy_name: Optional[str],
policy_path: Optional[Path],
policy_name: list[str],
policy_path: list[Path],
Comment on lines +73 to +74
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use tuple[str, ...] and tuple[Path, ...] instead of list because Click passes tuples for options with multiple=True. Apply this to _load_policies, run, tests_export, and plans_export.

Suggested change
policy_name: list[str],
policy_path: list[Path],
policy_name: tuple[str, ...],
policy_path: tuple[Path, ...],

policy_root: Optional[Path],
) -> list[tmt.policy.Policy]:
"""
Expand All @@ -85,30 +85,32 @@ def _load_policies(
directory.
"""

if policy_name is not None:
if policy_name:
if policy_root is None:
raise GeneralError(
"Policy can be loaded by its name only when '--policy-root' is specified."
)

if policy_path is not None:
if policy_path:
raise GeneralError(
"Options '--policy-name' and '--policy-file' are mutually exclusive."
)

return [
tmt.policy.Policy.load_by_name(
name=policy_name,
name=name,
root=policy_root,
)
for name in policy_name
]

if policy_path is not None:
if policy_path:
return [
tmt.policy.Policy.load_by_filepath(
path=policy_path,
path=path,
root=policy_root,
)
for path in policy_path
]

return []
Expand Down Expand Up @@ -353,8 +355,8 @@ def run(
context: Context,
id_: Optional[str],
workdir_root: Optional[Path],
policy_file: Optional[Path],
policy_name: Optional[str],
policy_file: list[Path],
policy_name: list[str],
policy_root: Optional[Path],
recipe: Optional[Path],
**kwargs: Any,
Expand Down Expand Up @@ -964,8 +966,8 @@ def tests_export(
nitrate: bool,
bugzilla: bool,
template: Optional[str],
policy_file: Optional[Path],
policy_name: Optional[str],
policy_file: list[Path],
policy_name: list[str],
policy_root: Optional[Path],
**kwargs: Any,
) -> None:
Expand Down Expand Up @@ -1227,8 +1229,8 @@ def plans_export(
how: str,
format: str,
template: Optional[str],
policy_file: Optional[Path],
policy_name: Optional[str],
policy_file: list[Path],
policy_name: list[str],
policy_root: Optional[Path],
**kwargs: Any,
) -> None:
Expand Down
2 changes: 2 additions & 0 deletions tmt/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ def option(
metavar='PATH',
type=Path(),
envvar='TMT_POLICY_FILE',
multiple=True,
help="""
Location of a file with policy rules for modification of
test metadata keys. Both absolute and relative paths are
Expand All @@ -569,6 +570,7 @@ def option(
'--policy-name',
metavar='NAME',
envvar='TMT_POLICY_NAME',
multiple=True,
help="""
Name of the file with policy rules for modification of test
metadata keys. The name would be extended with ``.yaml``
Expand Down
Loading