@@ -19,6 +19,7 @@ package library
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "github.com/google/cel-go/common/types/ref"
22
23
"testing"
23
24
24
25
"github.com/google/cel-go/cel"
@@ -30,6 +31,7 @@ import (
30
31
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
31
32
32
33
"k8s.io/apiserver/pkg/authorization/authorizer"
34
+ apiservercel "k8s.io/apiserver/pkg/cel"
33
35
)
34
36
35
37
const (
@@ -1231,10 +1233,10 @@ func TestSize(t *testing.T) {
1231
1233
est := & CostEstimator {SizeEstimator : & testCostEstimator {}}
1232
1234
for _ , tc := range cases {
1233
1235
t .Run (tc .name , func (t * testing.T ) {
1234
- var targetNode checker.AstNode = testSizeNode {size : tc .targetSize }
1236
+ var targetNode checker.AstNode = testNode {size : tc .targetSize }
1235
1237
argNodes := make ([]checker.AstNode , len (tc .argSizes ))
1236
1238
for i , arg := range tc .argSizes {
1237
- argNodes [i ] = testSizeNode {size : arg }
1239
+ argNodes [i ] = testNode {size : arg }
1238
1240
}
1239
1241
result := est .EstimateCallCost (tc .function , tc .overload , & targetNode , argNodes )
1240
1242
if result .ResultSize == nil {
@@ -1247,25 +1249,63 @@ func TestSize(t *testing.T) {
1247
1249
}
1248
1250
}
1249
1251
1250
- type testSizeNode struct {
1252
+ // TestTypeEquality ensures that cost is tested for all custom types used by Kubernetes libraries.
1253
+ func TestTypeEquality (t * testing.T ) {
1254
+ examples := map [string ]ref.Val {
1255
+ // Add example ref.Val's for custom types in Kubernetes here:
1256
+ "kubernetes.authorization.Authorizer" : authorizerVal {},
1257
+ "kubernetes.authorization.PathCheck" : pathCheckVal {},
1258
+ "kubernetes.authorization.GroupCheck" : groupCheckVal {},
1259
+ "kubernetes.authorization.ResourceCheck" : resourceCheckVal {},
1260
+ "kubernetes.authorization.Decision" : decisionVal {},
1261
+ "kubernetes.URL" : apiservercel.URL {},
1262
+ "kubernetes.Quantity" : apiservercel.Quantity {},
1263
+ "net.IP" : apiservercel.IP {},
1264
+ "net.CIDR" : apiservercel.CIDR {},
1265
+ "kubernetes.NamedFormat" : apiservercel.Format {},
1266
+ "kubernetes.Semver" : apiservercel.Semver {},
1267
+ }
1268
+
1269
+ originalPanicOnUnknown := panicOnUnknown
1270
+ panicOnUnknown = true
1271
+ t .Cleanup (func () { panicOnUnknown = originalPanicOnUnknown })
1272
+ est := & CostEstimator {SizeEstimator : & testCostEstimator {}}
1273
+
1274
+ for _ , lib := range KnownLibraries () {
1275
+ for _ , kt := range lib .Types () {
1276
+ t .Run (kt .TypeName (), func (t * testing.T ) {
1277
+ typeNode := testNode {size : checker.SizeEstimate {Min : 10 , Max : 100 }, typ : kt }
1278
+ est .EstimateCallCost ("_==_" , "" , nil , []checker.AstNode {typeNode , typeNode })
1279
+ ex , ok := examples [kt .TypeName ()]
1280
+ if ! ok {
1281
+ t .Errorf ("missing example for type: %s" , kt .TypeName ())
1282
+ }
1283
+ est .CallCost ("_==_" , "" , []ref.Val {ex , ex }, nil )
1284
+ })
1285
+ }
1286
+ }
1287
+ }
1288
+
1289
+ type testNode struct {
1251
1290
size checker.SizeEstimate
1291
+ typ * types.Type
1252
1292
}
1253
1293
1254
- var _ checker.AstNode = (* testSizeNode )(nil )
1294
+ var _ checker.AstNode = (* testNode )(nil )
1255
1295
1256
- func (t testSizeNode ) Path () []string {
1296
+ func (t testNode ) Path () []string {
1257
1297
return nil // not needed
1258
1298
}
1259
1299
1260
- func (t testSizeNode ) Type () * types.Type {
1261
- return nil // not needed
1300
+ func (t testNode ) Type () * types.Type {
1301
+ return t . typ // not needed
1262
1302
}
1263
1303
1264
- func (t testSizeNode ) Expr () ast.Expr {
1304
+ func (t testNode ) Expr () ast.Expr {
1265
1305
return nil // not needed
1266
1306
}
1267
1307
1268
- func (t testSizeNode ) ComputedSize () * checker.SizeEstimate {
1308
+ func (t testNode ) ComputedSize () * checker.SizeEstimate {
1269
1309
return & t .size
1270
1310
}
1271
1311
0 commit comments