Skip to content

Commit 704493b

Browse files
Merge pull request #734 from karanjitsingh01/OSD-29167_testcases_pkg_account
OSD-29167: Testcases pkg account
2 parents 52a5e15 + 3acb96c commit 704493b

File tree

3 files changed

+304
-0
lines changed

3 files changed

+304
-0
lines changed

cmd/account/console_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package account
2+
3+
import (
4+
"testing"
5+
6+
"github.com/spf13/cobra"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestPrependRegionToURL(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
consoleURL string
14+
region string
15+
expectedURL string
16+
expectErr bool
17+
}{
18+
{
19+
name: "valid_url_with_destination",
20+
consoleURL: "https://example.com/login?Destination=https%3A%2F%2Fservice.com%2Fhome",
21+
region: "us-west-2",
22+
expectedURL: "https://example.com/login?Destination=https%3A%2F%2Fus-west-2.service.com%2Fhome",
23+
expectErr: false,
24+
},
25+
{
26+
name: "invalid_console_url",
27+
consoleURL: "http://[::1]:namedport",
28+
region: "us-west-2",
29+
expectedURL: "",
30+
expectErr: true,
31+
},
32+
{
33+
name: "missing_destination",
34+
consoleURL: "https://example.com/login",
35+
region: "us-west-2",
36+
expectedURL: "https://example.com/login?Destination=%2F%2Fus-west-2.",
37+
expectErr: false,
38+
},
39+
{
40+
name: "invalid_destination_url",
41+
consoleURL: "https://example.com/login?Destination=::badurl::",
42+
region: "us-west-2",
43+
expectedURL: "",
44+
expectErr: true,
45+
},
46+
{
47+
name: "valid_destination_with_query",
48+
consoleURL: "https://example.com/login?Destination=https%3A%2F%2Fservice.com%2Fhome%3Ffoo%3Dbar",
49+
region: "ap-southeast-1",
50+
expectedURL: "https://example.com/login?Destination=https%3A%2F%2Fap-southeast-1.service.com%2Fhome%3Ffoo%3Dbar",
51+
expectErr: false,
52+
},
53+
}
54+
55+
for _, tt := range tests {
56+
t.Run(tt.name, func(t *testing.T) {
57+
result, err := PrependRegionToURL(tt.consoleURL, tt.region)
58+
if tt.expectErr {
59+
assert.Error(t, err)
60+
} else {
61+
assert.NoError(t, err)
62+
assert.Equal(t, tt.expectedURL, result)
63+
}
64+
})
65+
}
66+
}
67+
68+
func TestNewCmdConsole(t *testing.T) {
69+
tests := []struct {
70+
name string
71+
flags map[string]string
72+
expectedErr bool
73+
expectedRegion string
74+
}{
75+
{
76+
name: "missing_account_id_flag",
77+
flags: map[string]string{},
78+
expectedErr: true,
79+
},
80+
{
81+
name: "valid_input_with_region",
82+
flags: map[string]string{
83+
"accountId": "123456789012",
84+
"region": "ap-south-1",
85+
},
86+
expectedErr: false,
87+
expectedRegion: "ap-south-1",
88+
},
89+
{
90+
name: "valid_input_without_region",
91+
flags: map[string]string{
92+
"accountId": "123456789012",
93+
},
94+
expectedErr: false,
95+
expectedRegion: "us-east-1",
96+
},
97+
}
98+
99+
for _, tt := range tests {
100+
t.Run(tt.name, func(t *testing.T) {
101+
ops := newConsoleOptions()
102+
103+
// Simulate cobra.Command context
104+
cmd := &cobra.Command{}
105+
cmd.Flags().StringVarP(&ops.awsAccountID, "accountId", "i", "", "")
106+
cmd.Flags().StringVarP(&ops.region, "region", "r", "", "")
107+
108+
// Set flags
109+
for k, v := range tt.flags {
110+
_ = cmd.Flags().Set(k, v)
111+
}
112+
113+
// Manually call ops.complete
114+
err := ops.complete(cmd)
115+
116+
if tt.expectedErr {
117+
assert.Error(t, err)
118+
} else {
119+
assert.NoError(t, err)
120+
assert.Equal(t, tt.expectedRegion, ops.region)
121+
}
122+
})
123+
}
124+
}

