| 1 |
/* |
| 2 |
Copyright 2022 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 library |
| 18 |
|
| 19 |
import ( |
| 20 |
"strings" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/google/cel-go/cel" |
| 24 |
"github.com/google/cel-go/common/decls" |
| 25 |
"github.com/google/cel-go/common/types" |
| 26 |
|
| 27 |
"k8s.io/apimachinery/pkg/util/sets" |
| 28 |
) |
| 29 |
|
| 30 |
func TestLibraryCompatibility(t *testing.T) { |
| 31 |
functionNames := sets.New[string]() |
| 32 |
for _, lib := range KnownLibraries() { |
| 33 |
if !strings.HasPrefix(lib.LibraryName(), "kubernetes.") { |
| 34 |
t.Errorf("Expected all kubernetes CEL libraries to have a name package with a 'kubernetes.' prefix but got %v", lib.LibraryName()) |
| 35 |
} |
| 36 |
for name := range lib.declarations() { |
| 37 |
functionNames[name] = struct{}{} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// WARN: All library changes must follow |
| 42 |
// https://github.com/kubernetes/enhancements/tree/master/keps/sig-api-machinery/2876-crd-validation-expression-language#function-library-updates |
| 43 |
// and must track the functions here along with which Kubernetes version introduced them. |
| 44 |
knownFunctions := sets.New( |
| 45 |
// Kubernetes 1.24: |
| 46 |
"isSorted", "sum", "max", "min", "indexOf", "lastIndexOf", "find", "findAll", "url", "getScheme", "getHost", "getHostname", |
| 47 |
"getPort", "getEscapedPath", "getQuery", "isURL", |
| 48 |
// Kubernetes <1.27>: |
| 49 |
"path", "group", "serviceAccount", "resource", "subresource", "namespace", "name", "check", "allowed", "reason", |
| 50 |
// Kubernetes <1.28>: |
| 51 |
"errored", "error", |
| 52 |
// Kubernetes <1.29>: |
| 53 |
"add", "asApproximateFloat", "asInteger", "compareTo", "isGreaterThan", "isInteger", "isLessThan", "isQuantity", "quantity", "sign", "sub", |
| 54 |
// Kubernetes <1.30>: |
| 55 |
"ip", "family", "isUnspecified", "isLoopback", "isLinkLocalMulticast", "isLinkLocalUnicast", "isGlobalUnicast", "ip.isCanonical", "isIP", "cidr", "containsIP", "containsCIDR", "masked", "prefixLength", "isCIDR", "string", |
| 56 |
// Kubernetes <1.31>: |
| 57 |
"fieldSelector", "labelSelector", "validate", "format.named", "isSemver", "major", "minor", "patch", "semver", |
| 58 |
// Kubernetes <1.32>: |
| 59 |
"jsonpatch.escapeKey", |
| 60 |
// Kubernetes <1.33>: |
| 61 |
"semver", "isSemver", "major", "minor", "patch", |
| 62 |
// Kubernetes <1.??>: |
| 63 |
) |
| 64 |
|
| 65 |
// TODO: test celgo function lists |
| 66 |
|
| 67 |
unexpected := functionNames.Difference(knownFunctions) |
| 68 |
missing := knownFunctions.Difference(functionNames) |
| 69 |
|
| 70 |
if len(unexpected) != 0 { |
| 71 |
t.Errorf("Expected all functions in the libraries to be assigned to a kubernetes release, but found the unexpected function names: %v", unexpected) |
| 72 |
} |
| 73 |
if len(missing) != 0 { |
| 74 |
t.Errorf("Expected all functions in the libraries to be assigned to a kubernetes release, but found the missing function names: %v", missing) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// TestTypeRegistration ensures that all custom types defined and used by Kubernetes CEL libraries |
| 79 |
// are returned by library.Types(). Other tests depend on Types() to provide an up-to-date list of |
| 80 |
// types declared in a library. |
| 81 |
func TestTypeRegistration(t *testing.T) { |
| 82 |
for _, lib := range KnownLibraries() { |
| 83 |
registeredTypes := sets.New[*cel.Type]() |
| 84 |
usedTypes := sets.New[*cel.Type]() |
| 85 |
// scan all registered function declarations for the library |
| 86 |
for _, fn := range lib.declarations() { |
| 87 |
fn, err := decls.NewFunction("placeholder-not-used", fn...) |
| 88 |
if err != nil { |
| 89 |
t.Fatal(err) |
| 90 |
} |
| 91 |
for _, o := range fn.OverloadDecls() { |
| 92 |
// ArgTypes include both the receiver type (if present) and |
| 93 |
// all function argument types. |
| 94 |
for _, at := range o.ArgTypes() { |
| 95 |
switch at.Kind() { |
| 96 |
// User defined types are either Opaque or Struct. |
| 97 |
case types.OpaqueKind, types.StructKind: |
| 98 |
usedTypes.Insert(at) |
| 99 |
default: |
| 100 |
// skip |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
for _, lb := range lib.Types() { |
| 106 |
registeredTypes.Insert(lb) |
| 107 |
if !strings.HasPrefix(lb.TypeName(), "kubernetes.") && !legacyTypeNames.Has(lb.TypeName()) { |
| 108 |
t.Errorf("Expected all types in kubernetes CEL libraries to have a type name packaged with a 'kubernetes.' prefix but got %v", lb.TypeName()) |
| 109 |
} |
| 110 |
} |
| 111 |
unregistered := usedTypes.Difference(registeredTypes) |
| 112 |
if len(unregistered) != 0 { |
| 113 |
t.Errorf("Expected types to be registered with the %s library Type() functions, but they were not: %v", lib.LibraryName(), unregistered) |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
// TODO: Consider renaming these to "kubernetes.net.IP" and "kubernetes.net.CIDR" if we decide not to promote them to cel-go |
| 119 |
var legacyTypeNames = sets.New[string]("net.IP", "net.CIDR") |