This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
156 lines (148 loc) · 3.48 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"bytes"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"gopkg.in/ini.v1"
)
func Test_profileName(t *testing.T) {
type args struct {
accountName string
roleName string
}
tests := []struct {
name string
args args
want string
}{
{name: "HappyCase", args: args{accountName: "app-account", roleName: "admin-profile"}, want: "app-account/admin-profile"},
{name: "CapsToLowercase", args: args{accountName: "App-Account", roleName: "aDmin-profile"}, want: "app-account/admin-profile"},
{name: "ReplaceChars", args: args{accountName: "app account.2", roleName: " admin-profile"}, want: "app-account-2/admin-profile"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := profileName(tt.args.accountName, tt.args.roleName); got != tt.want {
t.Errorf("profileName() = %v, want %v", got, tt.want)
}
})
}
}
func Test_updateProfiles(t *testing.T) {
overwriteArgs, err := ini.Load(bytes.NewBufferString(`[profile name]
sso_start_url = OVERWRITE_ME
sso_account_id = account-id
sso_role_name = sso-role-name
sso_region = sso-region
region = region
`))
if err != nil {
t.Fatal(err)
}
preserveArgs, err := ini.Load(bytes.NewBufferString(`[profile otherName]
sso_start_url = PRESERVE_ME
sso_account_id = account-id
sso_role_name = sso-role-name
sso_region = sso-region
region = region
`))
if err != nil {
t.Fatal(err)
}
type args struct {
ini *ini.File
profiles []Profile
}
tests := []struct {
name string
args args
want string
}{
{
name: "Happy",
args: args{
ini: ini.Empty(),
profiles: []Profile{
{
Name: "name",
SSORoleName: "sso-role-name",
SSOStartURL: "sso-start-url",
SSORegion: "sso-region",
SSOAccountID: "account-id",
Region: "region",
},
},
},
want: `[profile name]
sso_start_url = sso-start-url
sso_account_id = account-id
sso_role_name = sso-role-name
sso_region = sso-region
region = region
`,
},
{
name: "HappyOverwite",
args: args{
ini: overwriteArgs,
profiles: []Profile{
{
Name: "name",
SSORoleName: "sso-role-name",
SSOStartURL: "sso-start-url",
SSORegion: "sso-region",
SSOAccountID: "account-id",
Region: "region",
},
},
},
want: `[profile name]
sso_start_url = sso-start-url
sso_account_id = account-id
sso_role_name = sso-role-name
sso_region = sso-region
region = region
`,
},
{
name: "Preserve",
args: args{
ini: preserveArgs,
profiles: []Profile{
{
Name: "name",
SSORoleName: "sso-role-name",
SSOStartURL: "sso-start-url",
SSORegion: "sso-region",
SSOAccountID: "account-id",
Region: "region",
},
},
},
want: `[profile otherName]
sso_start_url = PRESERVE_ME
sso_account_id = account-id
sso_role_name = sso-role-name
sso_region = sso-region
region = region
[profile name]
sso_start_url = sso-start-url
sso_account_id = account-id
sso_role_name = sso-role-name
sso_region = sso-region
region = region
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
updateProfiles(tt.args.ini, tt.args.profiles)
var buf bytes.Buffer
tt.args.ini.WriteTo(&buf)
if strings.TrimSpace(tt.want) != strings.TrimSpace(buf.String()) {
t.Errorf("want: %s\ngot: %s\n", tt.want, buf.String())
t.Log(cmp.Diff(tt.want, buf.String()))
}
})
}
}