Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit f6ea5dc

Browse files
authored
Use cmp instead of reflect and json in tests. (#122)
* Use cmp instead of reflect and json in tests. * Add test ID to error message.
1 parent e393203 commit f6ea5dc

4 files changed

Lines changed: 113 additions & 99 deletions

File tree

equivalence_test.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ package stackdriver
1616

1717
import (
1818
"context"
19-
"encoding/json"
2019
"errors"
2120
"net"
22-
"reflect"
2321
"sync"
2422
"testing"
2523
"time"
@@ -103,17 +101,16 @@ func TestStatsAndMetricsEquivalence(t *testing.T) {
103101
if err != nil {
104102
t.Errorf("#%d: Stats.protoMetricDescriptorToMetricDescriptor: %v", i, err)
105103
}
106-
if !reflect.DeepEqual(sMD, pMD) {
107-
t.Errorf("MetricDescriptor Mismatch\nStats MetricDescriptor:\n\t%v\nProto MetricDescriptor:\n\t%v\n", sMD, pMD)
104+
if diff := cmpMDReq(pMD, sMD); diff != "" {
105+
t.Fatalf("MetricDescriptor Mismatch -FromMetrics +FromStats: %s", diff)
108106
}
109107

110108
vdl := []*view.Data{vd}
111109
sctreql := se.makeReq(vdl, maxTimeSeriesPerUpload)
112110
tsl, _ := se.protoMetricToTimeSeries(ctx, last.Node, last.Resource, last.Metrics[0])
113111
pctreql := se.combineTimeSeriesToCreateTimeSeriesRequest(tsl)
114-
if !reflect.DeepEqual(sctreql, pctreql) {
115-
t.Errorf("#%d: TimeSeries Mismatch\nStats CreateTimeSeriesRequest:\n\t%v\nProto CreateTimeSeriesRequest:\n\t%v\n",
116-
i, sctreql, pctreql)
112+
if diff := cmpTSReqs(pctreql, sctreql); diff != "" {
113+
t.Fatalf("TimeSeries Mismatch -FromMetrics +FromStats: %s", diff)
117114
}
118115
}
119116
}
@@ -277,18 +274,13 @@ func TestEquivalenceStatsVsMetricsUploads(t *testing.T) {
277274
})
278275

279276
// The results should be equal now
280-
if !reflect.DeepEqual(stackdriverTimeSeriesFromMetrics, stackdriverTimeSeriesFromStats) {
281-
blobFromMetrics := jsonBlob(stackdriverTimeSeriesFromMetrics)
282-
blobFromStats := jsonBlob(stackdriverTimeSeriesFromStats)
283-
t.Errorf("StackdriverTimeSeriesFromMetrics (%d):\n%s\n\nStackdriverTimeSeriesFromStats (%d):\n%s\n\n",
284-
len(stackdriverTimeSeriesFromMetrics), blobFromMetrics,
285-
len(stackdriverTimeSeriesFromStats), blobFromStats)
277+
if diff := cmpTSReqs(stackdriverTimeSeriesFromMetrics, stackdriverTimeSeriesFromStats); diff != "" {
278+
t.Fatalf("Unexpected CreateTimeSeriesRequests -FromMetrics +FromStats: %s", diff)
286279
}
287280

288281
// Examining the metric descriptors too.
289-
if !reflect.DeepEqual(stackdriverMetricDescriptorsFromMetrics, stackdriverMetricDescriptorsFromStats) {
290-
t.Errorf("StackdriverMetricDescriptorsFromMetrics:\n%v\nStackdriverMetricDescriptors:\n%v\n\n",
291-
stackdriverMetricDescriptorsFromMetrics, stackdriverMetricDescriptorsFromStats)
282+
if diff := cmpMDReqs(stackdriverMetricDescriptorsFromMetrics, stackdriverMetricDescriptorsFromStats); diff != "" {
283+
t.Fatalf("Unexpected CreateMetricDescriptorRequests -FromMetrics +FromStats: %s", diff)
292284
}
293285
}
294286

@@ -425,8 +417,3 @@ func (ma *metricsAgent) GetMonitoredResourceDescriptor(ctx context.Context, req
425417
func (ma *metricsAgent) ListMonitoredResourceDescriptors(ctx context.Context, req *monitoringpb.ListMonitoredResourceDescriptorsRequest) (*monitoringpb.ListMonitoredResourceDescriptorsResponse, error) {
426418
return new(monitoringpb.ListMonitoredResourceDescriptorsResponse), nil
427419
}
428-
429-
func jsonBlob(v interface{}) []byte {
430-
blob, _ := json.MarshalIndent(v, "", " ")
431-
return blob
432-
}

metrics_proto_test.go

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ package stackdriver
1616

1717
import (
1818
"context"
19-
"encoding/json"
20-
"reflect"
2119
"strings"
2220
"testing"
2321

2422
monitoring "cloud.google.com/go/monitoring/apiv3"
2523
"github.com/golang/protobuf/ptypes/timestamp"
2624
distributionpb "google.golang.org/genproto/googleapis/api/distribution"
25+
labelpb "google.golang.org/genproto/googleapis/api/label"
2726
googlemetricpb "google.golang.org/genproto/googleapis/api/metric"
2827
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
2928
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
@@ -71,11 +70,8 @@ func TestProtoResourceToMonitoringResource(t *testing.T) {
7170

7271
for i, tt := range tests {
7372
got := protoResourceToMonitoredResource(tt.in)
74-
if !reflect.DeepEqual(got, tt.want) {
75-
gj, wj := serializeAsJSON(got), serializeAsJSON(tt.want)
76-
if gj != wj {
77-
t.Errorf("#%d: Unmatched JSON\nGot:\n\t%s\nWant:\n\t%s", i, gj, wj)
78-
}
73+
if diff := cmpResource(got, tt.want); diff != "" {
74+
t.Fatalf("Test %d failed. Unexpected Resource -got +want: %s", i, diff)
7975
}
8076
}
8177
}
@@ -141,7 +137,8 @@ func TestProtoMetricToCreateTimeSeriesRequest(t *testing.T) {
141137
TimeSeries: []*monitoringpb.TimeSeries{
142138
{
143139
Metric: &googlemetricpb.Metric{
144-
Type: "custom.googleapis.com/opencensus/with_metric_descriptor",
140+
Type: "custom.googleapis.com/opencensus/with_metric_descriptor",
141+
Labels: map[string]string{},
145142
},
146143
Resource: &monitoredrespb.MonitoredResource{
147144
Type: "global",
@@ -196,13 +193,10 @@ func TestProtoMetricToCreateTimeSeriesRequest(t *testing.T) {
196193
}
197194

198195
got := se.combineTimeSeriesToCreateTimeSeriesRequest(tsl)
199-
if !reflect.DeepEqual(got, tt.want) {
200-
// Our saving grace is serialization equality since some
201-
// unexported fields could be present in the various values.
202-
gj, wj := serializeAsJSON(got), serializeAsJSON(tt.want)
203-
if gj != wj {
204-
t.Errorf("#%d: Unmatched JSON\nGot:\n\t%s\nWant:\n\t%s", i, gj, wj)
205-
}
196+
// Our saving grace is serialization equality since some
197+
// unexported fields could be present in the various values.
198+
if diff := cmpTSReqs(got, tt.want); diff != "" {
199+
t.Fatalf("Test %d failed. Unexpected CreateTimeSeriesRequests -got +want: %s", i, diff)
206200
}
207201
}
208202
}
@@ -234,6 +228,7 @@ func TestProtoToMonitoringMetricDescriptor(t *testing.T) {
234228
want: &googlemetricpb.MetricDescriptor{
235229
Name: "projects/test/metricDescriptors/custom.googleapis.com/opencensus/with_metric_descriptor",
236230
Type: "custom.googleapis.com/opencensus/with_metric_descriptor",
231+
Labels: []*labelpb.LabelDescriptor{},
237232
DisplayName: "OpenCensus/with_metric_descriptor",
238233
Description: "This is with metric descriptor",
239234
Unit: "By",
@@ -259,13 +254,10 @@ func TestProtoToMonitoringMetricDescriptor(t *testing.T) {
259254
continue
260255
}
261256

262-
if !reflect.DeepEqual(got, tt.want) {
263-
// Our saving grace is serialization equality since some
264-
// unexported fields could be present in the various values.
265-
gj, wj := serializeAsJSON(got), serializeAsJSON(tt.want)
266-
if gj != wj {
267-
t.Errorf("#%d: Unmatched JSON\nGot:\n\t%s\nWant:\n\t%s", i, gj, wj)
268-
}
257+
// Our saving grace is serialization equality since some
258+
// unexported fields could be present in the various values.
259+
if diff := cmpMD(got, tt.want); diff != "" {
260+
t.Fatalf("Test %d failed. Unexpected MetricDescriptor -got +want: %s", i, diff)
269261
}
270262
}
271263
}
@@ -374,13 +366,10 @@ func TestProtoMetricsToMonitoringMetrics_fromProtoPoint(t *testing.T) {
374366
continue
375367
}
376368

377-
if g, w := mpt, tt.want; !reflect.DeepEqual(g, w) {
378-
// Our saving grace is serialization equality since some
379-
// unexported fields could be present in the various values.
380-
gj, wj := serializeAsJSON(g), serializeAsJSON(w)
381-
if gj != wj {
382-
t.Errorf("#%d: Unmatched JSON\nGot:\n\t%s\nWant:\n\t%s", i, gj, wj)
383-
}
369+
// Our saving grace is serialization equality since some
370+
// unexported fields could be present in the various values.
371+
if diff := cmpPoint(mpt, tt.want); diff != "" {
372+
t.Fatalf("Test %d failed. Unexpected Point -got +want: %s", i, diff)
384373
}
385374
}
386375
}
@@ -480,17 +469,8 @@ func TestCombineTimeSeriesAndDeduplication(t *testing.T) {
480469

481470
for i, tt := range tests {
482471
got := se.combineTimeSeriesToCreateTimeSeriesRequest(tt.in)
483-
want := tt.want
484-
if !reflect.DeepEqual(got, want) {
485-
gj, wj := serializeAsJSON(got), serializeAsJSON(want)
486-
if gj != wj {
487-
t.Errorf("#%d: Unmatched JSON\nGot:\n\t%s\nWant:\n\t%s", i, gj, wj)
488-
}
472+
if diff := cmpTSReqs(got, tt.want); diff != "" {
473+
t.Fatalf("Test %d failed. Unexpected CreateTimeSeriesRequests -got +want: %s", i, diff)
489474
}
490475
}
491476
}
492-
493-
func serializeAsJSON(v interface{}) string {
494-
blob, _ := json.MarshalIndent(v, "", " ")
495-
return string(blob)
496-
}

metrics_test.go

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ package stackdriver
1616

1717
import (
1818
"context"
19-
"reflect"
2019
"strings"
2120
"testing"
2221

2322
"github.com/golang/protobuf/ptypes/timestamp"
23+
2424
distributionpb "google.golang.org/genproto/googleapis/api/distribution"
25+
labelpb "google.golang.org/genproto/googleapis/api/label"
2526
googlemetricpb "google.golang.org/genproto/googleapis/api/metric"
2627
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
2728
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
2829

2930
"go.opencensus.io/metric/metricdata"
3031
"go.opencensus.io/resource"
31-
"time"
3232
)
3333

3434
var se = &statsExporter{
@@ -74,11 +74,8 @@ func TestMetricResourceToMonitoringResource(t *testing.T) {
7474

7575
for i, tt := range tests {
7676
got := metricRscToMpbRsc(tt.in)
77-
if !reflect.DeepEqual(got, tt.want) {
78-
gj, wj := serializeAsJSON(got), serializeAsJSON(tt.want)
79-
if gj != wj {
80-
t.Errorf("#%d: Unmatched JSON\nGot:\n\t%s\nWant:\n\t%s", i, gj, wj)
81-
}
77+
if diff := cmpResource(got, tt.want); diff != "" {
78+
t.Fatalf("Test %d failed. Unexpected Resource -got +want: %s", i, diff)
8279
}
8380
}
8481
}
@@ -136,7 +133,8 @@ func TestMetricToCreateTimeSeriesRequest(t *testing.T) {
136133
TimeSeries: []*monitoringpb.TimeSeries{
137134
{
138135
Metric: &googlemetricpb.Metric{
139-
Type: "custom.googleapis.com/opencensus/with_metric_descriptor",
136+
Type: "custom.googleapis.com/opencensus/with_metric_descriptor",
137+
Labels: map[string]string{},
140138
},
141139
Resource: &monitoredrespb.MonitoredResource{
142140
Type: "global",
@@ -207,7 +205,8 @@ func TestMetricToCreateTimeSeriesRequest(t *testing.T) {
207205
TimeSeries: []*monitoringpb.TimeSeries{
208206
{
209207
Metric: &googlemetricpb.Metric{
210-
Type: "custom.googleapis.com/opencensus/with_metric_descriptor",
208+
Type: "custom.googleapis.com/opencensus/with_metric_descriptor",
209+
Labels: map[string]string{},
211210
},
212211
Resource: &monitoredrespb.MonitoredResource{
213212
Type: "global",
@@ -258,13 +257,10 @@ func TestMetricToCreateTimeSeriesRequest(t *testing.T) {
258257
}
259258

260259
got := se.combineTimeSeriesToCreateTimeSeriesRequest(tsl)
261-
if !reflect.DeepEqual(got, tt.want) {
262-
// Our saving grace is serialization equality since some
263-
// unexported fields could be present in the various values.
264-
gj, wj := serializeAsJSON(got), serializeAsJSON(tt.want)
265-
if gj != wj {
266-
t.Errorf("#%d: Unmatched JSON\nGot:\n\t%s\nWant:\n\t%s", i, gj, wj)
267-
}
260+
// Our saving grace is serialization equality since some
261+
// unexported fields could be present in the various values.
262+
if diff := cmpTSReqs(got, tt.want); diff != "" {
263+
t.Fatalf("Test %d failed. Unexpected CreateTimeSeriesRequests -got +want: %s", i, diff)
268264
}
269265
}
270266
}
@@ -281,6 +277,7 @@ func TestMetricDescriptorToMonitoringMetricDescriptor(t *testing.T) {
281277
want: &googlemetricpb.MetricDescriptor{
282278
Name: "projects/foo/metricDescriptors/custom.googleapis.com/opencensus",
283279
Type: "custom.googleapis.com/opencensus",
280+
Labels: []*labelpb.LabelDescriptor{},
284281
DisplayName: "OpenCensus",
285282
MetricKind: googlemetricpb.MetricDescriptor_GAUGE,
286283
ValueType: googlemetricpb.MetricDescriptor_INT64,
@@ -298,6 +295,7 @@ func TestMetricDescriptorToMonitoringMetricDescriptor(t *testing.T) {
298295
want: &googlemetricpb.MetricDescriptor{
299296
Name: "projects/foo/metricDescriptors/custom.googleapis.com/opencensus/with_metric_descriptor",
300297
Type: "custom.googleapis.com/opencensus/with_metric_descriptor",
298+
Labels: []*labelpb.LabelDescriptor{},
301299
DisplayName: "OpenCensus/with_metric_descriptor",
302300
Description: "This is with metric descriptor",
303301
Unit: "By",
@@ -321,13 +319,10 @@ func TestMetricDescriptorToMonitoringMetricDescriptor(t *testing.T) {
321319
continue
322320
}
323321

324-
if !reflect.DeepEqual(got, tt.want) {
325-
// Our saving grace is serialization equality since some
326-
// unexported fields could be present in the various values.
327-
gj, wj := serializeAsJSON(got), serializeAsJSON(tt.want)
328-
if gj != wj {
329-
t.Errorf("#%d: Unmatched JSON\nGot:\n\t%s\nWant:\n\t%s", i, gj, wj)
330-
}
322+
// Our saving grace is serialization equality since some
323+
// unexported fields could be present in the various values.
324+
if diff := cmpMD(got, tt.want); diff != "" {
325+
t.Fatalf("Test %d failed. Unexpected MetricDescriptor -got +want: %s", i, diff)
331326
}
332327
}
333328
}
@@ -505,21 +500,10 @@ func TestMetricsToMonitoringMetrics_fromProtoPoint(t *testing.T) {
505500
continue
506501
}
507502

508-
if g, w := mpt, tt.want; !reflect.DeepEqual(g, w) {
509-
// Our saving grace is serialization equality since some
510-
// unexported fields could be present in the various values.
511-
gj, wj := serializeAsJSON(g), serializeAsJSON(w)
512-
if gj != wj {
513-
t.Errorf("#%d: Unmatched JSON\nGot:\n\t%s\nWant:\n\t%s", i, gj, wj)
514-
}
503+
// Our saving grace is serialization equality since some
504+
// unexported fields could be present in the various values.
505+
if diff := cmpPoint(mpt, tt.want); diff != "" {
506+
t.Fatalf("Test %d failed. Unexpected Point -got +want: %s", i, diff)
515507
}
516508
}
517509
}
518-
519-
func timestampToTime(ts *timestamp.Timestamp) time.Time {
520-
if ts == nil {
521-
return time.Unix(0, 0).UTC()
522-
} else {
523-
return time.Unix(ts.Seconds, int64(ts.Nanos)).UTC()
524-
}
525-
}

metrics_test_utils.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package stackdriver
16+
17+
/*
18+
Common test utilities for comparing Stackdriver metrics.
19+
*/
20+
21+
import (
22+
"github.com/golang/protobuf/ptypes/timestamp"
23+
"github.com/google/go-cmp/cmp"
24+
"github.com/google/go-cmp/cmp/cmpopts"
25+
26+
googlemetricpb "google.golang.org/genproto/googleapis/api/metric"
27+
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
28+
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
29+
30+
"time"
31+
)
32+
33+
func timestampToTime(ts *timestamp.Timestamp) time.Time {
34+
if ts == nil {
35+
return time.Unix(0, 0).UTC()
36+
} else {
37+
return time.Unix(ts.Seconds, int64(ts.Nanos)).UTC()
38+
}
39+
}
40+
41+
func cmpResource(got, want *monitoredrespb.MonitoredResource) string {
42+
return cmp.Diff(got, want, cmpopts.IgnoreUnexported(monitoredrespb.MonitoredResource{}))
43+
}
44+
45+
func cmpTSReqs(got, want []*monitoringpb.CreateTimeSeriesRequest) string {
46+
return cmp.Diff(got, want, cmpopts.IgnoreUnexported(monitoringpb.CreateTimeSeriesRequest{}))
47+
}
48+
49+
func cmpMD(got, want *googlemetricpb.MetricDescriptor) string {
50+
return cmp.Diff(got, want, cmpopts.IgnoreUnexported(googlemetricpb.MetricDescriptor{}))
51+
}
52+
53+
func cmpMDReq(got, want *monitoringpb.CreateMetricDescriptorRequest) string {
54+
return cmp.Diff(got, want, cmpopts.IgnoreUnexported(monitoringpb.CreateMetricDescriptorRequest{}))
55+
}
56+
57+
func cmpMDReqs(got, want []*monitoringpb.CreateMetricDescriptorRequest) string {
58+
return cmp.Diff(got, want, cmpopts.IgnoreUnexported(monitoringpb.CreateMetricDescriptorRequest{}))
59+
}
60+
61+
func cmpPoint(got, want *monitoringpb.Point) string {
62+
return cmp.Diff(got, want, cmpopts.IgnoreUnexported(monitoringpb.Point{}))
63+
}

0 commit comments

Comments
 (0)