1414package stackdriver
1515
1616import (
17- "bytes"
1817 "context"
19- "encoding/json"
2018 "errors"
2119 "fmt"
22- monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
2320 "net"
2421 "sync"
2522 "testing"
@@ -33,6 +30,7 @@ import (
3330 distributionpb "google.golang.org/genproto/googleapis/api/distribution"
3431 "google.golang.org/genproto/googleapis/api/metric"
3532 metricpb "google.golang.org/genproto/googleapis/api/metric"
33+ monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
3634 monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
3735 "google.golang.org/grpc"
3836 "google.golang.org/grpc/test/bufconn"
@@ -94,6 +92,12 @@ func BenchmarkReport10(b *testing.B) { benchmarkCopy(10, 10, 10, b) }
9492func BenchmarkReport50 (b * testing.B ) { benchmarkCopy (50 , 50 , 50 , b ) }
9593func BenchmarkReport100 (b * testing.B ) { benchmarkCopy (100 , 100 , 100 , b ) }
9694
95+ func diffTest (t * testing.T , title string , x , y interface {}) {
96+ if diff := cmp .Diff (x , y ); diff != "" {
97+ t .Errorf ("%s mismatch (-want +got):\n %s" , title , diff )
98+ }
99+ }
100+
97101func sPtr (s string ) * string {
98102 return & s
99103}
@@ -1095,83 +1099,67 @@ func diffCreateMsg(want, got *monitoringpb.CreateTimeSeriesRequest) string {
10951099 return out
10961100}
10971101
1098- func TestCustomMonitorResource (t * testing.T ) {
1099- checkLabels := func (sink * Sink , labels map [string ]string ) error {
1100- expectedLabels := sink .monitoredResource .GetLabels ()
1101-
1102- expectedLabelsBytes , _ := json .Marshal (expectedLabels )
1103- labelsBytes , _ := json .Marshal (labels )
1104-
1105- if expectedLabelsBytes == nil || ! bytes .Equal (expectedLabelsBytes , labelsBytes ) {
1106- return errors .New ("invalid labels" )
1107- }
1102+ func monitoredResourceDiff (t * testing.T , s * Sink , labels map [string ]string ) {
1103+ diffTest (t , "Monitored Resource labels" , s .monitoredResource .GetLabels (), labels )
1104+ }
11081105
1109- return nil
1106+ func TestCustomMonitorResource (t * testing.T ) {
1107+ labels := map [string ]string {
1108+ "project_id" : "project" ,
1109+ "location" : "zone" ,
1110+ "cluster_name" : "cluster" ,
1111+ "container_name" : "container_name" ,
1112+ "namespace_name" : "namespace_name" ,
1113+ "pod_name" : "pod_name" ,
11101114 }
11111115
1112- {
1113- labels := map [string ]string {
1114- "project_id" : "project" ,
1115- "location" : "zone" ,
1116- "cluster_name" : "cluster" ,
1117- "container_name" : "container_name" ,
1118- "namespace_name" : "namespace_name" ,
1119- "pod_name" : "pod_name" ,
1120- }
1116+ sink := NewSink (nil , & Config {
1117+ ProjectID : "example_project" ,
1118+ Prefix : sPtr ("" ),
1119+ MonitoredResource : & monitoredrespb.MonitoredResource {
1120+ Labels : labels ,
1121+ Type : "k8s_container" ,
1122+ },
1123+ })
11211124
1122- sink := NewSink (nil , & Config {
1123- ProjectID : "example_project" ,
1124- Prefix : sPtr ("" ),
1125- MonitoredResource : & monitoredrespb.MonitoredResource {
1126- Labels : labels ,
1127- Type : "k8s_container" ,
1128- },
1129- })
1125+ monitoredResourceDiff (t , sink , labels )
1126+ }
11301127
1131- if err := checkLabels (sink , labels ); err != nil {
1132- t .Error (err )
1133- }
1134- }
1128+ func TestCustomMonitorResourceWithDefaultLabels (t * testing.T ) {
1129+ sink := NewSink (nil , & Config {
1130+ ProjectID : "example_project" ,
1131+ Prefix : sPtr ("" ),
1132+ })
11351133
1136- {
1137- sink := NewSink (nil , & Config {
1138- ProjectID : "example_project" ,
1139- Prefix : sPtr ("" ),
1140- })
1134+ labels := defaultMonitoredResource (sink .taskInfo ).GetLabels ()
11411135
1142- labels := defaultMonitoredResource (sink .taskInfo ).GetLabels ()
1136+ monitoredResourceDiff (t , sink , labels )
1137+ }
11431138
1144- if err := checkLabels (sink , labels ); err != nil {
1145- t .Error (err )
1146- }
1139+ func TestCustomMonitorResourceWithInvalidLabels (t * testing.T ) {
1140+ labels := map [string ]string {
1141+ "project_id" : "project" ,
1142+ "location" : "zone" ,
1143+ "cluster_name" : "cluster" ,
1144+ "container_name" : "container_name" ,
1145+ "namespace_name" : "namespace_name" ,
1146+ "pod_name" : "pod_name" ,
11471147 }
11481148
1149- {
1150- labels := map [string ]string {
1151- "project_id" : "project" ,
1152- "location" : "zone" ,
1153- "cluster_name" : "cluster" ,
1154- "container_name" : "container_name" ,
1155- "namespace_name" : "namespace_name" ,
1156- "pod_name" : "pod_name" ,
1157- }
1158-
1159- invalidLabels := map [string ]string {
1160- "project_id" : "project" ,
1161- }
1149+ invalidLabels := map [string ]string {
1150+ "project_id" : "project" ,
1151+ }
11621152
1163- sink := NewSink (nil , & Config {
1164- ProjectID : "example_project" ,
1165- Prefix : sPtr ("" ),
1166- MonitoredResource : & monitoredrespb.MonitoredResource {
1167- Labels : labels ,
1168- Type : "k8s_container" ,
1169- },
1170- })
1153+ sink := NewSink (nil , & Config {
1154+ ProjectID : "example_project" ,
1155+ Prefix : sPtr ("" ),
1156+ MonitoredResource : & monitoredrespb.MonitoredResource {
1157+ Labels : labels ,
1158+ Type : "k8s_container" ,
1159+ },
1160+ })
11711161
1172- if err := checkLabels (sink , invalidLabels ); err == nil {
1173- t .Error ("labels should be the same" )
1174- }
1162+ if diff := cmp .Diff (sink .monitoredResource .GetLabels (), invalidLabels ); diff == "" {
1163+ t .Error ("Monitored Resource labels should not be equal" )
11751164 }
1176-
11771165}
0 commit comments