...

Source file src/k8s.io/kubernetes/cmd/kube-apiserver/app/options/globalflags_test.go

Documentation: k8s.io/kubernetes/cmd/kube-apiserver/app/options

     1  /*
     2  Copyright 2018 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 options
    18  
    19  import (
    20  	"flag"
    21  	"reflect"
    22  	"sort"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/spf13/pflag"
    27  
    28  	cliflag "k8s.io/component-base/cli/flag"
    29  	"k8s.io/component-base/cli/globalflag"
    30  	"k8s.io/component-base/logs"
    31  )
    32  
    33  func TestAddCustomGlobalFlags(t *testing.T) {
    34  	namedFlagSets := &cliflag.NamedFlagSets{}
    35  
    36  	// Note that we will register all flags (including klog flags) into the same
    37  	// flag set. This allows us to test against all global flags from
    38  	// flags.CommandLine.
    39  	nfs := namedFlagSets.FlagSet("test")
    40  	nfs.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
    41  	globalflag.AddGlobalFlags(nfs, "test-cmd")
    42  	AddCustomGlobalFlags(nfs)
    43  
    44  	actualFlag := []string{}
    45  	nfs.VisitAll(func(flag *pflag.Flag) {
    46  		actualFlag = append(actualFlag, flag.Name)
    47  	})
    48  
    49  	// Get all flags from flags.CommandLine, except flag `test.*`.
    50  	wantedFlag := []string{"help"}
    51  	pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
    52  	logs.AddFlags(pflag.CommandLine)
    53  	normalizeFunc := nfs.GetNormalizeFunc()
    54  	pflag.VisitAll(func(flag *pflag.Flag) {
    55  		if !strings.Contains(flag.Name, "test.") {
    56  			wantedFlag = append(wantedFlag, string(normalizeFunc(nfs, flag.Name)))
    57  		}
    58  	})
    59  	sort.Strings(wantedFlag)
    60  
    61  	if !reflect.DeepEqual(wantedFlag, actualFlag) {
    62  		t.Errorf("[Default]: expected %+v, got %+v", wantedFlag, actualFlag)
    63  	}
    64  }
    65  

View as plain text