k8s.io/apiserver@v0.31.1/pkg/cel/environment/base_test.go (about)

     1  /*
     2  Copyright 2023 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 environment
    18  
    19  import (
    20  	"sort"
    21  	"testing"
    22  
    23  	"github.com/google/cel-go/cel"
    24  
    25  	"k8s.io/apimachinery/pkg/util/version"
    26  )
    27  
    28  // BenchmarkLoadBaseEnv is expected to be very fast, because a
    29  // a cached environment is loaded for each MustBaseEnvSet call.
    30  func BenchmarkLoadBaseEnv(b *testing.B) {
    31  	ver := DefaultCompatibilityVersion()
    32  	MustBaseEnvSet(ver, true)
    33  	b.ResetTimer()
    34  	for i := 0; i < b.N; i++ {
    35  		MustBaseEnvSet(ver, true)
    36  	}
    37  }
    38  
    39  // BenchmarkLoadBaseEnvDifferentVersions is expected to be relatively slow, because a
    40  // a new environment must be created for each MustBaseEnvSet call.
    41  func BenchmarkLoadBaseEnvDifferentVersions(b *testing.B) {
    42  	b.ResetTimer()
    43  	for i := 0; i < b.N; i++ {
    44  		MustBaseEnvSet(version.MajorMinor(1, uint(i)), true)
    45  	}
    46  }
    47  
    48  // TestLibraryCoverage lints the management of libraries in baseOpts by
    49  // checking for:
    50  //
    51  //   - No gaps and overlap in library inclusion, including when libraries are version bumped
    52  //   - RemovedVersion is always greater than IntroducedVersion
    53  //   - Libraries are not removed once added (although they can be replaced with new versions)
    54  func TestLibraryCoverage(t *testing.T) {
    55  	vops := make([]VersionedOptions, len(baseOpts))
    56  	copy(vops, baseOpts)
    57  	sort.SliceStable(vops, func(i, j int) bool {
    58  		return vops[i].IntroducedVersion.LessThan(vops[j].IntroducedVersion)
    59  	})
    60  
    61  	tracked := map[string]*versionTracker{}
    62  
    63  	for _, vop := range vops {
    64  		if vop.RemovedVersion != nil {
    65  			if vop.IntroducedVersion == nil {
    66  				t.Errorf("VersionedOptions with RemovedVersion %v is missing required IntroducedVersion", vop.RemovedVersion)
    67  			}
    68  			if !vop.IntroducedVersion.LessThan(vop.RemovedVersion) {
    69  				t.Errorf("VersionedOptions with IntroducedVersion %s must be less than RemovedVersion %v", vop.IntroducedVersion, vop.RemovedVersion)
    70  			}
    71  		}
    72  
    73  		for _, name := range librariesInVersions(t, vop) {
    74  			versionTracking, ok := tracked[name]
    75  			if !ok {
    76  				versionTracking = &versionTracker{}
    77  				tracked[name] = versionTracking
    78  			}
    79  			if versionTracking.added != nil {
    80  				t.Errorf("Did not expect %s library to be added again at version %v. It was already added at version %v", name, vop.IntroducedVersion, versionTracking.added)
    81  			} else {
    82  				versionTracking.added = vop.IntroducedVersion
    83  				if versionTracking.removed != nil {
    84  					if versionTracking.removed.LessThan(vop.IntroducedVersion) {
    85  						t.Errorf("Did not expect gap in presence of %s library. It was "+
    86  							"removed in %v and not added again until %v. When versioning "+
    87  							"libraries, introduce a new version of the library as the same "+
    88  							"kubernetes version that the old version of the library is removed.", name, versionTracking.removed, vop.IntroducedVersion)
    89  					} else if vop.IntroducedVersion.LessThan(versionTracking.removed) {
    90  						t.Errorf("Did not expect overlap in presence of %s library. It was "+
    91  							"added again at version %v while scheduled to be removed at %v. When versioning "+
    92  							"libraries, introduce a new version of the library as the same "+
    93  							"kubernetes version that the old version of the library is removed.", name, vop.IntroducedVersion, versionTracking.removed)
    94  					}
    95  				}
    96  				versionTracking.removed = nil
    97  			}
    98  			if vop.RemovedVersion != nil {
    99  				if versionTracking.removed != nil {
   100  					t.Errorf("Unexpected RemovedVersion of %v for library %s already removed at version %v", vop.RemovedVersion, name, versionTracking.removed)
   101  				}
   102  				versionTracking.added = nil
   103  				versionTracking.removed = vop.RemovedVersion
   104  			}
   105  		}
   106  	}
   107  	for name, lib := range tracked {
   108  		if lib.removed != nil {
   109  			t.Errorf("Unexpected RemovedVersion of %v for library %s without replacement. "+
   110  				"For backward compatibility, libraries should not be removed without being replaced by a new version.", lib.removed, name)
   111  		}
   112  	}
   113  }
   114  
   115  func librariesInVersions(t *testing.T, vops ...VersionedOptions) []string {
   116  	env, err := cel.NewCustomEnv()
   117  	if err != nil {
   118  		t.Fatalf("Error creating env: %v", err)
   119  	}
   120  	for _, vop := range vops {
   121  		env, err = env.Extend(vop.EnvOptions...)
   122  		if err != nil {
   123  			t.Fatalf("Error updating env: %v", err)
   124  		}
   125  	}
   126  	libs := env.Libraries()
   127  	return libs
   128  }
   129  
   130  type versionTracker struct {
   131  	added   *version.Version
   132  	removed *version.Version
   133  }