Skip to content

Commit

Permalink
Improve unit test of flowexporter package
Browse files Browse the repository at this point in the history
Fix one of the unit test TestFlowExporter_sendFlowRecords, and
adds a testcase to TestFlowExporter_initFlowExporter.

For antrea-io#4142

Signed-off-by: heanlan <hanlan@vmware.com>
  • Loading branch information
heanlan committed Aug 30, 2022
1 parent ca6e64c commit 3ec23f1
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions pkg/agent/flowexporter/exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,21 +268,42 @@ func TestFlowExporter_initFlowExporter(t *testing.T) {
if err != nil {
t.Fatalf("error when resolving UDP address: %v", err)
}
conn, err := net.ListenUDP("udp", udpAddr)
conn1, err := net.ListenUDP("udp", udpAddr)
if err != nil {
t.Fatalf("error when creating a local server: %v", err)
}
defer conn.Close()
exp := &FlowExporter{
process: nil,
exporterInput: exporter.ExporterInput{
CollectorProtocol: conn.LocalAddr().Network(),
CollectorAddress: conn.LocalAddr().String(),
},
defer conn1.Close()
tcpAddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("error when resolving TCP address: %v", err)
}
conn2, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
t.Fatalf("error when creating a local server: %v", err)
}
defer conn2.Close()

for _, tc := range []struct {
protocol string
address string
expectedTempRefTimeout uint32
}{
{conn1.LocalAddr().Network(), conn1.LocalAddr().String(), uint32(1800)},
{conn2.Addr().Network(), conn2.Addr().String(), uint32(0)},
} {
exp := &FlowExporter{
process: nil,
exporterInput: exporter.ExporterInput{
CollectorProtocol: tc.protocol,
CollectorAddress: tc.address,
},
}
err = exp.initFlowExporter()
assert.NoError(t, err)
assert.Equal(t, tc.expectedTempRefTimeout, exp.exporterInput.TempRefTimeout)
checkTotalReconnectionsMetric(t)
metrics.ReconnectionsToFlowCollector.Dec()
}
err = exp.initFlowExporter()
assert.NoError(t, err)
checkTotalReconnectionsMetric(t)
}

func checkTotalReconnectionsMetric(t *testing.T) {
Expand Down Expand Up @@ -422,7 +443,8 @@ func testSendFlowRecords(t *testing.T, v4Enabled bool, v6Enabled bool) {
elementsListv6: elemListv6,
templateIDv4: testTemplateIDv4,
templateIDv6: testTemplateIDv6,
v4Enabled: true}
v4Enabled: v4Enabled,
v6Enabled: v6Enabled}

if v4Enabled {
runSendFlowRecordTests(t, flowExp, false)
Expand Down

0 comments on commit 3ec23f1

Please sign in to comment.