cmd/account/generate-secret_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package account
2+
3+
import (
4+
"testing"
5+
6+
"github.com/spf13/cobra"
7+
"github.com/stretchr/testify/assert"
8+
"k8s.io/cli-runtime/pkg/genericclioptions"
9+
)
10+
11+
func TestGenerateSecretOptions_Complete(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
args []string
15+
flags map[string]string
16+
expectedErr bool
17+
expectedErrMsg string
18+
expectedUsername string
19+
expectedAcctID string
20+
expectedAcctName string
21+
}{
22+
{
23+
name: "ccs_missing_account_name",
24+
args: []string{},
25+
flags: map[string]string{"ccs": "true"},
26+
expectedErr: true,
27+
expectedErrMsg: "Account CR name argument is required\nSee ' -h' for help and examples",
28+
},
29+
{
30+
name: "missing_iam_username_and_account_identifiers",
31+
args: []string{},
32+
flags: map[string]string{},
33+
expectedErr: true,
34+
expectedErrMsg: "IAM User name argument is required\nSee ' -h' for help and examples",
35+
},
36+
{
37+
name: "missing_account_name_and_id",
38+
args: []string{"iam-user"},
39+
flags: map[string]string{},
40+
expectedErr: true,
41+
expectedErrMsg: "AWS account CR name and AWS account ID cannot be empty at the same time\nSee ' -h' for help and examples",
42+
},
43+
{
44+
name: "both_account_name_and_id_provided",
45+
args: []string{"iam-user"},
46+
flags: map[string]string{"account-name": "acct-cr", "account-id": "123456789"},
47+
expectedErr: true,
48+
expectedErrMsg: "AWS account CR name and AWS account ID cannot be set at the same time\nSee ' -h' for help and examples",
49+
},
50+
{
51+
name: "valid_account_name",
52+
args: []string{"iam-user"},
53+
flags: map[string]string{"account-name": "acct-cr"},
54+
expectedErr: false,
55+
expectedUsername: "iam-user",
56+
expectedAcctName: "acct-cr",
57+
},
58+
{
59+
name: "valid_account_id",
60+
args: []string{"iam-user"},
61+
flags: map[string]string{"account-id": "123456789"},
62+
expectedErr: false,
63+
expectedUsername: "iam-user",
64+
expectedAcctID: "123456789",
65+
},
66+
}
67+
68+
for _, tt := range tests {
69+
t.Run(tt.name, func(t *testing.T) {
70+
ops := newGenerateSecretOptions(genericclioptions.IOStreams{}, nil)
71+
72+
// Simulate cobra.Command context
73+
cmd := &cobra.Command{}
74+
cmd.Flags().StringVar(&ops.accountName, "account-name", "", "")
75+
cmd.Flags().StringVar(&ops.accountNamespace, "account-namespace", "", "")
76+
cmd.Flags().StringVar(&ops.accountID, "account-id", "", "")
77+
cmd.Flags().StringVar(&ops.profile, "aws-profile", "", "")
78+
cmd.Flags().StringVar(&ops.secretName, "secret-name", "", "")
79+
cmd.Flags().StringVar(&ops.secretNamespace, "secret-namespace", "", "")
80+
cmd.Flags().BoolVar(&ops.quiet, "quiet", false, "")
81+
cmd.Flags().BoolVar(&ops.ccs, "ccs", false, "")
82+
83+
// Set flags
84+
for k, v := range tt.flags {
85+
_ = cmd.Flags().Set(k, v)
86+
}
87+
88+
err := ops.complete(cmd, tt.args)
89+
90+
if tt.expectedErr {
91+
assert.Error(t, err)
92+
assert.EqualError(t, err, tt.expectedErrMsg)
93+
} else {
94+
assert.NoError(t, err)
95+
assert.Equal(t, tt.expectedUsername, ops.iamUsername)
96+
assert.Equal(t, tt.expectedAcctID, ops.accountID)
97+
assert.Equal(t, tt.expectedAcctName, ops.accountName)
98+
assert.NotNil(t, ops.awsAccountTimeout)
99+
assert.Equal(t, int32(900), *ops.awsAccountTimeout)
100+
}
101+
})
102+
}
103+
}

cmd/account/rotate-secret_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package account
2+
3+
import (
4+
"testing"
5+
6+
"github.com/spf13/cobra"
7+
"github.com/stretchr/testify/assert"
8+
"k8s.io/cli-runtime/pkg/genericclioptions"
9+
)
10+
11+
func TestRotateSecretOptions_Complete(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
args []string
15+
flags map[string]string
16+
expectedErr bool
17+
expectedErrMsg string
18+
expectedName string
19+
expectedProfile string
20+
}{
21+
{
22+
name: "missing_argument",
23+
args: []string{},
24+
flags: map[string]string{"reason": "OHSS-123"},
25+
expectedErr: true,
26+
expectedErrMsg: "Account CR argument is required\nSee ' -h' for help and examples",
27+
},
28+
{
29+
name: "valid_argument_and_flags",
30+
args: []string{"my-account-cr"},
31+
flags: map[string]string{"aws-profile": "custom", "reason": "PD-456"},
32+
expectedErr: false,
33+
expectedName: "my-account-cr",
34+
expectedProfile: "custom",
35+
},
36+
{
37+
name: "default_profile_used",
38+
args: []string{"another-cr"},
39+
flags: map[string]string{"reason": "PD-789"},
40+
expectedErr: false,
41+
expectedName: "another-cr",
42+
expectedProfile: "default",
43+
},
44+
{
45+
name: "invalid_admin_username",
46+
args: []string{"bad-cr"},
47+
flags: map[string]string{"admin-username": "notosdManagedAdmin", "reason": "OHSS-000"},
48+
expectedErr: true,
49+
expectedErrMsg: "admin-username must start with osdManagedAdmin\nSee ' -h' for help and examples",
50+
},
51+
}
52+
53+
for _, tt := range tests {
54+
t.Run(tt.name, func(t *testing.T) {
55+
ops := newRotateSecretOptions(genericclioptions.IOStreams{}, nil)
56+
cmd := &cobra.Command{}
57+
cmd.Flags().StringVar(&ops.profile, "aws-profile", "", "")
58+
cmd.Flags().BoolVar(&ops.updateCcsCreds, "ccs", false, "")
59+
cmd.Flags().StringVar(&ops.reason, "reason", "", "")
60+
cmd.Flags().StringVar(&ops.osdManagedAdminUsername, "admin-username", "", "")
61+
for k, v := range tt.flags {
62+
_ = cmd.Flags().Set(k, v)
63+
}
64+
err := ops.complete(cmd, tt.args)
65+
if tt.expectedErr {
66+
assert.Error(t, err)
67+
assert.EqualError(t, err, tt.expectedErrMsg)
68+
} else {
69+
assert.NoError(t, err)
70+
assert.Equal(t, tt.expectedName, ops.accountCRName)
71+
assert.Equal(t, tt.expectedProfile, ops.profile)
72+
assert.NotNil(t, ops.awsAccountTimeout)
73+
assert.Equal(t, int32(900), *ops.awsAccountTimeout)
74+
}
75+
})
76+
}
77+
}

0 commit comments

Comments
 (0)