Skip to content

Commit af42910

Browse files
committed
Support OTel config in policy tests
1 parent 71bbe8e commit af42910

File tree

2 files changed

+113
-2
lines changed

2 files changed

+113
-2
lines changed

internal/testrunner/runners/policy/policy.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func expectedPathFor(testPath string) string {
9696
type policyEntryFilter struct {
9797
name string
9898
elementsEntries []policyEntryFilter
99+
elementsReplace *policyEntryReplace
99100
memberReplace *policyEntryReplace
100101
onlyIfEmpty bool
101102
}
@@ -152,6 +153,29 @@ var policyEntryFilters = []policyEntryFilter{
152153

153154
// Namespaces may not be present in older versions of the stack.
154155
{name: "namespaces", onlyIfEmpty: true},
156+
157+
// OTel Collector IDs are relevant, but just check that they are there.
158+
{name: "extensions", memberReplace: &otelComponentIDReplace},
159+
{name: "receivers", memberReplace: &otelComponentIDReplace},
160+
{name: "processors", memberReplace: &otelComponentIDReplace},
161+
{name: "exporters", memberReplace: &otelComponentIDReplace},
162+
{name: "service.extensions", elementsReplace: &otelComponentIDReplace},
163+
164+
// TODO: The signals here will need patterns at some moment, as they can also contain ids.
165+
{name: "service.pipelines.logs.receivers", elementsReplace: &otelComponentIDReplace},
166+
{name: "service.pipelines.logs.processors", elementsReplace: &otelComponentIDReplace},
167+
{name: "service.pipelines.logs.exporters", elementsReplace: &otelComponentIDReplace},
168+
{name: "service.pipelines.metrics.receivers", elementsReplace: &otelComponentIDReplace},
169+
{name: "service.pipelines.metrics.processors", elementsReplace: &otelComponentIDReplace},
170+
{name: "service.pipelines.metrics.exporters", elementsReplace: &otelComponentIDReplace},
171+
{name: "service.pipelines.traces.receivers", elementsReplace: &otelComponentIDReplace},
172+
{name: "service.pipelines.traces.processors", elementsReplace: &otelComponentIDReplace},
173+
{name: "service.pipelines.traces.exporters", elementsReplace: &otelComponentIDReplace},
174+
}
175+
176+
var otelComponentIDReplace = policyEntryReplace{
177+
regexp: regexp.MustCompile(`^([^/]+)/.*$`),
178+
replace: "$1/componentid",
155179
}
156180

157181
// cleanPolicy prepares a policy YAML as returned by the download API to be compared with other
@@ -206,10 +230,31 @@ func cleanPolicyMap(policyMap common.MapStr, entries []policyEntryFilter) (commo
206230
if !ok {
207231
return nil, fmt.Errorf("expected map, found %T", v)
208232
}
233+
regexp := entry.memberReplace.regexp
234+
replacement := entry.memberReplace.replace
209235
for k, e := range m {
210-
if entry.memberReplace.regexp.MatchString(k) {
236+
key := k
237+
if regexp.MatchString(k) {
211238
delete(m, k)
212-
m[entry.memberReplace.replace] = e
239+
key = regexp.ReplaceAllString(k, replacement)
240+
m[key] = e
241+
}
242+
strElement, ok := e.(string)
243+
if ok && regexp.MatchString(strElement) {
244+
m[key] = regexp.ReplaceAllString(strElement, replacement)
245+
}
246+
}
247+
case entry.elementsReplace != nil:
248+
list, ok := v.([]any)
249+
if !ok {
250+
return nil, fmt.Errorf("expected list, found %T", v)
251+
}
252+
regexp := entry.elementsReplace.regexp
253+
replacement := entry.elementsReplace.replace
254+
for i, e := range list {
255+
strElement, ok := e.(string)
256+
if ok && regexp.MatchString(strElement) {
257+
list[i] = regexp.ReplaceAllString(strElement, replacement)
213258
}
214259
}
215260
default:

internal/testrunner/runners/policy/policy_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,72 @@ secret_references:
276276
`,
277277
equal: false,
278278
},
279+
{
280+
title: "otel ids",
281+
expected: `
282+
inputs: []
283+
output_permissions:
284+
default:
285+
_elastic_agent_checks:
286+
cluster:
287+
- monitor
288+
_elastic_agent_monitoring:
289+
indices: []
290+
uuid-for-permissions-on-related-indices:
291+
indices:
292+
- names:
293+
- logs-*-*
294+
privileges:
295+
- auto_configure
296+
- create_doc
297+
receivers:
298+
httpcheck/componentid:
299+
collection_interval: 1m
300+
targets:
301+
- endpoints:
302+
- https://epr.elastic.co
303+
method: GET
304+
secret_references: []
305+
service:
306+
pipelines:
307+
logs:
308+
receivers:
309+
- httpcheck/componentid
310+
311+
`,
312+
found: `
313+
inputs: []
314+
output_permissions:
315+
default:
316+
_elastic_agent_checks:
317+
cluster:
318+
- monitor
319+
_elastic_agent_monitoring:
320+
indices: []
321+
uuid-for-permissions-on-related-indices:
322+
indices:
323+
- names:
324+
- logs-*-*
325+
privileges:
326+
- auto_configure
327+
- create_doc
328+
receivers:
329+
httpcheck/b0f518d6-4e2d-4c5d-bda7-f9808df537b7:
330+
collection_interval: 1m
331+
targets:
332+
- endpoints:
333+
- https://epr.elastic.co
334+
method: GET
335+
secret_references: []
336+
service:
337+
pipelines:
338+
logs:
339+
receivers:
340+
- httpcheck/b0f518d6-4e2d-4c5d-bda7-f9808df537b7
341+
342+
`,
343+
equal: true,
344+
},
279345
}
280346

281347
for _, c := range cases {

0 commit comments

Comments
 (0)