Skip to content

Commit

Permalink
test: use random TPC port, restore centos & win test
Browse files Browse the repository at this point in the history
  • Loading branch information
varas committed Nov 23, 2020
1 parent b8c4c00 commit 80e1661
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/integrations/v4/testhelp/testemit/testemit.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (t *RecordEmitter) Emit(metadata integration.Definition, extraLabels data.M
ch <- EmittedData{
DataSet: protocol.PluginDataSetV3{PluginDataSet: protocol.PluginDataSet{
Entity: ds.Entity,
// TODO but for now enough the assertion mechanism:
// TODO but for now it's enough for the assertion mechanism:
Metrics: make([]protocol.MetricData, len(ds.Metrics)),
}},
Metadata: metadata,
Expand Down
13 changes: 6 additions & 7 deletions internal/socketapi/socketapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,33 @@ package socketapi

import (
"context"
"fmt"
"net"
"runtime"
"strings"
"testing"
"time"

"github.com/newrelic/infrastructure-agent/internal/integrations/v4/integration"
"github.com/newrelic/infrastructure-agent/internal/integrations/v4/testhelp/testemit"
"github.com/newrelic/infrastructure-agent/internal/os/distro"
network_helpers "github.com/newrelic/infrastructure-agent/pkg/helpers/network"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPayloadFwServer_Serve(t *testing.T) {
if distro.IsCentos5() || runtime.GOOS == "windows" {
t.Skip("centos5 & windows CI not reliable")
}
port, err := network_helpers.TCPPort()
require.NoError(t, err)

e := &testemit.RecordEmitter{}
pf := NewServer(e, 17171)
pf := NewServer(e, port)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go pf.Serve(ctx)

payloadWritten := make(chan struct{})
go func() {
conn, err := net.Dial("tcp", "localhost:17171")
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", port))
require.NoError(t, err)
_, err = conn.Write([]byte(strings.Replace(`{
"protocol_version": "4",
Expand Down
15 changes: 15 additions & 0 deletions pkg/helpers/network/network_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,18 @@ func IPAddressesByType(addrs []net.InterfaceAddr) (ipv4, ipv6 string) {
}
return
}

// TCPPort returns a random free TCP port.
func TCPPort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
}

l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}

0 comments on commit 80e1661

Please sign in to comment.