Skip to content

Commit

Permalink
Remove different network options (#5549)
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme authored Oct 26, 2024
1 parent bdf58fb commit 17e7349
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 38 deletions.
2 changes: 1 addition & 1 deletion tests/installation/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func getPackagePath(t testing.TB, suffix string) string {
}

func TestDefaultConfigDDiscoversPostgres(t *testing.T) {
tc := testutils.NewTestcase(t, testutils.OTLPReceiverSinkBindToBridgeGateway)
tc := testutils.NewTestcase(t)
defer tc.PrintLogsOnFailure()
defer tc.ShutdownOTLPReceiverSink()

Expand Down
40 changes: 3 additions & 37 deletions tests/testutils/testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"testing"
"time"

"github.com/docker/docker/api/types/network"
docker "github.com/docker/docker/client"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -36,24 +34,8 @@ import (
"github.com/signalfx/splunk-otel-collector/tests/testutils/telemetry"
)

type TestOption int

const (
OTLPReceiverSinkAllInterfaces TestOption = iota
OTLPReceiverSinkBindToBridgeGateway
)

type CollectorBuilder func(Collector) Collector

func HasTestOption(opt TestOption, opts []TestOption) bool {
for _, o := range opts {
if o == opt {
return true
}
}
return false
}

// A Testcase is a central helper utility to provide Container, OTLPReceiverSink, ResourceMetrics,
// SplunkOtelCollector, and ObservedLogs to integration tests with minimal boilerplate. It also embeds testing.TB
// for easy testing and testify usage.
Expand All @@ -70,13 +52,13 @@ type Testcase struct {

// NewTestcase is the recommended constructor that will automatically configure an OTLPReceiverSink
// with available endpoint and ObservedLogs.
func NewTestcase(t testing.TB, opts ...TestOption) *Testcase {
func NewTestcase(t testing.TB) *Testcase {
tc := Testcase{TB: t}
var logCore zapcore.Core
logCore, tc.ObservedLogs = observer.New(zap.DebugLevel)
tc.Logger = zap.New(logCore)

tc.setOTLPEndpoint(opts)
tc.setOTLPEndpoint()
var err error
tc.OTLPReceiverSink, err = NewOTLPReceiverSink().WithEndpoint(tc.OTLPEndpoint).Build()
require.NoError(tc, err)
Expand All @@ -88,25 +70,9 @@ func NewTestcase(t testing.TB, opts ...TestOption) *Testcase {
return &tc
}

func (t *Testcase) setOTLPEndpoint(opts []TestOption) {
func (t *Testcase) setOTLPEndpoint() {
otlpPort := GetAvailablePort(t)
otlpHost := "localhost"
switch {
case HasTestOption(OTLPReceiverSinkAllInterfaces, opts):
otlpHost = "0.0.0.0"
case HasTestOption(OTLPReceiverSinkBindToBridgeGateway, opts):
client, err := docker.NewClientWithOpts(docker.FromEnv)
require.NoError(t, err)
client.NegotiateAPIVersion(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
network, err := client.NetworkInspect(ctx, "bridge", network.InspectOptions{})
require.NoError(t, err)
for _, ipam := range network.IPAM.Config {
otlpHost = ipam.Gateway
}
require.NotEmpty(t, otlpHost, "no bridge network gateway detected. Host IP is inaccessible.")
}
t.OTLPEndpoint = fmt.Sprintf("%s:%d", otlpHost, otlpPort)
t.OTLPEndpointForCollector = t.OTLPEndpoint
}
Expand Down

0 comments on commit 17e7349

Please sign in to comment.