Skip to content

Commit 2e1f0a1

Browse files
committed
Skip by default compareResults in pipeline tests for serverless
1 parent af6a093 commit 2e1f0a1

2 files changed

Lines changed: 38 additions & 52 deletions

File tree

internal/testrunner/runners/pipeline/runner.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/elastic/elastic-package/internal/common"
2121
"github.com/elastic/elastic-package/internal/elasticsearch/ingest"
22+
"github.com/elastic/elastic-package/internal/environment"
2223
"github.com/elastic/elastic-package/internal/fields"
2324
"github.com/elastic/elastic-package/internal/logger"
2425
"github.com/elastic/elastic-package/internal/multierror"
@@ -33,9 +34,15 @@ const (
3334
TestType testrunner.TestType = "pipeline"
3435
)
3536

37+
var (
38+
serverlessEnableCompareResults = environment.WithElasticPackagePrefix("SERVERLESS_PIPELINE_TEST_ENABLE_COMPARE_RESULTS")
39+
)
40+
3641
type runner struct {
3742
options testrunner.TestOptions
3843
pipelines []ingest.Pipeline
44+
45+
runCompareResults bool
3946
}
4047

4148
type IngestPipelineReroute struct {
@@ -61,6 +68,22 @@ func (r *runner) String() string {
6168
// Run runs the pipeline tests defined under the given folder
6269
func (r *runner) Run(options testrunner.TestOptions) ([]testrunner.TestResult, error) {
6370
r.options = options
71+
72+
stackConfig, err := stack.LoadConfig(r.options.Profile)
73+
if err != nil {
74+
return nil, err
75+
}
76+
77+
r.runCompareResults = true
78+
if stackConfig.Provider == stack.ProviderServerless {
79+
r.runCompareResults = false
80+
81+
v, ok := os.LookupEnv(serverlessEnableCompareResults)
82+
if ok && strings.ToLower(v) != "false" {
83+
r.runCompareResults = true
84+
}
85+
}
86+
6487
return r.run()
6588
}
6689

@@ -303,20 +326,14 @@ func (r *runner) verifyResults(testCaseFile string, config *testConfig, result *
303326
}
304327

305328
// TODO: currently GeoIP related fields are being removed when the serverless provider is used.
306-
stackConfig, err := stack.LoadConfig(r.options.Profile)
307-
if err != nil {
308-
return err
309-
}
310-
skipGeoIP := false
311-
if stackConfig.Provider == stack.ProviderServerless {
312-
skipGeoIP = true
313-
}
314-
err = compareResults(testCasePath, config, result, skipGeoIP, *specVersion)
315-
if _, ok := err.(testrunner.ErrTestCaseFailed); ok {
316-
return err
317-
}
318-
if err != nil {
319-
return fmt.Errorf("comparing test results failed: %w", err)
329+
if r.runCompareResults {
330+
err = compareResults(testCasePath, config, result, *specVersion)
331+
if _, ok := err.(testrunner.ErrTestCaseFailed); ok {
332+
return err
333+
}
334+
if err != nil {
335+
return fmt.Errorf("comparing test results failed: %w", err)
336+
}
320337
}
321338

322339
result = stripEmptyTestResults(result)

internal/testrunner/runners/pipeline/test_result.go

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,6 @@ import (
2424

2525
const expectedTestResultSuffix = "-expected.json"
2626

27-
var geoIPKeys = []string{
28-
"as",
29-
"geo",
30-
"client.as",
31-
"client.geo",
32-
"destination.as",
33-
"destination.geo",
34-
"host.geo", // not defined host.as in ECS
35-
"observer.geo", // not defined observer.as in ECS
36-
"server.as",
37-
"server.geo",
38-
"source.as",
39-
"source.geo",
40-
"threat.enrichments.indicateor.as",
41-
"threat.enrichments.indicateor.geo",
42-
"threat.indicateor.as",
43-
"threat.indicateor.geo",
44-
// packages using geo fields in nested objects
45-
"netskope.alerts.user.geo",
46-
"netskope.events.user.geo",
47-
}
48-
4927
type testResult struct {
5028
events []json.RawMessage
5129
}
@@ -69,8 +47,8 @@ func writeTestResult(testCasePath string, result *testResult, specVersion semver
6947
return nil
7048
}
7149

72-
func compareResults(testCasePath string, config *testConfig, result *testResult, skipGeoip bool, specVersion semver.Version) error {
73-
resultsWithoutDynamicFields, err := adjustTestResult(result, config, skipGeoip)
50+
func compareResults(testCasePath string, config *testConfig, result *testResult, specVersion semver.Version) error {
51+
resultsWithoutDynamicFields, err := adjustTestResult(result, config)
7452
if err != nil {
7553
return fmt.Errorf("can't adjust test results: %w", err)
7654
}
@@ -80,7 +58,7 @@ func compareResults(testCasePath string, config *testConfig, result *testResult,
8058
return fmt.Errorf("marshalling actual test results failed: %w", err)
8159
}
8260

83-
expectedResults, err := readExpectedTestResult(testCasePath, config, skipGeoip)
61+
expectedResults, err := readExpectedTestResult(testCasePath, config)
8462
if err != nil {
8563
return fmt.Errorf("reading expected test result failed: %w", err)
8664
}
@@ -161,7 +139,7 @@ func diffJson(want, got []byte, specVersion semver.Version) (string, error) {
161139
return buf.String(), err
162140
}
163141

164-
func readExpectedTestResult(testCasePath string, config *testConfig, skipGeoIP bool) (*testResult, error) {
142+
func readExpectedTestResult(testCasePath string, config *testConfig) (*testResult, error) {
165143
testCaseDir := filepath.Dir(testCasePath)
166144
testCaseFile := filepath.Base(testCasePath)
167145

@@ -176,15 +154,15 @@ func readExpectedTestResult(testCasePath string, config *testConfig, skipGeoIP b
176154
return nil, fmt.Errorf("unmarshalling expected test result failed: %w", err)
177155
}
178156

179-
adjusted, err := adjustTestResult(u, config, skipGeoIP)
157+
adjusted, err := adjustTestResult(u, config)
180158
if err != nil {
181159
return nil, fmt.Errorf("adjusting test result failed: %w", err)
182160
}
183161
return adjusted, nil
184162
}
185163

186-
func adjustTestResult(result *testResult, config *testConfig, skipGeoIP bool) (*testResult, error) {
187-
if !skipGeoIP && (config == nil || config.DynamicFields == nil) {
164+
func adjustTestResult(result *testResult, config *testConfig) (*testResult, error) {
165+
if config == nil || config.DynamicFields == nil {
188166
return result, nil
189167
}
190168
var stripped testResult
@@ -210,15 +188,6 @@ func adjustTestResult(result *testResult, config *testConfig, skipGeoIP bool) (*
210188
}
211189
}
212190

213-
if skipGeoIP {
214-
for _, key := range geoIPKeys {
215-
err := m.Delete(key)
216-
if err != nil && err != common.ErrKeyNotFound {
217-
return nil, fmt.Errorf("can't remove geoIP field: %w", err)
218-
}
219-
}
220-
}
221-
222191
b, err := json.Marshal(&m)
223192
if err != nil {
224193
return nil, fmt.Errorf("can't marshal event: %w", err)

0 commit comments

Comments
 (0)