Skip to content

Support OTel config in policy tests #2799

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
49 changes: 47 additions & 2 deletions internal/testrunner/runners/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func expectedPathFor(testPath string) string {
type policyEntryFilter struct {
name string
elementsEntries []policyEntryFilter
elementsReplace *policyEntryReplace
memberReplace *policyEntryReplace
onlyIfEmpty bool
}
Expand Down Expand Up @@ -152,6 +153,29 @@ var policyEntryFilters = []policyEntryFilter{

// Namespaces may not be present in older versions of the stack.
{name: "namespaces", onlyIfEmpty: true},

// OTel Collector IDs are relevant, but just check that they are there.
{name: "extensions", memberReplace: &otelComponentIDReplace},
{name: "receivers", memberReplace: &otelComponentIDReplace},
{name: "processors", memberReplace: &otelComponentIDReplace},
{name: "exporters", memberReplace: &otelComponentIDReplace},
{name: "service.extensions", elementsReplace: &otelComponentIDReplace},

// TODO: The signals here will need patterns at some moment, as they can also contain ids.
{name: "service.pipelines.logs.receivers", elementsReplace: &otelComponentIDReplace},
{name: "service.pipelines.logs.processors", elementsReplace: &otelComponentIDReplace},
{name: "service.pipelines.logs.exporters", elementsReplace: &otelComponentIDReplace},
{name: "service.pipelines.metrics.receivers", elementsReplace: &otelComponentIDReplace},
{name: "service.pipelines.metrics.processors", elementsReplace: &otelComponentIDReplace},
{name: "service.pipelines.metrics.exporters", elementsReplace: &otelComponentIDReplace},
{name: "service.pipelines.traces.receivers", elementsReplace: &otelComponentIDReplace},
{name: "service.pipelines.traces.processors", elementsReplace: &otelComponentIDReplace},
{name: "service.pipelines.traces.exporters", elementsReplace: &otelComponentIDReplace},
}

var otelComponentIDReplace = policyEntryReplace{
regexp: regexp.MustCompile(`^([^/]+)/.*$`),
replace: "$1/componentid",
}

// cleanPolicy prepares a policy YAML as returned by the download API to be compared with other
Expand Down Expand Up @@ -206,10 +230,31 @@ func cleanPolicyMap(policyMap common.MapStr, entries []policyEntryFilter) (commo
if !ok {
return nil, fmt.Errorf("expected map, found %T", v)
}
regexp := entry.memberReplace.regexp
replacement := entry.memberReplace.replace
for k, e := range m {
if entry.memberReplace.regexp.MatchString(k) {
key := k
if regexp.MatchString(k) {
delete(m, k)
m[entry.memberReplace.replace] = e
key = regexp.ReplaceAllString(k, replacement)
m[key] = e
}
strElement, ok := e.(string)
if ok && regexp.MatchString(strElement) {
m[key] = regexp.ReplaceAllString(strElement, replacement)
}
}
case entry.elementsReplace != nil:
list, ok := v.([]any)
if !ok {
return nil, fmt.Errorf("expected list, found %T", v)
}
regexp := entry.elementsReplace.regexp
replacement := entry.elementsReplace.replace
for i, e := range list {
strElement, ok := e.(string)
if ok && regexp.MatchString(strElement) {
list[i] = regexp.ReplaceAllString(strElement, replacement)
}
}
default:
Expand Down
66 changes: 66 additions & 0 deletions internal/testrunner/runners/policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,72 @@ secret_references:
`,
equal: false,
},
{
title: "otel ids",
expected: `
inputs: []
output_permissions:
default:
_elastic_agent_checks:
cluster:
- monitor
_elastic_agent_monitoring:
indices: []
uuid-for-permissions-on-related-indices:
indices:
- names:
- logs-*-*
privileges:
- auto_configure
- create_doc
receivers:
httpcheck/componentid:
collection_interval: 1m
targets:
- endpoints:
- https://epr.elastic.co
method: GET
secret_references: []
service:
pipelines:
logs:
receivers:
- httpcheck/componentid

`,
found: `
inputs: []
output_permissions:
default:
_elastic_agent_checks:
cluster:
- monitor
_elastic_agent_monitoring:
indices: []
uuid-for-permissions-on-related-indices:
indices:
- names:
- logs-*-*
privileges:
- auto_configure
- create_doc
receivers:
httpcheck/b0f518d6-4e2d-4c5d-bda7-f9808df537b7:
collection_interval: 1m
targets:
- endpoints:
- https://epr.elastic.co
method: GET
secret_references: []
service:
pipelines:
logs:
receivers:
- httpcheck/b0f518d6-4e2d-4c5d-bda7-f9808df537b7

`,
equal: true,
},
}

for _, c := range cases {
Expand Down