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/google/go-cmp/cmp"
25	"github.com/google/go-cmp/cmp/cmpopts"
26	"google.golang.org/protobuf/encoding/prototext"
27	"google.golang.org/protobuf/proto"
28
29	googlemetricpb "google.golang.org/genproto/googleapis/api/metric"
30	monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
31	monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
32	"google.golang.org/protobuf/testing/protocmp"
33)
34
35func cmpResource(got, want *monitoredrespb.MonitoredResource) string {
36	return cmp.Diff(got, want, cmpopts.IgnoreUnexported(monitoredrespb.MonitoredResource{}))
37}
38
39func requireTimeSeriesRequestEqual(t *testing.T, got, want []*monitoringpb.CreateTimeSeriesRequest) {
40	if len(got) != len(want) {
41		t.Fatalf("Unexpected slice len got: %d want: %d", len(got), len(want))
42	}
43	for i, g := range got {
44		w := want[i]
45		if !proto.Equal(g, w) {
46			gBytes, err := prototext.Marshal(g)
47			if err != nil {
48				t.Fatalf("Error marshaling time series: %s", err)
49			}
50			wBytes, err := prototext.Marshal(w)
51			if err != nil {
52				t.Fatalf("Error marshaling time series: %s", err)
53			}
54			t.Fatalf("Unexpected proto difference got: %s want: %s", string(gBytes), string(wBytes))
55		}
56	}
57}
58
59func cmpTSReqs(got, want []*monitoringpb.CreateTimeSeriesRequest) string {
60	return cmp.Diff(got, want, protocmp.Transform(), protocmp.IgnoreEnums(googlemetricpb.MetricDescriptor_METRIC_KIND_UNSPECIFIED, googlemetricpb.MetricDescriptor_VALUE_TYPE_UNSPECIFIED))
61}
62
63func cmpMD(got, want *googlemetricpb.MetricDescriptor) string {
64	return cmp.Diff(got, want, protocmp.Transform())
65}
66
67func cmpMDReq(got, want *monitoringpb.CreateMetricDescriptorRequest) string {
68	return cmp.Diff(got, want, protocmp.Transform())
69}
70
71func cmpMDReqs(got, want []*monitoringpb.CreateMetricDescriptorRequest) string {
72	return cmp.Diff(got, want, protocmp.Transform())
73}
74
75func cmpPoint(got, want *monitoringpb.Point) string {
76	return cmp.Diff(got, want, protocmp.Transform())
77}
78