istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/schema/collection/schemas_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 collection_test 16 17 import ( 18 "testing" 19 20 . "github.com/onsi/gomega" 21 _ "google.golang.org/protobuf/types/known/emptypb" 22 _ "google.golang.org/protobuf/types/known/structpb" 23 24 "istio.io/istio/pkg/config" 25 "istio.io/istio/pkg/config/schema/collection" 26 "istio.io/istio/pkg/config/schema/resource" 27 ) 28 29 var ( 30 emptyResource = resource.Builder{ 31 Kind: "Empty", 32 Plural: "empties", 33 ProtoPackage: "google.golang.org/protobuf/types/known/emptypb", 34 Proto: "google.protobuf.Empty", 35 }.MustBuild() 36 37 structResource = resource.Builder{ 38 Kind: "Struct", 39 Plural: "structs", 40 ProtoPackage: "google.golang.org/protobuf/types/known/structpb", 41 Proto: "google.protobuf.Struct", 42 }.MustBuild() 43 ) 44 45 func TestSchemas_Basic(t *testing.T) { 46 g := NewWithT(t) 47 48 schemas := collection.SchemasFor(emptyResource) 49 g.Expect(schemas.All()).To(HaveLen(1)) 50 g.Expect(schemas.All()[0]).To(Equal(emptyResource)) 51 } 52 53 func TestSchemas_MustAdd(t *testing.T) { 54 g := NewWithT(t) 55 defer func() { 56 r := recover() 57 g.Expect(r).To(BeNil()) 58 }() 59 b := collection.NewSchemasBuilder() 60 61 b.MustAdd(emptyResource) 62 } 63 64 func TestSchemas_MustRegister_Panic(t *testing.T) { 65 g := NewWithT(t) 66 defer func() { 67 r := recover() 68 g.Expect(r).NotTo(BeNil()) 69 }() 70 b := collection.NewSchemasBuilder() 71 72 b.MustAdd(emptyResource) 73 b.MustAdd(emptyResource) 74 } 75 76 func TestSchema_FindByGroupVersionKind(t *testing.T) { 77 g := NewWithT(t) 78 79 s := resource.Builder{ 80 ProtoPackage: "github.com/gogo/protobuf/types", 81 Proto: "google.protobuf.Empty", 82 Group: "mygroup", 83 Kind: "Empty", 84 Plural: "empties", 85 Version: "v1", 86 }.MustBuild() 87 88 schemas := collection.SchemasFor(s) 89 90 s2, found := schemas.FindByGroupVersionKind(config.GroupVersionKind{ 91 Group: "mygroup", 92 Version: "v1", 93 Kind: "Empty", 94 }) 95 g.Expect(found).To(BeTrue()) 96 g.Expect(s2).To(Equal(s)) 97 98 _, found = schemas.FindByGroupVersionKind(config.GroupVersionKind{ 99 Group: "fake", 100 Version: "v1", 101 Kind: "Empty", 102 }) 103 g.Expect(found).To(BeFalse()) 104 } 105 106 func TestSchemas_Kinds(t *testing.T) { 107 g := NewWithT(t) 108 109 s := collection.SchemasFor(emptyResource, structResource) 110 111 actual := s.Kinds() 112 expected := []string{emptyResource.Kind(), structResource.Kind()} 113 g.Expect(actual).To(Equal(expected)) 114 } 115 116 func TestSchemas_Validate(t *testing.T) { 117 cases := []struct { 118 name string 119 schemas []resource.Schema 120 expectError bool 121 }{ 122 { 123 name: "valid", 124 schemas: []resource.Schema{ 125 resource.Builder{ 126 Kind: "Empty1", 127 Plural: "Empty1s", 128 Proto: "google.protobuf.Empty", 129 }.MustBuild(), 130 resource.Builder{ 131 Kind: "Empty2", 132 Plural: "Empty2s", 133 Proto: "google.protobuf.Empty", 134 }.MustBuild(), 135 }, 136 expectError: false, 137 }, 138 } 139 140 for _, c := range cases { 141 t.Run(c.name, func(t *testing.T) { 142 g := NewWithT(t) 143 b := collection.NewSchemasBuilder() 144 for _, s := range c.schemas { 145 b.MustAdd(s) 146 } 147 err := b.Build().Validate() 148 if c.expectError { 149 g.Expect(err).ToNot(BeNil()) 150 } else { 151 g.Expect(err).To(BeNil()) 152 } 153 }) 154 } 155 } 156 157 func TestSchemas_Validate_Error(t *testing.T) { 158 g := NewWithT(t) 159 b := collection.NewSchemasBuilder() 160 161 s1 := resource.Builder{ 162 Kind: "Zoo", 163 ProtoPackage: "github.com/gogo/protobuf/types", 164 Proto: "zoo", 165 }.BuildNoValidate() 166 b.MustAdd(s1) 167 168 err := b.Build().Validate() 169 g.Expect(err).NotTo(BeNil()) 170 } 171 172 func TestSchemas_ForEach(t *testing.T) { 173 schemas := collection.SchemasFor(emptyResource, structResource) 174 175 cases := []struct { 176 name string 177 expected []string 178 actual func() []string 179 }{ 180 { 181 name: "all", 182 expected: []string{"Empty", "Struct"}, 183 actual: func() []string { 184 a := make([]string, 0) 185 schemas.ForEach(func(s resource.Schema) bool { 186 a = append(a, s.Kind()) 187 return false 188 }) 189 return a 190 }, 191 }, 192 { 193 name: "exit early", 194 expected: []string{"Empty"}, 195 actual: func() []string { 196 a := make([]string, 0) 197 schemas.ForEach(func(s resource.Schema) bool { 198 a = append(a, s.Kind()) 199 return true 200 }) 201 return a 202 }, 203 }, 204 } 205 206 for _, c := range cases { 207 t.Run(c.name, func(t *testing.T) { 208 g := NewWithT(t) 209 actual := c.actual() 210 g.Expect(actual).To(Equal(c.expected)) 211 }) 212 } 213 } 214 215 func TestSchemas_Remove(t *testing.T) { 216 g := NewWithT(t) 217 218 schemas := collection.SchemasFor(emptyResource, structResource) 219 g.Expect(schemas.Remove(structResource)).To(Equal(collection.SchemasFor(emptyResource))) 220 g.Expect(schemas.Remove(emptyResource, structResource)).To(Equal(collection.SchemasFor())) 221 g.Expect(schemas).To(Equal(collection.SchemasFor(emptyResource, structResource))) 222 } 223 224 func TestSchemas_Add(t *testing.T) { 225 g := NewWithT(t) 226 227 schemas := collection.SchemasFor(emptyResource) 228 g.Expect(schemas.Add(structResource)).To(Equal(collection.SchemasFor(emptyResource, structResource))) 229 g.Expect(schemas).To(Equal(collection.SchemasFor(emptyResource))) 230 }