Debian Codegraph @

pkg/cel/mutation/dynamic/objects_test.go

raw
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 dynamic
18
19 import (
20 "reflect"
21 "strings"
22 "testing"
23
24 "github.com/google/cel-go/common/types"
25 "github.com/google/cel-go/common/types/ref"
26 )
27
28 func TestOptional(t *testing.T) {
29 for _, tc := range []struct {
30 name string
31 fields map[string]ref.Val
32 expected map[string]any
33 }{
34 {
35 name: "present",
36 fields: map[string]ref.Val{
37 "zero": types.OptionalOf(types.IntZero),
38 },
39 expected: map[string]any{
40 "zero": int64(0),
41 },
42 },
43 {
44 name: "none",
45 fields: map[string]ref.Val{
46 "absent": types.OptionalNone,
47 },
48 expected: map[string]any{
49 // right now no way to differ from a plain null.
50 // we will need to filter out optional.none() before this conversion.
51 "absent": nil,
52 },
53 },
54 } {
55 t.Run(tc.name, func(t *testing.T) {
56 v := &ObjectVal{
57 objectType: nil, // safe in this test, otherwise put a mock
58 fields: tc.fields,
59 }
60 converted := v.Value()
61 if !reflect.DeepEqual(tc.expected, converted) {
62 t.Errorf("wrong result, expected %v but got %v", tc.expected, converted)
63 }
64 })
65 }
66 }
67
68 func TestCheckTypeNamesMatchFieldPathNames(t *testing.T) {
69 for _, tc := range []struct {
70 name string
71 obj *ObjectVal
72 expectError string
73 }{
74 {
75 name: "valid",
76 obj: &ObjectVal{
77 objectType: types.NewObjectType("Object"),
78 fields: map[string]ref.Val{
79 "spec": &ObjectVal{
80 objectType: types.NewObjectType("Object.spec"),
81 fields: map[string]ref.Val{
82 "replicas": types.Int(100),
83 "m": types.NewRefValMap(nil, map[ref.Val]ref.Val{
84 types.String("k1"): &ObjectVal{
85 objectType: types.NewObjectType("Object.spec.m"),
86 },
87 }),
88 "l": types.NewRefValList(nil, []ref.Val{
89 &ObjectVal{
90 objectType: types.NewObjectType("Object.spec.l"),
91 },
92 }),
93 },
94 },
95 },
96 },
97 },
98 {
99 name: "invalid struct field",
100 obj: &ObjectVal{
101 objectType: types.NewObjectType("Object"),
102 fields: map[string]ref.Val{"invalid": &ObjectVal{
103 objectType: types.NewObjectType("Object.spec"),
104 fields: map[string]ref.Val{"replicas": types.Int(100)},
105 }},
106 },
107 expectError: "unexpected type name \"Object.spec\", expected \"Object.invalid\"",
108 },
109 {
110 name: "invalid map field",
111 obj: &ObjectVal{
112 objectType: types.NewObjectType("Object"),
113 fields: map[string]ref.Val{
114 "spec": &ObjectVal{
115 objectType: types.NewObjectType("Object.spec"),
116 fields: map[string]ref.Val{
117 "replicas": types.Int(100),
118 "m": types.NewRefValMap(nil, map[ref.Val]ref.Val{
119 types.String("k1"): &ObjectVal{
120 objectType: types.NewObjectType("Object.spec.invalid"),
121 },
122 }),
123 },
124 },
125 },
126 },
127 expectError: "unexpected type name \"Object.spec.invalid\", expected \"Object.spec.m\"",
128 },
129 {
130 name: "invalid list field",
131 obj: &ObjectVal{
132 objectType: types.NewObjectType("Object"),
133 fields: map[string]ref.Val{
134 "spec": &ObjectVal{
135 objectType: types.NewObjectType("Object.spec"),
136 fields: map[string]ref.Val{
137 "replicas": types.Int(100),
138 "l": types.NewRefValList(nil, []ref.Val{
139 &ObjectVal{
140 objectType: types.NewObjectType("Object.spec.invalid"),
141 },
142 }),
143 },
144 },
145 },
146 },
147 expectError: "unexpected type name \"Object.spec.invalid\", expected \"Object.spec.l\"",
148 },
149 } {
150 t.Run(tc.name, func(t *testing.T) {
151 err := tc.obj.CheckTypeNamesMatchFieldPathNames()
152 if tc.expectError == "" {
153 if err != nil {
154 t.Errorf("unexpected error: %v", err)
155 }
156 } else {
157 if err == nil {
158 t.Errorf("expected error")
159 }
160 if !strings.Contains(err.Error(), tc.expectError) {
161 t.Errorf("expected error to contain %v, got %v", tc.expectError, err)
162 }
163 }
164 })
165 }
166 }