From 0afab6c06847fafc317f1964fcdd424345e5a5e2 Mon Sep 17 00:00:00 2001 From: RoyUP9 <87927115+RoyUP9@users.noreply.github.com> Date: Thu, 12 Aug 2021 16:01:33 +0300 Subject: [PATCH] added set hierarchy, removed allowed set flags (#205) --- cli/config/config.go | 146 +++++------ cli/config/configStruct.go | 4 - cli/config/configStructs/tapConfig.go | 3 - cli/config/config_internal_test.go | 340 ++++++++++++++++++++++++++ cli/config/config_test.go | 6 +- 5 files changed, 420 insertions(+), 79 deletions(-) create mode 100644 cli/config/config_internal_test.go diff --git a/cli/config/config.go b/cli/config/config.go index 43c4749e6..9c2467414 100644 --- a/cli/config/config.go +++ b/cli/config/config.go @@ -27,18 +27,10 @@ const ( ReadonlyTag = "readonly" ) -var allowedSetFlags = []string{ - AgentImageConfigName, - MizuResourcesNamespaceConfigName, - TelemetryConfigName, - DumpLogsConfigName, - KubeConfigPathName, - configStructs.AnalysisDestinationTapName, - configStructs.SleepIntervalSecTapName, - configStructs.IgnoredUserAgentsTapName, -} - -var Config = ConfigStruct{} +var ( + Config = ConfigStruct{} + cmdName string +) func (config *ConfigStruct) Validate() error { if config.IsNsRestrictedMode() { @@ -52,6 +44,8 @@ func (config *ConfigStruct) Validate() error { } func InitConfig(cmd *cobra.Command) error { + cmdName = cmd.Name() + if err := defaults.Set(&Config); err != nil { return err } @@ -105,121 +99,135 @@ func mergeConfigFile() error { } func initFlag(f *pflag.Flag) { - configElem := reflect.ValueOf(&Config).Elem() + configElemValue := reflect.ValueOf(&Config).Elem() + + flagPath := []string {cmdName, f.Name} sliceValue, isSliceValue := f.Value.(pflag.SliceValue) if !isSliceValue { - mergeFlagValue(configElem, f.Name, f.Value.String()) + if err := mergeFlagValue(configElemValue, flagPath, strings.Join(flagPath, "."), f.Value.String()); err != nil { + logger.Log.Warningf(uiUtils.Warning, err) + } return } if f.Name == SetCommandName { - mergeSetFlag(configElem, sliceValue.GetSlice()) + if err := mergeSetFlag(configElemValue, sliceValue.GetSlice()); err != nil { + logger.Log.Warningf(uiUtils.Warning, err) + } return } - mergeFlagValues(configElem, f.Name, sliceValue.GetSlice()) + if err := mergeFlagValues(configElemValue, flagPath, strings.Join(flagPath, "."), sliceValue.GetSlice()); err != nil { + logger.Log.Warningf(uiUtils.Warning, err) + } } -func mergeSetFlag(configElem reflect.Value, setValues []string) { +func mergeSetFlag(configElemValue reflect.Value, setValues []string) error { + var setErrors []string setMap := map[string][]string{} for _, setValue := range setValues { if !strings.Contains(setValue, Separator) { - logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Ignoring set argument %s (set argument format: =)", setValue)) + setErrors = append(setErrors, fmt.Sprintf("Ignoring set argument %s (set argument format: =)", setValue)) continue } split := strings.SplitN(setValue, Separator, 2) - if len(split) != 2 { - logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Ignoring set argument %s (set argument format: =)", setValue)) - continue - } - argumentKey, argumentValue := split[0], split[1] setMap[argumentKey] = append(setMap[argumentKey], argumentValue) } for argumentKey, argumentValues := range setMap { - if !mizu.Contains(allowedSetFlags, argumentKey) { - logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Ignoring set argument name \"%s\", flag name must be one of the following: \"%s\"", argumentKey, strings.Join(allowedSetFlags, "\", \""))) - continue - } + flagPath := strings.Split(argumentKey, ".") if len(argumentValues) > 1 { - mergeFlagValues(configElem, argumentKey, argumentValues) + if err := mergeFlagValues(configElemValue, flagPath, argumentKey, argumentValues); err != nil { + setErrors = append(setErrors, fmt.Sprintf("%v", err)) + } } else { - mergeFlagValue(configElem, argumentKey, argumentValues[0]) + if err := mergeFlagValue(configElemValue, flagPath, argumentKey, argumentValues[0]); err != nil { + setErrors = append(setErrors, fmt.Sprintf("%v", err)) + } } } + + if len(setErrors) > 0 { + return fmt.Errorf(strings.Join(setErrors, "\n")) + } + + return nil } -func mergeFlagValue(currentElem reflect.Value, flagKey string, flagValue string) { - for i := 0; i < currentElem.NumField(); i++ { - currentField := currentElem.Type().Field(i) - currentFieldByName := currentElem.FieldByName(currentField.Name) - currentFieldKind := currentField.Type.Kind() - - if currentFieldKind == reflect.Struct { - mergeFlagValue(currentFieldByName, flagKey, flagValue) - continue - } - - if getFieldNameByTag(currentField) != flagKey { - continue - } +func mergeFlagValue(configElemValue reflect.Value, flagPath []string, fullFlagName string, flagValue string) error { + mergeFunction := func(flagName string, currentFieldStruct reflect.StructField, currentFieldElemValue reflect.Value, currentElemValue reflect.Value) error { + currentFieldKind := currentFieldStruct.Type.Kind() if currentFieldKind == reflect.Slice { - mergeFlagValues(currentElem, flagKey, []string{flagValue}) - return + return mergeFlagValues(currentElemValue, []string{flagName}, fullFlagName, []string{flagValue}) } parsedValue, err := getParsedValue(currentFieldKind, flagValue) if err != nil { - logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Invalid value %s for flag name %s, expected %s", flagValue, flagKey, currentFieldKind)) - return + return fmt.Errorf("invalid value %s for flag name %s, expected %s", flagValue, flagName, currentFieldKind) } - currentFieldByName.Set(parsedValue) + currentFieldElemValue.Set(parsedValue) + return nil } + + return mergeFlag(configElemValue, flagPath, fullFlagName, mergeFunction) } -func mergeFlagValues(currentElem reflect.Value, flagKey string, flagValues []string) { - for i := 0; i < currentElem.NumField(); i++ { - currentField := currentElem.Type().Field(i) - currentFieldByName := currentElem.FieldByName(currentField.Name) - currentFieldKind := currentField.Type.Kind() - - if currentFieldKind == reflect.Struct { - mergeFlagValues(currentFieldByName, flagKey, flagValues) - continue - } - - if getFieldNameByTag(currentField) != flagKey { - continue - } +func mergeFlagValues(configElemValue reflect.Value, flagPath []string, fullFlagName string, flagValues []string) error { + mergeFunction := func(flagName string, currentFieldStruct reflect.StructField, currentFieldElemValue reflect.Value, currentElemValue reflect.Value) error { + currentFieldKind := currentFieldStruct.Type.Kind() if currentFieldKind != reflect.Slice { - logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Invalid values %s for flag name %s, expected %s", strings.Join(flagValues, ","), flagKey, currentFieldKind)) - return + return fmt.Errorf("invalid values %s for flag name %s, expected %s", strings.Join(flagValues, ","), flagName, currentFieldKind) } - flagValueKind := currentField.Type.Elem().Kind() + flagValueKind := currentFieldStruct.Type.Elem().Kind() - parsedValues := reflect.MakeSlice(reflect.SliceOf(currentField.Type.Elem()), 0, 0) + parsedValues := reflect.MakeSlice(reflect.SliceOf(currentFieldStruct.Type.Elem()), 0, 0) for _, flagValue := range flagValues { parsedValue, err := getParsedValue(flagValueKind, flagValue) if err != nil { - logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Invalid value %s for flag name %s, expected %s", flagValue, flagKey, flagValueKind)) - return + return fmt.Errorf("invalid value %s for flag name %s, expected %s", flagValue, flagName, flagValueKind) } parsedValues = reflect.Append(parsedValues, parsedValue) } - currentFieldByName.Set(parsedValues) + currentFieldElemValue.Set(parsedValues) + return nil } + + return mergeFlag(configElemValue, flagPath, fullFlagName, mergeFunction) +} + +func mergeFlag(currentElemValue reflect.Value, currentFlagPath []string, fullFlagName string, mergeFunction func(flagName string, currentFieldStruct reflect.StructField, currentFieldElemValue reflect.Value, currentElemValue reflect.Value) error) error { + if len(currentFlagPath) == 0 { + return fmt.Errorf("flag \"%s\" not found", fullFlagName) + } + + for i := 0; i < currentElemValue.NumField(); i++ { + currentFieldStruct := currentElemValue.Type().Field(i) + currentFieldElemValue := currentElemValue.FieldByName(currentFieldStruct.Name) + + if currentFieldStruct.Type.Kind() == reflect.Struct && getFieldNameByTag(currentFieldStruct) == currentFlagPath[0] { + return mergeFlag(currentFieldElemValue, currentFlagPath[1:], fullFlagName, mergeFunction) + } + + if len(currentFlagPath) > 1 || getFieldNameByTag(currentFieldStruct) != currentFlagPath[0] { + continue + } + + return mergeFunction(currentFlagPath[0], currentFieldStruct, currentFieldElemValue, currentElemValue) + } + + return fmt.Errorf("flag \"%s\" not found", fullFlagName) } func getFieldNameByTag(field reflect.StructField) string { diff --git a/cli/config/configStruct.go b/cli/config/configStruct.go index c8947cf52..db7aad9b7 100644 --- a/cli/config/configStruct.go +++ b/cli/config/configStruct.go @@ -7,11 +7,7 @@ import ( ) const ( - AgentImageConfigName = "agent-image" MizuResourcesNamespaceConfigName = "mizu-resources-namespace" - TelemetryConfigName = "telemetry" - DumpLogsConfigName = "dump-logs" - KubeConfigPathName = "kube-config-path" ) type ConfigStruct struct { diff --git a/cli/config/configStructs/tapConfig.go b/cli/config/configStructs/tapConfig.go index 16403ca57..5f7a68eae 100644 --- a/cli/config/configStructs/tapConfig.go +++ b/cli/config/configStructs/tapConfig.go @@ -10,15 +10,12 @@ import ( ) const ( - AnalysisDestinationTapName = "dest" - SleepIntervalSecTapName = "upload-interval" GuiPortTapName = "gui-port" NamespacesTapName = "namespaces" AnalysisTapName = "analysis" AllNamespacesTapName = "all-namespaces" PlainTextFilterRegexesTapName = "regex-masking" DisableRedactionTapName = "no-redact" - IgnoredUserAgentsTapName = "ignored-user-agents" HumanMaxEntriesDBSizeTapName = "max-entries-db-size" DirectionTapName = "direction" DryRunTapName = "dry-run" diff --git a/cli/config/config_internal_test.go b/cli/config/config_internal_test.go new file mode 100644 index 000000000..101addb6f --- /dev/null +++ b/cli/config/config_internal_test.go @@ -0,0 +1,340 @@ +package config + +import ( + "reflect" + "testing" +) + +type ConfigMock struct { + SectionMock SectionMock `yaml:"section"` + Test string `yaml:"test"` + StringField string `yaml:"string-field"` + IntField int `yaml:"int-field"` + BoolField bool `yaml:"bool-field"` + UintField uint `yaml:"uint-field"` + StringSliceField []string `yaml:"string-slice-field"` + IntSliceField []int `yaml:"int-slice-field"` + BoolSliceField []bool `yaml:"bool-slice-field"` + UintSliceField []uint `yaml:"uint-slice-field"` +} + +type SectionMock struct { + Test string `yaml:"test"` +} + +func TestMergeSetFlagNoSeparator(t *testing.T) { + tests := [][]string{{""}, {"t"}, {"", "t"}, {"t", "test", "test:true"}, {"test", "test:true", "testing!", "true"}} + + for _, setValues := range tests { + configMock := ConfigMock{} + configMockElemValue := reflect.ValueOf(&configMock).Elem() + + err := mergeSetFlag(configMockElemValue, setValues) + + if err == nil { + t.Errorf("unexpected unhandled error - setValues: %v", setValues) + continue + } + + for i := 0; i < configMockElemValue.NumField(); i++ { + currentField := configMockElemValue.Type().Field(i) + currentFieldByName := configMockElemValue.FieldByName(currentField.Name) + + if !currentFieldByName.IsZero() { + t.Errorf("unexpected value with not default value - setValues: %v", setValues) + } + } + } +} + +func TestMergeSetFlagInvalidFlagName(t *testing.T) { + tests := [][]string{{"invalid_flag=true"}, {"section.invalid_flag=test"}, {"section=test"}, {"=true"}, {"invalid_flag=true", "config.invalid_flag=test", "section=test", "=true"}} + + for _, setValues := range tests { + configMock := ConfigMock{} + configMockElemValue := reflect.ValueOf(&configMock).Elem() + + err := mergeSetFlag(configMockElemValue, setValues) + + if err == nil { + t.Errorf("unexpected unhandled error - setValues: %v", setValues) + continue + } + + for i := 0; i < configMockElemValue.NumField(); i++ { + currentField := configMockElemValue.Type().Field(i) + currentFieldByName := configMockElemValue.FieldByName(currentField.Name) + + if !currentFieldByName.IsZero() { + t.Errorf("unexpected case - setValues: %v", setValues) + } + } + } +} + +func TestMergeSetFlagInvalidFlagValue(t *testing.T) { + tests := [][]string{{"int-field=true"}, {"bool-field:5"}, {"uint-field=-1"}, {"int-slice-field=true"}, {"bool-slice-field=5"}, {"uint-slice-field=-1"}, {"int-field=6", "int-field=66"}} + + for _, setValues := range tests { + configMock := ConfigMock{} + configMockElemValue := reflect.ValueOf(&configMock).Elem() + + err := mergeSetFlag(configMockElemValue, setValues) + + if err == nil { + t.Errorf("unexpected unhandled error - setValues: %v", setValues) + continue + } + + for i := 0; i < configMockElemValue.NumField(); i++ { + currentField := configMockElemValue.Type().Field(i) + currentFieldByName := configMockElemValue.FieldByName(currentField.Name) + + if !currentFieldByName.IsZero() { + t.Errorf("unexpected case - setValues: %v", setValues) + } + } + } +} + +func TestMergeSetFlagNotSliceValues(t *testing.T) { + tests := [][]struct { + SetValue string + FieldName string + FieldValue interface{} + }{ + {{SetValue: "string-field=test", FieldName: "StringField", FieldValue: "test"}}, + {{SetValue: "int-field=6", FieldName: "IntField", FieldValue: 6}}, + {{SetValue: "bool-field=true", FieldName: "BoolField", FieldValue: true}}, + {{SetValue: "uint-field=6", FieldName: "UintField", FieldValue: uint(6)}}, + { + {SetValue: "string-field=test", FieldName: "StringField", FieldValue: "test"}, + {SetValue: "int-field=6", FieldName: "IntField", FieldValue: 6}, + {SetValue: "bool-field=true", FieldName: "BoolField", FieldValue: true}, + {SetValue: "uint-field=6", FieldName: "UintField", FieldValue: uint(6)}, + }, + } + + for _, test := range tests { + configMock := ConfigMock{} + configMockElemValue := reflect.ValueOf(&configMock).Elem() + + var setValues []string + for _, setValueInfo := range test { + setValues = append(setValues, setValueInfo.SetValue) + } + + err := mergeSetFlag(configMockElemValue, setValues) + + if err != nil { + t.Errorf("unexpected error result - err: %v", err) + continue + } + + for _, setValueInfo := range test { + fieldValue := configMockElemValue.FieldByName(setValueInfo.FieldName).Interface() + if fieldValue != setValueInfo.FieldValue { + t.Errorf("unexpected result - expected: %v, actual: %v", setValueInfo.FieldValue, fieldValue) + } + } + } +} + +func TestMergeSetFlagSliceValues(t *testing.T) { + tests := [][]struct { + SetValues []string + FieldName string + FieldValue interface{} + }{ + {{SetValues: []string{"string-slice-field=test"}, FieldName: "StringSliceField", FieldValue: []string{"test"}}}, + {{SetValues: []string{"int-slice-field=6"}, FieldName: "IntSliceField", FieldValue: []int{6}}}, + {{SetValues: []string{"bool-slice-field=true"}, FieldName: "BoolSliceField", FieldValue: []bool{true}}}, + {{SetValues: []string{"uint-slice-field=6"}, FieldName: "UintSliceField", FieldValue: []uint{uint(6)}}}, + { + {SetValues: []string{"string-slice-field=test"}, FieldName: "StringSliceField", FieldValue: []string{"test"}}, + {SetValues: []string{"int-slice-field=6"}, FieldName: "IntSliceField", FieldValue: []int{6}}, + {SetValues: []string{"bool-slice-field=true"}, FieldName: "BoolSliceField", FieldValue: []bool{true}}, + {SetValues: []string{"uint-slice-field=6"}, FieldName: "UintSliceField", FieldValue: []uint{uint(6)}}, + }, + {{SetValues: []string{"string-slice-field=test", "string-slice-field=test2"}, FieldName: "StringSliceField", FieldValue: []string{"test", "test2"}}}, + {{SetValues: []string{"int-slice-field=6", "int-slice-field=66"}, FieldName: "IntSliceField", FieldValue: []int{6, 66}}}, + {{SetValues: []string{"bool-slice-field=true", "bool-slice-field=false"}, FieldName: "BoolSliceField", FieldValue: []bool{true, false}}}, + {{SetValues: []string{"uint-slice-field=6", "uint-slice-field=66"}, FieldName: "UintSliceField", FieldValue: []uint{uint(6), uint(66)}}}, + { + {SetValues: []string{"string-slice-field=test", "string-slice-field=test2"}, FieldName: "StringSliceField", FieldValue: []string{"test", "test2"}}, + {SetValues: []string{"int-slice-field=6", "int-slice-field=66"}, FieldName: "IntSliceField", FieldValue: []int{6, 66}}, + {SetValues: []string{"bool-slice-field=true", "bool-slice-field=false"}, FieldName: "BoolSliceField", FieldValue: []bool{true, false}}, + {SetValues: []string{"uint-slice-field=6", "uint-slice-field=66"}, FieldName: "UintSliceField", FieldValue: []uint{uint(6), uint(66)}}, + }, + } + + for _, test := range tests { + configMock := ConfigMock{} + configMockElemValue := reflect.ValueOf(&configMock).Elem() + + var setValues []string + for _, setValueInfo := range test { + for _, setValue := range setValueInfo.SetValues { + setValues = append(setValues, setValue) + } + } + + err := mergeSetFlag(configMockElemValue, setValues) + + if err != nil { + t.Errorf("unexpected error result - err: %v", err) + continue + } + + for _, setValueInfo := range test { + fieldValue := configMockElemValue.FieldByName(setValueInfo.FieldName).Interface() + if !reflect.DeepEqual(fieldValue, setValueInfo.FieldValue) { + t.Errorf("unexpected result - expected: %v, actual: %v", setValueInfo.FieldValue, fieldValue) + } + } + } +} + +func TestMergeSetFlagMixValues(t *testing.T) { + tests := [][]struct { + SetValues []string + FieldName string + FieldValue interface{} + }{ + { + {SetValues: []string{"string-slice-field=test"}, FieldName: "StringSliceField", FieldValue: []string{"test"}}, + {SetValues: []string{"int-slice-field=6"}, FieldName: "IntSliceField", FieldValue: []int{6}}, + {SetValues: []string{"bool-slice-field=true"}, FieldName: "BoolSliceField", FieldValue: []bool{true}}, + {SetValues: []string{"uint-slice-field=6"}, FieldName: "UintSliceField", FieldValue: []uint{uint(6)}}, + {SetValues: []string{"string-field=test"}, FieldName: "StringField", FieldValue: "test"}, + {SetValues: []string{"int-field=6"}, FieldName: "IntField", FieldValue: 6}, + {SetValues: []string{"bool-field=true"}, FieldName: "BoolField", FieldValue: true}, + {SetValues: []string{"uint-field=6"}, FieldName: "UintField", FieldValue: uint(6)}, + }, + { + {SetValues: []string{"string-slice-field=test", "string-slice-field=test2"}, FieldName: "StringSliceField", FieldValue: []string{"test", "test2"}}, + {SetValues: []string{"int-slice-field=6", "int-slice-field=66"}, FieldName: "IntSliceField", FieldValue: []int{6, 66}}, + {SetValues: []string{"bool-slice-field=true", "bool-slice-field=false"}, FieldName: "BoolSliceField", FieldValue: []bool{true, false}}, + {SetValues: []string{"uint-slice-field=6", "uint-slice-field=66"}, FieldName: "UintSliceField", FieldValue: []uint{uint(6), uint(66)}}, + {SetValues: []string{"string-field=test"}, FieldName: "StringField", FieldValue: "test"}, + {SetValues: []string{"int-field=6"}, FieldName: "IntField", FieldValue: 6}, + {SetValues: []string{"bool-field=true"}, FieldName: "BoolField", FieldValue: true}, + {SetValues: []string{"uint-field=6"}, FieldName: "UintField", FieldValue: uint(6)}, + }, + } + + for _, test := range tests { + configMock := ConfigMock{} + configMockElemValue := reflect.ValueOf(&configMock).Elem() + + var setValues []string + for _, setValueInfo := range test { + for _, setValue := range setValueInfo.SetValues { + setValues = append(setValues, setValue) + } + } + + err := mergeSetFlag(configMockElemValue, setValues) + + if err != nil { + t.Errorf("unexpected error result - err: %v", err) + continue + } + + for _, setValueInfo := range test { + fieldValue := configMockElemValue.FieldByName(setValueInfo.FieldName).Interface() + if !reflect.DeepEqual(fieldValue, setValueInfo.FieldValue) { + t.Errorf("unexpected result - expected: %v, actual: %v", setValueInfo.FieldValue, fieldValue) + } + } + } +} + +func TestGetParsedValueValidValue(t *testing.T) { + tests := []struct { + StringValue string + Kind reflect.Kind + ActualValue interface{} + }{ + {StringValue: "test", Kind: reflect.String, ActualValue: "test"}, + {StringValue: "123", Kind: reflect.String, ActualValue: "123"}, + {StringValue: "true", Kind: reflect.Bool, ActualValue: true}, + {StringValue: "false", Kind: reflect.Bool, ActualValue: false}, + {StringValue: "6", Kind: reflect.Int, ActualValue: 6}, + {StringValue: "-6", Kind: reflect.Int, ActualValue: -6}, + {StringValue: "6", Kind: reflect.Int8, ActualValue: int8(6)}, + {StringValue: "-6", Kind: reflect.Int8, ActualValue: int8(-6)}, + {StringValue: "6", Kind: reflect.Int16, ActualValue: int16(6)}, + {StringValue: "-6", Kind: reflect.Int16, ActualValue: int16(-6)}, + {StringValue: "6", Kind: reflect.Int32, ActualValue: int32(6)}, + {StringValue: "-6", Kind: reflect.Int32, ActualValue: int32(-6)}, + {StringValue: "6", Kind: reflect.Int64, ActualValue: int64(6)}, + {StringValue: "-6", Kind: reflect.Int64, ActualValue: int64(-6)}, + {StringValue: "6", Kind: reflect.Uint, ActualValue: uint(6)}, + {StringValue: "66", Kind: reflect.Uint, ActualValue: uint(66)}, + {StringValue: "6", Kind: reflect.Uint8, ActualValue: uint8(6)}, + {StringValue: "66", Kind: reflect.Uint8, ActualValue: uint8(66)}, + {StringValue: "6", Kind: reflect.Uint16, ActualValue: uint16(6)}, + {StringValue: "66", Kind: reflect.Uint16, ActualValue: uint16(66)}, + {StringValue: "6", Kind: reflect.Uint32, ActualValue: uint32(6)}, + {StringValue: "66", Kind: reflect.Uint32, ActualValue: uint32(66)}, + {StringValue: "6", Kind: reflect.Uint64, ActualValue: uint64(6)}, + {StringValue: "66", Kind: reflect.Uint64, ActualValue: uint64(66)}, + } + + for _, test := range tests { + parsedValue, err := getParsedValue(test.Kind, test.StringValue) + + if err != nil { + t.Errorf("unexpected error result - err: %v", err) + continue + } + + if parsedValue.Interface() != test.ActualValue { + t.Errorf("unexpected result - expected: %v, actual: %v", test.ActualValue, parsedValue) + } + } +} + +func TestGetParsedValueInvalidValue(t *testing.T) { + tests := []struct { + StringValue string + Kind reflect.Kind + }{ + {StringValue: "test", Kind: reflect.Bool}, + {StringValue: "123", Kind: reflect.Bool}, + {StringValue: "test", Kind: reflect.Int}, + {StringValue: "true", Kind: reflect.Int}, + {StringValue: "test", Kind: reflect.Int8}, + {StringValue: "true", Kind: reflect.Int8}, + {StringValue: "test", Kind: reflect.Int16}, + {StringValue: "true", Kind: reflect.Int16}, + {StringValue: "test", Kind: reflect.Int32}, + {StringValue: "true", Kind: reflect.Int32}, + {StringValue: "test", Kind: reflect.Int64}, + {StringValue: "true", Kind: reflect.Int64}, + {StringValue: "test", Kind: reflect.Uint}, + {StringValue: "-6", Kind: reflect.Uint}, + {StringValue: "test", Kind: reflect.Uint8}, + {StringValue: "-6", Kind: reflect.Uint8}, + {StringValue: "test", Kind: reflect.Uint16}, + {StringValue: "-6", Kind: reflect.Uint16}, + {StringValue: "test", Kind: reflect.Uint32}, + {StringValue: "-6", Kind: reflect.Uint32}, + {StringValue: "test", Kind: reflect.Uint64}, + {StringValue: "-6", Kind: reflect.Uint64}, + } + + for _, test := range tests { + parsedValue, err := getParsedValue(test.Kind, test.StringValue) + + if err == nil { + t.Errorf("unexpected unhandled error - stringValue: %v, Kind: %v", test.StringValue, test.Kind) + continue + } + + if parsedValue != reflect.ValueOf(nil) { + t.Errorf("unexpected parsed value - parsedValue: %v", parsedValue) + } + } +} diff --git a/cli/config/config_test.go b/cli/config/config_test.go index 286d497b4..8e3fcbb38 100644 --- a/cli/config/config_test.go +++ b/cli/config/config_test.go @@ -13,10 +13,10 @@ func TestConfigWriteIgnoresReadonlyFields(t *testing.T) { configElem := reflect.ValueOf(&config.ConfigStruct{}).Elem() getFieldsWithReadonlyTag(configElem, &readonlyFields) - config, _ := config.GetConfigWithDefaults() + configWithDefaults, _ := config.GetConfigWithDefaults() for _, readonlyField := range readonlyFields { - if strings.Contains(config, readonlyField) { - t.Errorf("unexpected result - readonly field: %v, config: %v", readonlyField, config) + if strings.Contains(configWithDefaults, readonlyField) { + t.Errorf("unexpected result - readonly field: %v, config: %v", readonlyField, configWithDefaults) } } }