Skip to content

Commit 964e5e0

Browse files
authored
Merge pull request kubernetes#129596 from cici37/cvTest
Add tests for CEL library with compatible version
2 parents e07aeb7 + e179f0e commit 964e5e0

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

staging/src/k8s.io/apiserver/pkg/admission/plugin/cel/condition_test.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ func TestCondition(t *testing.T) {
184184

185185
v130 := version.MajorMinor(1, 30)
186186
v131 := version.MajorMinor(1, 31)
187+
v127 := version.MajorMinor(1, 27)
187188

188189
var nilUnstructured *unstructured.Unstructured
189190
cases := []struct {
@@ -200,6 +201,7 @@ func TestCondition(t *testing.T) {
200201
enableSelectors bool
201202

202203
compatibilityVersion *version.Version
204+
envType environment.Type
203205
}{
204206
{
205207
name: "valid syntax for object",
@@ -867,6 +869,52 @@ func TestCondition(t *testing.T) {
867869
hasParamKind: false,
868870
namespaceObject: nsObject,
869871
},
872+
{
873+
name: "cel lib not recognized in version earlier than introduced version",
874+
validations: []ExpressionAccessor{
875+
&testCondition{
876+
Expression: "isQuantity(\"20M\")",
877+
},
878+
},
879+
attributes: newValidAttribute(&podObject, false),
880+
results: []EvaluationResult{
881+
{
882+
Error: fmt.Errorf("isQuantity"),
883+
},
884+
},
885+
compatibilityVersion: v127,
886+
},
887+
{
888+
name: "cel lib recognized in version later than introduced version",
889+
validations: []ExpressionAccessor{
890+
&testCondition{
891+
Expression: "isQuantity(\"20M\")",
892+
},
893+
},
894+
results: []EvaluationResult{
895+
{
896+
EvalResult: celtypes.True,
897+
},
898+
},
899+
attributes: newValidAttribute(&podObject, false),
900+
compatibilityVersion: v130,
901+
},
902+
{
903+
name: "cel lib always recognized in stored expression",
904+
validations: []ExpressionAccessor{
905+
&testCondition{
906+
Expression: "isQuantity(\"20M\")",
907+
},
908+
},
909+
attributes: newValidAttribute(&podObject, false),
910+
results: []EvaluationResult{
911+
{
912+
EvalResult: celtypes.True,
913+
},
914+
},
915+
envType: environment.StoredExpressions,
916+
compatibilityVersion: version.MajorMinor(1, 2),
917+
},
870918
}
871919

872920
for _, tc := range cases {
@@ -891,7 +939,11 @@ func TestCondition(t *testing.T) {
891939
t.Fatal(err)
892940
}
893941
c := NewConditionCompiler(env)
894-
f := c.CompileCondition(tc.validations, OptionalVariableDeclarations{HasParams: tc.hasParamKind, HasAuthorizer: tc.authorizer != nil, StrictCost: tc.strictCost}, environment.NewExpressions)
942+
envType := tc.envType
943+
if envType == "" {
944+
envType = environment.NewExpressions
945+
}
946+
f := c.CompileCondition(tc.validations, OptionalVariableDeclarations{HasParams: tc.hasParamKind, HasAuthorizer: tc.authorizer != nil, StrictCost: tc.strictCost}, envType)
895947
if f == nil {
896948
t.Fatalf("unexpected nil validator")
897949
}

0 commit comments

Comments
 (0)