github.com/google/cloudprober@v0.11.3/targets/file/file_test.go (about) 1 // Copyright 2021 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 file 16 17 import ( 18 "reflect" 19 "testing" 20 21 rdspb "github.com/google/cloudprober/rds/proto" 22 "github.com/google/cloudprober/targets/endpoint" 23 configpb "github.com/google/cloudprober/targets/file/proto" 24 "google.golang.org/protobuf/proto" 25 ) 26 27 var testExpectedEndpoints = []endpoint.Endpoint{ 28 { 29 Name: "switch-xx-1", 30 Port: 8080, 31 Labels: map[string]string{ 32 "device_type": "switch", 33 "cluster": "xx", 34 }, 35 }, 36 { 37 Name: "switch-xx-2", 38 Port: 8081, 39 Labels: map[string]string{ 40 "cluster": "xx", 41 }, 42 }, 43 { 44 Name: "switch-yy-1", 45 Port: 8080, 46 }, 47 { 48 Name: "switch-zz-1", 49 Port: 8080, 50 }, 51 } 52 53 var testExpectedIP = map[string]string{ 54 "switch-xx-1": "10.1.1.1", 55 "switch-xx-2": "10.1.1.2", 56 "switch-yy-1": "10.1.2.1", 57 "switch-zz-1": "::aaa:1", 58 } 59 60 func TestListEndpointsWithFilter(t *testing.T) { 61 for _, test := range []struct { 62 desc string 63 f []*rdspb.Filter 64 wantEndpoints []endpoint.Endpoint 65 }{ 66 { 67 desc: "no_filter", 68 wantEndpoints: testExpectedEndpoints, 69 }, 70 { 71 desc: "with_filter", 72 f: []*rdspb.Filter{{ 73 Key: proto.String("labels.cluster"), 74 Value: proto.String("xx"), 75 }}, 76 wantEndpoints: testExpectedEndpoints[:2], 77 }, 78 } { 79 t.Run(test.desc, func(t *testing.T) { 80 ft, err := New(&configpb.TargetsConf{ 81 FilePath: proto.String("../../rds/file/testdata/targets.json"), 82 Filter: test.f, 83 }, nil, nil) 84 85 if err != nil { 86 t.Fatalf("Unexpected error while parsing textpb: %v", err) 87 } 88 89 got := ft.ListEndpoints() 90 91 if len(got) != len(test.wantEndpoints) { 92 t.Fatalf("Got endpoints: %d, expected: %d", len(got), len(test.wantEndpoints)) 93 } 94 for i := range test.wantEndpoints { 95 want := test.wantEndpoints[i] 96 97 if got[i].Name != want.Name || got[i].Port != want.Port || !reflect.DeepEqual(got[i].Labels, want.Labels) { 98 t.Errorf("ListResources: got:\n%v\nexpected:\n%v", got[i], want) 99 } 100 } 101 102 for _, ep := range got { 103 resolvedIP, err := ft.Resolve(ep.Name, 0) 104 if err != nil { 105 t.Errorf("unexpected error while resolving %s: %v", ep.Name, err) 106 } 107 ip := resolvedIP.String() 108 if ip != testExpectedIP[ep.Name] { 109 t.Errorf("ft.Resolve(%s): got=%s, expected=%s", ep.Name, ip, testExpectedIP[ep.Name]) 110 } 111 } 112 }) 113 } 114 115 }