github.com/google/cloudprober@v0.11.3/targets/gce/gce_utils_test.go (about) 1 // Copyright 2017-2019 The Cloudprober 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 gce 16 17 import ( 18 "testing" 19 20 "github.com/golang/protobuf/proto" 21 rdspb "github.com/google/cloudprober/rds/proto" 22 ) 23 24 func TestParseLabels(t *testing.T) { 25 tests := []struct { 26 desc string 27 label string 28 shouldFail bool 29 want []*rdspb.Filter 30 }{ 31 { 32 "Valid label should succeed", 33 "k:v", 34 false, 35 []*rdspb.Filter{ 36 { 37 Key: proto.String("labels.k"), 38 Value: proto.String("v"), 39 }, 40 }, 41 }, 42 { 43 "Multiple separators should fail", 44 "k:v:t", 45 true, 46 nil, 47 }, 48 { 49 "No separator should fail", 50 "kv", 51 true, 52 nil, 53 }, 54 } 55 56 for _, test := range tests { 57 t.Run(test.desc, func(t *testing.T) { 58 got, err := parseLabels([]string{test.label}) 59 if test.shouldFail && err == nil { 60 t.Errorf("parseLabels() error got:nil, want:error") 61 } else if !test.shouldFail && err != nil { 62 t.Errorf("parseLabels() error got:%s, want:nil", err) 63 } 64 65 eq := len(got) == len(test.want) 66 for i := 0; i < len(got) && i < len(test.want); i++ { 67 eq = eq && proto.Equal(got[i], test.want[i]) 68 } 69 if !eq { 70 t.Errorf("parseLabels() got:%s, want:%s", got, test.want) 71 } 72 }) 73 } 74 }