/
config_test.go
574 lines (536 loc) · 17.8 KB
/
config_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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// The MIT License (MIT)
//
// Copyright (c) 2015 Douglas Thrift
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
. "github.com/rightscale/right_st"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
)
var _ = Describe("Config", func() {
It("Gets the default config file", func() {
configFile := DefaultConfigFile()
Expect(filepath.Base(configFile)).To(Equal(".right_st.yml"))
Expect(filepath.IsAbs(configFile)).To(BeTrue())
})
Describe("Read config", func() {
var (
tempDir string
buffer *gbytes.Buffer
encryptedPasswordRegexp = regexp.MustCompile(`\{ENCRYPTED\}[A-Za-z0-9+/]+={0,2}`)
)
BeforeEach(func() {
var err error
tempDir, err = ioutil.TempDir("", "config")
if err != nil {
panic(err)
}
buffer = gbytes.NewBuffer()
})
AfterEach(func() {
os.RemoveAll(tempDir)
})
Context("With a nonexistent config file", func() {
var nonexistentConfigFile string
BeforeEach(func() {
nonexistentConfigFile = filepath.Join(tempDir, ".right_st.yml")
})
It("Returns an error", func() {
err := ReadConfig(nonexistentConfigFile, "")
Expect(err).To(HaveOccurred())
})
Context("With OS environment variables set", func() {
BeforeEach(func() {
if err := os.Setenv("RIGHT_ST_LOGIN_ACCOUNT_ID", "67890"); err != nil {
panic(err)
}
if err := os.Setenv("RIGHT_ST_LOGIN_ACCOUNT_HOST", "us-4.rightscale.com"); err != nil {
panic(err)
}
if err := os.Setenv("RIGHT_ST_LOGIN_ACCOUNT_REFRESH_TOKEN",
"fedcba0987654321febcba0987654321fedcba09"); err != nil {
panic(err)
}
})
AfterEach(func() {
if err := os.Unsetenv("RIGHT_ST_LOGIN_ACCOUNT_ID"); err != nil {
panic(err)
}
if err := os.Unsetenv("RIGHT_ST_LOGIN_ACCOUNT_HOST"); err != nil {
panic(err)
}
if err := os.Unsetenv("RIGHT_ST_LOGIN_ACCOUNT_REFRESH_TOKEN"); err != nil {
panic(err)
}
})
It("Loads the default account from the OS environment variables", func() {
Expect(ReadConfig(nonexistentConfigFile, "")).To(Succeed())
Expect(Config.Accounts).To(BeEmpty())
Expect(Config.Account).To(Equal(&Account{
Id: 67890,
Host: "us-4.rightscale.com",
RefreshToken: "fedcba0987654321febcba0987654321fedcba09",
}))
})
})
Context("With a nonexistent parent directory for the config file", func() {
BeforeEach(func() {
os.RemoveAll(tempDir)
})
Describe("Set account", func() {
Context("With refresh token", func() {
It("Creates a config file with the set account", func() {
Expect(ReadConfig(nonexistentConfigFile, "")).NotTo(Succeed())
input := new(bytes.Buffer)
fmt.Fprintln(input, 12345)
fmt.Fprintln(input, "us-3.rightscale.com")
fmt.Fprintln(input, "abcdef1234567890abcdef1234567890abcdef12")
Expect(Config.SetAccount("production", false, false, input, buffer)).To(Succeed())
Expect(buffer.Contents()).To(BeEquivalentTo("Account ID: " +
"API endpoint host: " +
"Refresh token: "))
config, err := ioutil.ReadFile(nonexistentConfigFile)
Expect(err).NotTo(HaveOccurred())
Expect(string(config)).To(MatchYAML(`login:
accounts:
production:
host: us-3.rightscale.com
id: 12345
refresh_token: abcdef1234567890abcdef1234567890abcdef12
default_account: production
update:
check: true
`))
})
})
Context("With username and password", func() {
It("Creates a config file with the set account", func() {
Expect(ReadConfig(nonexistentConfigFile, "")).NotTo(Succeed())
input := new(bytes.Buffer)
fmt.Fprintln(input, 101112)
fmt.Fprintln(input, "us-4.rightscale.com")
fmt.Fprintln(input, "cool.dude@rightscale.com")
fmt.Fprintln(input, "hunter2")
Expect(Config.SetAccount("dev", false, true, input, buffer)).To(Succeed())
Expect(buffer.Contents()).To(BeEquivalentTo("Account ID: " +
"API endpoint host: " +
"Username: " +
"Password: "))
config, err := ioutil.ReadFile(nonexistentConfigFile)
Expect(err).NotTo(HaveOccurred())
encryptedPassword := encryptedPasswordRegexp.FindString(string(config))
Expect(encryptedPassword).NotTo(BeEmpty())
Expect(config).To(MatchYAML(`login:
accounts:
dev:
host: us-4.rightscale.com
id: 101112
username: cool.dude@rightscale.com
password: '` + encryptedPassword + `'
default_account: dev
update:
check: true
`))
})
})
})
})
})
Context("With a bad account config file", func() {
var badAccountConfigFile string
BeforeEach(func() {
badAccountConfigFile = filepath.Join(tempDir, ".right_st.yml")
err := ioutil.WriteFile(badAccountConfigFile, []byte(`# Bad account config with an array instead of a dictionary
---
login:
accounts:
- production:
host: us-3.rightscale.com
id: 12345
refresh_token: abcdef1234567890abcdef1234567890abcdef12
- staging:
host: us-4.rightscale.com
id: 67890
refresh_token: fedcba0987654321febcba0987654321fedcba09
default_account: production
`), 0600)
if err != nil {
panic(err)
}
})
It("Returns an error", func() {
err := ReadConfig(badAccountConfigFile, "")
Expect(err).To(HaveOccurred())
})
})
Context("With a missing default account in the config file", func() {
var missingDefaultAccountConfigFile string
BeforeEach(func() {
missingDefaultAccountConfigFile = filepath.Join(tempDir, ".right_st.yml")
err := ioutil.WriteFile(missingDefaultAccountConfigFile, []byte(`# Account config with missing default account
---
login:
accounts:
production:
host: us-3.rightscale.com
id: 12345
refresh_token: abcdef1234567890abcdef1234567890abcdef12
staging:
host: us-4.rightscale.com
id: 67890
refresh_token: fedcba0987654321febcba0987654321fedcba09
default_account: development
`), 0600)
if err != nil {
panic(err)
}
})
It("Returns an error", func() {
err := ReadConfig(missingDefaultAccountConfigFile, "")
Expect(err).To(MatchError(missingDefaultAccountConfigFile + ": could not find default account: development"))
})
})
Context("With a valid config file", func() {
var configFile string
BeforeEach(func() {
configFile = filepath.Join(tempDir, ".right_st.yml")
err := ioutil.WriteFile(configFile, []byte(`---
login:
default_account: production
accounts:
production:
host: us-3.rightscale.com
id: 12345
refresh_token: abcdef1234567890abcdef1234567890abcdef12
staging:
host: us-4.rightscale.com
id: 67890
refresh_token: fedcba0987654321febcba0987654321fedcba09
dev:
host: us-4.rightscale.com
id: 101112
username: cool.dude@rightscale.com
password: hunter2
`), 0600)
if err != nil {
panic(err)
}
})
It("Loads the accounts from the config file and sets the default account", func() {
Expect(ReadConfig(configFile, "")).To(Succeed())
Expect(Config.Accounts).To(Equal(map[string]*Account{
"production": {
Id: 12345,
Host: "us-3.rightscale.com",
RefreshToken: "abcdef1234567890abcdef1234567890abcdef12",
},
"staging": {
Id: 67890,
Host: "us-4.rightscale.com",
RefreshToken: "fedcba0987654321febcba0987654321fedcba09",
},
"dev": {
Id: 101112,
Host: "us-4.rightscale.com",
Username: "cool.dude@rightscale.com",
Password: "hunter2",
},
}))
Expect(Config.Account).To(Equal(&Account{
Id: 12345,
Host: "us-3.rightscale.com",
RefreshToken: "abcdef1234567890abcdef1234567890abcdef12",
}))
})
It("Loads the accounts from the config file and set a specified account", func() {
Expect(ReadConfig(configFile, "staging")).To(Succeed())
Expect(Config.Accounts).To(Equal(map[string]*Account{
"production": {
Id: 12345,
Host: "us-3.rightscale.com",
RefreshToken: "abcdef1234567890abcdef1234567890abcdef12",
},
"staging": {
Id: 67890,
Host: "us-4.rightscale.com",
RefreshToken: "fedcba0987654321febcba0987654321fedcba09",
},
"dev": {
Id: 101112,
Host: "us-4.rightscale.com",
Username: "cool.dude@rightscale.com",
Password: "hunter2",
},
}))
Expect(Config.Account).To(Equal(&Account{
Id: 67890,
Host: "us-4.rightscale.com",
RefreshToken: "fedcba0987654321febcba0987654321fedcba09",
}))
})
It("Returns an error when the config file does not contain the specified account", func() {
err := ReadConfig(configFile, "development")
Expect(err).To(MatchError(configFile + ": could not find account: development"))
})
Describe("Get account", func() {
It("Gets an account with a specified account and host", func() {
Expect(ReadConfig(configFile, "")).To(Succeed())
account, err := Config.GetAccount(67890, "us-4.rightscale.com")
Expect(err).NotTo(HaveOccurred())
Expect(account).To(Equal(&Account{
Id: 67890,
Host: "us-4.rightscale.com",
RefreshToken: "fedcba0987654321febcba0987654321fedcba09",
}))
})
It("Returns an error if the specified account and host are not in the configuration", func() {
Expect(ReadConfig(configFile, "")).To(Succeed())
account, err := Config.GetAccount(12345, "us-4.rightscale.com")
Expect(err).To(MatchError("Could not find account for account/host: 12345 us-4.rightscale.com"))
Expect(account).To(BeNil())
})
})
Context("With OS environment variables", func() {
BeforeEach(func() {
if err := os.Setenv("RIGHT_ST_LOGIN_ACCOUNT_ID", "67890"); err != nil {
panic(err)
}
if err := os.Setenv("RIGHT_ST_LOGIN_ACCOUNT_HOST", "us-4.rightscale.com"); err != nil {
panic(err)
}
if err := os.Setenv("RIGHT_ST_LOGIN_ACCOUNT_REFRESH_TOKEN",
"fedcba0987654321febcba0987654321fedcba09"); err != nil {
panic(err)
}
})
AfterEach(func() {
if err := os.Unsetenv("RIGHT_ST_LOGIN_ACCOUNT_ID"); err != nil {
panic(err)
}
if err := os.Unsetenv("RIGHT_ST_LOGIN_ACCOUNT_HOST"); err != nil {
panic(err)
}
if err := os.Unsetenv("RIGHT_ST_LOGIN_ACCOUNT_REFRESH_TOKEN"); err != nil {
panic(err)
}
})
It("Loads the default account from the OS environment variables", func() {
Expect(ReadConfig(configFile, "")).To(Succeed())
Expect(Config.Accounts).To(Equal(map[string]*Account{
"production": {
Id: 12345,
Host: "us-3.rightscale.com",
RefreshToken: "abcdef1234567890abcdef1234567890abcdef12",
},
"staging": {
Id: 67890,
Host: "us-4.rightscale.com",
RefreshToken: "fedcba0987654321febcba0987654321fedcba09",
},
"dev": {
Id: 101112,
Host: "us-4.rightscale.com",
Username: "cool.dude@rightscale.com",
Password: "hunter2",
},
}))
Expect(Config.Account).To(Equal(&Account{
Id: 67890,
Host: "us-4.rightscale.com",
RefreshToken: "fedcba0987654321febcba0987654321fedcba09",
}))
})
})
Describe("Set account", func() {
Context("With refresh token", func() {
It("Updates the config file with the new account", func() {
Expect(ReadConfig(configFile, "")).To(Succeed())
input := new(bytes.Buffer)
fmt.Fprintln(input, 54321)
fmt.Fprintln(input, "us-4.rightscale.com")
fmt.Fprintln(input, "21fedcba0987654321fedcba0987654321fedcba")
Expect(Config.SetAccount("testing", false, false, input, buffer)).To(Succeed())
Expect(buffer.Contents()).To(BeEquivalentTo("Account ID: " +
"API endpoint host: " +
"Refresh token: "))
config, err := ioutil.ReadFile(configFile)
Expect(err).NotTo(HaveOccurred())
Expect(config).To(MatchYAML(`login:
accounts:
production:
host: us-3.rightscale.com
id: 12345
refresh_token: abcdef1234567890abcdef1234567890abcdef12
staging:
host: us-4.rightscale.com
id: 67890
refresh_token: fedcba0987654321febcba0987654321fedcba09
dev:
host: us-4.rightscale.com
id: 101112
username: cool.dude@rightscale.com
password: hunter2
testing:
host: us-4.rightscale.com
id: 54321
refresh_token: 21fedcba0987654321fedcba0987654321fedcba
default_account: production
update:
check: true
`))
})
It("Updates the default account and uses defaults when modifying an existing account", func() {
Expect(ReadConfig(configFile, "")).To(Succeed())
Expect(Config.SetAccount("staging", true, false, new(bytes.Buffer), buffer)).To(Succeed())
Expect(buffer.Contents()).To(BeEquivalentTo("Account ID (67890): " +
"API endpoint host (us-4.rightscale.com): " +
"Refresh token (fedcba0987654321febcba0987654321fedcba09): "))
config, err := ioutil.ReadFile(configFile)
Expect(err).NotTo(HaveOccurred())
Expect(config).To(MatchYAML(`login:
accounts:
production:
host: us-3.rightscale.com
id: 12345
refresh_token: abcdef1234567890abcdef1234567890abcdef12
staging:
host: us-4.rightscale.com
id: 67890
refresh_token: fedcba0987654321febcba0987654321fedcba09
dev:
host: us-4.rightscale.com
id: 101112
username: cool.dude@rightscale.com
password: hunter2
default_account: staging
update:
check: true
`))
})
})
Context("With username and password", func() {
It("Updates the config file with the new account", func() {
Expect(ReadConfig(configFile, "")).To(Succeed())
input := new(bytes.Buffer)
fmt.Fprintln(input, 54321)
fmt.Fprintln(input, "us-4.rightscale.com")
fmt.Fprintln(input, "cool.dude@rightscale.com")
fmt.Fprintln(input, "hunter2")
Expect(Config.SetAccount("testing", false, true, input, buffer)).To(Succeed())
Expect(buffer.Contents()).To(BeEquivalentTo("Account ID: " +
"API endpoint host: " +
"Username: " +
"Password: "))
config, err := ioutil.ReadFile(configFile)
Expect(err).NotTo(HaveOccurred())
encryptedPassword := encryptedPasswordRegexp.FindString(string(config))
Expect(encryptedPassword).NotTo(BeEmpty())
Expect(config).To(MatchYAML(`login:
accounts:
production:
host: us-3.rightscale.com
id: 12345
refresh_token: abcdef1234567890abcdef1234567890abcdef12
staging:
host: us-4.rightscale.com
id: 67890
refresh_token: fedcba0987654321febcba0987654321fedcba09
dev:
host: us-4.rightscale.com
id: 101112
username: cool.dude@rightscale.com
password: hunter2
testing:
host: us-4.rightscale.com
id: 54321
username: cool.dude@rightscale.com
password: '` + encryptedPassword + `'
default_account: production
update:
check: true
`))
})
It("Updates the default account and uses defaults when modifying an existing account", func() {
Expect(ReadConfig(configFile, "")).To(Succeed())
Expect(Config.SetAccount("dev", true, true, new(bytes.Buffer), buffer)).To(Succeed())
Expect(buffer.Contents()).To(BeEquivalentTo("Account ID (101112): " +
"API endpoint host (us-4.rightscale.com): " +
"Username (cool.dude@rightscale.com): " +
"Password (*******): "))
config, err := ioutil.ReadFile(configFile)
Expect(err).NotTo(HaveOccurred())
Expect(config).To(MatchYAML(`login:
accounts:
production:
host: us-3.rightscale.com
id: 12345
refresh_token: abcdef1234567890abcdef1234567890abcdef12
staging:
host: us-4.rightscale.com
id: 67890
refresh_token: fedcba0987654321febcba0987654321fedcba09
dev:
host: us-4.rightscale.com
id: 101112
username: cool.dude@rightscale.com
password: hunter2
default_account: dev
update:
check: true
`))
})
})
})
Describe("Show configuration", func() {
It("Prints the configuration", func() {
Expect(ReadConfig(configFile, "")).To(Succeed())
Expect(Config.ShowConfiguration(buffer)).To(Succeed())
Expect(buffer.Contents()).To(MatchYAML(`login:
accounts:
production:
host: us-3.rightscale.com
id: 12345
refresh_token: abcdef1234567890abcdef1234567890abcdef12
staging:
host: us-4.rightscale.com
id: 67890
refresh_token: fedcba0987654321febcba0987654321fedcba09
dev:
host: us-4.rightscale.com
id: 101112
username: cool.dude@rightscale.com
password: '*******'
default_account: production
update:
check: true
`))
})
})
})
})
})