Debian Codegraph @

policy/config_test.go

raw symbols SCIP
1 // Copyright 2024 Google LLC
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 // https://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 policy
16
17 import (
18 "testing"
19
20 "github.com/google/cel-go/cel"
21 "github.com/google/cel-go/common/env"
22
23 "go.yaml.in/yaml/v3"
24
25 proto3pb "github.com/google/cel-go/test/proto3pb"
26 )
27
28 func TestConfig(t *testing.T) {
29 tests := []string{
30 `name: hello`,
31 `description: empty`,
32 `container: pb.pkg`,
33 `
34 extensions:
35 - name: "bindings"
36 - name: "encoders"
37 - name: "lists"
38 - name: "math"
39 - name: "optional"
40 - name: "protos"
41 - name: "sets"
42 - name: "strings"
43 version: 1`,
44 `
45 functions:
46 - name: "coalesce"
47 overloads:
48 - id: "null_coalesce_int"
49 target:
50 type_name: "null_type"
51 args:
52 - type_name: "int"
53 return:
54 type_name: "int"
55 - id: "coalesce_null_int"
56 args:
57 - type_name: "null_type"
58 - type_name: "int"
59 return:
60 type_name: "int"
61 - id: "int_coalesce_int"
62 target:
63 type_name: "int"
64 args:
65 - type_name: "int"
66 return:
67 type_name: "int"
68 - id: "optional_T_coalesce_T"
69 target:
70 type_name: "optional_type"
71 params:
72 - type_name: "T"
73 is_type_param: true
74 args:
75 - type_name: "T"
76 is_type_param: true
77 return:
78 type_name: "T"
79 is_type_param: true
80 `,
81 `
82 variables:
83 - name: "request"
84 type:
85 type_name: "map"
86 params:
87 - type_name: "string"
88 - type_name: "dyn"
89 `,
90 `
91 variables:
92 - name: "request"
93 type:
94 type_name: "google.expr.proto3.test.TestAllTypes"
95 `,
96 }
97 baseEnv, err := cel.NewEnv(
98 cel.OptionalTypes(),
99 cel.Types(&proto3pb.TestAllTypes{}),
100 )
101 if err != nil {
102 t.Fatalf("cel.NewEnv() failed: %v", err)
103 }
104 for _, tst := range tests {
105 c := parseConfigYaml(t, tst)
106 _, err := baseEnv.Extend(FromConfig(c))
107 if err != nil {
108 t.Errorf("AsEnvOptions() generated error: %v", err)
109 }
110 }
111 }
112
113 func TestConfigErrors(t *testing.T) {
114 tests := []struct {
115 config string
116 err string
117 }{
118 {
119 config: `
120 extensions:
121 - name: "bad_name"`,
122 err: "unrecognized extension: bad_name",
123 },
124 {
125 config: `
126 variables:
127 - name: "bad_type"
128 type:
129 type_name: "strings"`,
130 err: `invalid variable "bad_type": undefined type name: "strings"`,
131 },
132 {
133 config: `
134 variables:
135 - name: "bad_list"
136 type:
137 type_name: "list"`,
138 err: `invalid variable "bad_list": invalid type: list expects 1 parameter, got 0`,
139 },
140 {
141 config: `
142 variables:
143 - name: "bad_map"
144 type:
145 type_name: "map"
146 params:
147 - type_name: "string"`,
148 err: `invalid variable "bad_map": invalid type: map expects 2 parameters, got 1`,
149 },
150 {
151 config: `
152 variables:
153 - name: "bad_list_type_param"
154 type:
155 type_name: "list"
156 params:
157 - type_name: "number"`,
158 err: `invalid variable "bad_list_type_param": undefined type name: "number"`,
159 },
160 {
161 config: `
162 variables:
163 - name: "bad_map_type_param"
164 type:
165 type_name: "map"
166 params:
167 - type_name: "string"
168 - type_name: "invalid_opaque_type"`,
169 err: `invalid variable "bad_map_type_param": undefined type name: "invalid_opaque_type"`,
170 },
171 {
172 config: `
173 context_variable:
174 type_name: "bad.proto.MessageType"
175 `,
176 err: `invalid context proto type: "bad.proto.MessageType"`,
177 },
178 {
179 config: `
180 variables:
181 - type:
182 type_name: "no variable name"`,
183 err: "invalid variable: missing variable name",
184 },
185
186 {
187 config: `
188 functions:
189 - name: "bad_return"
190 overloads:
191 - id: "zero_arity"
192 return:
193 type_name: "mystery"`,
194 err: `invalid function "bad_return": undefined type name: "mystery"`,
195 },
196 {
197 config: `
198 functions:
199 - name: "bad_target"
200 overloads:
201 - id: "unary_member"
202 target:
203 type_name: "unknown"
204 return:
205 type_name: "null_type"`,
206 err: `invalid function "bad_target": undefined type name: "unknown"`,
207 },
208 {
209 config: `
210 functions:
211 - name: "bad_arg"
212 overloads:
213 - id: "unary_global"
214 args:
215 - type_name: "unknown"
216 return:
217 type_name: "null_type"`,
218 err: `invalid function "bad_arg": undefined type name: "unknown"`,
219 },
220 {
221 config: `
222 functions:
223 - name: "missing_return"
224 overloads:
225 - id: "unary_global"
226 args:
227 - type_name: "null_type"`,
228 err: `invalid function "missing_return": invalid overload "unary_global" return: invalid type: nil`,
229 },
230 }
231 baseEnv, err := cel.NewEnv(cel.OptionalTypes())
232 if err != nil {
233 t.Fatalf("cel.NewEnv() failed: %v", err)
234 }
235 for _, tst := range tests {
236 c := parseConfigYaml(t, tst.config)
237 _, err := baseEnv.Extend(FromConfig(c))
238 if err == nil || err.Error() != tst.err {
239 t.Errorf("AsEnvOptions() got error: %v, wanted %s", err, tst.err)
240 }
241 }
242 }
243
244 func parseConfigYaml(t *testing.T, doc string) *env.Config {
245 config := &env.Config{}
246 if err := yaml.Unmarshal([]byte(doc), config); err != nil {
247 t.Fatalf("yaml.Unmarshal(%q) failed: %v", doc, err)
248 }
249 return config
250 }