github.com/google/cloudprober@v0.11.3/rds/kubernetes/ingresses_test.go (about) 1 // Copyright 2020 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 kubernetes 16 17 import ( 18 "io/ioutil" 19 "reflect" 20 "testing" 21 22 "github.com/golang/protobuf/proto" 23 pb "github.com/google/cloudprober/rds/proto" 24 ) 25 26 func testIngressInfo(k resourceKey, ingressIP, hostname string, labels map[string]string) *ingressInfo { 27 si := &ingressInfo{Metadata: kMetadata{Name: k.name, Namespace: k.namespace, Labels: labels}} 28 29 if ingressIP != "" || hostname != "" { 30 si.Status.LoadBalancer.Ingress = []struct{ IP, Hostname string }{ 31 { 32 IP: ingressIP, 33 Hostname: hostname, 34 }, 35 } 36 } 37 return si 38 } 39 40 func listerFromDataFile(t *testing.T) *ingressesLister { 41 t.Helper() 42 43 ingressesListFile := "./testdata/ingresses.json" 44 data, err := ioutil.ReadFile(ingressesListFile) 45 46 if err != nil { 47 t.Fatalf("error reading test data file: %s", ingressesListFile) 48 } 49 keys, ingresses, err := parseIngressesJSON(data) 50 51 if err != nil { 52 t.Fatalf("Error while parsing ingresses JSON data: %v", err) 53 } 54 55 return &ingressesLister{ 56 keys: keys, 57 cache: ingresses, 58 } 59 } 60 61 func TestParseIngressJSON(t *testing.T) { 62 lister := listerFromDataFile(t) 63 64 key := resourceKey{"default", "rds-ingress"} 65 if len(lister.keys) != 1 || lister.cache[key] == nil { 66 t.Errorf("Expected exactly one ingress with name rds-ingress, got: %+v", lister.cache) 67 } 68 } 69 70 func TestListIngressResources(t *testing.T) { 71 lister := listerFromDataFile(t) 72 73 // Add one more ingress for better testing. 74 key := resourceKey{name: "cloudprober-ingress", namespace: "monitoring"} 75 lister.keys = append(lister.keys, key) 76 lister.cache[key] = testIngressInfo(key, "", "cloudprober.monitoring.com", map[string]string{}) 77 78 tests := []struct { 79 desc string 80 filters map[string]string 81 wantNames []string 82 wantFQDNs []string 83 wantURLs []string 84 wantIPs []string 85 }{ 86 { 87 desc: "no filter", 88 wantNames: []string{"rds-ingress_foo.bar.com__health", "rds-ingress_foo.bar.com__rds", "rds-ingress_prometheus.bar.com", "cloudprober-ingress"}, 89 wantFQDNs: []string{"foo.bar.com", "foo.bar.com", "prometheus.bar.com", ""}, 90 wantURLs: []string{"/health", "/rds", "/", ""}, 91 wantIPs: []string{"241.120.51.35", "241.120.51.35", "241.120.51.35", "cloudprober.monitoring.com"}, 92 }, 93 { 94 desc: "namespace filter", 95 filters: map[string]string{"namespace": "monitoring"}, 96 wantNames: []string{"cloudprober-ingress"}, 97 wantFQDNs: []string{""}, 98 wantURLs: []string{""}, 99 wantIPs: []string{"cloudprober.monitoring.com"}, 100 }, 101 { 102 desc: "name filter for host regex", 103 filters: map[string]string{"name": ".*foo.bar.com.*"}, 104 wantNames: []string{"rds-ingress_foo.bar.com__health", "rds-ingress_foo.bar.com__rds"}, 105 wantFQDNs: []string{"foo.bar.com", "foo.bar.com"}, 106 wantURLs: []string{"/health", "/rds"}, 107 wantIPs: []string{"241.120.51.35", "241.120.51.35"}, 108 }, 109 { 110 desc: "name and label filter", 111 filters: map[string]string{"name": ".*foo.bar.com.*", "labels.relative_url": "/rds"}, 112 wantNames: []string{"rds-ingress_foo.bar.com__rds"}, 113 wantFQDNs: []string{"foo.bar.com"}, 114 wantURLs: []string{"/rds"}, 115 wantIPs: []string{"241.120.51.35"}, 116 }, 117 { 118 desc: "fqdn filter", 119 filters: map[string]string{"labels.fqdn": "prometheus.bar.com"}, 120 wantNames: []string{"rds-ingress_prometheus.bar.com"}, 121 wantFQDNs: []string{"prometheus.bar.com"}, 122 wantURLs: []string{"/"}, 123 wantIPs: []string{"241.120.51.35"}, 124 }, 125 } 126 127 for _, test := range tests { 128 t.Run(test.desc, func(t *testing.T) { 129 var filters []*pb.Filter 130 131 for k, v := range test.filters { 132 filters = append(filters, &pb.Filter{ 133 Key: proto.String(k), 134 Value: proto.String(v), 135 }) 136 } 137 138 resources, err := lister.listResources(&pb.ListResourcesRequest{Filter: filters}) 139 if err != nil { 140 t.Errorf("Error while listing resources: %v", err) 141 } 142 var gotNames, gotFQDNs, gotURLs, gotIPs []string 143 for _, res := range resources { 144 gotNames = append(gotNames, res.GetName()) 145 gotFQDNs = append(gotFQDNs, res.GetLabels()["fqdn"]) 146 gotURLs = append(gotURLs, res.GetLabels()["relative_url"]) 147 gotIPs = append(gotIPs, res.GetIp()) 148 } 149 150 if !reflect.DeepEqual(gotNames, test.wantNames) { 151 t.Errorf("gotName: %v, wantNames: %v", gotNames, test.wantNames) 152 } 153 154 if !reflect.DeepEqual(gotFQDNs, test.wantFQDNs) { 155 t.Errorf("gotFQDNs: %v, wantFQDNs: %v", gotFQDNs, test.wantFQDNs) 156 } 157 158 if !reflect.DeepEqual(gotURLs, test.wantURLs) { 159 t.Errorf("gotURLs: %v, wantURLs: %v", gotURLs, test.wantURLs) 160 } 161 162 if !reflect.DeepEqual(gotIPs, test.wantIPs) { 163 t.Errorf("gotIPs: %v, wantIPs: %v", gotIPs, test.wantIPs) 164 } 165 }) 166 } 167 }