Skip to content
This repository has been archived by the owner on Jan 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1321 from anchore/base-trigger-parameters-unit-test
Browse files Browse the repository at this point in the history
Add unit test for the BaseTrigger.parameters() method
  • Loading branch information
emily-lopez authored Jan 11, 2022
2 parents bb18b70 + 5671602 commit e6db8d5
Showing 1 changed file with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import pytest

from anchore_engine.services.policy_engine.engine.policy.gates import (
PackageCheckGate,
BaseTrigger,
)
from anchore_engine.services.policy_engine.engine.policy.gates.dockerfile import (
EffectiveUserTrigger,
)
from anchore_engine.services.policy_engine.engine.policy.gates.npms import (
PkgMatchTrigger,
)
from anchore_engine.services.policy_engine.engine.policy.params import (
CommaDelimitedStringListParameter,
EnumStringParameter,
TriggerParameter,
)


class TestBaseTrigger:

"""
For the purposes of this test, we are using a few random trigger and gate (instead of testing every gate/trigger class combo)
To verify the parameters method works well.
This is specific to the random trigger that was selected, essentially verifying that
the parameters method does what it's supposed to, which is retrieving a dict of
data attributes and their values.
Note: for the gate parameter it is crucial to use a gate that has __lifecycle_state__ == LifecycleStates.eol for this test.
Otherwise, the BaseTrigger constructor won't be able to execute because the parameter validation will fail
"""

@pytest.mark.parametrize(
"param",
[
pytest.param(
{
"trigger": EffectiveUserTrigger,
"gate": PackageCheckGate,
"expected_params": {
"user": CommaDelimitedStringListParameter,
"allowed_type": EnumStringParameter,
},
},
id="effective-user-trigger",
),
pytest.param(
{
"trigger": PkgMatchTrigger,
"gate": PackageCheckGate,
"expected_params": {
"name": TriggerParameter,
"version": TriggerParameter,
},
},
id="npm-pkg-match-trigger",
),
],
)
def test_parameters(self, param):
parameters = param["trigger"](param["gate"]).parameters()
for key, value in param["expected_params"].items():
assert parameters.get(key).__class__ == value

def test_reset(self):
trigger = BaseTrigger(PackageCheckGate)
trigger._fired_instances = [1, 2, 3]
trigger.reset()
assert trigger._fired_instances == []

0 comments on commit e6db8d5

Please sign in to comment.