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