Skip to content

Commit

Permalink
Fix dict merge
Browse files Browse the repository at this point in the history
  • Loading branch information
ademariag committed Dec 17, 2022
1 parent bbb80e9 commit e955887
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 37 deletions.
20 changes: 11 additions & 9 deletions kapitan/validator/kubernetes_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,18 @@ def _validate_config_block(self, config):
continue

# Evaluating annotation
validation_enabled_annotation = (
validate_instance.get("metadata", {})
.get("annotations", {})
.get(defaults.VALIDATION_ENABLED_ANNOTATION, "true")
)
if validation_enabled_annotation.lower() in ["false", "disabled"]:
exclusions.append(
f"Validation: skipping kind {kind} inside {validate_file} because of ignore annotation"
annotations = validate_instance.get("metadata", {}).get("annotations", {})

if annotations:
validation_enabled_annotation = annotations.get(
defaults.VALIDATION_ENABLED_ANNOTATION, "true"
)
continue

if validation_enabled_annotation.lower() in ["false", "disabled"]:
exclusions.append(
f"Validation: skipping kind {kind} inside {validate_file} because of ignore annotation"
)
continue

try:
validator = validators_kind_cache.setdefault(
Expand Down
76 changes: 48 additions & 28 deletions tests/test_kubernetes_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
targetPort: 9376
"""


class KubernetesValidatorTest(unittest.TestCase):
def setUp(self):
os.chdir(os.path.join("examples", "kubernetes"))
Expand Down Expand Up @@ -112,47 +113,66 @@ def test_validate(self):
_INVALID_EXCLUDE_PATH = self._get_manifest(INVALID_MANIFEST, "service_manifest_exclude_path.yaml")
_INVALID_W_ANNOTATION = self._get_manifest(INVALID_WITH_ANNOTATION, "service_manifest_annotated.yaml")

basic_config = {"target_name": "test_validate", "version": "1.26.0"}
validate_objects = list()
validate_objects += [{"output_files": [_VALID]} | basic_config]
validate_objects += [{"output_files": [_INVALID], "fail_on_error": False} | basic_config]
validate_objects += [
{"output_files": [_INVALID_EXCLUDE_KIND], "exclude": {"kinds": ["Service"]}} | basic_config
]
validate_objects += [
{"output_files": [_INVALID_EXCLUDE_PATH], "excluded_files": [_INVALID_EXCLUDE_PATH]}
| basic_config
]
validate_objects += [{"output_files": [_INVALID_W_ANNOTATION]} | basic_config]
self.validator.validate(("1.26.0", validate_objects))
objs = list()
objs.append({"target_name": "test", "output_files": [_VALID]})
objs.append({"target_name": "test", "output_files": [_INVALID], "fail_on_error": False})
objs.append(
{
"target_name": "test",
"output_files": [_INVALID_EXCLUDE_KIND],
"exclude": {"kinds": ["Service"]},
}
)
objs.append(
{
"target_name": "test",
"output_files": [_INVALID_EXCLUDE_PATH],
"excluded_files": [_INVALID_EXCLUDE_PATH],
}
)
objs.append({"target_name": "test", "output_files": [_INVALID_W_ANNOTATION]})
self.validator.validate(("1.26.0", objs))

def test_validate_failure(self):
# None of these should fail
_INVALID = self._get_manifest(INVALID_MANIFEST, "service_manifest_invalid.yaml")
_INVALID_EXCLUDE_KIND = self._get_manifest(INVALID_MANIFEST, "service_manifest_exclude_kind.yaml")
_INVALID_EXCLUDE_PATH = self._get_manifest(INVALID_MANIFEST, "service_manifest_exclude_path.yaml")
_INVALID_W_ANNOTATION_TRUE = self._get_manifest(INVALID_WITH_ANNOTATION_TRUE, "service_manifest_annotated.yaml")

basic_config = {"target_name": "test_validate", "version": "1.26.0"}
_INVALID_W_ANNOTATION_TRUE = self._get_manifest(
INVALID_WITH_ANNOTATION_TRUE, "service_manifest_annotated.yaml"
)

validate_objects = [{"output_files": [_INVALID]} | basic_config]
self.assertRaises(KubernetesManifestValidationError, self.validator.validate, ("1.26.0", validate_objects))
validate_objects = [{"target_name": "test", "output_files": [_INVALID]}]
self.assertRaises(
KubernetesManifestValidationError, self.validator.validate, ("1.26.0", validate_objects)
)

validate_objects = [
{"output_files": [_INVALID_EXCLUDE_KIND], "exclude": {"kinds": ["Deployment"]}} | basic_config
{
"target_name": "test",
"output_files": [_INVALID_EXCLUDE_KIND],
"exclude": {"kinds": ["Deployment"]},
}
]
self.assertRaises(KubernetesManifestValidationError, self.validator.validate, ("1.26.0", validate_objects))

self.assertRaises(
KubernetesManifestValidationError, self.validator.validate, ("1.26.0", validate_objects)
)

validate_objects = [
{"output_files": [_INVALID_EXCLUDE_PATH], "excluded_files": ["another_path"]}
| basic_config
{
"target_name": "test",
"output_files": [_INVALID_EXCLUDE_PATH],
"excluded_files": ["another_path"],
}
]
self.assertRaises(KubernetesManifestValidationError, self.validator.validate, ("1.26.0", validate_objects))

validate_objects = [{"output_files": [_INVALID_W_ANNOTATION_TRUE]} | basic_config]
self.assertRaises(KubernetesManifestValidationError, self.validator.validate, ("1.26.0", validate_objects))

self.assertRaises(
KubernetesManifestValidationError, self.validator.validate, ("1.26.0", validate_objects)
)

validate_objects = [{"target_name": "test", "output_files": [_INVALID_W_ANNOTATION_TRUE]}]
self.assertRaises(
KubernetesManifestValidationError, self.validator.validate, ("1.26.0", validate_objects)
)

def test_validate_command_pass(self):
sys.argv = ["kapitan", "validate", "--schemas-path", self.cache_dir]
Expand Down

0 comments on commit e955887

Please sign in to comment.