...
1
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
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
65 expectedFlag: []string{"help", "log-flush-frequency", "v", "vmodule"},
66 matchExpected: false,
67 },
68 {
69
70 expectedFlag: []string{"logtostderr", "log-dir"},
71 matchExpected: true,
72 },
73 {
74
75 expectedFlag: []string{},
76 matchExpected: true,
77 },
78 {
79
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