@@ -16,11 +16,14 @@ package cmd_test
16
16
17
17
import (
18
18
"bytes"
19
+ "fmt"
20
+ "strings"
19
21
"testing"
20
22
21
23
"regexp"
22
24
23
25
"github.com/aerogear/mobile-cli/pkg/cmd"
26
+ "github.com/pkg/errors"
24
27
"github.com/spf13/cobra"
25
28
"k8s.io/apimachinery/pkg/runtime"
26
29
"k8s.io/client-go/kubernetes"
@@ -34,11 +37,11 @@ import (
34
37
func TestClientConfigCmd_GetClientConfigCmd (t * testing.T ) {
35
38
getFakeCbrCmd := func () * cobra.Command {
36
39
return & cobra.Command {
37
- Use : "clientconfig" ,
40
+ Use : "clientconfig <clientID> " ,
38
41
Short : "get clientconfig returns a client ready filtered configuration of the available services." ,
39
42
Long : `get clientconfig
40
- mobile --namespace=myproject get clientconfig
41
- kubectl plugin mobile get clientconfig` ,
43
+ mobile --namespace=myproject get clientconfig
44
+ kubectl plugin mobile get clientconfig` ,
42
45
RunE : func (cmd * cobra.Command , args []string ) error {
43
46
return nil
44
47
},
@@ -49,28 +52,40 @@ func TestClientConfigCmd_GetClientConfigCmd(t *testing.T) {
49
52
name string
50
53
k8Client func () kubernetes.Interface
51
54
namespace string
55
+ args []string
52
56
cobraCmd * cobra.Command
53
57
ExpectError bool
54
58
ErrorPattern string
59
+ ValidateOut func (bytes.Buffer ) error
55
60
}{
56
61
{
57
62
name : "get client config command with empty namespace" ,
58
63
k8Client : func () kubernetes.Interface {
59
64
return & kFake.Clientset {}
60
65
},
61
66
namespace : "" ,
67
+ args : []string {"client-id" },
62
68
cobraCmd : getFakeCbrCmd (),
63
69
ExpectError : true ,
64
70
ErrorPattern : "no namespace present. Cannot continue. Please set the --namespace flag or the KUBECTL_PLUGINS_CURRENT_NAMESPACE env var" ,
71
+ ValidateOut : func (out bytes.Buffer ) error { return nil },
65
72
},
66
73
{
67
- name : "get client config command with testing-ns namespace " ,
74
+ name : "get client config command with no services " ,
68
75
k8Client : func () kubernetes.Interface {
69
76
return & kFake.Clientset {}
70
77
},
71
78
namespace : "testing-ns" ,
79
+ args : []string {"client-id" },
72
80
cobraCmd : getFakeCbrCmd (),
73
81
ExpectError : false ,
82
+ ValidateOut : func (out bytes.Buffer ) error {
83
+ expected := `{"services":[],"namespace":"testing-ns","client_id":"client-id"}`
84
+ if strings .TrimSpace (out .String ()) != expected {
85
+ return errors .New (fmt .Sprintf ("expected: '%v', got: '%v'" , expected , strings .TrimSpace (out .String ())))
86
+ }
87
+ return nil
88
+ },
74
89
},
75
90
{
76
91
name : "get client config command with services" ,
@@ -110,8 +125,16 @@ func TestClientConfigCmd_GetClientConfigCmd(t *testing.T) {
110
125
return fakeclient
111
126
},
112
127
namespace : "testing-ns" ,
128
+ args : []string {"client-id" },
113
129
cobraCmd : getFakeCbrCmd (),
114
130
ExpectError : false ,
131
+ ValidateOut : func (out bytes.Buffer ) error {
132
+ expected := `{"services":[{"config":{"headers":{},"name":"test-service","uri":""},"name":"test-service"},{"config":{"headers":{}},"name":"keycloak"}],"namespace":"testing-ns","client_id":"client-id"}`
133
+ if strings .TrimSpace (out .String ()) != expected {
134
+ return errors .New (fmt .Sprintf ("expected: '%v', got: '%v'" , expected , strings .TrimSpace (out .String ())))
135
+ }
136
+ return nil
137
+ },
115
138
},
116
139
}
117
140
for _ , tc := range tests {
@@ -129,7 +152,7 @@ func TestClientConfigCmd_GetClientConfigCmd(t *testing.T) {
129
152
130
153
runE := got .RunE
131
154
tc .cobraCmd .Flags ().String ("namespace" , tc .namespace , "Namespace for software installation" )
132
- err := runE (tc .cobraCmd , nil ) // args are not used in RunE function
155
+ err := runE (tc .cobraCmd , tc . args ) // args are not used in RunE function
133
156
if tc .ExpectError && err == nil {
134
157
t .Errorf ("ClientConfigCmd.GetClientConfigCmd().RunE() expected an error but got none" )
135
158
}
@@ -141,6 +164,9 @@ func TestClientConfigCmd_GetClientConfigCmd(t *testing.T) {
141
164
t .Errorf ("expected regex %v to match error %v" , tc .ErrorPattern , err )
142
165
}
143
166
}
167
+ if err := tc .ValidateOut (out ); err != nil {
168
+ t .Errorf ("%v\n " , err )
169
+ }
144
170
})
145
171
}
146
172
}
0 commit comments