Skip to content

Commit a6d7b59

Browse files
authored
[Policy tests] Add suffix to agent and package policies to avoid collisions (#3118)
This PR adds a random suffix to the Agent policy and package policies to avoid name collisions in policy tests, ensuring that each policy created by each test defined different names.
1 parent 3409db3 commit a6d7b59

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

‎internal/testrunner/runners/policy/policy.go‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ func expectedPathFor(testPath string) string {
101101
}
102102

103103
type policyEntryFilter struct {
104-
name string
105-
elementsEntries []policyEntryFilter
106-
memberReplace *policyEntryReplace
107-
onlyIfEmpty bool
108-
ignoreValues []any
104+
name string
105+
elementsEntries []policyEntryFilter
106+
memberReplace *policyEntryReplace
107+
stringValueReplace *policyEntryReplace
108+
onlyIfEmpty bool
109+
ignoreValues []any
109110
}
110111

111112
type policyEntryReplace struct {
@@ -125,6 +126,10 @@ var policyEntryFilters = []policyEntryFilter{
125126
{name: "streams", elementsEntries: []policyEntryFilter{
126127
{name: "id"},
127128
}},
129+
{name: "name", stringValueReplace: &policyEntryReplace{
130+
regexp: regexp.MustCompile(`^(.+)-[0-9]+$`),
131+
replace: "$1",
132+
}},
128133
}},
129134
{name: "secret_references", elementsEntries: []policyEntryFilter{
130135
{name: "id"},
@@ -310,6 +315,17 @@ func cleanPolicyMap(policyMap common.MapStr, entries []policyEntryFilter) (commo
310315
m[key] = e
311316
}
312317
}
318+
case entry.stringValueReplace != nil:
319+
vStr, ok := v.(string)
320+
if !ok {
321+
return nil, fmt.Errorf("expected string, found %T", v)
322+
}
323+
regexp := entry.stringValueReplace.regexp
324+
replacement := entry.stringValueReplace.replace
325+
if regexp.MatchString(vStr) {
326+
value := regexp.ReplaceAllString(vStr, replacement)
327+
policyMap.Put(entry.name, value)
328+
}
313329
default:
314330
if entry.onlyIfEmpty && !isEmpty(v, entry.ignoreValues) {
315331
continue

‎internal/testrunner/runners/policy/policy_test.go‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,31 @@ namespaces: []
8383
`,
8484
found: `
8585
namespaces: [foo]
86+
`,
87+
equal: false,
88+
},
89+
{
90+
title: "clean suffix in package policy name",
91+
expected: `
92+
inputs:
93+
- data_stream:
94+
namespace: ep
95+
meta:
96+
package:
97+
name: test_package
98+
name: test-name
99+
streams: []
100+
type: test_package/logs
101+
use_output: default
102+
`,
103+
found: `
104+
inputs:
105+
- data_stream:
106+
namespace: ep
107+
meta:
108+
package:
109+
name: test_package
110+
name: test-name-12345
86111
`,
87112
equal: false,
88113
},
@@ -95,7 +120,7 @@ inputs:
95120
meta:
96121
package:
97122
name: sql_input
98-
name: test-mysql-sql_input
123+
name: test-mysql-sql_input-12345
99124
streams:
100125
- data_stream:
101126
dataset: sql_input.sql_query

‎internal/testrunner/runners/policy/tester.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ package policy
66

77
import (
88
"context"
9+
"fmt"
910
"path/filepath"
1011
"strings"
1112

1213
"github.com/elastic/go-resource"
1314

15+
"github.com/elastic/elastic-package/internal/common"
1416
"github.com/elastic/elastic-package/internal/kibana"
1517
"github.com/elastic/elastic-package/internal/logger"
1618
"github.com/elastic/elastic-package/internal/resources"
@@ -108,12 +110,13 @@ func (r *tester) runTest(ctx context.Context, manager *resources.Manager, testPa
108110
return result.WithSkip(skip)
109111
}
110112

113+
policyTestSuffix := common.CreateTestRunID()
111114
policy := resources.FleetAgentPolicy{
112-
Name: testName,
115+
Name: fmt.Sprintf("%s-%s", testName, policyTestSuffix),
113116
Namespace: "ep",
114117
PackagePolicies: []resources.FleetPackagePolicy{
115118
{
116-
Name: testName + "-" + r.testFolder.Package,
119+
Name: fmt.Sprintf("%s-%s-%s", testName, r.testFolder.Package, policyTestSuffix),
117120
PackageRoot: r.packageRoot,
118121
DataStreamName: r.testFolder.DataStream,
119122
InputName: testConfig.Input,

0 commit comments

Comments
 (0)