Skip to content

Commit

Permalink
Use "github.com/stretchr/testify/require" in receiver/opencensusrecei…
Browse files Browse the repository at this point in the history
…ver (#404)

* migrate code base to use "github.com/stretchr/testify/require" in receiver/opencensusreceiver

* fix go vet warning

* use require.Equal
  • Loading branch information
abelharisov authored and Paulo Janotti committed Oct 23, 2019
1 parent aea4392 commit 4133d64
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 182 deletions.
17 changes: 7 additions & 10 deletions receiver/opencensusreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector/config/configcheck"
Expand Down Expand Up @@ -111,11 +112,9 @@ func TestCreateTraceReceiver(t *testing.T) {
}
if tr != nil {
mh := receivertest.NewMockHost()
if err := tr.StartTraceReception(mh); err == nil {
tr.StopTraceReception()
} else {
t.Fatalf("StartTraceReception() error = %v", err)
}
err := tr.StartTraceReception(mh)
require.NoError(t, err, "StartTraceReception() error = %v", err)
tr.StopTraceReception()
}
})
}
Expand Down Expand Up @@ -184,11 +183,9 @@ func TestCreateMetricReceiver(t *testing.T) {
}
if tc != nil {
mh := receivertest.NewMockHost()
if err := tc.StartMetricsReception(mh); err == nil {
tc.StopMetricsReception()
} else {
t.Fatalf("StartTraceReception() error = %v", err)
}
err := tc.StartMetricsReception(mh)
require.NoError(t, err, "StartTraceReception() error = %v", err)
tc.StopMetricsReception()
}
})
}
Expand Down
66 changes: 24 additions & 42 deletions receiver/opencensusreceiver/ocmetrics/opencensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
agentmetricspb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/metrics/v1"
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"

"github.com/open-telemetry/opentelemetry-collector/consumer"
Expand All @@ -55,9 +56,7 @@ func TestExportMultiplexing(t *testing.T) {
defer doneFn()

metricsClient, metricsClientDoneFn, err := makeMetricsServiceClient(port)
if err != nil {
t.Fatalf("Failed to create the gRPC MetricsService_ExportClient: %v", err)
}
require.NoError(t, err, "Failed to create the gRPC MetricsService_ExportClient: %v", err)
defer metricsClientDoneFn()

// Step 1) The initiation.
Expand All @@ -69,55 +68,48 @@ func TestExportMultiplexing(t *testing.T) {
LibraryInfo: &commonpb.LibraryInfo{Language: commonpb.LibraryInfo_JAVA},
}

if err := metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: initiatingNode}); err != nil {
t.Fatalf("Failed to send the initiating message: %v", err)
}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: initiatingNode})
require.NoError(t, err, "Failed to send the initiating message: %v", err)

// Step 1a) Send some metrics without a node, they should be registered as coming from the initiating node.
mLi := []*metricspb.Metric{makeMetric(1)}
if err := metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: nil, Metrics: mLi}); err != nil {
t.Fatalf("Failed to send the proxied message from app1: %v", err)
}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: nil, Metrics: mLi})
require.NoError(t, err, "Failed to send the proxied message from app1: %v", err)

// Step 2) Send a "proxied" metrics message from app1 with "node1"
node1 := &commonpb.Node{
Identifier: &commonpb.ProcessIdentifier{Pid: 9489, HostName: "nodejs-host"},
LibraryInfo: &commonpb.LibraryInfo{Language: commonpb.LibraryInfo_NODE_JS},
}
mL1 := []*metricspb.Metric{makeMetric(2)}
if err := metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: node1, Metrics: mL1}); err != nil {
t.Fatalf("Failed to send the proxied message from app1: %v", err)
}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: node1, Metrics: mL1})
require.NoError(t, err, "Failed to send the proxied message from app1: %v", err)

// Step 3) Send a metrics message without a node but with metrics: this
// should be registered as belonging to the last used node i.e. "node1".
mLn1 := []*metricspb.Metric{makeMetric(3)}
if err := metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: nil, Metrics: mLn1}); err != nil {
t.Fatalf("Failed to send the proxied message without a node: %v", err)
}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: nil, Metrics: mLn1})
require.NoError(t, err, "Failed to send the proxied message without a node: %v", err)

// Step 4) Send a metrics message from a differently proxied node "node2" from app2
node2 := &commonpb.Node{
Identifier: &commonpb.ProcessIdentifier{Pid: 7752, HostName: "golang-host"},
LibraryInfo: &commonpb.LibraryInfo{Language: commonpb.LibraryInfo_GO_LANG},
}
mL2 := []*metricspb.Metric{makeMetric(4)}
if err := metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: node2, Metrics: mL2}); err != nil {
t.Fatalf("Failed to send the proxied message from app2: %v", err)
}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: node2, Metrics: mL2})
require.NoError(t, err, "Failed to send the proxied message from app2: %v", err)

