Skip to content

Commit

Permalink
[pkg/networkdevice] Move GetFreePort to pkg/networkdevice (#19978)
Browse files Browse the repository at this point in the history
* [pkg/networkdevice] Move GetFreePort to pkg/networkdevice

* sort import

* add package comment
  • Loading branch information
AlexandreYang authored Oct 11, 2023
1 parent 06da51f commit 82a8af8
Showing 9 changed files with 52 additions and 53 deletions.
3 changes: 2 additions & 1 deletion comp/netflow/flowaggregator/aggregator_test.go
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@ import (
ddlog "github.com/DataDog/datadog-agent/pkg/util/log"

"github.com/DataDog/datadog-agent/pkg/networkdevice/metadata"
ndmtestutils "github.com/DataDog/datadog-agent/pkg/networkdevice/testutils"

"github.com/DataDog/datadog-agent/comp/netflow/common"
"github.com/DataDog/datadog-agent/comp/netflow/config"
@@ -214,7 +215,7 @@ stopLoop:
}

func TestAggregator_withMockPayload(t *testing.T) {
port, err := testutil.GetFreePort()
port, err := ndmtestutils.GetFreePort()
require.NoError(t, err)
flushTime, _ := time.Parse(time.RFC3339, "2019-02-18T16:00:06Z")

14 changes: 9 additions & 5 deletions comp/netflow/server/integration_test.go
Original file line number Diff line number Diff line change
@@ -16,13 +16,17 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/fx"

"github.com/DataDog/datadog-agent/pkg/epforwarder"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"

"github.com/DataDog/datadog-agent/comp/ndmtmp/forwarder"

ndmtestutils "github.com/DataDog/datadog-agent/pkg/networkdevice/testutils"

"github.com/DataDog/datadog-agent/comp/netflow/common"
nfconfig "github.com/DataDog/datadog-agent/comp/netflow/config"
"github.com/DataDog/datadog-agent/comp/netflow/flowaggregator"
"github.com/DataDog/datadog-agent/comp/netflow/testutil"
"github.com/DataDog/datadog-agent/pkg/epforwarder"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
)

func singleListenerConfig(flowType common.FlowType, port uint16) *nfconfig.NetflowConfig {
@@ -60,7 +64,7 @@ func assertFlowEventsCount(t *testing.T, port uint16, srv *Server, packetData []
}

func TestNetFlow_IntegrationTest_NetFlow5(t *testing.T) {
port, err := testutil.GetFreePort()
port, err := ndmtestutils.GetFreePort()
require.NoError(t, err)
var epForwarder forwarder.MockComponent
srv := fxutil.Test[Component](t, fx.Options(
@@ -85,7 +89,7 @@ func TestNetFlow_IntegrationTest_NetFlow5(t *testing.T) {
}

func TestNetFlow_IntegrationTest_NetFlow9(t *testing.T) {
port, err := testutil.GetFreePort()
port, err := ndmtestutils.GetFreePort()
require.NoError(t, err)
var epForwarder forwarder.MockComponent
srv := fxutil.Test[Component](t, fx.Options(
@@ -108,7 +112,7 @@ func TestNetFlow_IntegrationTest_NetFlow9(t *testing.T) {
}

func TestNetFlow_IntegrationTest_SFlow5(t *testing.T) {
port, err := testutil.GetFreePort()
port, err := ndmtestutils.GetFreePort()
require.NoError(t, err)
var epForwarder forwarder.MockComponent
srv := fxutil.Test[Component](t, fx.Options(
6 changes: 4 additions & 2 deletions comp/netflow/server/server_test.go
Original file line number Diff line number Diff line change
@@ -24,9 +24,11 @@ import (
"github.com/DataDog/datadog-agent/comp/ndmtmp/aggregator"
"github.com/DataDog/datadog-agent/comp/ndmtmp/forwarder"
"github.com/DataDog/datadog-agent/comp/ndmtmp/sender"

ndmtestutils "github.com/DataDog/datadog-agent/pkg/networkdevice/testutils"

nfconfig "github.com/DataDog/datadog-agent/comp/netflow/config"
"github.com/DataDog/datadog-agent/comp/netflow/goflowlib"
"github.com/DataDog/datadog-agent/comp/netflow/testutil"
)

type dummyFlowProcessor struct {
@@ -81,7 +83,7 @@ var testOptions = fx.Options(
)

func TestStartServerAndStopServer(t *testing.T) {
port, err := testutil.GetFreePort()
port, err := ndmtestutils.GetFreePort()
require.NoError(t, err)
var component Component
app := fxtest.New(t, fx.Options(
9 changes: 9 additions & 0 deletions pkg/networkdevice/testutils/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-present Datadog, Inc.

//go:build test

// Package testutils contains utils function for testing ndm features (snmp/traps/netflow)
package testutils
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@

//go:build test

package testutil
package testutils

import (
"net"
5 changes: 5 additions & 0 deletions pkg/snmp/traps/forwarder_test.go
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@ import (

"github.com/DataDog/datadog-agent/pkg/aggregator/mocksender"
"github.com/DataDog/datadog-agent/pkg/epforwarder"

ndmtestutils "github.com/DataDog/datadog-agent/pkg/networkdevice/testutils"
)

type DummyFormatter struct{}
@@ -41,6 +43,9 @@ func (f DummyFormatter) FormatPacket(packet *SnmpPacket) ([]byte, error) {
}

func createForwarder(t *testing.T) (forwarder *TrapForwarder, err error) {
serverPort, err := ndmtestutils.GetFreePort()
require.NoError(t, err)

packetsIn := make(PacketsChannel)
mockSender := mocksender.NewMockSender("snmp-traps-listener")
mockSender.SetupAcceptAll()
20 changes: 18 additions & 2 deletions pkg/snmp/traps/listener_test.go
Original file line number Diff line number Diff line change
@@ -7,16 +7,18 @@ package traps

import (
"errors"
"github.com/DataDog/datadog-agent/pkg/aggregator/mocksender"
"testing"
"time"

"github.com/gosnmp/gosnmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/DataDog/datadog-agent/pkg/aggregator/mocksender"

ndmtestutils "github.com/DataDog/datadog-agent/pkg/networkdevice/testutils"
)

var serverPort = getFreePort()
var initialTrapsPacketsAuthErrors int64

const defaultTimeout = 1 * time.Second
@@ -38,6 +40,8 @@ func listenerTestSetup(t *testing.T, config Config) (*mocksender.MockSender, *Tr
}

func TestListenV1GenericTrap(t *testing.T) {
serverPort, err := ndmtestutils.GetFreePort()
require.NoError(t, err)
config := Config{Port: serverPort, CommunityStrings: []string{"public"}, Namespace: "totoro"}
_, trapListener := listenerTestSetup(t, config)
defer trapListener.Stop()
@@ -50,6 +54,8 @@ func TestListenV1GenericTrap(t *testing.T) {
}

func TestServerV1SpecificTrap(t *testing.T) {
serverPort, err := ndmtestutils.GetFreePort()
require.NoError(t, err)
config := Config{Port: serverPort, CommunityStrings: []string{"public"}}
_, trapListener := listenerTestSetup(t, config)
defer trapListener.Stop()
@@ -62,6 +68,8 @@ func TestServerV1SpecificTrap(t *testing.T) {
}

func TestServerV2(t *testing.T) {
serverPort, err := ndmtestutils.GetFreePort()
require.NoError(t, err)
config := Config{Port: serverPort, CommunityStrings: []string{"public"}}
_, trapListener := listenerTestSetup(t, config)
defer trapListener.Stop()
@@ -74,6 +82,8 @@ func TestServerV2(t *testing.T) {
}

func TestServerV2BadCredentials(t *testing.T) {
serverPort, err := ndmtestutils.GetFreePort()
require.NoError(t, err)
config := Config{Port: serverPort, CommunityStrings: []string{"public"}, Namespace: "totoro"}
mockSender, trapListener := listenerTestSetup(t, config)
defer trapListener.Stop()
@@ -87,6 +97,8 @@ func TestServerV2BadCredentials(t *testing.T) {
}

func TestServerV3(t *testing.T) {
serverPort, err := ndmtestutils.GetFreePort()
require.NoError(t, err)
userV3 := UserV3{Username: "user", AuthKey: "password", AuthProtocol: "sha", PrivKey: "password", PrivProtocol: "aes"}
config := Config{Port: serverPort, Users: []UserV3{userV3}}
_, trapListener := listenerTestSetup(t, config)
@@ -106,6 +118,8 @@ func TestServerV3(t *testing.T) {
}

func TestServerV3BadCredentials(t *testing.T) {
serverPort, err := ndmtestutils.GetFreePort()
require.NoError(t, err)
userV3 := UserV3{Username: "user", AuthKey: "password", AuthProtocol: "sha", PrivKey: "password", PrivProtocol: "aes"}
config := Config{Port: serverPort, Users: []UserV3{userV3}}
_, trapListener := listenerTestSetup(t, config)
@@ -123,6 +137,8 @@ func TestServerV3BadCredentials(t *testing.T) {
}

func TestListenerTrapsReceivedTelemetry(t *testing.T) {
serverPort, err := ndmtestutils.GetFreePort()
require.NoError(t, err)
config := Config{Port: serverPort, CommunityStrings: []string{"public"}, Namespace: "totoro"}
mockSender, trapListener := listenerTestSetup(t, config)
defer trapListener.Stop()
6 changes: 4 additions & 2 deletions pkg/snmp/traps/server_test.go
Original file line number Diff line number Diff line change
@@ -11,15 +11,17 @@ import (
"github.com/stretchr/testify/require"

"github.com/DataDog/datadog-agent/pkg/aggregator/mocksender"
)

var freePort = getFreePort()
ndmtestutils "github.com/DataDog/datadog-agent/pkg/networkdevice/testutils"
)

func TestStartFailure(t *testing.T) {
/*
Start two servers with the same config to trigger an "address already in use" error.
*/

freePort, err := ndmtestutils.GetFreePort()
require.NoError(t, err)
config := Config{Port: freePort, CommunityStrings: []string{"public"}}
Configure(t, config)

40 changes: 0 additions & 40 deletions pkg/snmp/traps/traps_test.go
Original file line number Diff line number Diff line change
@@ -8,9 +8,6 @@
package traps

import (
"github.com/DataDog/datadog-agent/pkg/aggregator/mocksender"
"net"
"strconv"
"strings"
"testing"
"time"
@@ -84,43 +81,6 @@ var (
}
)

func getFreePort() uint16 {
sender := mocksender.NewMockSender("")

var port uint16
for i := 0; i < 5; i++ {
conn, err := net.ListenPacket("udp", ":0")
if err != nil {
continue
}
conn.Close()
port, err = parsePort(conn.LocalAddr().String())
if err != nil {
continue
}
listener, err := startSNMPTrapListener(Config{Port: port}, sender, nil)
if err != nil {
continue
}
listener.Stop()
return port
}
panic("unable to find free port for starting the trap listener")
}

func parsePort(addr string) (uint16, error) {
_, portString, err := net.SplitHostPort(addr)
if err != nil {
return 0, err
}

port, err := strconv.ParseUint(portString, 10, 16)
if err != nil {
return 0, err
}
return uint16(port), nil
}

// Configure sets Datadog Agent configuration from a config object.
func Configure(t *testing.T, trapConfig Config) {
ConfigureWithGlobalNamespace(t, trapConfig, "")

0 comments on commit 82a8af8

Please sign in to comment.