|
| 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 flagz |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/spf13/pflag" |
| 24 | + |
| 25 | + "k8s.io/component-base/cli/flag" |
| 26 | +) |
| 27 | + |
| 28 | +func TestConvertNamedFlagSetToFlags(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + flagSets *flag.NamedFlagSets |
| 32 | + want map[string]string |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "basic flags", |
| 36 | + flagSets: &flag.NamedFlagSets{ |
| 37 | + FlagSets: map[string]*pflag.FlagSet{ |
| 38 | + "test": flagSet(t, map[string]flagValue{ |
| 39 | + "flag1": {value: "value1", sensitive: false}, |
| 40 | + "flag2": {value: "value2", sensitive: false}, |
| 41 | + }), |
| 42 | + }, |
| 43 | + }, |
| 44 | + want: map[string]string{ |
| 45 | + "flag1": "value1", |
| 46 | + "flag2": "value2", |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "classified flags", |
| 51 | + flagSets: &flag.NamedFlagSets{ |
| 52 | + FlagSets: map[string]*pflag.FlagSet{ |
| 53 | + "test": flagSet(t, map[string]flagValue{ |
| 54 | + "secret1": {value: "value1", sensitive: true}, |
| 55 | + "flag2": {value: "value2", sensitive: false}, |
| 56 | + }), |
| 57 | + }, |
| 58 | + }, |
| 59 | + want: map[string]string{ |
| 60 | + "flag2": "value2", |
| 61 | + "secret1": "CLASSIFIED", |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tt := range tests { |
| 67 | + t.Run(tt.name, func(t *testing.T) { |
| 68 | + got := convertNamedFlagSetToFlags(tt.flagSets) |
| 69 | + if !reflect.DeepEqual(got, tt.want) { |
| 70 | + t.Errorf("ConvertNamedFlagSetToFlags() = %v, want %v", got, tt.want) |
| 71 | + } |
| 72 | + }) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +type flagValue struct { |
| 77 | + value string |
| 78 | + sensitive bool |
| 79 | +} |
| 80 | + |
| 81 | +func flagSet(t *testing.T, flags map[string]flagValue) *pflag.FlagSet { |
| 82 | + fs := pflag.NewFlagSet("test-set", pflag.ContinueOnError) |
| 83 | + for flagName, flagVal := range flags { |
| 84 | + flagValue := "" |
| 85 | + fs.StringVar(&flagValue, flagName, flagVal.value, "test-usage") |
| 86 | + if flagVal.sensitive { |
| 87 | + err := fs.SetAnnotation(flagName, "classified", []string{"true"}) |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("unexpected error when setting flag annotation: %v", err) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return fs |
| 95 | +} |
0 commit comments