// Step 5a) Send a metrics message without a node but with metrics: this
// should be registered as belonging to the last used node i.e. "node2".
mLn2a := []*metricspb.Metric{makeMetric(5)}
if err := metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: nil, Metrics: mLn2a}); err != nil {
t.Fatalf("Failed to send the proxied message without a node: %v", err)
}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: nil, Metrics: mLn2a})
require.NoError(t, err, "Failed to send the proxied message without a node: %v", err)

// Step 5b)
mLn2b := []*metricspb.Metric{makeMetric(6)}
if err := metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: nil, Metrics: mLn2b}); err != nil {
t.Fatalf("Failed to send the proxied message without a node: %v", err)
}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: nil, Metrics: mLn2b})
require.NoError(t, err, "Failed to send the proxied message without a node: %v", err)
// Give the process sometime to send data over the wire and perform batching
<-time.After(150 * time.Millisecond)

Expand Down Expand Up @@ -174,15 +166,12 @@ func TestExportProtocolViolations_nodelessFirstMessage(t *testing.T) {
defer doneFn()

metricsClient, metricsClientDoneFn, err := makeMetricsServiceClient(port)
if err != nil {
t.Fatalf("Failed to create the gRPC MetricsService_ExportClient: %v", err)
}
require.NoError(t, err, "Failed to create the gRPC MetricsService_ExportClient: %v", err)
defer metricsClientDoneFn()

// Send a Nodeless first message
if err := metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: nil}); err != nil {
t.Fatalf("Unexpectedly failed to send the first message: %v", err)
}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: nil})
require.NoError(t, err, "Unexpectedly failed to send the first message: %v", err)

longDuration := 2 * time.Second
testDone := make(chan bool, 1)
Expand Down Expand Up @@ -251,19 +240,16 @@ func TestExportProtocolConformation_metricsInFirstMessage(t *testing.T) {
defer doneFn()

metricsClient, metricsClientDoneFn, err := makeMetricsServiceClient(port)
if err != nil {
t.Fatalf("Failed to create the gRPC MetricsService_ExportClient: %v", err)
}
require.NoError(t, err, "Failed to create the gRPC MetricsService_ExportClient: %v", err)
defer metricsClientDoneFn()

mLi := []*metricspb.Metric{makeMetric(10), makeMetric(11)}
ni := &commonpb.Node{
Identifier: &commonpb.ProcessIdentifier{Pid: 1},
LibraryInfo: &commonpb.LibraryInfo{Language: commonpb.LibraryInfo_JAVA},
}
if err := metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: ni, Metrics: mLi}); err != nil {
t.Fatalf("Failed to send the first message: %v", err)
}
err = metricsClient.Send(&agentmetricspb.ExportMetricsServiceRequest{Node: ni, Metrics: mLi})
require.NoError(t, err, "Failed to send the first message: %v", err)

