Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0826 committed Dec 30, 2020
1 parent d73cc47 commit 30b0942
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
5 changes: 2 additions & 3 deletions cmd/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (sc *SwitchCommand) runSwitch(command *cobra.Command, args []string) error

func handleQuickSwitch(config *clientcmdapi.Config, name string) (*clientcmdapi.Config, error) {
if _, ok := config.Contexts[name]; !ok {
return nil, errors.New("cannot find context named 「" + name + "」")
return config, errors.New("cannot find context named 「" + name + "」")
}
config.CurrentContext = name
return config, nil
Expand All @@ -83,15 +83,14 @@ func handleOperation(config *clientcmdapi.Config) (*clientcmdapi.Config, error)
// exit option
kubeItems, err := ExitOption(kubeItems)
if err != nil {
return nil, err
return config, err
}
num := SelectUI(kubeItems, "Select Kube Context")
kubeName := kubeItems[num].Name
config.CurrentContext = kubeName
return config, nil
}

//TODO need add test
//TODO need update docs

func switchExample() string {
Expand Down
57 changes: 57 additions & 0 deletions cmd/switch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cmd

import (
"reflect"
"testing"

clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

var (
switchConfig = clientcmdapi.Config{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"black-user": {Token: "black-token"}},
Clusters: map[string]*clientcmdapi.Cluster{
"pig-cluster": {Server: "http://pig.org:8080"}},
Contexts: map[string]*clientcmdapi.Context{
"root-context": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}},
}
currentContextSwitchConfig = clientcmdapi.Config{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"black-user": {Token: "black-token"}},
Clusters: map[string]*clientcmdapi.Cluster{
"pig-cluster": {Server: "http://pig.org:8080"}},
Contexts: map[string]*clientcmdapi.Context{
"root-context": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}},
CurrentContext: "root-context",
}
)

func Test_handleQuickSwitch(t *testing.T) {
type args struct {
config *clientcmdapi.Config
name string
}
tests := []struct {
name string
args args
want *clientcmdapi.Config
wantErr bool
}{
// TODO: Add test cases.
{"exist-context", args{&switchConfig, "root-context"}, &currentContextSwitchConfig, false},
{"no-exist-context", args{&switchConfig, "test"}, &currentContextSwitchConfig, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := handleQuickSwitch(tt.args.config, tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("handleQuickSwitch() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("handleQuickSwitch() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 30b0942

Please sign in to comment.