diff --git a/receiver/opencensusreceiver/factory_test.go b/receiver/opencensusreceiver/factory_test.go index 975f4cc432a0..2a049ac80e04 100644 --- a/receiver/opencensusreceiver/factory_test.go +++ b/receiver/opencensusreceiver/factory_test.go @@ -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" @@ -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() } }) } @@ -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() } }) } diff --git a/receiver/opencensusreceiver/ocmetrics/opencensus_test.go b/receiver/opencensusreceiver/ocmetrics/opencensus_test.go index 699f8fc9c517..41b53976495e 100644 --- a/receiver/opencensusreceiver/ocmetrics/opencensus_test.go +++ b/receiver/opencensusreceiver/ocmetrics/opencensus_test.go @@ -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" @@ -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. @@ -69,15 +68,13 @@ 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{ @@ -85,16 +82,14 @@ func TestExportMultiplexing(t *testing.T) { 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{ @@ -102,22 +97,19 @@ func TestExportMultiplexing(t *testing.T) { 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) @@ -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) @@ -251,9 +240,7 @@ 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)} @@ -261,9 +248,8 @@ func TestExportProtocolConformation_metricsInFirstMessage(t *testing.T) { 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) @@ -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() { @@ -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() diff --git a/receiver/opencensusreceiver/octrace/observability_test.go b/receiver/opencensusreceiver/octrace/observability_test.go index 526b2590aa98..c9ab414caa06 100644 --- a/receiver/opencensusreceiver/octrace/observability_test.go +++ b/receiver/opencensusreceiver/octrace/observability_test.go @@ -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" @@ -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) { @@ -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) { @@ -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) @@ -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 { diff --git a/receiver/opencensusreceiver/octrace/opencensus_test.go b/receiver/opencensusreceiver/octrace/opencensus_test.go index 5a8864dd0cfe..6ad1d91a45dc 100644 --- a/receiver/opencensusreceiver/octrace/opencensus_test.go +++ b/receiver/opencensusreceiver/octrace/opencensus_test.go @@ -34,6 +34,7 @@ import ( 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/golang/protobuf/proto" + "github.com/stretchr/testify/require" "go.opencensus.io/trace" "go.opencensus.io/trace/tracestate" "google.golang.org/grpc" @@ -55,9 +56,7 @@ func TestReceiver_endToEnd(t *testing.T) { // Now the opencensus-agent exporter. address := fmt.Sprintf("localhost:%d", port) oce, err := ocagent.NewExporter(ocagent.WithAddress(address), ocagent.WithInsecure()) - if err != nil { - t.Fatalf("Failed to create the ocagent-exporter: %v", err) - } + require.NoError(t, err, "Failed to create the ocagent-exporter: %v", err) trace.RegisterExporter(oce) @@ -176,9 +175,7 @@ func TestExportMultiplexing(t *testing.T) { defer doneFn() traceClient, traceClientDoneFn, err := makeTraceServiceClient(port) - if err != nil { - t.Fatalf("Failed to create the gRPC TraceService_ExportClient: %v", err) - } + require.NoError(t, err, "Failed to create the gRPC TraceService_ExportClient: %v", err) defer traceClientDoneFn() // Step 1) The initiation. @@ -190,15 +187,13 @@ func TestExportMultiplexing(t *testing.T) { LibraryInfo: &commonpb.LibraryInfo{Language: commonpb.LibraryInfo_JAVA}, } - if err := traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: initiatingNode}); err != nil { - t.Fatalf("Failed to send the initiating message: %v", err) - } + err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: initiatingNode}) + require.NoError(t, err, "Failed to send the initiating message: %v", err) // Step 1a) Send some spans without a node, they should be registered as coming from the initiating node. sLi := []*tracepb.Span{{TraceId: []byte("1234567890abcde")}} - if err := traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: nil, Spans: sLi}); err != nil { - t.Fatalf("Failed to send the proxied message from app1: %v", err) - } + err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: nil, Spans: sLi}) + require.NoError(t, err, "Failed to send the proxied message from app1: %v", err) // Step 2) Send a "proxied" trace message from app1 with "node1" node1 := &commonpb.Node{ @@ -206,16 +201,14 @@ func TestExportMultiplexing(t *testing.T) { LibraryInfo: &commonpb.LibraryInfo{Language: commonpb.LibraryInfo_NODE_JS}, } sL1 := []*tracepb.Span{{TraceId: []byte("abcdefghijklmno")}} - if err := traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: node1, Spans: sL1}); err != nil { - t.Fatalf("Failed to send the proxied message from app1: %v", err) - } + err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: node1, Spans: sL1}) + require.NoError(t, err, "Failed to send the proxied message from app1: %v", err) // Step 3) Send a trace message without a node but with spans: this // should be registered as belonging to the last used node i.e. "node1". sLn1 := []*tracepb.Span{{TraceId: []byte("ABCDEFGHIJKLMNO")}, {TraceId: []byte("1234567890abcde")}} - if err := traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: nil, Spans: sLn1}); err != nil { - t.Fatalf("Failed to send the proxied message without a node: %v", err) - } + err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: nil, Spans: sLn1}) + require.NoError(t, err, "Failed to send the proxied message without a node: %v", err) // Step 4) Send a trace message from a differently proxied node "node2" from app2 node2 := &commonpb.Node{ @@ -223,22 +216,19 @@ func TestExportMultiplexing(t *testing.T) { LibraryInfo: &commonpb.LibraryInfo{Language: commonpb.LibraryInfo_GO_LANG}, } sL2 := []*tracepb.Span{{TraceId: []byte("_B_D_F_H_J_L_N_")}} - if err := traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: node2, Spans: sL2}); err != nil { - t.Fatalf("Failed to send the proxied message from app2: %v", err) - } + err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: node2, Spans: sL2}) + require.NoError(t, err, "Failed to send the proxied message from app2: %v", err) // Step 5a) Send a trace message without a node but with spans: this // should be registered as belonging to the last used node i.e. "node2". sLn2a := []*tracepb.Span{{TraceId: []byte("_BCDEFGHIJKLMN_")}, {TraceId: []byte("_234567890abcd_")}} - if err := traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: nil, Spans: sLn2a}); err != nil { - t.Fatalf("Failed to send the proxied message without a node: %v", err) - } + err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: nil, Spans: sLn2a}) + require.NoError(t, err, "Failed to send the proxied message without a node: %v", err) // Step 5b) sLn2b := []*tracepb.Span{{TraceId: []byte("_xxxxxxxxxxxxx_")}, {TraceId: []byte("B234567890abcdA")}} - if err := traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: nil, Spans: sLn2b}); err != nil { - t.Fatalf("Failed to send the proxied message without a node: %v", err) - } + err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: nil, Spans: sLn2b}) + 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) @@ -312,15 +302,12 @@ func TestExportProtocolViolations_nodelessFirstMessage(t *testing.T) { defer doneFn() traceClient, traceClientDoneFn, err := makeTraceServiceClient(port) - if err != nil { - t.Fatalf("Failed to create the gRPC TraceService_ExportClient: %v", err) - } + require.NoError(t, err, "Failed to create the gRPC TraceService_ExportClient: %v", err) defer traceClientDoneFn() // Send a Nodeless first message - if err := traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: nil}); err != nil { - t.Fatalf("Unexpectedly failed to send the first message: %v", err) - } + err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: nil}) + require.NoError(t, err, "Unexpectedly failed to send the first message: %v", err) longDuration := 2 * time.Second testDone := make(chan bool, 1) @@ -380,9 +367,7 @@ func TestExportProtocolConformation_spansInFirstMessage(t *testing.T) { defer doneFn() traceClient, traceClientDoneFn, err := makeTraceServiceClient(port) - if err != nil { - t.Fatalf("Failed to create the gRPC TraceService_ExportClient: %v", err) - } + require.NoError(t, err, "Failed to create the gRPC TraceService_ExportClient: %v", err) defer traceClientDoneFn() sLi := []*tracepb.Span{{TraceId: []byte("1234567890abcde")}, {TraceId: []byte("XXXXXXXXXXabcde")}} @@ -390,9 +375,8 @@ func TestExportProtocolConformation_spansInFirstMessage(t *testing.T) { Identifier: &commonpb.ProcessIdentifier{Pid: 1}, LibraryInfo: &commonpb.LibraryInfo{Language: commonpb.LibraryInfo_JAVA}, } - if err := traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: ni, Spans: sLi}); err != nil { - t.Fatalf("Failed to send the first message: %v", err) - } + err = traceClient.Send(&agenttracepb.ExportTraceServiceRequest{Node: ni, Spans: sLi}) + 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) @@ -476,9 +460,7 @@ func (sa *spanAppender) ConsumeTraceData(ctx context.Context, td consumerdata.Tr func ocReceiverOnGRPCServer(t *testing.T, sr consumer.TraceConsumer, opts ...Option) (oci *Receiver, port int, done func()) { ln, err := net.Listen("tcp", "localhost:0") - 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() { @@ -499,9 +481,7 @@ func ocReceiverOnGRPCServer(t *testing.T, sr consumer.TraceConsumer, opts ...Opt } 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() diff --git a/receiver/opencensusreceiver/opencensus_test.go b/receiver/opencensusreceiver/opencensus_test.go index fd3c09885148..26da232010d5 100644 --- a/receiver/opencensusreceiver/opencensus_test.go +++ b/receiver/opencensusreceiver/opencensus_test.go @@ -46,14 +46,11 @@ func TestGrpcGateway_endToEnd(t *testing.T) { // Set the buffer count to 1 to make it flush the test span immediately. sink := new(exportertest.SinkTraceExporter) ocr, err := New(addr, sink, nil) - if err != nil { - t.Fatalf("Failed to create trace receiver: %v", err) - } + require.NoError(t, err, "Failed to create trace receiver: %v", err) mh := receivertest.NewMockHost() - if err := ocr.StartTraceReception(mh); err != nil { - t.Fatalf("Failed to start trace receiver: %v", err) - } + err = ocr.StartTraceReception(mh) + require.NoError(t, err, "Failed to start trace receiver: %v", err) defer ocr.StopTraceReception() // TODO(songy23): make starting server deterministic @@ -85,16 +82,12 @@ func TestGrpcGateway_endToEnd(t *testing.T) { ] }`) req, err := http.NewRequest("POST", url, bytes.NewBuffer(traceJSON)) - if err != nil { - t.Fatalf("Error creating trace POST request: %v", err) - } + require.NoError(t, err, "Error creating trace POST request: %v", err) req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) - if err != nil { - t.Fatalf("Error posting trace to grpc-gateway server: %v", err) - } + require.NoError(t, err, "Error posting trace to grpc-gateway server: %v", err) respBytes, err := ioutil.ReadAll(resp.Body) if err != nil { @@ -157,15 +150,12 @@ func TestTraceGrpcGatewayCors_endToEnd(t *testing.T) { sink := new(exportertest.SinkTraceExporter) ocr, err := New(addr, sink, nil, WithCorsOrigins(corsOrigins)) - if err != nil { - t.Fatalf("Failed to create trace receiver: %v", err) - } + require.NoError(t, err, "Failed to create trace receiver: %v", err) defer ocr.StopTraceReception() mh := receivertest.NewMockHost() - if err := ocr.StartTraceReception(mh); err != nil { - t.Fatalf("Failed to start trace receiver: %v", err) - } + err = ocr.StartTraceReception(mh) + require.NoError(t, err, "Failed to start trace receiver: %v", err) // TODO(songy23): make starting server deterministic // Wait for the servers to start @@ -186,15 +176,12 @@ func TestMetricsGrpcGatewayCors_endToEnd(t *testing.T) { sink := new(exportertest.SinkMetricsExporter) ocr, err := New(addr, nil, sink, WithCorsOrigins(corsOrigins)) - if err != nil { - t.Fatalf("Failed to create metrics receiver: %v", err) - } + require.NoError(t, err, "Failed to create metrics receiver: %v", err) defer ocr.StopMetricsReception() mh := receivertest.NewMockHost() - if err := ocr.StartMetricsReception(mh); err != nil { - t.Fatalf("Failed to start metrics receiver: %v", err) - } + err = ocr.StartMetricsReception(mh) + require.NoError(t, err, "Failed to start metrics receiver: %v", err) // TODO(songy23): make starting server deterministic // Wait for the servers to start @@ -218,14 +205,11 @@ func TestAcceptAllGRPCProtoAffiliatedContentTypes(t *testing.T) { addr := testutils.GetAvailableLocalAddress(t) cbts := new(exportertest.SinkTraceExporter) ocr, err := New(addr, cbts, nil) - if err != nil { - t.Fatalf("Failed to create trace receiver: %v", err) - } + require.NoError(t, err, "Failed to create trace receiver: %v", err) mh := receivertest.NewMockHost() - if err := ocr.StartTraceReception(mh); err != nil { - t.Fatalf("Failed to start the trace receiver: %v", err) - } + err = ocr.StartTraceReception(mh) + require.NoError(t, err, "Failed to start the trace receiver: %v", err) defer ocr.StopTraceReception() // Now start the client with the various Proto affiliated gRPC Content-SubTypes as per: @@ -307,17 +291,13 @@ func runContentTypeTests(addr string, contentTypeDesignation bool, contentType s func verifyCorsResp(t *testing.T, url string, origin string, wantStatus int, wantAllowed bool) { req, err := http.NewRequest("OPTIONS", url, nil) - if err != nil { - t.Fatalf("Error creating trace OPTIONS request: %v", err) - } + require.NoError(t, err, "Error creating trace OPTIONS request: %v", err) req.Header.Set("Origin", origin) req.Header.Set("Access-Control-Request-Method", "POST") client := &http.Client{} resp, err := client.Do(req) - if err != nil { - t.Fatalf("Error sending OPTIONS to grpc-gateway server: %v", err) - } + require.NoError(t, err, "Error sending OPTIONS to grpc-gateway server: %v", err) err = resp.Body.Close() if err != nil { @@ -349,9 +329,7 @@ func verifyCorsResp(t *testing.T, url string, origin string, wantStatus int, wan func TestStopWithoutStartNeverCrashes(t *testing.T) { addr := testutils.GetAvailableLocalAddress(t) ocr, err := New(addr, nil, nil) - if err != nil { - t.Fatalf("Failed to create an OpenCensus receiver: %v", err) - } + require.NoError(t, err, "Failed to create an OpenCensus receiver: %v", err) // Stop it before ever invoking Start*. ocr.stop() } @@ -359,18 +337,12 @@ func TestStopWithoutStartNeverCrashes(t *testing.T) { func TestNewPortAlreadyUsed(t *testing.T) { addr := testutils.GetAvailableLocalAddress(t) ln, err := net.Listen("tcp", addr) - if err != nil { - t.Fatalf("failed to listen on %q: %v", addr, err) - } + require.NoError(t, err, "failed to listen on %q: %v", addr, err) defer ln.Close() r, err := New(addr, nil, nil) - if err == nil { - t.Fatalf("want err got nil") - } - if r != nil { - t.Fatalf("want nil got %v", r) - } + require.Error(t, err) + require.Nil(t, r) } func TestMultipleStopReceptionShouldNotError(t *testing.T) { diff --git a/receiver/opencensusreceiver/options_test.go b/receiver/opencensusreceiver/options_test.go index b55e455f8b71..517adcdd62a4 100644 --- a/receiver/opencensusreceiver/options_test.go +++ b/receiver/opencensusreceiver/options_test.go @@ -15,8 +15,9 @@ package opencensusreceiver import ( - "reflect" "testing" + + "github.com/stretchr/testify/require" ) func TestNoopOption(t *testing.T) { @@ -28,7 +29,5 @@ func TestNoopOption(t *testing.T) { opt.withReceiver(subjectReceiver) } - if !reflect.DeepEqual(plainReceiver, subjectReceiver) { - t.Fatalf("noopOption has side effects\nGot: %+v\nWant: %+v", subjectReceiver, plainReceiver) - } + require.Equal(t, plainReceiver, subjectReceiver, "noopOption has side effects") }