...

Source file src/k8s.io/kubernetes/pkg/features/client_adapter_test.go

Documentation: k8s.io/kubernetes/pkg/features

     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 features
    18  
    19  import (
    20  	"testing"
    21  
    22  	clientfeatures "k8s.io/client-go/features"
    23  	"k8s.io/component-base/featuregate"
    24  )
    25  
    26  func TestClientAdapterEnabled(t *testing.T) {
    27  	fg := featuregate.NewFeatureGate()
    28  	if err := fg.Add(map[featuregate.Feature]featuregate.FeatureSpec{
    29  		"Foo": {Default: true},
    30  	}); err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	a := &clientAdapter{fg}
    35  	if !a.Enabled("Foo") {
    36  		t.Error("expected Enabled(\"Foo\") to return true")
    37  	}
    38  	var r interface{}
    39  	func() {
    40  		defer func() {
    41  			r = recover()
    42  		}()
    43  		a.Enabled("Bar")
    44  	}()
    45  	if r == nil {
    46  		t.Error("expected Enabled(\"Bar\") to panic due to unknown feature name")
    47  	}
    48  }
    49  
    50  func TestClientAdapterAdd(t *testing.T) {
    51  	fg := featuregate.NewFeatureGate()
    52  	a := &clientAdapter{fg}
    53  	defaults := fg.GetAll()
    54  	if err := a.Add(map[clientfeatures.Feature]clientfeatures.FeatureSpec{
    55  		"FeatureAlpha":      {PreRelease: clientfeatures.Alpha, Default: true},
    56  		"FeatureBeta":       {PreRelease: clientfeatures.Beta, Default: false},
    57  		"FeatureGA":         {PreRelease: clientfeatures.GA, Default: true, LockToDefault: true},
    58  		"FeatureDeprecated": {PreRelease: clientfeatures.Deprecated, Default: false, LockToDefault: true},
    59  	}); err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	all := fg.GetAll()
    63  	allexpected := map[featuregate.Feature]featuregate.FeatureSpec{
    64  		"FeatureAlpha":      {PreRelease: featuregate.Alpha, Default: true},
    65  		"FeatureBeta":       {PreRelease: featuregate.Beta, Default: false},
    66  		"FeatureGA":         {PreRelease: featuregate.GA, Default: true, LockToDefault: true},
    67  		"FeatureDeprecated": {PreRelease: featuregate.Deprecated, Default: false, LockToDefault: true},
    68  	}
    69  	for name, spec := range defaults {
    70  		allexpected[name] = spec
    71  	}
    72  	if len(all) != len(allexpected) {
    73  		t.Errorf("expected %d registered features, got %d", len(allexpected), len(all))
    74  	}
    75  	for name, expected := range allexpected {
    76  		actual, ok := all[name]
    77  		if !ok {
    78  			t.Errorf("expected feature %q not found", name)
    79  			continue
    80  		}
    81  
    82  		if actual != expected {
    83  			t.Errorf("expected feature %q spec %#v, got spec %#v", name, expected, actual)
    84  		}
    85  	}
    86  
    87  	var r interface{}
    88  	func() {
    89  		defer func() {
    90  			r = recover()
    91  		}()
    92  		_ = a.Add(map[clientfeatures.Feature]clientfeatures.FeatureSpec{
    93  			"FeatureAlpha": {PreRelease: "foobar"},
    94  		})
    95  	}()
    96  	if r == nil {
    97  		t.Error("expected panic when adding feature with unknown prerelease")
    98  	}
    99  }
   100  

View as plain text