istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/model/endpointshards_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 model 16 17 import ( 18 "testing" 19 ) 20 21 func TestUpdateServiceAccount(t *testing.T) { 22 var ( 23 c1Key = ShardKey{Cluster: "c1"} 24 c2Key = ShardKey{Cluster: "c2"} 25 ) 26 27 cluster1Endppoints := []*IstioEndpoint{ 28 {Address: "10.172.0.1", ServiceAccount: "sa1"}, 29 {Address: "10.172.0.2", ServiceAccount: "sa-vm1"}, 30 } 31 32 testCases := []struct { 33 name string 34 shardKey ShardKey 35 endpoints []*IstioEndpoint 36 expect bool 37 }{ 38 { 39 name: "added new endpoint", 40 shardKey: c1Key, 41 endpoints: append(cluster1Endppoints, &IstioEndpoint{Address: "10.172.0.3", ServiceAccount: "sa1"}), 42 expect: false, 43 }, 44 { 45 name: "added new sa", 46 shardKey: c1Key, 47 endpoints: append(cluster1Endppoints, &IstioEndpoint{Address: "10.172.0.3", ServiceAccount: "sa2"}), 48 expect: true, 49 }, 50 { 51 name: "updated endpoints address", 52 shardKey: c1Key, 53 endpoints: []*IstioEndpoint{ 54 {Address: "10.172.0.5", ServiceAccount: "sa1"}, 55 {Address: "10.172.0.2", ServiceAccount: "sa-vm1"}, 56 }, 57 expect: false, 58 }, 59 { 60 name: "deleted one endpoint with unique sa", 61 shardKey: c1Key, 62 endpoints: []*IstioEndpoint{ 63 {Address: "10.172.0.1", ServiceAccount: "sa1"}, 64 }, 65 expect: true, 66 }, 67 { 68 name: "deleted one endpoint with duplicate sa", 69 shardKey: c1Key, 70 endpoints: []*IstioEndpoint{ 71 {Address: "10.172.0.2", ServiceAccount: "sa-vm1"}, 72 }, 73 expect: false, 74 }, 75 { 76 name: "deleted endpoints", 77 shardKey: c1Key, 78 endpoints: nil, 79 expect: true, 80 }, 81 } 82 83 for _, tc := range testCases { 84 t.Run(tc.name, func(t *testing.T) { 85 originalEndpointsShard := &EndpointShards{ 86 Shards: map[ShardKey][]*IstioEndpoint{ 87 c1Key: cluster1Endppoints, 88 c2Key: {{Address: "10.244.0.1", ServiceAccount: "sa1"}, {Address: "10.244.0.2", ServiceAccount: "sa-vm2"}}, 89 }, 90 ServiceAccounts: map[string]struct{}{ 91 "sa1": {}, 92 "sa-vm1": {}, 93 "sa-vm2": {}, 94 }, 95 } 96 originalEndpointsShard.Shards[tc.shardKey] = tc.endpoints 97 ret := updateShardServiceAccount(originalEndpointsShard, "test-svc") 98 if ret != tc.expect { 99 t.Errorf("expect UpdateServiceAccount %v, but got %v", tc.expect, ret) 100 } 101 }) 102 } 103 }