mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-09-08 22:10:50 +00:00
added set hierarchy, removed allowed set flags (#205)
This commit is contained in:
@@ -27,18 +27,10 @@ const (
|
|||||||
ReadonlyTag = "readonly"
|
ReadonlyTag = "readonly"
|
||||||
)
|
)
|
||||||
|
|
||||||
var allowedSetFlags = []string{
|
var (
|
||||||
AgentImageConfigName,
|
Config = ConfigStruct{}
|
||||||
MizuResourcesNamespaceConfigName,
|
cmdName string
|
||||||
TelemetryConfigName,
|
)
|
||||||
DumpLogsConfigName,
|
|
||||||
KubeConfigPathName,
|
|
||||||
configStructs.AnalysisDestinationTapName,
|
|
||||||
configStructs.SleepIntervalSecTapName,
|
|
||||||
configStructs.IgnoredUserAgentsTapName,
|
|
||||||
}
|
|
||||||
|
|
||||||
var Config = ConfigStruct{}
|
|
||||||
|
|
||||||
func (config *ConfigStruct) Validate() error {
|
func (config *ConfigStruct) Validate() error {
|
||||||
if config.IsNsRestrictedMode() {
|
if config.IsNsRestrictedMode() {
|
||||||
@@ -52,6 +44,8 @@ func (config *ConfigStruct) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func InitConfig(cmd *cobra.Command) error {
|
func InitConfig(cmd *cobra.Command) error {
|
||||||
|
cmdName = cmd.Name()
|
||||||
|
|
||||||
if err := defaults.Set(&Config); err != nil {
|
if err := defaults.Set(&Config); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -105,121 +99,135 @@ func mergeConfigFile() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initFlag(f *pflag.Flag) {
|
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)
|
sliceValue, isSliceValue := f.Value.(pflag.SliceValue)
|
||||||
if !isSliceValue {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if f.Name == SetCommandName {
|
if f.Name == SetCommandName {
|
||||||
mergeSetFlag(configElem, sliceValue.GetSlice())
|
if err := mergeSetFlag(configElemValue, sliceValue.GetSlice()); err != nil {
|
||||||
|
logger.Log.Warningf(uiUtils.Warning, err)
|
||||||
|
}
|
||||||
return
|
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{}
|
setMap := map[string][]string{}
|
||||||
|
|
||||||
for _, setValue := range setValues {
|
for _, setValue := range setValues {
|
||||||
if !strings.Contains(setValue, Separator) {
|
if !strings.Contains(setValue, Separator) {
|
||||||
logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Ignoring set argument %s (set argument format: <flag name>=<flag value>)", setValue))
|
setErrors = append(setErrors, fmt.Sprintf("Ignoring set argument %s (set argument format: <flag name>=<flag value>)", setValue))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
split := strings.SplitN(setValue, Separator, 2)
|
split := strings.SplitN(setValue, Separator, 2)
|
||||||
if len(split) != 2 {
|
|
||||||
logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Ignoring set argument %s (set argument format: <flag name>=<flag value>)", setValue))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
argumentKey, argumentValue := split[0], split[1]
|
argumentKey, argumentValue := split[0], split[1]
|
||||||
|
|
||||||
setMap[argumentKey] = append(setMap[argumentKey], argumentValue)
|
setMap[argumentKey] = append(setMap[argumentKey], argumentValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
for argumentKey, argumentValues := range setMap {
|
for argumentKey, argumentValues := range setMap {
|
||||||
if !mizu.Contains(allowedSetFlags, argumentKey) {
|
flagPath := strings.Split(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
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(argumentValues) > 1 {
|
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 {
|
} 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) {
|
func mergeFlagValue(configElemValue reflect.Value, flagPath []string, fullFlagName string, flagValue string) error {
|
||||||
for i := 0; i < currentElem.NumField(); i++ {
|
mergeFunction := func(flagName string, currentFieldStruct reflect.StructField, currentFieldElemValue reflect.Value, currentElemValue reflect.Value) error {
|
||||||
currentField := currentElem.Type().Field(i)
|
currentFieldKind := currentFieldStruct.Type.Kind()
|
||||||
currentFieldByName := currentElem.FieldByName(currentField.Name)
|
|
||||||
currentFieldKind := currentField.Type.Kind()
|
|
||||||
|
|
||||||
if currentFieldKind == reflect.Struct {
|
|
||||||
mergeFlagValue(currentFieldByName, flagKey, flagValue)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if getFieldNameByTag(currentField) != flagKey {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if currentFieldKind == reflect.Slice {
|
if currentFieldKind == reflect.Slice {
|
||||||
mergeFlagValues(currentElem, flagKey, []string{flagValue})
|
return mergeFlagValues(currentElemValue, []string{flagName}, fullFlagName, []string{flagValue})
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedValue, err := getParsedValue(currentFieldKind, flagValue)
|
parsedValue, err := getParsedValue(currentFieldKind, flagValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Invalid value %s for flag name %s, expected %s", flagValue, flagKey, currentFieldKind))
|
return fmt.Errorf("invalid value %s for flag name %s, expected %s", flagValue, flagName, currentFieldKind)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
currentFieldByName.Set(parsedValue)
|
currentFieldElemValue.Set(parsedValue)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return mergeFlag(configElemValue, flagPath, fullFlagName, mergeFunction)
|
||||||
}
|
}
|
||||||
|
|
||||||
func mergeFlagValues(currentElem reflect.Value, flagKey string, flagValues []string) {
|
func mergeFlagValues(configElemValue reflect.Value, flagPath []string, fullFlagName string, flagValues []string) error {
|
||||||
for i := 0; i < currentElem.NumField(); i++ {
|
mergeFunction := func(flagName string, currentFieldStruct reflect.StructField, currentFieldElemValue reflect.Value, currentElemValue reflect.Value) error {
|
||||||
currentField := currentElem.Type().Field(i)
|
currentFieldKind := currentFieldStruct.Type.Kind()
|
||||||
currentFieldByName := currentElem.FieldByName(currentField.Name)
|
|
||||||
currentFieldKind := currentField.Type.Kind()
|
|
||||||
|
|
||||||
if currentFieldKind == reflect.Struct {
|
|
||||||
mergeFlagValues(currentFieldByName, flagKey, flagValues)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if getFieldNameByTag(currentField) != flagKey {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if currentFieldKind != reflect.Slice {
|
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 fmt.Errorf("invalid values %s for flag name %s, expected %s", strings.Join(flagValues, ","), flagName, currentFieldKind)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
for _, flagValue := range flagValues {
|
||||||
parsedValue, err := getParsedValue(flagValueKind, flagValue)
|
parsedValue, err := getParsedValue(flagValueKind, flagValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Invalid value %s for flag name %s, expected %s", flagValue, flagKey, flagValueKind))
|
return fmt.Errorf("invalid value %s for flag name %s, expected %s", flagValue, flagName, flagValueKind)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedValues = reflect.Append(parsedValues, parsedValue)
|
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 {
|
func getFieldNameByTag(field reflect.StructField) string {
|
||||||
|
@@ -7,11 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
AgentImageConfigName = "agent-image"
|
|
||||||
MizuResourcesNamespaceConfigName = "mizu-resources-namespace"
|
MizuResourcesNamespaceConfigName = "mizu-resources-namespace"
|
||||||
TelemetryConfigName = "telemetry"
|
|
||||||
DumpLogsConfigName = "dump-logs"
|
|
||||||
KubeConfigPathName = "kube-config-path"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ConfigStruct struct {
|
type ConfigStruct struct {
|
||||||
|
@@ -10,15 +10,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
AnalysisDestinationTapName = "dest"
|
|
||||||
SleepIntervalSecTapName = "upload-interval"
|
|
||||||
GuiPortTapName = "gui-port"
|
GuiPortTapName = "gui-port"
|
||||||
NamespacesTapName = "namespaces"
|
NamespacesTapName = "namespaces"
|
||||||
AnalysisTapName = "analysis"
|
AnalysisTapName = "analysis"
|
||||||
AllNamespacesTapName = "all-namespaces"
|
AllNamespacesTapName = "all-namespaces"
|
||||||
PlainTextFilterRegexesTapName = "regex-masking"
|
PlainTextFilterRegexesTapName = "regex-masking"
|
||||||
DisableRedactionTapName = "no-redact"
|
DisableRedactionTapName = "no-redact"
|
||||||
IgnoredUserAgentsTapName = "ignored-user-agents"
|
|
||||||
HumanMaxEntriesDBSizeTapName = "max-entries-db-size"
|
HumanMaxEntriesDBSizeTapName = "max-entries-db-size"
|
||||||
DirectionTapName = "direction"
|
DirectionTapName = "direction"
|
||||||
DryRunTapName = "dry-run"
|
DryRunTapName = "dry-run"
|
||||||
|
340
cli/config/config_internal_test.go
Normal file
340
cli/config/config_internal_test.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -13,10 +13,10 @@ func TestConfigWriteIgnoresReadonlyFields(t *testing.T) {
|
|||||||
configElem := reflect.ValueOf(&config.ConfigStruct{}).Elem()
|
configElem := reflect.ValueOf(&config.ConfigStruct{}).Elem()
|
||||||
getFieldsWithReadonlyTag(configElem, &readonlyFields)
|
getFieldsWithReadonlyTag(configElem, &readonlyFields)
|
||||||
|
|
||||||
config, _ := config.GetConfigWithDefaults()
|
configWithDefaults, _ := config.GetConfigWithDefaults()
|
||||||
for _, readonlyField := range readonlyFields {
|
for _, readonlyField := range readonlyFields {
|
||||||
if strings.Contains(config, readonlyField) {
|
if strings.Contains(configWithDefaults, readonlyField) {
|
||||||
t.Errorf("unexpected result - readonly field: %v, config: %v", readonlyField, config)
|
t.Errorf("unexpected result - readonly field: %v, config: %v", readonlyField, configWithDefaults)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user