github.com/google/cloudprober@v0.11.3/rds/kubernetes/pods_test.go (about) 1 // Copyright 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 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 testPodInfo(name, ns, ip string, labels map[string]string) *podInfo { 27 labels["namespace"] = ns 28 pi := &podInfo{Metadata: kMetadata{Name: name, Namespace: ns, Labels: labels}} 29 pi.Status.PodIP = ip 30 return pi 31 } 32 33 func TestListResources(t *testing.T) { 34 pl := &podsLister{ 35 cache: make(map[resourceKey]*podInfo), 36 } 37 for _, pi := range []*podInfo{ 38 testPodInfo("podA", "nsAB", "10.1.1.1", map[string]string{"app": "appA"}), 39 testPodInfo("podB", "nsAB", "10.1.1.2", map[string]string{"app": "appB"}), 40 testPodInfo("podC", "nsC", "10.1.1.3", map[string]string{"app": "appC", "func": "web"}), 41 testPodInfo("podC", "devC", "10.2.1.3", map[string]string{"app": "appC", "func": "web"}), 42 } { 43 key := resourceKey{pi.Metadata.Namespace, pi.Metadata.Name} 44 pl.keys = append(pl.keys, key) 45 pl.cache[key] = pi 46 } 47 48 tests := []struct { 49 desc string 50 nameFilter string 51 filters map[string]string 52 labelsFilter map[string]string 53 wantPods []resourceKey 54 wantErr bool 55 }{ 56 { 57 desc: "bad filter key, expect error", 58 filters: map[string]string{"names": "pod(B|C)"}, 59 wantErr: true, 60 }, 61 { 62 desc: "only name filter for podB and podC", 63 filters: map[string]string{"name": "pod(B|C)"}, 64 wantPods: []resourceKey{{"nsAB", "podB"}, {"nsC", "podC"}, {"devC", "podC"}}, 65 }, 66 { 67 desc: "name filter for podB and podC, and namespace filter", 68 filters: map[string]string{"name": "pod(B|C)", "namespace": "ns.*"}, 69 wantPods: []resourceKey{{"nsAB", "podB"}, {"nsC", "podC"}}, 70 }, 71 { 72 desc: "name and namespace filter for podB", 73 filters: map[string]string{"name": "pod(B|C)", "namespace": "nsAB"}, 74 wantPods: []resourceKey{{"nsAB", "podB"}}, 75 }, 76 { 77 desc: "only namespace filter for podA and podB", 78 filters: map[string]string{"namespace": "nsAB"}, 79 wantPods: []resourceKey{{"nsAB", "podA"}, {"nsAB", "podB"}}, 80 }, 81 } 82 83 for _, test := range tests { 84 t.Run(test.desc, func(t *testing.T) { 85 var filtersPB []*pb.Filter 86 for k, v := range test.filters { 87 filtersPB = append(filtersPB, &pb.Filter{Key: proto.String(k), Value: proto.String(v)}) 88 } 89 90 results, err := pl.listResources(&pb.ListResourcesRequest{Filter: filtersPB}) 91 if err != nil { 92 if !test.wantErr { 93 t.Errorf("got unexpected error: %v", err) 94 } 95 return 96 } 97 98 var gotPods []resourceKey 99 for _, res := range results { 100 gotPods = append(gotPods, resourceKey{res.GetLabels()["namespace"], res.GetName()}) 101 } 102 103 if !reflect.DeepEqual(gotPods, test.wantPods) { 104 t.Errorf("pods.listResources: got=%v, expected=%v", gotPods, test.wantPods) 105 } 106 }) 107 } 108 } 109 110 func TestParseResourceList(t *testing.T) { 111 podsListFile := "./testdata/pods.json" 112 data, err := ioutil.ReadFile(podsListFile) 113 114 if err != nil { 115 t.Fatalf("error reading test data file: %s", podsListFile) 116 } 117 _, podsByKey, err := parsePodsJSON(data) 118 119 if err != nil { 120 t.Fatalf("Error while parsing pods JSON data: %v", err) 121 } 122 123 for _, ns := range []string{"prod", "dev"} { 124 cpPodKey := resourceKey{ns, "cloudprober-54778d95f5-7hqtd"} 125 if podsByKey[cpPodKey] == nil { 126 t.Errorf("didn't get pod by the key: %+v", cpPodKey) 127 continue 128 } 129 130 if podsByKey[cpPodKey].Metadata.Labels["app"] != "cloudprober" { 131 t.Errorf("cloudprober pod app label: got=%s, want=cloudprober", podsByKey[cpPodKey].Metadata.Labels["app"]) 132 } 133 134 cpPodIP := "10.28.0.3" 135 if podsByKey[cpPodKey].Status.PodIP != cpPodIP { 136 t.Errorf("cloudprober pod ip: got=%s, want=%s", podsByKey[cpPodKey].Status.PodIP, cpPodIP) 137 } 138 } 139 140 // Verify that we didn't the pending pod. 141 if podsByKey[resourceKey{"default", "test"}] != nil { 142 t.Error("got a non-running pod in the list: test") 143 } 144 }