istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/model/addressmap_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_test 16 17 import ( 18 "testing" 19 20 . "github.com/onsi/gomega" 21 22 "istio.io/istio/pilot/pkg/model" 23 "istio.io/istio/pkg/cluster" 24 ) 25 26 const ( 27 c1ID = "cluster-1" 28 c2ID = "cluster-2" 29 ) 30 31 var ( 32 c1Addresses = []string{"1.1.1.1", "1.1.1.2"} 33 c2Addresses = []string{"2.1.1.1", "2.1.1.2"} 34 ) 35 36 func TestAddressMapLen(t *testing.T) { 37 cases := []struct { 38 name string 39 newMap func() *model.AddressMap 40 expected int 41 }{ 42 { 43 name: "nil addresses map", 44 newMap: func() *model.AddressMap { 45 return nil 46 }, 47 expected: 0, 48 }, 49 { 50 name: "empty addresses map", 51 newMap: func() *model.AddressMap { 52 m := model.AddressMap{} 53 m.AddAddressesFor(c1ID, make([]string, 0)) 54 return &m 55 }, 56 expected: 0, 57 }, 58 { 59 name: "non-empty addresses map", 60 newMap: func() *model.AddressMap { 61 m := model.AddressMap{} 62 m.AddAddressesFor(c1ID, c1Addresses) 63 return &m 64 }, 65 expected: 1, 66 }, 67 } 68 69 for _, c := range cases { 70 t.Run(c.name, func(t *testing.T) { 71 g := NewWithT(t) 72 g.Expect(c.newMap().Len()).To(Equal(c.expected)) 73 }) 74 } 75 } 76 77 func TestAddressMapGetAddressesFor(t *testing.T) { 78 g := NewWithT(t) 79 80 m := model.AddressMap{ 81 Addresses: map[cluster.ID][]string{ 82 c1ID: c1Addresses, 83 c2ID: c2Addresses, 84 }, 85 } 86 87 g.Expect(m.GetAddressesFor(c1ID)).To(Equal(c1Addresses)) 88 g.Expect(m.GetAddressesFor(c2ID)).To(Equal(c2Addresses)) 89 } 90 91 func TestAddressMapSetAddressesFor(t *testing.T) { 92 g := NewWithT(t) 93 94 m := model.AddressMap{} 95 m.SetAddressesFor(c1ID, c1Addresses) 96 m.SetAddressesFor(c2ID, c2Addresses) 97 98 g.Expect(m.GetAddressesFor(c1ID)).To(Equal(c1Addresses)) 99 g.Expect(m.GetAddressesFor(c2ID)).To(Equal(c2Addresses)) 100 } 101 102 func TestAddressMapAddAddressesFor(t *testing.T) { 103 g := NewWithT(t) 104 105 m := model.AddressMap{} 106 m.SetAddressesFor(c1ID, c1Addresses) 107 m.AddAddressesFor(c1ID, []string{"1.1.1.3", "1.1.1.4"}) 108 109 g.Expect(m.GetAddressesFor(c1ID)).To(Equal([]string{"1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4"})) 110 } 111 112 func TestAddressMapForEach(t *testing.T) { 113 g := NewWithT(t) 114 115 m := model.AddressMap{ 116 Addresses: map[cluster.ID][]string{ 117 c1ID: c1Addresses, 118 c2ID: c2Addresses, 119 }, 120 } 121 122 found := make(map[cluster.ID][]string) 123 m.ForEach(func(id cluster.ID, addrs []string) { 124 found[id] = addrs 125 }) 126 127 g.Expect(found[c1ID]).To(Equal(c1Addresses)) 128 g.Expect(found[c2ID]).To(Equal(c2Addresses)) 129 }