k8s.io/apiserver@v0.31.1/pkg/cel/library/library_compatibility_test.go (about)

     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  	"testing"
    21  
    22  	"github.com/google/cel-go/cel"
    23  
    24  	"k8s.io/apimachinery/pkg/util/sets"
    25  )
    26  
    27  func TestLibraryCompatibility(t *testing.T) {
    28  	var libs []map[string][]cel.FunctionOpt
    29  	libs = append(libs, authzLibraryDecls, listsLibraryDecls, regexLibraryDecls, urlLibraryDecls, quantityLibraryDecls, ipLibraryDecls, cidrLibraryDecls, formatLibraryDecls, authzSelectorsLibraryDecls)
    30  	functionNames := sets.New[string]()
    31  	for _, lib := range libs {
    32  		for name := range lib {
    33  			functionNames[name] = struct{}{}
    34  		}
    35  	}
    36  
    37  	// WARN: All library changes must follow
    38  	// https://github.com/kubernetes/enhancements/tree/master/keps/sig-api-machinery/2876-crd-validation-expression-language#function-library-updates
    39  	// and must track the functions here along with which Kubernetes version introduced them.
    40  	knownFunctions := sets.New(
    41  		// Kubernetes 1.24:
    42  		"isSorted", "sum", "max", "min", "indexOf", "lastIndexOf", "find", "findAll", "url", "getScheme", "getHost", "getHostname",
    43  		"getPort", "getEscapedPath", "getQuery", "isURL",
    44  		// Kubernetes <1.27>:
    45  		"path", "group", "serviceAccount", "resource", "subresource", "namespace", "name", "check", "allowed", "reason",
    46  		// Kubernetes <1.28>:
    47  		"errored", "error",
    48  		// Kubernetes <1.29>:
    49  		"add", "asApproximateFloat", "asInteger", "compareTo", "isGreaterThan", "isInteger", "isLessThan", "isQuantity", "quantity", "sign", "sub",
    50  		// Kubernetes <1.30>:
    51  		"ip", "family", "isUnspecified", "isLoopback", "isLinkLocalMulticast", "isLinkLocalUnicast", "isGlobalUnicast", "ip.isCanonical", "isIP", "cidr", "containsIP", "containsCIDR", "masked", "prefixLength", "isCIDR", "string",
    52  		// Kubernetes <1.31>:
    53  		"fieldSelector", "labelSelector", "validate", "format.named",
    54  		// Kubernetes <1.??>:
    55  	)
    56  
    57  	// TODO: test celgo function lists
    58  
    59  	unexpected := functionNames.Difference(knownFunctions)
    60  	missing := knownFunctions.Difference(functionNames)
    61  
    62  	if len(unexpected) != 0 {
    63  		t.Errorf("Expected all functions in the libraries to be assigned to a kubernetes release, but found the unexpected function names: %v", unexpected)
    64  	}
    65  	if len(missing) != 0 {
    66  		t.Errorf("Expected all functions in the libraries to be assigned to a kubernetes release, but found the missing function names: %v", missing)
    67  	}
    68  }