-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathawscfg_test.go
238 lines (225 loc) · 6.27 KB
/
awscfg_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package awsconfigfile
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/ini.v1"
)
func parseIni(t *testing.T, data string) *ini.File {
ini, err := ini.Load([]byte(data))
if err != nil {
t.Fatal(err)
}
return ini
}
func TestMerge(t *testing.T) {
tests := []struct {
name string
args MergeOpts
want string
wantErr bool
}{
{
name: "ok",
args: MergeOpts{
Config: parseIni(t, `
[profile example]
test = 1
`),
Profiles: []SSOProfile{
{
SSOStartURL: "https://example.com",
SSORegion: "ap-southeast-2",
AccountID: "123456789012",
AccountName: "testing",
RoleName: "DevRole",
GeneratedFrom: "aws-sso",
CommonFateURL: "https://commonfate.example.com",
},
},
},
want: `
[profile example]
test = 1
[profile testing/DevRole]
granted_sso_start_url = https://example.com
granted_sso_region = ap-southeast-2
granted_sso_account_id = 123456789012
granted_sso_role_name = DevRole
common_fate_generated_from = aws-sso
credential_process = granted credential-process --profile testing/DevRole --url https://commonfate.example.com
`,
},
{
name: "ok with no credential process",
args: MergeOpts{
Config: parseIni(t, `
[profile example]
test = 1
`),
NoCredentialProcess: true,
Profiles: []SSOProfile{
{
SSOStartURL: "https://example.com",
SSORegion: "ap-southeast-2",
AccountID: "123456789012",
AccountName: "testing",
RoleName: "DevRole",
GeneratedFrom: "aws-sso",
CommonFateURL: "https://commonfate.example.com",
},
},
},
want: `
[profile example]
test = 1
[profile testing/DevRole]
sso_start_url = https://example.com
sso_region = ap-southeast-2
sso_account_id = 123456789012
common_fate_generated_from = aws-sso
sso_role_name = DevRole
`,
},
{
name: "no common fate url",
args: MergeOpts{
Config: parseIni(t, `
[profile example]
test = 1
`),
Profiles: []SSOProfile{
{
SSOStartURL: "https://example.com",
SSORegion: "ap-southeast-2",
AccountID: "123456789012",
AccountName: "testing",
RoleName: "DevRole",
GeneratedFrom: "aws-sso",
},
},
},
want: `
[profile example]
test = 1
[profile testing/DevRole]
granted_sso_start_url = https://example.com
granted_sso_region = ap-southeast-2
granted_sso_account_id = 123456789012
granted_sso_role_name = DevRole
common_fate_generated_from = aws-sso
credential_process = granted credential-process --profile testing/DevRole
`,
},
{
name: "ok with sprig formatting",
args: MergeOpts{
Config: parseIni(t, `
[profile example]
test = 1
`),
SectionNameTemplate: "{{ replace ` ` `-` .AccountName | lower }}/{{ .RoleName }}",
Profiles: []SSOProfile{
{
SSOStartURL: "https://example.com",
SSORegion: "ap-southeast-2",
AccountID: "123456789012",
AccountName: "Testing Title Case With Space",
RoleName: "DevRole",
GeneratedFrom: "aws-sso",
CommonFateURL: "https://commonfate.example.com",
},
},
},
want: `
[profile example]
test = 1
[profile testing-title-case-with-space/DevRole]
granted_sso_start_url = https://example.com
granted_sso_region = ap-southeast-2
granted_sso_account_id = 123456789012
granted_sso_role_name = DevRole
common_fate_generated_from = aws-sso
credential_process = granted credential-process --profile testing-title-case-with-space/DevRole --url https://commonfate.example.com
`,
},
{
name: "profile names (AccountName/RoleName) sorted alphabetically",
args: MergeOpts{
Config: parseIni(t, ""),
Profiles: []SSOProfile{
{
SSOStartURL: "https://example.awsapps.com/start",
SSORegion: "ap-southeast-2",
AccountID: "123456789012",
AccountName: "account1",
RoleName: "DevRoleTwo",
GeneratedFrom: "aws-sso",
Region: "us-west-2",
},
{
SSOStartURL: "https://example.awsapps.com/start",
SSORegion: "ap-southeast-2",
AccountID: "123456789012",
AccountName: "account1",
RoleName: "DevRoleOne",
GeneratedFrom: "aws-sso",
Region: "us-west-2",
},
{
SSOStartURL: "https://example.awsapps.com/start",
SSORegion: "ap-southeast-2",
AccountID: "123456789012",
AccountName: "account2",
RoleName: "DevRoleOne",
GeneratedFrom: "aws-sso",
Region: "us-west-2",
},
},
},
want: `
[profile account1/DevRoleOne]
granted_sso_start_url = https://example.awsapps.com/start
granted_sso_region = ap-southeast-2
granted_sso_account_id = 123456789012
granted_sso_role_name = DevRoleOne
common_fate_generated_from = aws-sso
credential_process = granted credential-process --profile account1/DevRoleOne
region = us-west-2
[profile account1/DevRoleTwo]
granted_sso_start_url = https://example.awsapps.com/start
granted_sso_region = ap-southeast-2
granted_sso_account_id = 123456789012
granted_sso_role_name = DevRoleTwo
common_fate_generated_from = aws-sso
credential_process = granted credential-process --profile account1/DevRoleTwo
region = us-west-2
[profile account2/DevRoleOne]
granted_sso_start_url = https://example.awsapps.com/start
granted_sso_region = ap-southeast-2
granted_sso_account_id = 123456789012
granted_sso_role_name = DevRoleOne
common_fate_generated_from = aws-sso
credential_process = granted credential-process --profile account2/DevRoleOne
region = us-west-2
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Merge(tt.args); (err != nil) != tt.wantErr {
t.Errorf("Merge() error = %v, wantErr %v", err, tt.wantErr)
}
var b bytes.Buffer
_, err := tt.args.Config.WriteTo(&b)
if err != nil {
t.Fatal(err)
}
// ignore leading/trailing whitespace so it's easier to format the 'want' section in the test table
got := strings.TrimSpace(b.String())
want := strings.TrimSpace(tt.want)
assert.Equal(t, want, got)
})
}
}