bug(cli)fix kubectl config unset unexist map key will add this key, should tell user this key not exist

pull/6/head
zhengjiajin 2017-09-24 22:23:13 +08:00
parent 5f8726e427
commit 56930674c9
3 changed files with 55 additions and 16 deletions

View File

@ -152,6 +152,9 @@ func modifyConfig(curr reflect.Value, steps *navigationSteps, propertyValue stri
needToSetNewMapValue := currMapValue.Kind() == reflect.Invalid needToSetNewMapValue := currMapValue.Kind() == reflect.Invalid
if needToSetNewMapValue { if needToSetNewMapValue {
if unset {
return fmt.Errorf("current map key `%v` is invalid", mapKey.Interface())
}
currMapValue = reflect.New(mapValueType.Elem()).Elem().Addr() currMapValue = reflect.New(mapValueType.Elem()).Elem().Addr()
actualCurrValue.SetMapIndex(mapKey, currMapValue) actualCurrValue.SetMapIndex(mapKey, currMapValue)
} }

View File

@ -48,16 +48,16 @@ func NewCmdConfigUnset(out io.Writer, configAccess clientcmd.ConfigAccess) *cobr
Short: i18n.T("Unsets an individual value in a kubeconfig file"), Short: i18n.T("Unsets an individual value in a kubeconfig file"),
Long: unset_long, Long: unset_long,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(options.complete(cmd)) cmdutil.CheckErr(options.complete(cmd, args))
cmdutil.CheckErr(options.run()) cmdutil.CheckErr(options.run(out))
fmt.Fprintf(out, "Property %q unset.\n", options.propertyName)
}, },
} }
return cmd return cmd
} }
func (o unsetOptions) run() error { func (o unsetOptions) run(out io.Writer) error {
err := o.validate() err := o.validate()
if err != nil { if err != nil {
return err return err
@ -77,16 +77,21 @@ func (o unsetOptions) run() error {
return err return err
} }
return clientcmd.ModifyConfig(o.configAccess, *config, false) if err := clientcmd.ModifyConfig(o.configAccess, *config, false); err != nil {
return err
}
if _, err := fmt.Fprintf(out, "Property %q unset.\n", o.propertyName); err != nil {
return err
}
return nil
} }
func (o *unsetOptions) complete(cmd *cobra.Command) error { func (o *unsetOptions) complete(cmd *cobra.Command, args []string) error {
endingArgs := cmd.Flags().Args() if len(args) != 1 {
if len(endingArgs) != 1 { return helpErrorf(cmd, "Unexpected args: %v", args)
return helpErrorf(cmd, "Unexpected args: %v", endingArgs)
} }
o.propertyName = endingArgs[0] o.propertyName = args[0]
return nil return nil
} }

View File

@ -28,9 +28,10 @@ import (
type unsetConfigTest struct { type unsetConfigTest struct {
description string description string
config clientcmdapi.Config //initiate kubectl config config clientcmdapi.Config
args []string //kubectl config unset args args []string
expected string //expect out expected string
expectedErr string
} }
func TestUnsetConfigString(t *testing.T) { func TestUnsetConfigString(t *testing.T) {
@ -79,6 +80,31 @@ func TestUnsetConfigMap(t *testing.T) {
test.run(t) test.run(t)
} }
func TestUnsetUnexistConfig(t *testing.T) {
conf := clientcmdapi.Config{
Kind: "Config",
APIVersion: "v1",
Clusters: map[string]*clientcmdapi.Cluster{
"minikube": {Server: "https://192.168.99.100:8443"},
"my-cluster": {Server: "https://192.168.0.1:3434"},
},
Contexts: map[string]*clientcmdapi.Context{
"minikube": {AuthInfo: "minikube", Cluster: "minikube"},
"my-cluster": {AuthInfo: "mu-cluster", Cluster: "my-cluster"},
},
CurrentContext: "minikube",
}
test := unsetConfigTest{
description: "Testing for kubectl config unset a unexist map key",
config: conf,
args: []string{"contexts.foo.namespace"},
expectedErr: "current map key `foo` is invalid",
}
test.run(t)
}
func (test unsetConfigTest) run(t *testing.T) { func (test unsetConfigTest) run(t *testing.T) {
fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "") fakeKubeFile, err := ioutil.TempFile(os.TempDir(), "")
if err != nil { if err != nil {
@ -94,14 +120,19 @@ func (test unsetConfigTest) run(t *testing.T) {
pathOptions.EnvVar = "" pathOptions.EnvVar = ""
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
cmd := NewCmdConfigUnset(buf, pathOptions) cmd := NewCmdConfigUnset(buf, pathOptions)
cmd.SetArgs(test.args) opts := &unsetOptions{configAccess: pathOptions}
if err := cmd.Execute(); err != nil { err = opts.complete(cmd, test.args)
t.Fatalf("unexpected error executing command: %v,kubectl unset args: %v", err, test.args) if err == nil {
err = opts.run(buf)
} }
config, err := clientcmd.LoadFromFile(fakeKubeFile.Name()) config, err := clientcmd.LoadFromFile(fakeKubeFile.Name())
if err != nil { if err != nil {
t.Fatalf("unexpected error loading kubeconfig file: %v", err) t.Fatalf("unexpected error loading kubeconfig file: %v", err)
} }
if err != nil && err.Error() != test.expectedErr {
t.Fatalf("expected error:\n %v\nbut got error:\n%v", test.expectedErr, err)
}
if len(test.expected) != 0 { if len(test.expected) != 0 {
if buf.String() != test.expected { if buf.String() != test.expected {
t.Errorf("Failed in :%q\n expected %v\n but got %v", test.description, test.expected, buf.String()) t.Errorf("Failed in :%q\n expected %v\n but got %v", test.description, test.expected, buf.String())