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

🧑🏼‍💻 [feat/0.8] Rules: Fallback rule implementation (aka rules that will run only if no other rule was executed) #592

Open
wants to merge 4 commits into
base: 0.8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions harp_apps/rules/examples/teapot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ rules:
on_request: |
from harp.http import HttpResponse
response = HttpResponse("I'm a teapot.", status=418)
"__default__":
"GET /*":
# language=python
on_request: |
from harp.http import HttpResponse
response = HttpResponse("I'm the fallback for acme.", status=418)
30 changes: 26 additions & 4 deletions harp_apps/rules/models/rulesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,37 @@

from harp_apps.rules.constants import DEFAULT_LEVELS, DEFAULT_RULES_LEVELS
from harp_apps.rules.models.compilers import BaseRuleSetCompiler
from harp_apps.rules.models.patterns import Pattern


DEFAULT_RULE_MATCHER = Pattern("__default__")


class Itemize:
def __init__(self, seq) -> None:
self._seq = seq

def __iter__(self):
try:
return iter(self._seq.items())
except AttributeError:
return iter(self._seq)


def _match_level(rules, against):
default = None
has_match = False
for pattern, rules in rules:
if pattern == DEFAULT_RULE_MATCHER:
default = rules
continue

if pattern.match(against):
if hasattr(rules, "items"):
yield from rules.items()
else:
yield from rules
has_match = True
return Itemize(rules)

if not has_match and default:
return Itemize(default)
Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I understand, this changes the behaviour of rules, allowing only one match to be made amongst one level (by returning instead of yielding from). The «itemize» object brings more confusion than clarity, on top of that.

the current test suite is a bit incomplete on the rules side, but catches an issue there (see harp_apps.rules.tests.test_models_rulesets.test_multilevel).



def _rules_as_human_dict(rules: dict, *, show_scripts=True):
Expand Down
12 changes: 12 additions & 0 deletions harp_apps/rules/tests/examples/test_teapot.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,24 @@ def get_rules(self):
settings = RulesSettings(**self.source)
return settings.ruleset

def test_on_proxy_no_match_gets_default(self):
rules = self.get_rules()
event = self.create_proxy_filter_event("proxy.filter.request", endpoint="xxx*", request=self.create_request())

scripts = list(rules.match(*event.criteria))
assert len(scripts) == 1
assert "I'm the fallback for acme." in scripts[0].source

event.execute_script(scripts[0])
assert event.response.status == 418

def test_on_proxy_filter_request(self):
rules = self.get_rules()
event = self.create_proxy_filter_event("proxy.filter.request", endpoint="acme1", request=self.create_request())

scripts = list(rules.match(*event.criteria))
assert len(scripts) == 1
assert "I'm a teapot." in scripts[0].source

event.execute_script(scripts[0])
assert event.response.status == 418