...

Source file src/k8s.io/component-base/cli/globalflag/globalflags_test.go

Documentation: k8s.io/component-base/cli/globalflag

     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 globalflag
    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/logs"
    30  )
    31  
    32  func TestAddGlobalFlags(t *testing.T) {
    33  	namedFlagSets := &cliflag.NamedFlagSets{}
    34  	nfs := namedFlagSets.FlagSet("global")
    35  	nfs.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
    36  	AddGlobalFlags(nfs, "test-cmd")
    37  
    38  	actualFlag := []string{}
    39  	nfs.VisitAll(func(flag *pflag.Flag) {
    40  		actualFlag = append(actualFlag, flag.Name)
    41  	})
    42  
    43  	// Get all flags from flags.CommandLine, except flag `test.*`.
    44  	wantedFlag := []string{"help"}
    45  	pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
    46  	logs.AddFlags(pflag.CommandLine)
    47  	normalizeFunc := nfs.GetNormalizeFunc()
    48  	pflag.VisitAll(func(flag *pflag.Flag) {
    49  		if !strings.Contains(flag.Name, "test.") {
    50  			wantedFlag = append(wantedFlag, string(normalizeFunc(nfs, flag.Name)))
    51  		}
    52  	})
    53  	sort.Strings(wantedFlag)
    54  
    55  	if !reflect.DeepEqual(wantedFlag, actualFlag) {
    56  		t.Errorf("[Default]: expected %+v, got %+v", wantedFlag, actualFlag)
    57  	}
    58  
    59  	tests := []struct {
    60  		expectedFlag  []string
    61  		matchExpected bool
    62  	}{
    63  		{
    64  			// Happy case
    65  			expectedFlag:  []string{"help", "log-flush-frequency", "v", "vmodule"},
    66  			matchExpected: false,
    67  		},
    68  		{
    69  			// Missing flag
    70  			expectedFlag:  []string{"logtostderr", "log-dir"},
    71  			matchExpected: true,
    72  		},
    73  		{
    74  			// Empty flag
    75  			expectedFlag:  []string{},
    76  			matchExpected: true,
    77  		},
    78  		{
    79  			// Invalid flag
    80  			expectedFlag:  []string{"foo"},
    81  			matchExpected: true,
    82  		},
    83  	}
    84  
    85  	for i, test := range tests {
    86  		if reflect.DeepEqual(test.expectedFlag, actualFlag) == test.matchExpected {
    87  			t.Errorf("[%d]: expected %+v, got %+v", i, test.expectedFlag, actualFlag)
    88  		}
    89  	}
    90  }
    91  

View as plain text