diff --git a/.chloggen/1302.yaml b/.chloggen/1302.yaml index b23d0e5a70..4f2cc12296 100644 --- a/.chloggen/1302.yaml +++ b/.chloggen/1302.yaml @@ -1,4 +1,4 @@ change_type: enhancement component: docs -note: Add note on tooling limitations for attribute names and enforce attribute name format and uniqueness. +note: Add note on tooling limitations for attribute names and enforce name format. issues: [1124] diff --git a/policies/attribute_name_collisions_test.rego b/policies/attribute_name_collisions_test.rego new file mode 100644 index 0000000000..970c2b4749 --- /dev/null +++ b/policies/attribute_name_collisions_test.rego @@ -0,0 +1,21 @@ +package attribute_name_collisions_test + +import data.attribute_name_collisions +import future.keywords + +test_fails_on_const_name_collision if { + collision := {"groups": [ + {"id": "test1", "attributes": [{"name": "foo.bar.baz"}]}, + {"id": "test2", "attributes": [{"name": "foo.bar_baz"}]} + ]} + # each attribute counts as a collision, so there are 2 collisions + count(attribute_name_collisions.deny) == 2 with input as collision +} + +test_fails_on_namespace_collision if { + collision := {"groups": [ + {"id": "test1", "attributes": [{"name": "foo.bar.baz"}]}, + {"id": "test2", "attributes": [{"name": "foo.bar"}]} + ]} + count(attribute_name_collisions.deny) == 1 with input as collision +} diff --git a/policies/yaml_schema_test.rego b/policies/yaml_schema_test.rego new file mode 100644 index 0000000000..73c28002f4 --- /dev/null +++ b/policies/yaml_schema_test.rego @@ -0,0 +1,64 @@ +package yaml_schema_test + +import data.yaml_schema +import future.keywords + +test_fails_on_invalid_attribute_name if { + every name in invalid_names { + count(yaml_schema.deny) == 1 with input as {"groups": create_attribute_group(name)} + } +} + +test_fails_on_invalid_metric_name if { + every name in invalid_names { + count(yaml_schema.deny) == 1 with input as {"groups": create_metric(name)} + } +} + +test_fails_on_invalid_event_name if { + every name in invalid_names { + count(yaml_schema.deny) == 1 with input as {"groups": create_event(name)} + } +} + +test_passes_on_valid_names if { + every name in valid_names { + count(yaml_schema.deny) == 0 with input as {"groups": create_attribute_group(name)} + count(yaml_schema.deny) == 0 with input as {"groups": create_metric(name)} + count(yaml_schema.deny) == 0 with input as {"groups": create_event(name)} + } +} + +create_attribute_group(attr) = json { + json := [{"id": "yaml_schema.test", "attributes": [{"id": attr}]}] +} + +create_metric(name) = json { + json := [{"id": "yaml_schema.test", "type": "metric", "metric_name": name}] +} + +create_event(name) = json { + json := [{"id": "yaml_schema.test", "type": "event", "name": name}] +} + +invalid_names := [ + "1foo.bar", + "_foo.bar", + ".foo.bar", + "foo.bar_", + "foo.bar.", + "foo..bar", + "foo._bar", + "foo__bar", + "foo_.bar", + "foo,bar", + "fü.bär", +] + +valid_names := [ + "foo.bar", + "foo.1bar", + "foo_1.bar", + "foo.bar.baz", + "foo.bar_baz", +]