Skip to content

Commit

Permalink
Update unit test for clientConfig.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalii Chepeliuk authored and vchepeli committed Jan 17, 2018
1 parent 539de74 commit 3d467b1
Showing 1 changed file with 62 additions and 40 deletions.
102 changes: 62 additions & 40 deletions pkg/cmd/clientConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,109 @@ import (
"reflect"
"testing"

"regexp"

"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
kFake "k8s.io/client-go/kubernetes/fake"
)

type args struct {
k8Client kubernetes.Interface
}

func TestNewClientConfigCmd(t *testing.T) {
k8Client := &kFake.Clientset{}
getFakeK8Client := func() kubernetes.Interface {
return &kFake.Clientset{}
}
fakeK8Client := getFakeK8Client()
tests := []struct {
name string
args args
want *ClientConfigCmd
name string
k8Client kubernetes.Interface
want *ClientConfigCmd
}{
{
name: "new client config command",
args: args{k8Client: k8Client},
name: "new client config command",
k8Client: fakeK8Client,
want: &ClientConfigCmd{
k8Client: k8Client,
k8Client: fakeK8Client,
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := NewClientConfigCmd(tc.args.k8Client); !reflect.DeepEqual(got, tc.want) {
if got := NewClientConfigCmd(tc.k8Client); !reflect.DeepEqual(got, tc.want) {
t.Errorf("NewClientConfigCmd() = %v, want %v", got, tc.want)
}
})
}
}

func TestClientConfigCmd_GetClientConfigCmd(t *testing.T) {
fakeK8Client := &kFake.Clientset{}
fakeCbrCmd := &cobra.Command{
Use: "clientconfig",
Short: "get clientconfig returns a client ready filtered configuration of the available services.",
Long: `get clientconfig
getFakeCbrCmd := func() *cobra.Command {
return &cobra.Command{
Use: "clientconfig",
Short: "get clientconfig returns a client ready filtered configuration of the available services.",
Long: `get clientconfig
mobile --namespace=myproject get clientconfig
kubectl plugin mobile get clientconfig`,
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}
}

tests := []struct {
name string
args args
name string
k8Client func() kubernetes.Interface
namespace string
cobraCmd *cobra.Command
ExpectError bool
ErrorPattern string
}{
{
name: "get client config command",
args: args{k8Client: fakeK8Client},
name: "get client config command with empty namespace",
k8Client: func() kubernetes.Interface {
return &kFake.Clientset{}
},
namespace: "",
cobraCmd: getFakeCbrCmd(),
ExpectError: true,
ErrorPattern: "no namespace present. Cannot continue. Please set the --namespace flag or the KUBECTL_PLUGINS_CURRENT_NAMESPACE env var",
},
{
name: "get client config command with testing-ns namespace",
k8Client: func() kubernetes.Interface {
return &kFake.Clientset{}
},
namespace: "testing-ns",
cobraCmd: getFakeCbrCmd(),
ExpectError: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ccCmd := &ClientConfigCmd{
k8Client: tc.args.k8Client,
k8Client: tc.k8Client(),
}

got := ccCmd.GetClientConfigCmd()
if use := got.Use; !reflect.DeepEqual(use, fakeCbrCmd.Use) {
t.Errorf("ClientConfigCmd.GetClientConfigCmd().Use = %v, want %v", use, fakeCbrCmd.Use)
if use := got.Use; use != tc.cobraCmd.Use {
t.Errorf("ClientConfigCmd.GetClientConfigCmd().Use = %v, want %v", use, tc.cobraCmd.Use)
}
if short := got.Short; !reflect.DeepEqual(short, fakeCbrCmd.Short) {
t.Errorf("ClientConfigCmd.GetClientConfigCmd().Short = %v, want %v", short, fakeCbrCmd.Short)
}
if runE := got.RunE; !reflect.DeepEqual(reflect.TypeOf(runE), reflect.TypeOf(fakeCbrCmd.RunE)) {
t.Errorf("ClientConfigCmd.GetClientConfigCmd().RunE = %v, want %v", reflect.TypeOf(runE), reflect.TypeOf(fakeCbrCmd.RunE))
if short := got.Short; short != tc.cobraCmd.Short {
t.Errorf("ClientConfigCmd.GetClientConfigCmd().Short = %v, want %v", short, tc.cobraCmd.Short)
}

runE := got.RunE
fakeCbrCmd.Flags().String("namespace", "", "Namespace for software installation")
err := runE(fakeCbrCmd, nil) // args are not used in RunE function
if err == nil {
t.Errorf("ClientConfigCmd.GetClientConfigCmd().RunE() should fail with error")
tc.cobraCmd.Flags().String("namespace", tc.namespace, "Namespace for software installation")
err := runE(tc.cobraCmd, nil) // args are not used in RunE function
if tc.ExpectError && err == nil {
t.Errorf("ClientConfigCmd.GetClientConfigCmd().RunE() expected an error but got none")
}
if !tc.ExpectError && err != nil {
t.Errorf("ClientConfigCmd.GetClientConfigCmd().RunE() expect no error but got one %v", err)
}
fakeCbrCmd.Flags().Set("namespace", "tesing-ns")
err = runE(fakeCbrCmd, nil) // args are not used in RunE function
if err != nil {
t.Errorf("ClientConfigCmd.GetClientConfigCmd().RunE() should not fail with error")
if tc.ExpectError && err != nil {
if m, err := regexp.Match(tc.ErrorPattern, []byte(err.Error())); !m {
t.Errorf("expected regex %v to match error %v", tc.ErrorPattern, err)
}
}
})
}
Expand Down

0 comments on commit 3d467b1

Please sign in to comment.