Skip to content
This repository was archived by the owner on Jan 29, 2021. It is now read-only.

Commit 7b4c40e

Browse files
author
Logan Lembke
committed
Refactor getTestFlowX functions into newTestFlow() and package level variables
1 parent d67af3b commit 7b4c40e

File tree

2 files changed

+58
-55
lines changed

2 files changed

+58
-55
lines changed

converter/input/logstash/mongodb/id_bulk_buffer_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ func TestIDBulkBuffer(t *testing.T) {
2020
}
2121

2222
func testBufferOrder(buffer mongodb.Buffer, inputDB mongodb.LogstashMongoInputDB, t *testing.T) {
23-
testFlow1 := getTestFlow1()
24-
testFlow2 := getTestFlow2()
25-
2623
c := inputDB.NewInputConnection()
2724
err := c.Insert(testFlow1)
2825
require.Nil(t, err)

converter/input/logstash/mongodb/reader_test.go

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,60 +19,63 @@ import (
1919
"github.com/stretchr/testify/require"
2020
)
2121

22-
func getTestFlow1() *data.Flow {
23-
flow := &data.Flow{
24-
Host: "A",
25-
}
26-
flow.Netflow.SourceIPv4 = "1.1.1.1"
27-
flow.Netflow.SourcePort = 24846
28-
flow.Netflow.DestinationIPv4 = "2.2.2.2"
29-
flow.Netflow.DestinationPort = 53
30-
flow.Netflow.FlowStartMilliseconds = "2018-05-04T22:36:40.766Z"
31-
flow.Netflow.FlowEndMilliseconds = "2018-05-04T22:36:40.960Z"
32-
flow.Netflow.OctetTotalCount = int64(math.MaxUint32 + 1)
33-
flow.Netflow.PacketTotalCount = int64(math.MaxUint32 + 1)
34-
flow.Netflow.ProtocolIdentifier = protocols.UDP
35-
flow.Netflow.FlowEndReason = input.ActiveTimeout
36-
flow.Netflow.Version = 10
37-
return flow
38-
}
22+
func newTestFlow(
23+
host string, sourceIP string, sourcePort uint16, sourceV6 bool,
24+
destinationIP string, destinationPort uint16, destinationV6 bool,
25+
flowStart string, flowEnd string, octetCount int64, packetCount int64,
26+
protocol protocols.Identifier, endReason input.FlowEndReason, version uint8,
27+
) *data.Flow {
3928

40-
func getTestFlow2() *data.Flow {
4129
flow := &data.Flow{
42-
Host: "B",
30+
Host: host,
31+
}
32+
if sourceV6 {
33+
flow.Netflow.SourceIPv6 = sourceIP
34+
} else {
35+
flow.Netflow.SourceIPv4 = sourceIP
4336
}
44-
flow.Netflow.SourceIPv4 = "2.2.2.2"
45-
flow.Netflow.SourcePort = 53
46-
flow.Netflow.DestinationIPv4 = "1.1.1.1"
47-
flow.Netflow.DestinationPort = 24846
48-
flow.Netflow.FlowStartMilliseconds = "2018-05-04T22:40:40.555Z"
49-
flow.Netflow.FlowEndMilliseconds = "2018-05-04T22:40:40.555Z"
50-
flow.Netflow.OctetTotalCount = int64(math.MaxUint32 - 1)
51-
flow.Netflow.PacketTotalCount = int64(math.MaxUint32 - 1)
52-
flow.Netflow.ProtocolIdentifier = protocols.TCP
53-
flow.Netflow.FlowEndReason = input.IdleTimeout
54-
flow.Netflow.Version = 10
55-
return flow
56-
}
5737

58-
func getTestFlow3() *data.Flow {
59-
flow := &data.Flow{
60-
Host: "C",
38+
if destinationV6 {
39+
flow.Netflow.DestinationIPv6 = destinationIP
40+
} else {
41+
flow.Netflow.DestinationIPv4 = destinationIP
6142
}
62-
flow.Netflow.SourceIPv4 = "3.3.3.3"
63-
flow.Netflow.SourcePort = 28972
64-
flow.Netflow.DestinationIPv4 = "4.4.4.4"
65-
flow.Netflow.DestinationPort = 443
66-
flow.Netflow.FlowStartMilliseconds = "2018-05-02T05:40:12.333Z"
67-
flow.Netflow.FlowEndMilliseconds = "2018-05-02T05:40:12.444Z"
68-
flow.Netflow.OctetTotalCount = math.MaxInt64
69-
flow.Netflow.PacketTotalCount = math.MaxInt64
70-
flow.Netflow.ProtocolIdentifier = protocols.TCP
71-
flow.Netflow.FlowEndReason = input.ActiveTimeout
72-
flow.Netflow.Version = 10
43+
flow.Netflow.SourcePort = sourcePort
44+
flow.Netflow.DestinationPort = destinationPort
45+
flow.Netflow.FlowStartMilliseconds = flowStart
46+
flow.Netflow.FlowEndMilliseconds = flowEnd
47+
flow.Netflow.OctetTotalCount = octetCount
48+
flow.Netflow.PacketTotalCount = packetCount
49+
flow.Netflow.ProtocolIdentifier = protocol
50+
flow.Netflow.FlowEndReason = endReason
51+
flow.Netflow.Version = version
7352
return flow
7453
}
7554

55+
var testFlow1 = newTestFlow(
56+
"A", "1.1.1.1", 24846, false,
57+
"2.2.2.2", 53, false,
58+
"2018-05-04T22:36:40.766Z", "2018-05-04T22:36:40.960Z",
59+
int64(math.MaxUint32+1), int64(math.MaxUint32+1),
60+
protocols.UDP, input.ActiveTimeout, 10,
61+
)
62+
63+
var testFlow2 = newTestFlow(
64+
"B", "1.1.1.1", 24846, false,
65+
"2.2.2.2", 53, false,
66+
"2018-05-04T22:40:40.555Z", "2018-05-04T22:40:40.555Z",
67+
int64(math.MaxUint32-1), int64(math.MaxUint32-1),
68+
protocols.TCP, input.IdleTimeout, 10,
69+
)
70+
71+
var testFlow3 = newTestFlow(
72+
"C", "3.3.3.3", 28972, false,
73+
"4.4.4.4", 443, false,
74+
"2018-05-02T05:40:12.333Z", "2018-05-02T05:40:12.444Z",
75+
math.MaxInt64, math.MaxInt64,
76+
protocols.TCP, input.ActiveTimeout, 10,
77+
)
78+
7679
func TestReader(t *testing.T) {
7780
fixtures := fixturesManager.BeginTest(t)
7881
defer fixturesManager.EndTest(t)
@@ -83,9 +86,9 @@ func TestReader(t *testing.T) {
8386
reader := mongodb.NewReader(buff, 2*time.Second, env.Logger)
8487

8588
c := inputDB.NewInputConnection()
86-
err := c.Insert(getTestFlow1())
89+
err := c.Insert(testFlow1)
8790
require.Nil(t, err)
88-
err = c.Insert(getTestFlow2())
91+
err = c.Insert(testFlow2)
8992
require.Nil(t, err)
9093

9194
ctx, cancel := context.WithCancel(context.Background())
@@ -106,7 +109,8 @@ func TestReader(t *testing.T) {
106109
outFlow, ok := f.(*data.Flow)
107110
flowTestResults <- testResult{ok, "flow is *data.Flow", nil}
108111

109-
flow1 := getTestFlow1()
112+
flow1 := &data.Flow{}
113+
*flow1 = *testFlow1
110114
flow1.ID = outFlow.ID
111115
flowTestResults <- testResult{reflect.DeepEqual(flow1, outFlow), "flow1 read correctly", []interface{}{outFlow, flow1}}
112116

@@ -115,7 +119,8 @@ func TestReader(t *testing.T) {
115119
outFlow, ok = f.(*data.Flow)
116120
flowTestResults <- testResult{ok, "flow is *data.Flow", nil}
117121

118-
flow2 := getTestFlow2()
122+
flow2 := &data.Flow{}
123+
*flow2 = *testFlow2
119124
flow2.ID = outFlow.ID
120125
flowTestResults <- testResult{reflect.DeepEqual(flow2, outFlow), "flow2 read correctly", []interface{}{outFlow, flow2}}
121126

@@ -124,7 +129,8 @@ func TestReader(t *testing.T) {
124129
outFlow, ok = f.(*data.Flow)
125130
flowTestResults <- testResult{ok, "flow is *data.Flow", nil}
126131

127-
flow3 := getTestFlow3()
132+
flow3 := &data.Flow{}
133+
*flow3 = *testFlow3
128134
flow3.ID = outFlow.ID
129135
flowTestResults <- testResult{reflect.DeepEqual(flow3, outFlow), "flow3 read correctly", []interface{}{outFlow, flow3}}
130136
close(flowTestResults)
@@ -137,7 +143,7 @@ func TestReader(t *testing.T) {
137143
}(errorTestResults, errs, &wg)
138144

139145
time.Sleep(2 * time.Second)
140-
err = c.Insert(getTestFlow3())
146+
err = c.Insert(testFlow3)
141147
require.Nil(t, err)
142148
time.Sleep(2 * time.Second)
143149
cancel()

0 commit comments

Comments
 (0)