Skip to content

Commit a618a07

Browse files
mergify[bot]efd6
andauthored
x-pack/filebeat/input/netflow/decoder/...: move testing helpers out of exported package (#44481) (#44759)
Including the testing helpers in the template package results in unnecessarily polluting the filebeat binary with the testing package. So move it into its own package that is only imported by _test.go files. (cherry picked from commit 57b1f46) Co-authored-by: Dan Kortschak <dan.kortschak@elastic.co>
1 parent 84984bf commit a618a07

File tree

9 files changed

+33
-27
lines changed

9 files changed

+33
-27
lines changed

x-pack/filebeat/input/netflow/decoder/ipfix/decoder_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/fields"
1616
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template"
17+
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template/tmpltest"
1718
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/test"
1819
v9 "github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/v9"
1920
)
@@ -146,7 +147,7 @@ func TestDecoderV9_ReadFields(t *testing.T) {
146147
assert.Equal(t, tc.expected.Length, record.Length)
147148
assert.Equal(t, tc.expected.VariableLength, record.VariableLength)
148149
assert.Equal(t, tc.expected.ID, record.ID)
149-
template.AssertFieldsEquals(t, tc.expected.Fields, record.Fields)
150+
tmpltest.AssertFieldsEquals(t, tc.expected.Fields, record.Fields)
150151
})
151152
}
152153
}
@@ -267,7 +268,7 @@ func TestReadOptionsTemplateFlowSet(t *testing.T) {
267268
assert.Equal(t, tc.err, err)
268269
if assert.Len(t, templates, len(tc.expected)) {
269270
for idx := range tc.expected {
270-
template.AssertTemplateEquals(t, tc.expected[idx], templates[idx])
271+
tmpltest.AssertTemplateEquals(t, tc.expected[idx], templates[idx])
271272
}
272273
}
273274
})
@@ -340,7 +341,7 @@ func TestReadRecordTemplateFlowSet(t *testing.T) {
340341
assert.Equal(t, tc.err, err)
341342
if assert.Len(t, templates, len(tc.expected)) {
342343
for idx := range tc.expected {
343-
template.AssertTemplateEquals(t, tc.expected[idx], templates[idx])
344+
tmpltest.AssertTemplateEquals(t, tc.expected[idx], templates[idx])
344345
}
345346
}
346347
})

x-pack/filebeat/input/netflow/decoder/template/template_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// or more contributor license agreements. Licensed under the Elastic License;
33
// you may not use this file except in compliance with the Elastic License.
44

5-
package template
5+
package template_test
66

77
import (
88
"bytes"
@@ -14,6 +14,8 @@ import (
1414

1515
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/fields"
1616
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/record"
17+
. "github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template"
18+
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template/tmpltest"
1719
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/test"
1820
)
1921

@@ -639,7 +641,7 @@ func TestTemplateEquals(t *testing.T) {
639641
VariableLength: true,
640642
ScopeFields: 0,
641643
}
642-
assert.True(t, ValidateTemplate(t, &a))
644+
assert.True(t, tmpltest.ValidateTemplate(t, &a))
643645
b := a
644-
assert.True(t, AssertTemplateEquals(t, &a, &b))
646+
assert.True(t, tmpltest.AssertTemplateEquals(t, &a, &b))
645647
}

x-pack/filebeat/input/netflow/decoder/template/test_helpers.go renamed to x-pack/filebeat/input/netflow/decoder/template/tmpltest/test_helpers.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// or more contributor license agreements. Licensed under the Elastic License;
33
// you may not use this file except in compliance with the Elastic License.
44

5-
package template
5+
// Package tmpltest provides shared template testing functions for netflow templates.
6+
package tmpltest
67