// Give it time to be sent over the wire, then exported.
<-time.After(100 * time.Millisecond)
Expand Down Expand Up @@ -348,9 +334,7 @@ func (sa *metricAppender) ConsumeMetricsData(ctx context.Context, md consumerdat

func ocReceiverOnGRPCServer(t *testing.T, sr consumer.MetricsConsumer, opts ...Option) (oci *Receiver, port int, done func()) {
ln, err := net.Listen("tcp", "localhost:")
if err != nil {
t.Fatalf("Failed to find an available address to run the gRPC server: %v", err)
}
require.NoError(t, err, "Failed to find an available address to run the gRPC server: %v", err)

doneFnList := []func(){func() { ln.Close() }}
done = func() {
Expand All @@ -371,9 +355,7 @@ func ocReceiverOnGRPCServer(t *testing.T, sr consumer.MetricsConsumer, opts ...O
}

oci, err = New(sr, opts...)
if err != nil {
t.Fatalf("Failed to create the Receiver: %v", err)
}
require.NoError(t, err, "Failed to create the Receiver: %v", err)

// Now run it as a gRPC server
srv := observability.GRPCServerWithObservabilityEnabled()
Expand Down
57 changes: 24 additions & 33 deletions receiver/opencensusreceiver/octrace/observability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
agenttracepb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/trace/v1"
tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
"github.com/stretchr/testify/require"
"go.opencensus.io/trace"

"github.com/open-telemetry/opentelemetry-collector/exporter/exportertest"
Expand All @@ -48,23 +49,18 @@ func TestEnsureRecordedMetrics(t *testing.T) {
n := 20
// Now for the traceExporter that sends 0 length spans
traceSvcClient, traceSvcDoneFn, err := makeTraceServiceClient(port)
if err != nil {
t.Fatalf("Failed to create the trace service client: %v", err)
}
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++ {
if err := traceSvcClient.Send(&agenttracepb.ExportTraceServiceRequest{Spans: spans, Node: &commonpb.Node{}}); err != nil {
t.Fatalf("Failed to send requests to the service: %v", err)
}
err = traceSvcClient.Send(&agenttracepb.ExportTraceServiceRequest{Spans: spans, Node: &commonpb.Node{}})
require.NoError(t, err, "Failed to send requests to the service: %v", err)
}
flush(traceSvcDoneFn)

if err := observabilitytest.CheckValueViewReceiverReceivedSpans("oc_trace", n); err != nil {
t.Fatalf("When check recorded values: want nil got %v", err)
}
if err := observabilitytest.CheckValueViewReceiverDroppedSpans("oc_trace", 0); err != nil {
t.Fatalf("When check recorded values: want nil got %v", err)
}
err = observabilitytest.CheckValueViewReceiverReceivedSpans("oc_trace", n)
require.NoError(t, err, "When check recorded values: want nil got %v", err)
err = observabilitytest.CheckValueViewReceiverDroppedSpans("oc_trace", 0)
require.NoError(t, err, "When check recorded values: want nil got %v", err)
}

func TestEnsureRecordedMetrics_zeroLengthSpansSender(t *testing.T) {
Expand All @@ -77,22 +73,17 @@ func TestEnsureRecordedMetrics_zeroLengthSpansSender(t *testing.T) {
n := 20
// Now for the traceExporter that sends 0 length spans
traceSvcClient, traceSvcDoneFn, err := makeTraceServiceClient(port)
if err != nil {
t.Fatalf("Failed to create the trace service client: %v", err)
}
require.NoError(t, err, "Failed to create the trace service client: %v", err)
for i := 0; i <= n; i++ {
if err := traceSvcClient.Send(&agenttracepb.ExportTraceServiceRequest{Spans: nil, Node: &commonpb.Node{}}); err != nil {
t.Fatalf("Failed to send requests to the service: %v", err)
}
err = traceSvcClient.Send(&agenttracepb.ExportTraceServiceRequest{Spans: nil, Node: &commonpb.Node{}})
require.NoError(t, err, "Failed to send requests to the service: %v", err)
}
flush(traceSvcDoneFn)

if err := observabilitytest.CheckValueViewReceiverReceivedSpans("oc_trace", 0); err != nil {
t.Fatalf("When check recorded values: want nil got %v", err)
}
if err := observabilitytest.CheckValueViewReceiverDroppedSpans("oc_trace", 0); err != nil {
t.Fatalf("When check recorded values: want nil got %v", err)
}
err = observabilitytest.CheckValueViewReceiverReceivedSpans("oc_trace", 0)
require.NoError(t, err, "When check recorded values: want nil got %v", err)
err = observabilitytest.CheckValueViewReceiverDroppedSpans("oc_trace", 0)
require.NoError(t, err, "When check recorded values: want nil got %v", err)
}

func TestExportSpanLinkingMaintainsParentLink(t *testing.T) {
Expand All @@ -113,16 +104,13 @@ func TestExportSpanLinkingMaintainsParentLink(t *testing.T) {
defer doneFn()

traceSvcClient, traceSvcDoneFn, err := makeTraceServiceClient(port)
if err != nil {
t.Fatalf("Failed to create the trace service client: %v", err)
}
require.NoError(t, err, "Failed to create the trace service client: %v", err)

n := 5
for i := 0; i < n; i++ {
sl := []*tracepb.Span{{TraceId: []byte("abcdefghijklmnop"), SpanId: []byte{byte(i + 1), 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}}}
if err := traceSvcClient.Send(&agenttracepb.ExportTraceServiceRequest{Spans: sl, Node: &commonpb.Node{}}); err != nil {
t.Fatalf("Failed to send requests to the service: %v", err)
}
err = traceSvcClient.Send(&agenttracepb.ExportTraceServiceRequest{Spans: sl, Node: &commonpb.Node{}})
require.NoError(t, err, "Failed to send requests to the service: %v", err)
}

flush(traceSvcDoneFn)
Expand All @@ -131,9 +119,12 @@ func TestExportSpanLinkingMaintainsParentLink(t *testing.T) {
ocSpansSaver.mu.Lock()
defer ocSpansSaver.mu.Unlock()

if len(ocSpansSaver.spanData) == 0 {
t.Fatal("Unfortunately did not receive an exported span data. Please check this library's implementation or go.opencensus.io/trace")
}
require.NotEqual(
t,
len(ocSpansSaver.spanData),
0,
"Unfortunately did not receive an exported span data. Please check this library's implementation or go.opencensus.io/trace",
)

gotSpanData := ocSpansSaver.spanData[:]
if g, w := len(gotSpanData), n+1; g != w {
Expand Down
Loading

0 comments on commit 4133d64

Please sign in to comment.