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
15package stackdriver
16
17/*
18Common test utilities for comparing Stackdriver metrics.
19*/
20
21import (
22	"testing"
23
24	"github.com/golang/protobuf/proto"
25	"github.com/google/go-cmp/cmp"
26	"github.com/google/go-cmp/cmp/cmpopts"
27
28	googlemetricpb "google.golang.org/genproto/googleapis/api/metric"
29	monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
30	monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
31)
32
33func cmpResource(got, want *monitoredrespb.MonitoredResource) string {
34	return cmp.Diff(got, want, cmpopts.IgnoreUnexported(monitoredrespb.MonitoredResource{}))
35}
36
37func requireTimeSeriesRequestEqual(t *testing.T, got, want []*monitoringpb.CreateTimeSeriesRequest) {
38	if len(got) != len(want) {
39		t.Fatalf("Unexpected slice len got: %d want: %d", len(got), len(want))
40	}
41	for i, g := range got {
42		w := want[i]
43		if !proto.Equal(g, w) {
44			t.Fatalf("Unexpected proto difference got: %s want: %s", proto.MarshalTextString(g), proto.MarshalTextString(w))
45		}
46	}
47}
48
49func cmpTSReqs(got, want []*monitoringpb.CreateTimeSeriesRequest) string {
50	return cmp.Diff(got, want, cmpopts.IgnoreUnexported(monitoringpb.CreateTimeSeriesRequest{}), cmpopts.IgnoreTypes(googlemetricpb.MetricDescriptor_METRIC_KIND_UNSPECIFIED, googlemetricpb.MetricDescriptor_VALUE_TYPE_UNSPECIFIED))
51}
52
53func cmpMD(got, want *googlemetricpb.MetricDescriptor) string {
54	return cmp.Diff(got, want, cmpopts.IgnoreUnexported(googlemetricpb.MetricDescriptor{}))
55}
56
57func cmpMDReq(got, want *monitoringpb.CreateMetricDescriptorRequest) string {
58	return cmp.Diff(got, want, cmpopts.IgnoreUnexported(monitoringpb.CreateMetricDescriptorRequest{}))
59}
60
61func cmpMDReqs(got, want []*monitoringpb.CreateMetricDescriptorRequest) string {
62	return cmp.Diff(got, want, cmpopts.IgnoreUnexported(monitoringpb.CreateMetricDescriptorRequest{}))
63}
64
65func cmpPoint(got, want *monitoringpb.Point) string {
66	return cmp.Diff(got, want, cmpopts.IgnoreUnexported(monitoringpb.Point{}))
67}
68