k8s.io/apiserver@v0.31.1/pkg/cel/mutation/mock_test.go (about) 1 /* 2 Copyright 2024 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package mutation 18 19 import ( 20 "strings" 21 22 "github.com/google/cel-go/common/types" 23 "github.com/google/cel-go/common/types/ref" 24 25 "k8s.io/apiserver/pkg/cel/mutation/common" 26 ) 27 28 // mockTypeResolver is a mock implementation of TypeResolver that 29 // allows the object to contain any field. 30 type mockTypeResolver struct { 31 } 32 33 // mockTypeRef is a mock implementation of TypeRef that 34 // contains any field. 35 type mockTypeRef struct { 36 objectType *types.Type 37 resolver common.TypeResolver 38 } 39 40 func newMockTypeRef(resolver common.TypeResolver, name string) *mockTypeRef { 41 objectType := types.NewObjectType(name, common.ObjectTraits) 42 return &mockTypeRef{ 43 objectType: objectType, 44 resolver: resolver, 45 } 46 } 47 48 func (m *mockTypeRef) HasTrait(trait int) bool { 49 return common.ObjectTraits|trait != 0 50 } 51 52 func (m *mockTypeRef) TypeName() string { 53 return m.objectType.TypeName() 54 } 55 56 func (m *mockTypeRef) CELType() *types.Type { 57 return types.NewTypeTypeWithParam(m.objectType) 58 } 59 60 func (m *mockTypeRef) Field(name string) (*types.FieldType, bool) { 61 return &types.FieldType{ 62 Type: types.DynType, 63 IsSet: func(target any) bool { 64 return true 65 }, 66 GetFrom: func(target any) (any, error) { 67 return nil, nil 68 }, 69 }, true 70 } 71 72 func (m *mockTypeRef) Val(fields map[string]ref.Val) ref.Val { 73 return common.NewObjectVal(m, fields) 74 } 75 76 func (m *mockTypeResolver) Resolve(name string) (common.TypeRef, bool) { 77 if strings.HasPrefix(name, common.RootTypeReferenceName) { 78 return newMockTypeRef(m, name), true 79 } 80 return nil, false 81 } 82 83 // mockTypeResolverForOptional behaves the same as mockTypeResolver 84 // except returning a mockTypeRefForOptional instead of mockTypeRef 85 type mockTypeResolverForOptional struct { 86 *mockTypeResolver 87 } 88 89 // mockTypeRefForOptional behaves the same as the underlying TypeRef 90 // except treating "nonExisting" field as non-existing. 91 // This is used for optional tests. 92 type mockTypeRefForOptional struct { 93 common.TypeRef 94 } 95 96 // Field returns a mock FieldType, or false if the field should not exist. 97 func (m *mockTypeRefForOptional) Field(name string) (*types.FieldType, bool) { 98 if name == "nonExisting" { 99 return nil, false 100 } 101 return m.TypeRef.Field(name) 102 } 103 104 func (m *mockTypeResolverForOptional) Resolve(name string) (common.TypeRef, bool) { 105 r, ok := m.mockTypeResolver.Resolve(name) 106 if ok { 107 return &mockTypeRefForOptional{TypeRef: r}, ok 108 } 109 return nil, false 110 }