...

Source file src/k8s.io/client-go/features/testing/features_init_test.go

Documentation: k8s.io/client-go/features/testing

     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 testing
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"k8s.io/client-go/features"
    27  )
    28  
    29  func TestDriveInitDefaultFeatureGates(t *testing.T) {
    30  	featureGates := features.FeatureGates()
    31  	assertFunctionPanicsWithMessage(t, func() { featureGates.Enabled("FakeFeatureGate") }, "features.FeatureGates().Enabled", fmt.Sprintf("feature %q is not registered in FeatureGate", "FakeFeatureGate"))
    32  
    33  	fakeFeatureGates := &alwaysEnabledFakeGates{}
    34  	require.True(t, fakeFeatureGates.Enabled("FakeFeatureGate"))
    35  
    36  	features.ReplaceFeatureGates(fakeFeatureGates)
    37  	featureGates = features.FeatureGates()
    38  
    39  	assertFeatureGatesType(t, featureGates)
    40  	require.True(t, featureGates.Enabled("FakeFeatureGate"))
    41  }
    42  
    43  type alwaysEnabledFakeGates struct{}
    44  
    45  func (f *alwaysEnabledFakeGates) Enabled(features.Feature) bool {
    46  	return true
    47  }
    48  
    49  func assertFeatureGatesType(t *testing.T, fg features.Gates) {
    50  	_, ok := fg.(*alwaysEnabledFakeGates)
    51  	if !ok {
    52  		t.Fatalf("passed features.FeatureGates() is NOT of type *alwaysEnabledFakeGates, it is of type = %T", fg)
    53  	}
    54  }
    55  
    56  func assertFunctionPanicsWithMessage(t *testing.T, f func(), fName, errMessage string) {
    57  	didPanic, panicMessage := didFunctionPanic(f)
    58  	if !didPanic {
    59  		t.Fatalf("function %q did not panicked", fName)
    60  	}
    61  
    62  	panicError, ok := panicMessage.(error)
    63  	if !ok || !strings.Contains(panicError.Error(), errMessage) {
    64  		t.Fatalf("func %q should panic with error message:\t%#v\n\tPanic value:\t%#v\n", fName, errMessage, panicMessage)
    65  	}
    66  }
    67  
    68  func didFunctionPanic(f func()) (didPanic bool, panicMessage interface{}) {
    69  	didPanic = true
    70  
    71  	defer func() {
    72  		panicMessage = recover()
    73  	}()
    74  
    75  	f()
    76  	didPanic = false
    77  
    78  	return
    79  }
    80  

View as plain text