| 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 common |
| 18 |
|
| 19 |
import ( |
| 20 |
"github.com/google/cel-go/cel" |
| 21 |
"github.com/google/cel-go/common/types" |
| 22 |
"github.com/google/cel-go/common/types/ref" |
| 23 |
"reflect" |
| 24 |
"strings" |
| 25 |
"testing" |
| 26 |
|
| 27 |
"k8s.io/apimachinery/pkg/util/version" |
| 28 |
"k8s.io/apiserver/pkg/cel/environment" |
| 29 |
"k8s.io/apiserver/pkg/cel/mutation/dynamic" |
| 30 |
) |
| 31 |
|
| 32 |
func TestTypeProvider(t *testing.T) { |
| 33 |
for _, tc := range []struct { |
| 34 |
name string |
| 35 |
expression string |
| 36 |
expectedValue any |
| 37 |
expectCompileError string |
| 38 |
}{ |
| 39 |
{ |
| 40 |
name: "not an object", |
| 41 |
expression: `2 * 31 * 1847`, |
| 42 |
expectedValue: int64(114514), // type resolver should not interfere. |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "empty", |
| 46 |
expression: "Test{}", |
| 47 |
expectedValue: map[string]any{}, |
| 48 |
}, |
| 49 |
{ |
| 50 |
name: "simple", |
| 51 |
expression: "Test{x: 1}", |
| 52 |
expectedValue: map[string]any{"x": int64(1)}, |
| 53 |
}, |
| 54 |
{ |
| 55 |
name: "invalid type", |
| 56 |
expression: "Objectfoo{}", |
| 57 |
expectCompileError: "undeclared reference to 'Objectfoo'", |
| 58 |
}, |
| 59 |
} { |
| 60 |
t.Run(tc.name, func(t *testing.T) { |
| 61 |
_, option := NewResolverTypeProviderAndEnvOption(&mockTypeResolver{}) |
| 62 |
env := mustCreateEnv(t, option) |
| 63 |
ast, issues := env.Compile(tc.expression) |
| 64 |
if len(tc.expectCompileError) > 0 { |
| 65 |
if issues == nil { |
| 66 |
t.Fatalf("expected error %v but got no error", tc.expectCompileError) |
| 67 |
} |
| 68 |
if !strings.Contains(issues.String(), tc.expectCompileError) { |
| 69 |
t.Fatalf("expected error %v but got %v", tc.expectCompileError, issues.String()) |
| 70 |
} |
| 71 |
return |
| 72 |
} |
| 73 |
if issues != nil { |
| 74 |
t.Fatalf("unexpected issues during compilation: %v", issues) |
| 75 |
} |
| 76 |
program, err := env.Program(ast) |
| 77 |
if err != nil { |
| 78 |
t.Fatalf("unexpected error while creating program: %v", err) |
| 79 |
} |
| 80 |
r, _, err := program.Eval(map[string]any{}) |
| 81 |
if err != nil { |
| 82 |
t.Fatalf("unexpected error during evaluation: %v", err) |
| 83 |
} |
| 84 |
if v := r.Value(); !reflect.DeepEqual(tc.expectedValue, v) { |
| 85 |
t.Errorf("expected %v but got %v", tc.expectedValue, v) |
| 86 |
} |
| 87 |
}) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// mockTypeResolver is a mock implementation of DynamicTypeResolver that |
| 92 |
// allows the object to contain any field. |
| 93 |
type mockTypeResolver struct { |
| 94 |
} |
| 95 |
|
| 96 |
func (m *mockTypeResolver) Resolve(name string) (ResolvedType, bool) { |
| 97 |
if name == "Test" { |
| 98 |
return newMockResolvedType(m, name), true |
| 99 |
} |
| 100 |
return nil, false |
| 101 |
} |
| 102 |
|
| 103 |
// mockResolvedType is a mock implementation of ResolvedType that |
| 104 |
// contains any field. |
| 105 |
type mockResolvedType struct { |
| 106 |
objectType *types.Type |
| 107 |
resolver TypeResolver |
| 108 |
} |
| 109 |
|
| 110 |
func newMockResolvedType(resolver TypeResolver, name string) *mockResolvedType { |
| 111 |
objectType := types.NewObjectType(name) |
| 112 |
return &mockResolvedType{ |
| 113 |
objectType: objectType, |
| 114 |
resolver: resolver, |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
func (m *mockResolvedType) HasTrait(trait int) bool { |
| 119 |
return m.objectType.HasTrait(trait) |
| 120 |
} |
| 121 |
|
| 122 |
func (m *mockResolvedType) TypeName() string { |
| 123 |
return m.objectType.TypeName() |
| 124 |
} |
| 125 |
|
| 126 |
func (m *mockResolvedType) Type() *types.Type { |
| 127 |
return m.objectType |
| 128 |
} |
| 129 |
|
| 130 |
func (m *mockResolvedType) TypeType() *types.Type { |
| 131 |
return types.NewTypeTypeWithParam(m.objectType) |
| 132 |
} |
| 133 |
|
| 134 |
func (m *mockResolvedType) Field(name string) (*types.FieldType, bool) { |
| 135 |
return &types.FieldType{ |
| 136 |
Type: types.DynType, |
| 137 |
IsSet: func(target any) bool { |
| 138 |
return true |
| 139 |
}, |
| 140 |
GetFrom: func(target any) (any, error) { |
| 141 |
return nil, nil |
| 142 |
}, |
| 143 |
}, true |
| 144 |
} |
| 145 |
|
| 146 |
func (m *mockResolvedType) FieldNames() ([]string, bool) { |
| 147 |
return nil, true |
| 148 |
} |
| 149 |
|
| 150 |
func (m *mockResolvedType) Val(fields map[string]ref.Val) ref.Val { |
| 151 |
return dynamic.NewObjectVal(m.objectType, fields) |
| 152 |
} |
| 153 |
|
| 154 |
// mustCreateEnv creates the default env for testing, with given option. |
| 155 |
// it fatally fails the test if the env fails to set up. |
| 156 |
func mustCreateEnv(t testing.TB, envOptions ...cel.EnvOption) *cel.Env { |
| 157 |
envSet, err := environment.MustBaseEnvSet(environment.DefaultCompatibilityVersion(), true). |
| 158 |
Extend(environment.VersionedOptions{ |
| 159 |
IntroducedVersion: version.MajorMinor(1, 30), |
| 160 |
EnvOptions: envOptions, |
| 161 |
}) |
| 162 |
if err != nil { |
| 163 |
t.Fatalf("fail to create env set: %v", err) |
| 164 |
} |
| 165 |
env, err := envSet.Env(environment.StoredExpressions) |
| 166 |
if err != nil { |
| 167 |
t.Fatalf("fail to setup env: %v", env) |
| 168 |
} |
| 169 |
return env |
| 170 |
} |