istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/util/yml/cache_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 yml 16 17 import ( 18 "os" 19 "path" 20 "strings" 21 "testing" 22 23 . "github.com/onsi/gomega" 24 ) 25 26 var ( 27 gateway = ` 28 apiVersion: networking.istio.io/v1alpha3 29 kind: Gateway 30 metadata: 31 name: some-ingress 32 spec: 33 servers: 34 - port: 35 number: 80 36 name: http 37 protocol: http 38 hosts: 39 - "*.example.com" 40 ` 41 42 updatedGateway = ` 43 apiVersion: networking.istio.io/v1alpha3 44 kind: Gateway 45 metadata: 46 name: some-ingress 47 spec: 48 servers: 49 - port: 50 number: 8080 51 name: https 52 protocol: https 53 hosts: 54 - "*.example.com" 55 ` 56 57 virtualService = ` 58 apiVersion: networking.istio.io/v1alpha3 59 kind: VirtualService 60 metadata: 61 name: route-for-myapp 62 spec: 63 hosts: 64 - some.example.com 65 gateways: 66 - some-ingress 67 http: 68 - route: 69 - destination: 70 host: some.example.internal 71 ` 72 ) 73 74 func TestCache_Apply_Basic(t *testing.T) { 75 g := NewWithT(t) 76 d := t.TempDir() 77 t.Logf("Test Dir: %q", d) 78 79 c := NewCache(d) 80 81 keys, err := c.Apply(gateway) 82 g.Expect(err).To(BeNil()) 83 g.Expect(keys).To(HaveLen(1)) 84 expected := CacheKey{ 85 group: "networking.istio.io", 86 kind: "Gateway", 87 namespace: "", 88 name: "some-ingress", 89 } 90 g.Expect(keys[0]).To(Equal(expected)) 91 key1 := keys[0] 92 93 file := c.GetFileFor(keys[0]) 94 by, err := os.ReadFile(file) 95 g.Expect(err).To(BeNil()) 96 g.Expect(strings.TrimSpace(string(by))).To(Equal(strings.TrimSpace(gateway))) 97 98 keys, err = c.Apply(virtualService) 99 g.Expect(err).To(BeNil()) 100 g.Expect(keys).To(HaveLen(1)) 101 expected = CacheKey{ 102 group: "networking.istio.io", 103 kind: "VirtualService", 104 namespace: "", 105 name: "route-for-myapp", 106 } 107 g.Expect(keys[0]).To(Equal(expected)) 108 key2 := keys[0] 109 110 file = c.GetFileFor(keys[0]) 111 by, err = os.ReadFile(file) 112 g.Expect(err).To(BeNil()) 113 g.Expect(strings.TrimSpace(string(by))).To(Equal(strings.TrimSpace(virtualService))) 114 115 keys = c.AllKeys() 116 g.Expect(keys).To(HaveLen(2)) 117 g.Expect(keys).To(ContainElement(key1)) 118 g.Expect(keys).To(ContainElement(key2)) 119 120 items, err := os.ReadDir(d) 121 g.Expect(err).To(BeNil()) 122 g.Expect(items).To(HaveLen(2)) 123 } 124 125 func TestCache_Apply_MultiPart(t *testing.T) { 126 g := NewWithT(t) 127 d := t.TempDir() 128 t.Logf("Test Dir: %q", d) 129 130 c := NewCache(d) 131 132 keys, err := c.Apply(JoinString(gateway, virtualService)) 133 g.Expect(err).To(BeNil()) 134 g.Expect(keys).To(HaveLen(2)) 135 expected := CacheKey{ 136 group: "networking.istio.io", 137 kind: "Gateway", 138 namespace: "", 139 name: "some-ingress", 140 } 141 g.Expect(keys[0]).To(Equal(expected)) 142 143 file := c.GetFileFor(keys[0]) 144 by, err := os.ReadFile(file) 145 g.Expect(err).To(BeNil()) 146 g.Expect(strings.TrimSpace(string(by))).To(Equal(strings.TrimSpace(gateway))) 147 148 expected = CacheKey{ 149 group: "networking.istio.io", 150 kind: "VirtualService", 151 namespace: "", 152 name: "route-for-myapp", 153 } 154 g.Expect(keys[1]).To(Equal(expected)) 155 156 file = c.GetFileFor(keys[1]) 157 by, err = os.ReadFile(file) 158 g.Expect(err).To(BeNil()) 159 g.Expect(strings.TrimSpace(string(by))).To(Equal(strings.TrimSpace(virtualService))) 160 161 applyKeys := keys 162 keys = c.AllKeys() 163 g.Expect(keys).To(HaveLen(2)) 164 g.Expect(keys).To(ContainElement(applyKeys[0])) 165 g.Expect(keys).To(ContainElement(applyKeys[1])) 166 167 items, err := os.ReadDir(d) 168 g.Expect(err).To(BeNil()) 169 g.Expect(items).To(HaveLen(2)) 170 } 171 172 func TestCache_Apply_Add_Update(t *testing.T) { 173 g := NewWithT(t) 174 d := t.TempDir() 175 t.Logf("Test Dir: %q", d) 176 177 c := NewCache(d) 178 179 keys, err := c.Apply(gateway) 180 g.Expect(err).To(BeNil()) 181 182 file := c.GetFileFor(keys[0]) 183 by, err := os.ReadFile(file) 184 g.Expect(err).To(BeNil()) 185 g.Expect(strings.TrimSpace(string(by))).To(Equal(strings.TrimSpace(gateway))) 186 187 keys, err = c.Apply(updatedGateway) 188 g.Expect(err).To(BeNil()) 189 file = c.GetFileFor(keys[0]) 190 by, err = os.ReadFile(file) 191 g.Expect(err).To(BeNil()) 192 g.Expect(strings.TrimSpace(string(by))).To(Equal(strings.TrimSpace(updatedGateway))) 193 194 applyKeys := keys 195 keys = c.AllKeys() 196 g.Expect(keys).To(HaveLen(1)) 197 g.Expect(keys).To(ContainElement(applyKeys[0])) 198 199 items, err := os.ReadDir(d) 200 g.Expect(err).To(BeNil()) 201 g.Expect(items).To(HaveLen(1)) 202 } 203 204 func TestCache_Apply_SameContent(t *testing.T) { 205 g := NewWithT(t) 206 d := t.TempDir() 207 t.Logf("Test Dir: %q", d) 208 209 c := NewCache(d) 210 211 keys1, err := c.Apply(gateway) 212 g.Expect(err).To(BeNil()) 213 214 keys2, err := c.Apply(gateway) 215 g.Expect(err).To(BeNil()) 216 217 keys := c.AllKeys() 218 g.Expect(keys).To(HaveLen(1)) 219 g.Expect(keys1).To(HaveLen(1)) 220 g.Expect(keys2).To(HaveLen(1)) 221 g.Expect(keys).To(ContainElement(keys1[0])) 222 g.Expect(keys).To(ContainElement(keys2[0])) 223 224 items, err := os.ReadDir(d) 225 g.Expect(err).To(BeNil()) 226 g.Expect(items).To(HaveLen(1)) 227 } 228 229 func TestCache_Clear(t *testing.T) { 230 g := NewWithT(t) 231 d := t.TempDir() 232 t.Logf("Test Dir: %q", d) 233 234 c := NewCache(d) 235 236 _, err := c.Apply(gateway) 237 g.Expect(err).To(BeNil()) 238 239 _, err = c.Apply(virtualService) 240 g.Expect(err).To(BeNil()) 241 242 err = c.Clear() 243 g.Expect(err).To(BeNil()) 244 245 keys := c.AllKeys() 246 g.Expect(keys).To(HaveLen(0)) 247 248 items, err := os.ReadDir(d) 249 g.Expect(err).To(BeNil()) 250 g.Expect(items).To(HaveLen(0)) 251 } 252 253 func TestCache_GetFileFor_Empty(t *testing.T) { 254 g := NewWithT(t) 255 d := t.TempDir() 256 t.Logf("Test Dir: %q", d) 257 258 c := NewCache(d) 259 260 f := c.GetFileFor(CacheKey{}) 261 g.Expect(f).To(BeEmpty()) 262 } 263 264 func TestCache_Delete(t *testing.T) { 265 g := NewWithT(t) 266 d := t.TempDir() 267 t.Logf("Test Dir: %q", d) 268 269 c := NewCache(d) 270 271 _, err := c.Apply(gateway) 272 g.Expect(err).To(BeNil()) 273 274 keys1, err := c.Apply(virtualService) 275 g.Expect(err).To(BeNil()) 276 277 err = c.Delete(gateway) 278 g.Expect(err).To(BeNil()) 279 280 keys := c.AllKeys() 281 g.Expect(keys).To(HaveLen(1)) 282 g.Expect(keys1).To(HaveLen(1)) 283 g.Expect(keys).To(ContainElement(keys1[0])) 284 285 items, err := os.ReadDir(d) 286 g.Expect(err).To(BeNil()) 287 g.Expect(items).To(HaveLen(1)) 288 289 by, err := os.ReadFile(path.Join(d, items[0].Name())) 290 g.Expect(err).To(BeNil()) 291 g.Expect(strings.TrimSpace(string(by))).To(Equal(strings.TrimSpace(virtualService))) 292 } 293 294 func TestCache_Delete_Missing(t *testing.T) { 295 g := NewWithT(t) 296 d := t.TempDir() 297 t.Logf("Test Dir: %q", d) 298 299 c := NewCache(d) 300 301 _, err := c.Apply(gateway) 302 g.Expect(err).To(BeNil()) 303 304 err = c.Delete(virtualService) 305 g.Expect(err).To(BeNil()) 306 307 items, err := os.ReadDir(d) 308 g.Expect(err).To(BeNil()) 309 g.Expect(items).To(HaveLen(1)) 310 311 by, err := os.ReadFile(path.Join(d, items[0].Name())) 312 g.Expect(err).To(BeNil()) 313 g.Expect(strings.TrimSpace(string(by))).To(Equal(strings.TrimSpace(gateway))) 314 }