Skip to content

Commit ff273a0

Browse files
committed
add more unit tests for commands
Signed-off-by: Ben Ye <yb532204897@gmail.com>
1 parent fc304c3 commit ff273a0

15 files changed

+274
-13
lines changed

cmd/account/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (o *cliOptions) run() error {
120120
return err
121121
}
122122
if o.verbose {
123-
fmt.Println(callerIdentityOutput)
123+
fmt.Fprintln(o.Out, callerIdentityOutput)
124124
}
125125

126126
roleName := awsv1alpha1.AccountOperatorIAMRole

cmd/account/console.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (o *consoleOptions) run() error {
120120
return err
121121
}
122122
if o.verbose {
123-
fmt.Println(callerIdentityOutput)
123+
fmt.Fprintln(o.Out, callerIdentityOutput)
124124
}
125125

126126
roleName := awsv1alpha1.AccountOperatorIAMRole

cmd/account/get/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const (
1414
func NewCmdGet(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags) *cobra.Command {
1515
getCmd := &cobra.Command{
1616
Use: "get",
17-
Short: "get resources",
17+
Short: "Get resources",
1818
Args: cobra.NoArgs,
1919
DisableAutoGenTag: true,
2020
Run: help,
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package list
2+
3+
import (
4+
"os"
5+
"strings"
6+
"testing"
7+
8+
. "github.com/onsi/gomega"
9+
10+
"k8s.io/cli-runtime/pkg/genericclioptions"
11+
)
12+
13+
func TestGetAccountClaimCmdComplete(t *testing.T) {
14+
g := NewGomegaWithT(t)
15+
streams := genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
16+
kubeFlags := genericclioptions.NewConfigFlags(false)
17+
testCases := []struct {
18+
title string
19+
option *listAccountClaimOptions
20+
errExpected bool
21+
errContent string
22+
}{
23+
{
24+
title: "incorrect state",
25+
option: &listAccountClaimOptions{
26+
state: "foo",
27+
},
28+
errExpected: true,
29+
errContent: "unsupported account claim state foo",
30+
},
31+
{
32+
title: "empty state",
33+
option: &listAccountClaimOptions{
34+
state: "",
35+
flags: kubeFlags,
36+
},
37+
errExpected: false,
38+
},
39+
{
40+
title: "error state",
41+
option: &listAccountClaimOptions{
42+
state: "Error",
43+
flags: kubeFlags,
44+
},
45+
errExpected: false,
46+
},
47+
{
48+
title: "pending state",
49+
option: &listAccountClaimOptions{
50+
state: "Pending",
51+
flags: kubeFlags,
52+
},
53+
errExpected: false,
54+
},
55+
{
56+
title: "ready state",
57+
option: &listAccountClaimOptions{
58+
state: "Ready",
59+
flags: kubeFlags,
60+
},
61+
errExpected: false,
62+
},
63+
}
64+
65+
for _, tc := range testCases {
66+
t.Run(tc.title, func(t *testing.T) {
67+
cmd := newCmdListAccountClaim(streams, kubeFlags)
68+
err := tc.option.complete(cmd, nil)
69+
if tc.errExpected {
70+
g.Expect(err).Should(HaveOccurred())
71+
if tc.errContent != "" {
72+
g.Expect(true).Should(Equal(strings.Contains(err.Error(), tc.errContent)))
73+
}
74+
} else {
75+
g.Expect(err).ShouldNot(HaveOccurred())
76+
}
77+
})
78+
}
79+
}

cmd/account/list/account_test.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package list
2+
3+
import (
4+
"os"
5+
"strings"
6+
"testing"
7+
8+
. "github.com/onsi/gomega"
9+
10+
"k8s.io/cli-runtime/pkg/genericclioptions"
11+
)
12+
13+
func TestGetAccountCmdComplete(t *testing.T) {
14+
g := NewGomegaWithT(t)
15+
streams := genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
16+
kubeFlags := genericclioptions.NewConfigFlags(false)
17+
testCases := []struct {
18+
title string
19+
option *listAccountOptions
20+
errExpected bool
21+
errContent string
22+
}{
23+
{
24+
title: "incorrect state",
25+
option: &listAccountOptions{
26+
state: "foo",
27+
},
28+
errExpected: true,
29+
errContent: "unsupported account state foo",
30+
},
31+
{
32+
title: "empty state",
33+
option: &listAccountOptions{
34+
state: "",
35+
flags: kubeFlags,
36+
},
37+
errExpected: false,
38+
},
39+
{
40+
title: "all state",
41+
option: &listAccountOptions{
42+
state: "all",
43+
flags: kubeFlags,
44+
},
45+
errExpected: false,
46+
},
47+
{
48+
title: "Ready state",
49+
option: &listAccountOptions{
50+
state: "Ready",
51+
flags: kubeFlags,
52+
},
53+
errExpected: false,
54+
},
55+
{
56+
title: "bad reuse",
57+
option: &listAccountOptions{
58+
reused: "foo",
59+
},
60+
errExpected: true,
61+
errContent: "unsupported reused status filter foo",
62+
},
63+
{
64+
title: "bad reused status",
65+
option: &listAccountOptions{
66+
reused: "foo",
67+
},
68+
errExpected: true,
69+
errContent: "unsupported reused status filter foo",
70+
},
71+
{
72+
title: "bad claimed status",
73+
option: &listAccountOptions{
74+
claimed: "foo",
75+
},
76+
errExpected: true,
77+
errContent: "unsupported claimed status filter foo",
78+
},
79+
{
80+
title: "good reused true",
81+
option: &listAccountOptions{
82+
reused: "true",
83+
flags: kubeFlags,
84+
},
85+
errExpected: false,
86+
},
87+
{
88+
title: "good claim",
89+
option: &listAccountOptions{
90+
claimed: "false",
91+
flags: kubeFlags,
92+
},
93+
errExpected: false,
94+
},
95+
{
96+
title: "success",
97+
option: &listAccountOptions{
98+
state: "Ready",
99+
reused: "true",
100+
claimed: "false",
101+
flags: kubeFlags,
102+
},
103+
errExpected: false,
104+
},
105+
}
106+
107+
for _, tc := range testCases {
108+
t.Run(tc.title, func(t *testing.T) {
109+
cmd := newCmdListAccount(streams, kubeFlags)
110+
err := tc.option.complete(cmd, nil)
111+
if tc.errExpected {
112+
g.Expect(err).Should(HaveOccurred())
113+
if tc.errContent != "" {
114+
g.Expect(true).Should(Equal(strings.Contains(err.Error(), tc.errContent)))
115+
}
116+
} else {
117+
g.Expect(err).ShouldNot(HaveOccurred())
118+
}
119+
})
120+
}
121+
}

cmd/account/reset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (o *resetOptions) run() error {
101101
}
102102
for _, secret := range secrets.Items {
103103
if strings.HasPrefix(secret.Name, o.accountName) {
104-
fmt.Println("Deleting secret", secret.Name)
104+
fmt.Fprintln(o.Out, "Deleting secret "+secret.Name)
105105
if err := o.kubeCli.Delete(ctx, &secret, &client.DeleteOptions{}); err != nil {
106106
if apierrors.IsNotFound(err) {
107107
continue

cmd/clusterdeployment/list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"k8s.io/cli-runtime/pkg/genericclioptions"
99
)
1010

11-
func TestResetCmdComplete(t *testing.T) {
11+
func TestListCmdComplete(t *testing.T) {
1212
g := NewGomegaWithT(t)
1313
kubeFlags := genericclioptions.NewConfigFlags(false)
1414
testCases := []struct {

cmd/federatedrole/apply_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package federatedrole
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
. "github.com/onsi/gomega"
8+
9+
"k8s.io/cli-runtime/pkg/genericclioptions"
10+
)
11+
12+
func TestListCmdComplete(t *testing.T) {
13+
g := NewGomegaWithT(t)
14+
streams := genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
15+
kubeFlags := genericclioptions.NewConfigFlags(false)
16+
testCases := []struct {
17+
title string
18+
option *applyOptions
19+
errExpected bool
20+
errContent string
21+
}{
22+
{
23+
title: "url and file specified at the same time",
24+
option: &applyOptions{
25+
url: "http://example.com",
26+
file: "foo",
27+
},
28+
errExpected: true,
29+
errContent: "Flags file and url cannot be set at the same time",
30+
},
31+
{
32+
title: "url and file empty at the same time",
33+
option: &applyOptions{
34+
url: "",
35+
file: "",
36+
},
37+
errExpected: true,
38+
errContent: "Flags file and url cannot be empty at the same time",
39+
},
40+
{
41+
title: "success",
42+
option: &applyOptions{
43+
url: "foo",
44+
flags: kubeFlags,
45+
},
46+
errExpected: false,
47+
},
48+
}
49+
50+
for _, tc := range testCases {
51+
t.Run(tc.title, func(t *testing.T) {
52+
cmd := newCmdApply(streams, kubeFlags)
53+
err := tc.option.complete(cmd, nil)
54+
if tc.errExpected {
55+
g.Expect(err).Should(HaveOccurred())
56+
} else {
57+
g.Expect(err).ShouldNot(HaveOccurred())
58+
}
59+
})
60+
}
61+
}

docs/command/osdctl_account.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ osdctl account [flags]
3434
* [osdctl account clean-velero-snapshots](osdctl_account_clean-velero-snapshots.md) - Cleans up S3 buckets whose name start with managed-velero
3535
* [osdctl account cli](osdctl_account_cli.md) - Generate temporary AWS CLI credentials on demand
3636
* [osdctl account console](osdctl_account_console.md) - Generate an AWS console URL on the fly
37-
* [osdctl account get](osdctl_account_get.md) - get resources
37+
* [osdctl account get](osdctl_account_get.md) - Get resources
3838
* [osdctl account list](osdctl_account_list.md) - List resources
3939
* [osdctl account reset](osdctl_account_reset.md) - Reset AWS Account CR
4040
* [osdctl account rotate-secret](osdctl_account_rotate-secret.md) - Rotate IAM credentials secret

docs/command/osdctl_account_get.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## osdctl account get
22

3-
get resources
3+
Get resources
44

55
### Synopsis
66

7-
get resources
7+
Get resources
88

99
```
1010
osdctl account get [flags]

0 commit comments

Comments
 (0)