78
import (
89
"fmt"
@@ -13,6 +14,7 @@ import (
1314
"github.com/stretchr/testify/assert"
1415

1516
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/fields"
17+
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template"
1618
)
1719

1820
var (
@@ -26,13 +28,13 @@ func buildDecoderByNameMap() {
2628
}
2729
}
2830

29-
func ValidateTemplate(t testing.TB, template *Template) bool {
31+
func ValidateTemplate(t testing.TB, tmpl *template.Template) bool {
3032
once.Do(buildDecoderByNameMap)
3133

3234
sum := 0
3335
seen := make(map[string]bool)
34-
for idx, field := range template.Fields {
35-
isVariable := template.VariableLength && field.Length == VariableLength
36+
for idx, field := range tmpl.Fields {
37+
isVariable := tmpl.VariableLength && field.Length == template.VariableLength
3638
if !isVariable {
3739
sum += int(field.Length)
3840
} else {
@@ -55,11 +57,11 @@ func ValidateTemplate(t testing.TB, template *Template) bool {
5557
}
5658
}
5759
}
58-
return assert.Equal(t, template.Length, sum) &&
59-
assert.Equal(t, 0, template.ScopeFields)
60+
return assert.Equal(t, tmpl.Length, sum) &&
61+
assert.Equal(t, 0, tmpl.ScopeFields)
6062
}
6163

62-
func AssertFieldsEquals(t testing.TB, expected []FieldTemplate, actual []FieldTemplate) (succeeded bool) {
64+
func AssertFieldsEquals(t testing.TB, expected []template.FieldTemplate, actual []template.FieldTemplate) (succeeded bool) {
6365
if succeeded = assert.Len(t, actual, len(expected)); succeeded {
6466
for idx := range expected {
6567
succeeded = assert.Equal(t, expected[idx].Length, actual[idx].Length, strconv.Itoa(idx)) && succeeded
@@ -69,7 +71,7 @@ func AssertFieldsEquals(t testing.TB, expected []FieldTemplate, actual []FieldTe
6971
return
7072
}
7173

72-
func AssertTemplateEquals(t testing.TB, expected *Template, actual *Template) bool {
74+
func AssertTemplateEquals(t testing.TB, expected *template.Template, actual *template.Template) bool {
7375
if expected == nil && actual == nil {
7476
return true
7577
}

x-pack/filebeat/input/netflow/decoder/v1/v1_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/config"
1717
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/record"
18-
template2 "github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template"
18+
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template/tmpltest"
1919
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/test"
2020
"github.com/elastic/elastic-agent-libs/logp"
2121
)
@@ -123,5 +123,5 @@ func TestNetflowProtocol_BadPacket(t *testing.T) {
123123
}
124124

125125
func TestTemplate(t *testing.T) {
126-
template2.ValidateTemplate(t, &templateV1)
126+
tmpltest.ValidateTemplate(t, &templateV1)
127127
}

x-pack/filebeat/input/netflow/decoder/v5/v5_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/config"
1919
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/record"
20-
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template"
20+
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template/tmpltest"
2121
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/test"
2222
)
2323

@@ -138,5 +138,5 @@ func TestNetflowProtocol_BadPacket(t *testing.T) {
138138
}
139139

140140
func TestTemplate(t *testing.T) {
141-
template.ValidateTemplate(t, &templateV5)
141+
tmpltest.ValidateTemplate(t, &templateV5)
142142
}

x-pack/filebeat/input/netflow/decoder/v6/v6_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/config"
1919
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/record"
20-
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template"
20+
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template/tmpltest"
2121
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/test"
2222
)
2323

@@ -140,5 +140,5 @@ func TestNetflowProtocol_BadPacket(t *testing.T) {
140140
}
141141

142142
func TestTemplate(t *testing.T) {
143-
template.ValidateTemplate(t, &templateV6)
143+
tmpltest.ValidateTemplate(t, &templateV6)
144144
}

x-pack/filebeat/input/netflow/decoder/v7/v7_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/config"
1919
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/record"
20-
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template"
20+
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template/tmpltest"
2121
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/test"
2222
)
2323

@@ -134,5 +134,5 @@ func TestNetflowProtocol_BadPacket(t *testing.T) {
134134
}
135135

136136
func TestTemplate(t *testing.T) {
137-
template.ValidateTemplate(t, &v7template)
137+
tmpltest.ValidateTemplate(t, &v7template)
138138
}

x-pack/filebeat/input/netflow/decoder/v8/v8_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818

1919
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/config"
2020
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/record"
21-
template2 "github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template"
21+
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template/tmpltest"
2222
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/test"
2323
)
2424

@@ -28,7 +28,7 @@ func init() {
2828

2929
func TestTemplates(t *testing.T) {
3030
for code, template := range templates {
31-
if !template2.ValidateTemplate(t, template) {
31+
if !tmpltest.ValidateTemplate(t, template) {
3232
t.Fatal("Failed validating template for V8 record", code)
3333
}
3434
}

x-pack/filebeat/input/netflow/decoder/v9/decoder_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/fields"
1717
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template"
18+
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/template/tmpltest"
1819
"github.com/elastic/beats/v7/x-pack/filebeat/input/netflow/decoder/test"
1920
)
2021

@@ -188,7 +189,7 @@ func TestDecoderV9_ReadFields(t *testing.T) {
188189
assert.Equal(t, tc.expected.Length, record.Length)
189190
assert.Equal(t, tc.expected.VariableLength, record.VariableLength)
190191
assert.Equal(t, tc.expected.ID, record.ID)
191-
template.AssertFieldsEquals(t, tc.expected.Fields, record.Fields)
192+
tmpltest.AssertFieldsEquals(t, tc.expected.Fields, record.Fields)
192193
})
193194
}
194195
}
@@ -277,7 +278,7 @@ func TestReadOptionsTemplateFlowSet(t *testing.T) {
277278
assert.Equal(t, tc.err, err)
278279
if assert.Len(t, templates, len(tc.expected)) {
279280
for idx := range tc.expected {
280-
template.AssertTemplateEquals(t, tc.expected[idx], templates[idx])
281+
tmpltest.AssertTemplateEquals(t, tc.expected[idx], templates[idx])
281282
}
282283
}
283284
})
@@ -350,7 +351,7 @@ func TestReadTemplateFlowSet(t *testing.T) {
350351
assert.Equal(t, tc.err, err)
351352
if assert.Len(t, templates, len(tc.expected)) {
352353
for idx := range tc.expected {
353-
template.AssertTemplateEquals(t, tc.expected[idx], templates[idx])
354+
tmpltest.AssertTemplateEquals(t, tc.expected[idx], templates[idx])
354355
}
355356
}
356357
})

0 commit comments

Comments
 (0)