istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/host/names_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 host_test 16 17 import ( 18 "fmt" 19 "reflect" 20 "sort" 21 "testing" 22 23 "istio.io/istio/pkg/config/host" 24 ) 25 26 func TestNamesIntersection(t *testing.T) { 27 tests := []struct { 28 a, b, intersection host.Names 29 }{ 30 { 31 host.Names{"foo,com"}, 32 host.Names{"bar.com"}, 33 host.Names{}, 34 }, 35 { 36 host.Names{"foo.com", "bar.com"}, 37 host.Names{"bar.com"}, 38 host.Names{"bar.com"}, 39 }, 40 { 41 host.Names{"foo.com", "bar.com"}, 42 host.Names{"*.com"}, 43 host.Names{"foo.com", "bar.com"}, 44 }, 45 { 46 host.Names{"*.com"}, 47 host.Names{"foo.com", "bar.com"}, 48 host.Names{"foo.com", "bar.com"}, 49 }, 50 { 51 host.Names{"foo.com", "*.net"}, 52 host.Names{"*.com", "bar.net"}, 53 host.Names{"foo.com", "bar.net"}, 54 }, 55 { 56 host.Names{"foo.com", "*.net"}, 57 host.Names{"*.bar.net"}, 58 host.Names{"*.bar.net"}, 59 }, 60 { 61 host.Names{"foo.com", "bar.net"}, 62 host.Names{"*"}, 63 host.Names{"foo.com", "bar.net"}, 64 }, 65 { 66 host.Names{"foo.com"}, 67 host.Names{}, 68 host.Names{}, 69 }, 70 { 71 host.Names{}, 72 host.Names{"bar.com"}, 73 host.Names{}, 74 }, 75 { 76 host.Names{"*", "foo.com"}, 77 host.Names{"foo.com"}, 78 host.Names{"foo.com"}, 79 }, 80 { 81 host.Names{"*"}, 82 host.Names{"foo.com"}, 83 host.Names{"foo.com"}, 84 }, 85 { 86 host.Names{"foo.com"}, 87 host.Names{"Foo.com"}, 88 host.Names{}, 89 }, 90 } 91 92 for idx, tt := range tests { 93 t.Run(fmt.Sprintf("%d", idx), func(t *testing.T) { 94 result := tt.a.Intersection(tt.b) 95 if !reflect.DeepEqual(result, tt.intersection) { 96 t.Fatalf("%v.Intersection(%v) = %v, want %v", tt.a, tt.b, result, tt.intersection) 97 } 98 }) 99 } 100 } 101 102 func TestNamesForNamespace(t *testing.T) { 103 tests := []struct { 104 hosts []string 105 namespace string 106 want host.Names 107 }{ 108 { 109 []string{"ns1/foo.com", "ns2/bar.com"}, 110 "ns1", 111 host.Names{"foo.com"}, 112 }, 113 { 114 []string{"ns1/foo.com", "ns2/bar.com"}, 115 "ns3", 116 host.Names{}, 117 }, 118 { 119 []string{"ns1/foo.com", "*/bar.com"}, 120 "ns1", 121 host.Names{"foo.com", "bar.com"}, 122 }, 123 { 124 []string{"ns1/foo.com", "*/bar.com"}, 125 "ns3", 126 host.Names{"bar.com"}, 127 }, 128 { 129 []string{"foo.com", "ns2/bar.com"}, 130 "ns2", 131 host.Names{"foo.com", "bar.com"}, 132 }, 133 { 134 []string{"foo.com", "ns2/bar.com"}, 135 "ns3", 136 host.Names{"foo.com"}, 137 }, 138 } 139 140 for idx, tt := range tests { 141 t.Run(fmt.Sprintf("%d", idx), func(t *testing.T) { 142 result := host.NamesForNamespace(tt.hosts, tt.namespace) 143 if !reflect.DeepEqual(result, tt.want) { 144 t.Fatalf("host.NamesForNamespace(%v, %v) = %v, want %v", tt.hosts, tt.namespace, result, tt.want) 145 } 146 }) 147 } 148 } 149 150 func TestNamesSortOrder(t *testing.T) { 151 tests := []struct { 152 in, want host.Names 153 }{ 154 // Prove we sort alphabetically: 155 { 156 host.Names{"b", "a"}, 157 host.Names{"a", "b"}, 158 }, 159 { 160 host.Names{"bb", "cc", "aa"}, 161 host.Names{"aa", "bb", "cc"}, 162 }, 163 // Prove we sort longest first, alphabetically: 164 { 165 host.Names{"b", "a", "aa"}, 166 host.Names{"aa", "a", "b"}, 167 }, 168 { 169 host.Names{"foo.com", "bar.com", "foo.bar.com"}, 170 host.Names{"foo.bar.com", "bar.com", "foo.com"}, 171 }, 172 // We sort wildcards last, always 173 { 174 host.Names{"a", "*", "z"}, 175 host.Names{"a", "z", "*"}, 176 }, 177 { 178 host.Names{"foo.com", "bar.com", "*.com"}, 179 host.Names{"bar.com", "foo.com", "*.com"}, 180 }, 181 { 182 host.Names{"foo.com", "bar.com", "*.com", "*.foo.com", "*", "baz.bar.com"}, 183 host.Names{"baz.bar.com", "bar.com", "foo.com", "*.foo.com", "*.com", "*"}, 184 }, 185 } 186 187 for idx, tt := range tests { 188 t.Run(fmt.Sprintf("%d", idx), func(t *testing.T) { 189 // Save a copy to report errors with 190 tmp := make(host.Names, len(tt.in)) 191 copy(tmp, tt.in) 192 193 sort.Sort(tt.in) 194 if !reflect.DeepEqual(tt.in, tt.want) { 195 t.Fatalf("sort.Sort(%v) = %v, want %v", tmp, tt.in, tt.want) 196 } 197 }) 198 } 199 } 200 201 func BenchmarkNamesSort(b *testing.B) { 202 unsorted := host.Names{"foo.com", "bar.com", "*.com", "*.foo.com", "*", "baz.bar.com"} 203 204 for n := 0; n < b.N; n++ { 205 given := make(host.Names, len(unsorted)) 206 copy(given, unsorted) 207 sort.Sort(given) 208 } 209 }