k8s.io/apiserver@v0.31.1/pkg/cel/mutation/optional_test.go (about)

     1  /*
     2  Copyright 2024 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 mutation
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  
    23  	celtypes "github.com/google/cel-go/common/types"
    24  	"github.com/google/cel-go/common/types/ref"
    25  
    26  	"k8s.io/apiserver/pkg/cel/mutation/common"
    27  )
    28  
    29  // TestCELOptional is an exploration test to demonstrate how CEL optional library
    30  // behave for the use cases that the mutation library requires.
    31  func TestCELOptional(t *testing.T) {
    32  	for _, tc := range []struct {
    33  		name                 string
    34  		expression           string
    35  		expectedVal          ref.Val
    36  		expectedCompileError string
    37  	}{
    38  		{
    39  			// question mark syntax still requires the field to exist in object construction
    40  			name: "construct non-existing field, compile error",
    41  			expression: `Object{
    42  				?nonExisting: optional.none()
    43  			}`,
    44  			expectedCompileError: `undefined field 'nonExisting'`,
    45  		},
    46  		{
    47  			// The root cause of the behavior above is that, has on an object (or Message in the Language Def),
    48  			// still require the field to be declared in the schema.
    49  			//
    50  			// Quoting from
    51  			// https://github.com/google/cel-spec/blob/master/doc/langdef.md#field-selection
    52  			//
    53  			// To test for the presence of a field, the boolean-valued macro has(e.f) can be used.
    54  			//
    55  			// 2. If e evaluates to a message and f is not a declared field for the message,
    56  			// has(e.f) raises a no_such_field error.
    57  			name:                 "has(Object{}), de-sugared, compile error",
    58  			expression:           "has(Object{}.nonExisting)",
    59  			expectedCompileError: `undefined field 'nonExisting'`,
    60  		},
    61  		{
    62  			name: "construct existing field with none, empty object",
    63  			expression: `Object{
    64  				?existing: optional.none()
    65  			}`,
    66  			expectedVal: common.NewObjectVal(nil, map[string]ref.Val{
    67  				// "existing" field was not set.
    68  			}),
    69  		},
    70  		{
    71  			name:        "object of zero value, ofNonZeroValue",
    72  			expression:  `Object{?spec: optional.ofNonZeroValue(Object.spec{?replicas: Object{}.?replicas})}`,
    73  			expectedVal: common.NewObjectVal(nil, map[string]ref.Val{
    74  				// "existing" field was not set.
    75  			}),
    76  		},
    77  		{
    78  			name:                 "access non-existing field, return none",
    79  			expression:           `Object{}.?nonExisting`,
    80  			expectedCompileError: `undefined field 'nonExisting'`,
    81  		},
    82  		{
    83  			name:        "access existing field, return none",
    84  			expression:  `Object{}.?existing`,
    85  			expectedVal: celtypes.OptionalNone,
    86  		},
    87  		{
    88  			name:        "map non-existing field, return none",
    89  			expression:  `{"foo": 1}[?"bar"]`,
    90  			expectedVal: celtypes.OptionalNone,
    91  		},
    92  		{
    93  			name:        "map existing field, return actual value",
    94  			expression:  `{"foo": 1}[?"foo"]`,
    95  			expectedVal: celtypes.OptionalOf(celtypes.Int(1)),
    96  		},
    97  		{
    98  			// Map has a different behavior than Object
    99  			//
   100  			// Quoting from
   101  			// https://github.com/google/cel-spec/blob/master/doc/langdef.md#field-selection
   102  			//
   103  			// To test for the presence of a field, the boolean-valued macro has(e.f) can be used.
   104  			//
   105  			// 1. If e evaluates to a map, then has(e.f) indicates whether the string f is
   106  			// a key in the map (note that f must syntactically be an identifier).
   107  			//
   108  			name: "has on a map, de-sugared, non-existing field, returns false",
   109  			// has marco supports only the dot access syntax.
   110  			expression:  `has({"foo": 1}.bar)`,
   111  			expectedVal: celtypes.False,
   112  		},
   113  		{
   114  			name: "has on a map, de-sugared, existing field, returns true",
   115  			// has marco supports only the dot access syntax.
   116  			expression:  `has({"foo": 1}.foo)`,
   117  			expectedVal: celtypes.True,
   118  		},
   119  	} {
   120  		t.Run(tc.name, func(t *testing.T) {
   121  			_, option := NewTypeProviderAndEnvOption(&mockTypeResolverForOptional{
   122  				mockTypeResolver: &mockTypeResolver{},
   123  			})
   124  			env := mustCreateEnvWithOptional(t, option)
   125  			ast, issues := env.Compile(tc.expression)
   126  			if issues != nil {
   127  				if tc.expectedCompileError == "" {
   128  					t.Fatalf("unexpected issues during compilation: %v", issues)
   129  				} else if !strings.Contains(issues.String(), tc.expectedCompileError) {
   130  					t.Fatalf("unexpected compile error, want to contain %q but got %v", tc.expectedCompileError, issues)
   131  				}
   132  				return
   133  			}
   134  			program, err := env.Program(ast)
   135  			if err != nil {
   136  				t.Fatalf("unexpected error while creating program: %v", err)
   137  			}
   138  			r, _, err := program.Eval(map[string]any{})
   139  			if err != nil {
   140  				t.Fatalf("unexpected error during evaluation: %v", err)
   141  			}
   142  			if equals := tc.expectedVal.Equal(r); equals.Value() != true {
   143  				t.Errorf("expected %v but got %v", tc.expectedVal, r)
   144  			}
   145  		})
   146  	}
   147  }