istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/serviceregistry/util/workloadinstances/index_test.go (about) 1 // Copyright Istio 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 workloadinstances 16 17 import ( 18 "testing" 19 "time" 20 21 networking "istio.io/api/networking/v1alpha3" 22 "istio.io/istio/pilot/pkg/model" 23 "istio.io/istio/pkg/config" 24 "istio.io/istio/pkg/config/labels" 25 "istio.io/istio/pkg/config/schema/gvk" 26 "istio.io/istio/pkg/spiffe" 27 "istio.io/istio/pkg/test/util/assert" 28 ) 29 30 var GlobalTime = time.Now() 31 32 // ServiceEntry with a selector 33 var selector = &config.Config{ 34 Meta: config.Meta{ 35 GroupVersionKind: gvk.ServiceEntry, 36 Name: "selector", 37 Namespace: "selector", 38 CreationTimestamp: GlobalTime, 39 }, 40 Spec: &networking.ServiceEntry{ 41 Hosts: []string{"selector.com"}, 42 Ports: []*networking.ServicePort{ 43 {Number: 444, Name: "tcp-444", Protocol: "tcp"}, 44 {Number: 445, Name: "http-445", Protocol: "http"}, 45 }, 46 WorkloadSelector: &networking.WorkloadSelector{ 47 Labels: map[string]string{"app": "wle"}, 48 }, 49 Resolution: networking.ServiceEntry_STATIC, 50 }, 51 } 52 53 func TestIndex(t *testing.T) { 54 // Setup a couple of workload instances for test 55 wi1 := &model.WorkloadInstance{ 56 Name: selector.Name, 57 Namespace: selector.Namespace, 58 Endpoint: &model.IstioEndpoint{ 59 Address: "2.2.2.2", 60 Labels: map[string]string{"app": "wle"}, 61 ServiceAccount: spiffe.MustGenSpiffeURI(selector.Name, "default"), 62 TLSMode: model.IstioMutualTLSModeLabel, 63 }, 64 } 65 66 wi2 := &model.WorkloadInstance{ 67 Name: "some-other-name", 68 Namespace: selector.Namespace, 69 Endpoint: &model.IstioEndpoint{ 70 Address: "3.3.3.3", 71 Labels: map[string]string{"app": "wle"}, 72 ServiceAccount: spiffe.MustGenSpiffeURI(selector.Name, "default"), 73 TLSMode: model.IstioMutualTLSModeLabel, 74 }, 75 } 76 77 wi3 := &model.WorkloadInstance{ 78 Name: "another-name", 79 Namespace: "dns-selector", 80 Endpoint: &model.IstioEndpoint{ 81 Address: "2.2.2.2", 82 Labels: map[string]string{"app": "dns-wle"}, 83 ServiceAccount: spiffe.MustGenSpiffeURI("dns-selector", "default"), 84 TLSMode: model.IstioMutualTLSModeLabel, 85 }, 86 } 87 88 index := NewIndex() 89 90 // test update 91 index.Insert(wi1) 92 index.Insert(wi2) 93 index.Insert(wi3) 94 95 verifyGetByIP := func(ip string, expected []*model.WorkloadInstance) { 96 assert.Equal(t, expected, index.GetByIP(ip)) 97 } 98 99 // GetByIP should return 2 workload instances 100 101 verifyGetByIP("2.2.2.2", []*model.WorkloadInstance{wi3, wi1}) 102 103 // Delete should return previously inserted value 104 105 deleted := index.Delete(wi1) 106 assert.Equal(t, wi1, deleted) 107 108 // GetByIP should return 1 workload instance 109 110 verifyGetByIP("2.2.2.2", []*model.WorkloadInstance{wi3}) 111 112 // Delete should return nil since there is no such element in the index 113 114 deleted = index.Delete(wi1) 115 assert.Equal(t, nil, deleted) 116 117 // GetByIP should return nil 118 119 verifyGetByIP("1.1.1.1", nil) 120 } 121 122 func TestIndex_FindAll(t *testing.T) { 123 // Setup a couple of workload instances for test. These will be selected by the `selector` SE 124 wi1 := &model.WorkloadInstance{ 125 Name: selector.Name, 126 Namespace: selector.Namespace, 127 Endpoint: &model.IstioEndpoint{ 128 Address: "2.2.2.2", 129 Labels: map[string]string{"app": "wle"}, // should match 130 }, 131 } 132 133 wi2 := &model.WorkloadInstance{ 134 Name: "same-ip", 135 Namespace: selector.Namespace, 136 Endpoint: &model.IstioEndpoint{ 137 Address: "2.2.2.2", 138 Labels: map[string]string{"app": "wle"}, // should match 139 }, 140 } 141 142 wi3 := &model.WorkloadInstance{ 143 Name: "another-ip", 144 Namespace: selector.Namespace, 145 Endpoint: &model.IstioEndpoint{ 146 Address: "3.3.3.3", 147 Labels: map[string]string{"app": "another-wle"}, // should not match because of another label 148 }, 149 } 150 151 wi4 := &model.WorkloadInstance{ 152 Name: "another-name", 153 Namespace: "another-namespace", // should not match because of another namespace 154 Endpoint: &model.IstioEndpoint{ 155 Address: "2.2.2.2", 156 Labels: map[string]string{"app": "wle"}, 157 }, 158 } 159 160 index := NewIndex() 161 162 // test update 163 index.Insert(wi1) 164 index.Insert(wi2) 165 index.Insert(wi3) 166 index.Insert(wi4) 167 168 // test search by service selector 169 actual := FindAllInIndex(index, ByServiceSelector(selector.Namespace, labels.Instance{"app": "wle"})) 170 want := []*model.WorkloadInstance{wi1, wi2} 171 172 assert.Equal(t, len(want), len(actual)) 173 174 got := map[string]*model.WorkloadInstance{} 175 for _, instance := range actual { 176 got[instance.Name] = instance 177 } 178 179 for _, expected := range want { 180 assert.Equal(t, expected, got[expected.Name]) 181 } 182 }