-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edca534
commit 8ed41e8
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import re | ||
from typing import Dict, Optional | ||
|
||
from pycfmodel.model.cf_model import CFModel | ||
|
||
from cfripper.config.regex import REGEX_ALPHANUMERICAL_OR_HYPHEN | ||
from cfripper.model.enums import RuleMode, RuleRisk | ||
from cfripper.model.result import Result | ||
from cfripper.rules.base_rules import Rule | ||
|
||
|
||
class StackNameMatchesRegexRule(Rule): | ||
""" | ||
Checks that a given stack follows the naming convention given by a regex. | ||
""" | ||
|
||
RULE_MODE = RuleMode.DEBUG | ||
RISK_VALUE = RuleRisk.LOW | ||
REASON = ( | ||
"The stack name {} does not follow the naming convention (only alphanumerical characters and hyphens allowed)." | ||
) | ||
|
||
def _stack_name_matches_regex(self, stack_name: str) -> bool: | ||
"""Check that stack name follows naming convention.""" | ||
return bool(REGEX_ALPHANUMERICAL_OR_HYPHEN.match(stack_name)) | ||
|
||
def invoke(self, cfmodel: CFModel, extras: Optional[Dict] = None) -> Result: | ||
result = Result() | ||
stack_name = self._config.stack_name | ||
if not stack_name: | ||
return result | ||
if not extras: | ||
extras = {} | ||
|
||
if not self._stack_name_matches_regex(stack_name): | ||
self.add_failure_to_result( | ||
result, | ||
self.REASON.format(stack_name), | ||
context={"config": self._config, "extras": extras}, | ||
) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import pytest | ||
from pycfmodel.model.cf_model import CFModel | ||
|
||
from cfripper.config.config import Config | ||
from cfripper.rules import StackNameMatchesRegexRule | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"stack_name, expected_result", | ||
[ | ||
("justlowercase", True), | ||
("lowercase-with-hyphens", True), | ||
("lowercaseANDUPPERCASE", True), | ||
("lowercase-AND-UPPERCASE-with-hyphens", True), | ||
("including_underscore", False), | ||
("including space", False), | ||
("including-other-symbols!@£$%^&*()", False), | ||
], | ||
) | ||
def test_stack_name_matches_regex(stack_name, expected_result): | ||
rule = StackNameMatchesRegexRule(Config(stack_name=stack_name, rules=["StackNameMatchesRegexRule"])) | ||
assert rule._stack_name_matches_regex(stack_name) == expected_result | ||
|
||
|
||
def test_works_with_extras(): | ||
rule = StackNameMatchesRegexRule(Config(stack_name="some-valid-stack-name", rules=["StackNameMatchesRegexRule"])) | ||
extras = {"stack": {"tags": [{"key": "project", "value": "some_project"}]}} | ||
result = rule.invoke(cfmodel=CFModel(), extras=extras) | ||
assert result.valid | ||
|
||
|
||
def test_failure_is_added_for_invalid_stack_name(): | ||
rule = StackNameMatchesRegexRule(Config(stack_name="some_invalid_stack_name", rules=["StackNameMatchesRegexRule"])) | ||
result = rule.invoke(cfmodel=CFModel()) | ||
assert result.failures | ||
assert ( | ||
result.failures[0].reason | ||
== "The stack name some_invalid_stack_name does not follow the naming convention (only alphanumerical characters and hyphens allowed)." | ||
) |