Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lmolkova committed Aug 18, 2024
1 parent 8110e88 commit 7cf33a5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .chloggen/1302.yaml
Original file line number Diff line number Diff line change
@@ -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]
21 changes: 21 additions & 0 deletions policies/attribute_name_collisions_test.rego
Original file line number Diff line number Diff line change
@@ -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
}
64 changes: 64 additions & 0 deletions policies/yaml_schema_test.rego
Original file line number Diff line number Diff line change
@@ -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",
]

0 comments on commit 7cf33a5

Please sign in to comment.