Skip to content

Commit abbcc42

Browse files
committed
update tests to validate output
1 parent f4b1364 commit abbcc42

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

pkg/cmd/clientConfig_test.go

+31-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ package cmd_test
1616

1717
import (
1818
"bytes"
19+
"fmt"
20+
"strings"
1921
"testing"
2022

2123
"regexp"
2224

2325
"github.com/aerogear/mobile-cli/pkg/cmd"
26+
"github.com/pkg/errors"
2427
"github.com/spf13/cobra"
2528
"k8s.io/apimachinery/pkg/runtime"
2629
"k8s.io/client-go/kubernetes"
@@ -34,11 +37,11 @@ import (
3437
func TestClientConfigCmd_GetClientConfigCmd(t *testing.T) {
3538
getFakeCbrCmd := func() *cobra.Command {
3639
return &cobra.Command{
37-
Use: "clientconfig",
40+
Use: "clientconfig <clientID>",
3841
Short: "get clientconfig returns a client ready filtered configuration of the available services.",
3942
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`,
4245
RunE: func(cmd *cobra.Command, args []string) error {
4346
return nil
4447
},
@@ -49,28 +52,40 @@ func TestClientConfigCmd_GetClientConfigCmd(t *testing.T) {
4952
name string
5053
k8Client func() kubernetes.Interface
5154
namespace string
55+
args []string
5256
cobraCmd *cobra.Command
5357
ExpectError bool
5458
ErrorPattern string
59+
ValidateOut func(bytes.Buffer) error
5560
}{
5661
{
5762
name: "get client config command with empty namespace",
5863
k8Client: func() kubernetes.Interface {
5964
return &kFake.Clientset{}
6065
},
6166
namespace: "",
67+
args: []string{"client-id"},
6268
cobraCmd: getFakeCbrCmd(),
6369
ExpectError: true,
6470
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 },
6572
},
6673
{
67-
name: "get client config command with testing-ns namespace",
74+
name: "get client config command with no services",
6875
k8Client: func() kubernetes.Interface {
6976
return &kFake.Clientset{}
7077
},
7178
namespace: "testing-ns",
79+
args: []string{"client-id"},
7280
cobraCmd: getFakeCbrCmd(),
7381
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+
},
7489
},
7590
{
7691
name: "get client config command with services",
@@ -110,8 +125,16 @@ func TestClientConfigCmd_GetClientConfigCmd(t *testing.T) {
110125
return fakeclient
111126
},
112127
namespace: "testing-ns",
128+
args: []string{"client-id"},
113129
cobraCmd: getFakeCbrCmd(),
114130
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+
},
115138
},
116139
}
117140
for _, tc := range tests {
@@ -129,7 +152,7 @@ func TestClientConfigCmd_GetClientConfigCmd(t *testing.T) {
129152

130153
runE := got.RunE
131154
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
133156
if tc.ExpectError && err == nil {
134157
t.Errorf("ClientConfigCmd.GetClientConfigCmd().RunE() expected an error but got none")
135158
}
@@ -141,6 +164,9 @@ func TestClientConfigCmd_GetClientConfigCmd(t *testing.T) {
141164
t.Errorf("expected regex %v to match error %v", tc.ErrorPattern, err)
142165
}
143166
}
167+
if err := tc.ValidateOut(out); err != nil {
168+
t.Errorf("%v\n", err)
169+
}
144170
})
145171
}
146172
}

0 commit comments

Comments
 (0)