Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove testutil.HostPortFromAddr, no need to provide this helper #2919

Merged
merged 3 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## 🛑 Breaking changes 🛑

- Remove testutil.HostPortFromAddr, users can write their own parsing helper (#2919)

## 💡 Enhancements 💡

- Add `validatable` interface with `Validate()` to all `config.<component>` (#2898)
Expand Down
28 changes: 10 additions & 18 deletions receiver/opencensusreceiver/ocmetrics/opencensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ import (
func TestReceiver_endToEnd(t *testing.T) {
metricSink := new(consumertest.MetricsSink)

port, doneFn := ocReceiverOnGRPCServer(t, metricSink)
addr, doneFn := ocReceiverOnGRPCServer(t, metricSink)
defer doneFn()

address := fmt.Sprintf("localhost:%d", port)
expFactory := opencensusexporter.NewFactory()
expCfg := expFactory.CreateDefaultConfig().(*opencensusexporter.Config)
expCfg.GRPCClientSettings.TLSSetting.Insecure = true
expCfg.Endpoint = address
expCfg.Endpoint = addr.String()
expCfg.WaitForReady = true
oce, err := expFactory.CreateMetricsExporter(context.Background(), component.ExporterCreateParams{Logger: zap.NewNop()}, expCfg)
require.NoError(t, err)
Expand Down Expand Up @@ -88,10 +87,10 @@ func TestReceiver_endToEnd(t *testing.T) {
func TestExportMultiplexing(t *testing.T) {
metricSink := new(consumertest.MetricsSink)

port, doneFn := ocReceiverOnGRPCServer(t, metricSink)
addr, doneFn := ocReceiverOnGRPCServer(t, metricSink)
defer doneFn()

metricsClient, metricsClientDoneFn, err := makeMetricsServiceClient(port)
metricsClient, metricsClientDoneFn, err := makeMetricsServiceClient(addr)
require.NoError(t, err, "Failed to create the gRPC MetricsService_ExportClient: %v", err)
defer metricsClientDoneFn()

Expand Down Expand Up @@ -272,10 +271,10 @@ func TestExportProtocolConformation_metricsInFirstMessage(t *testing.T) {

metricSink := new(consumertest.MetricsSink)

port, doneFn := ocReceiverOnGRPCServer(t, metricSink)
addr, doneFn := ocReceiverOnGRPCServer(t, metricSink)
defer doneFn()

metricsClient, metricsClientDoneFn, err := makeMetricsServiceClient(port)
metricsClient, metricsClientDoneFn, err := makeMetricsServiceClient(addr)
require.NoError(t, err, "Failed to create the gRPC MetricsService_ExportClient: %v", err)
defer metricsClientDoneFn()

Expand Down Expand Up @@ -327,9 +326,8 @@ func TestExportProtocolConformation_metricsInFirstMessage(t *testing.T) {
}

// Helper functions from here on below
func makeMetricsServiceClient(port int) (agentmetricspb.MetricsService_ExportClient, func(), error) {
addr := fmt.Sprintf(":%d", port)
cc, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock())
func makeMetricsServiceClient(addr net.Addr) (agentmetricspb.MetricsService_ExportClient, func(), error) {
cc, err := grpc.Dial(addr.String(), grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return nil, nil, err
}
Expand All @@ -350,7 +348,7 @@ func nodeToKey(n *commonpb.Node) string {
return string(blob)
}

func ocReceiverOnGRPCServer(t *testing.T, sr consumer.Metrics) (int, func()) {
func ocReceiverOnGRPCServer(t *testing.T, sr consumer.Metrics) (net.Addr, func()) {
ln, err := net.Listen("tcp", "localhost:")
require.NoError(t, err, "Failed to find an available address to run the gRPC server: %v", err)

Expand All @@ -361,12 +359,6 @@ func ocReceiverOnGRPCServer(t *testing.T, sr consumer.Metrics) (int, func()) {
}
}

_, port, err := testutil.HostPortFromAddr(ln.Addr())
if err != nil {
done()
t.Fatalf("Failed to parse host:port from listener address: %s error: %v", ln.Addr(), err)
}

oci, err := New(receiverTagValue, sr)
require.NoError(t, err, "Failed to create the Receiver: %v", err)

Expand All @@ -377,7 +369,7 @@ func ocReceiverOnGRPCServer(t *testing.T, sr consumer.Metrics) (int, func()) {
_ = srv.Serve(ln)
}()

return port, done
return ln.Addr(), done
}

func makeMetric(val int) *metricspb.Metric {
Expand Down
4 changes: 2 additions & 2 deletions receiver/opencensusreceiver/octrace/observability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func TestEnsureRecordedMetrics(t *testing.T) {
require.NoError(t, err)
defer doneFn()

port, doneReceiverFn := ocReceiverOnGRPCServer(t, consumertest.NewNop())
addr, doneReceiverFn := ocReceiverOnGRPCServer(t, consumertest.NewNop())
defer doneReceiverFn()

n := 20
// Now for the traceExporter that sends 0 length spans
traceSvcClient, traceSvcDoneFn, err := makeTraceServiceClient(port)
traceSvcClient, traceSvcDoneFn, err := makeTraceServiceClient(addr)
require.NoError(t, err, "Failed to create the trace service client: %v", err)
spans := []*tracepb.Span{{TraceId: []byte("abcdefghijklmnop"), SpanId: []byte("12345678")}}
for i := 0; i < n; i++ {
Expand Down
21 changes: 6 additions & 15 deletions receiver/opencensusreceiver/octrace/opencensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"strings"
Expand Down Expand Up @@ -48,14 +47,13 @@ import (
func TestReceiver_endToEnd(t *testing.T) {
spanSink := new(consumertest.TracesSink)

port, doneFn := ocReceiverOnGRPCServer(t, spanSink)
addr, doneFn := ocReceiverOnGRPCServer(t, spanSink)
defer doneFn()

address := fmt.Sprintf("localhost:%d", port)
expFactory := opencensusexporter.NewFactory()
expCfg := expFactory.CreateDefaultConfig().(*opencensusexporter.Config)
expCfg.GRPCClientSettings.TLSSetting.Insecure = true
expCfg.Endpoint = address
expCfg.Endpoint = addr.String()
expCfg.WaitForReady = true
oce, err := expFactory.CreateTracesExporter(context.Background(), component.ExporterCreateParams{Logger: zap.NewNop()}, expCfg)
require.NoError(t, err)
Expand Down Expand Up @@ -343,9 +341,8 @@ func TestExportProtocolConformation_spansInFirstMessage(t *testing.T) {
}

// Helper functions from here on below
func makeTraceServiceClient(port int) (agenttracepb.TraceService_ExportClient, func(), error) {
addr := fmt.Sprintf(":%d", port)
cc, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock())
func makeTraceServiceClient(addr net.Addr) (agenttracepb.TraceService_ExportClient, func(), error) {
cc, err := grpc.Dial(addr.String(), grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return nil, nil, err
}
Expand All @@ -366,7 +363,7 @@ func nodeToKey(n *commonpb.Node) string {
return string(blob)
}

func ocReceiverOnGRPCServer(t *testing.T, sr consumer.Traces) (int, func()) {
func ocReceiverOnGRPCServer(t *testing.T, sr consumer.Traces) (net.Addr, func()) {
ln, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err, "Failed to find an available address to run the gRPC server: %v", err)

Expand All @@ -377,12 +374,6 @@ func ocReceiverOnGRPCServer(t *testing.T, sr consumer.Traces) (int, func()) {
}
}

_, port, err := testutil.HostPortFromAddr(ln.Addr())
if err != nil {
done()
t.Fatalf("Failed to parse host:port from listener address: %s error: %v", ln.Addr(), err)
}

oci, err := New(receiverTagValue, sr)
require.NoError(t, err, "Failed to create the Receiver: %v", err)

Expand All @@ -393,5 +384,5 @@ func ocReceiverOnGRPCServer(t *testing.T, sr consumer.Traces) (int, func()) {
_ = srv.Serve(ln)
}()

return port, done
return ln.Addr(), done
}
29 changes: 10 additions & 19 deletions receiver/otlpreceiver/logs/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package logs
import (
"context"
"errors"
"fmt"
"net"
"testing"

Expand All @@ -33,7 +32,6 @@ import (
collectorlog "go.opentelemetry.io/collector/internal/data/protogen/collector/logs/v1"
otlplog "go.opentelemetry.io/collector/internal/data/protogen/logs/v1"
"go.opentelemetry.io/collector/obsreport"
"go.opentelemetry.io/collector/testutil"
)

var _ collectorlog.LogsServiceServer = (*Receiver)(nil)
Expand All @@ -43,10 +41,10 @@ func TestExport(t *testing.T) {

logSink := new(consumertest.LogsSink)

port, doneFn := otlpReceiverOnGRPCServer(t, logSink)
addr, doneFn := otlpReceiverOnGRPCServer(t, logSink)
defer doneFn()

traceClient, traceClientDoneFn, err := makeLogsServiceClient(port)
traceClient, traceClientDoneFn, err := makeLogsServiceClient(addr)
require.NoError(t, err, "Failed to create the TraceServiceClient: %v", err)
defer traceClientDoneFn()

Expand Down Expand Up @@ -92,10 +90,10 @@ func TestExport(t *testing.T) {
func TestExport_EmptyRequest(t *testing.T) {
logSink := new(consumertest.LogsSink)

port, doneFn := otlpReceiverOnGRPCServer(t, logSink)
addr, doneFn := otlpReceiverOnGRPCServer(t, logSink)
defer doneFn()

logClient, logClientDoneFn, err := makeLogsServiceClient(port)
logClient, logClientDoneFn, err := makeLogsServiceClient(addr)
require.NoError(t, err, "Failed to create the TraceServiceClient: %v", err)
defer logClientDoneFn()

Expand All @@ -105,10 +103,10 @@ func TestExport_EmptyRequest(t *testing.T) {
}

func TestExport_ErrorConsumer(t *testing.T) {
port, doneFn := otlpReceiverOnGRPCServer(t, consumertest.NewErr(errors.New("my error")))
addr, doneFn := otlpReceiverOnGRPCServer(t, consumertest.NewErr(errors.New("my error")))
defer doneFn()

logClient, logClientDoneFn, err := makeLogsServiceClient(port)
logClient, logClientDoneFn, err := makeLogsServiceClient(addr)
require.NoError(t, err, "Failed to create the TraceServiceClient: %v", err)
defer logClientDoneFn()

Expand All @@ -133,9 +131,8 @@ func TestExport_ErrorConsumer(t *testing.T) {
assert.Nil(t, resp)
}

func makeLogsServiceClient(port int) (collectorlog.LogsServiceClient, func(), error) {
addr := fmt.Sprintf(":%d", port)
cc, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock())
func makeLogsServiceClient(addr net.Addr) (collectorlog.LogsServiceClient, func(), error) {
cc, err := grpc.Dial(addr.String(), grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return nil, nil, err
}
Expand All @@ -146,7 +143,7 @@ func makeLogsServiceClient(port int) (collectorlog.LogsServiceClient, func(), er
return logClient, doneFn, nil
}

func otlpReceiverOnGRPCServer(t *testing.T, tc consumer.Logs) (int, func()) {
func otlpReceiverOnGRPCServer(t *testing.T, tc consumer.Logs) (net.Addr, func()) {
ln, err := net.Listen("tcp", "localhost:")
require.NoError(t, err, "Failed to find an available address to run the gRPC server: %v", err)

Expand All @@ -157,12 +154,6 @@ func otlpReceiverOnGRPCServer(t *testing.T, tc consumer.Logs) (int, func()) {
}
}

_, port, err := testutil.HostPortFromAddr(ln.Addr())
if err != nil {
done()
t.Fatalf("Failed to parse host:port from listener address: %s error: %v", ln.Addr(), err)
}

r := New(receiverTagValue, tc)
require.NoError(t, err)

Expand All @@ -173,5 +164,5 @@ func otlpReceiverOnGRPCServer(t *testing.T, tc consumer.Logs) (int, func()) {
_ = srv.Serve(ln)
}()

return port, done
return ln.Addr(), done
}
22 changes: 8 additions & 14 deletions receiver/otlpreceiver/metrics/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package metrics
import (
"context"
"errors"
"fmt"
"net"
"testing"

Expand All @@ -33,7 +32,6 @@ import (
otlpcommon "go.opentelemetry.io/collector/internal/data/protogen/common/v1"
otlpmetrics "go.opentelemetry.io/collector/internal/data/protogen/metrics/v1"
"go.opentelemetry.io/collector/obsreport"
"go.opentelemetry.io/collector/testutil"
)

var _ collectormetrics.MetricsServiceServer = (*Receiver)(nil)
Expand Down Expand Up @@ -124,10 +122,10 @@ func TestExport_EmptyRequest(t *testing.T) {

metricSink := new(consumertest.MetricsSink)

port, doneFn := otlpReceiverOnGRPCServer(t, metricSink)
addr, doneFn := otlpReceiverOnGRPCServer(t, metricSink)
defer doneFn()

metricsClient, metricsClientDoneFn, err := makeMetricsServiceClient(port)
metricsClient, metricsClientDoneFn, err := makeMetricsServiceClient(addr)
require.NoError(t, err, "Failed to create the MetricsServiceClient: %v", err)
defer metricsClientDoneFn()

Expand All @@ -139,10 +137,10 @@ func TestExport_EmptyRequest(t *testing.T) {
func TestExport_ErrorConsumer(t *testing.T) {
// given

port, doneFn := otlpReceiverOnGRPCServer(t, consumertest.NewErr(errors.New("my error")))
addr, doneFn := otlpReceiverOnGRPCServer(t, consumertest.NewErr(errors.New("my error")))
defer doneFn()

metricsClient, metricsClientDoneFn, err := makeMetricsServiceClient(port)
metricsClient, metricsClientDoneFn, err := makeMetricsServiceClient(addr)
require.NoError(t, err, "Failed to create the MetricsServiceClient: %v", err)
defer metricsClientDoneFn()

Expand Down Expand Up @@ -180,9 +178,8 @@ func TestExport_ErrorConsumer(t *testing.T) {
assert.Nil(t, resp)
}

func makeMetricsServiceClient(port int) (collectormetrics.MetricsServiceClient, func(), error) {
addr := fmt.Sprintf(":%d", port)
cc, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock())
func makeMetricsServiceClient(addr net.Addr) (collectormetrics.MetricsServiceClient, func(), error) {
cc, err := grpc.Dial(addr.String(), grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return nil, nil, err
}
Expand All @@ -193,7 +190,7 @@ func makeMetricsServiceClient(port int) (collectormetrics.MetricsServiceClient,
return metricsClient, doneFn, nil
}

func otlpReceiverOnGRPCServer(t *testing.T, mc consumer.Metrics) (int, func()) {
func otlpReceiverOnGRPCServer(t *testing.T, mc consumer.Metrics) (net.Addr, func()) {
ln, err := net.Listen("tcp", "localhost:")
require.NoError(t, err, "Failed to find an available address to run the gRPC server: %v", err)

Expand All @@ -204,9 +201,6 @@ func otlpReceiverOnGRPCServer(t *testing.T, mc consumer.Metrics) (int, func()) {
}
}

_, port, err := testutil.HostPortFromAddr(ln.Addr())
require.NoError(t, err)

r := New(receiverTagValue, mc)
// Now run it as a gRPC server
srv := obsreport.GRPCServerWithObservabilityEnabled()
Expand All @@ -215,5 +209,5 @@ func otlpReceiverOnGRPCServer(t *testing.T, mc consumer.Metrics) (int, func()) {
_ = srv.Serve(ln)
}()

return port, done
return ln.Addr(), done
}
Loading