-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathaccountsfile_test.go
114 lines (95 loc) · 3.11 KB
/
accountsfile_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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuclient_test
import (
"os"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/internal/testing"
"github.com/juju/juju/juju/osenv"
"github.com/juju/juju/jujuclient"
)
type AccountsFileSuite struct {
testing.FakeJujuXDGDataHomeSuite
}
var _ = gc.Suite(&AccountsFileSuite{})
const testLegacyAccountsYAML = `
controllers:
ctrl:
user: admin@local
password: hunter2
last-known-access: superuser
kontroll:
user: bob@remote
`
const testAccountsYAML = `
controllers:
ctrl:
user: admin
password: hunter2
last-known-access: superuser
kontroll:
user: bob@remote
`
var testControllerAccounts = map[string]jujuclient.AccountDetails{
"ctrl": ctrlAdminAccountDetails,
"kontroll": kontrollBobRemoteAccountDetails,
}
var (
ctrlAdminAccountDetails = jujuclient.AccountDetails{
User: "admin",
Password: "hunter2",
LastKnownAccess: "superuser",
}
kontrollBobRemoteAccountDetails = jujuclient.AccountDetails{
User: "bob@remote",
}
)
func (s *AccountsFileSuite) TestWriteFile(c *gc.C) {
writeTestAccountsFile(c)
data, err := os.ReadFile(osenv.JujuXDGDataHomePath("accounts.yaml"))
c.Assert(err, jc.ErrorIsNil)
c.Assert(string(data), gc.Equals, testAccountsYAML[1:])
}
func (s *AccountsFileSuite) TestReadNoFile(c *gc.C) {
accounts, err := jujuclient.ReadAccountsFile("nowhere.yaml")
c.Assert(err, jc.ErrorIsNil)
c.Assert(accounts, gc.IsNil)
}
func (s *AccountsFileSuite) TestReadEmptyFile(c *gc.C) {
err := os.WriteFile(osenv.JujuXDGDataHomePath("accounts.yaml"), []byte(""), 0600)
c.Assert(err, jc.ErrorIsNil)
accounts, err := jujuclient.ReadAccountsFile(jujuclient.JujuAccountsPath())
c.Assert(err, jc.ErrorIsNil)
c.Assert(accounts, gc.HasLen, 0)
}
func (s *AccountsFileSuite) TestMigrateLegacyLocal(c *gc.C) {
err := os.WriteFile(jujuclient.JujuAccountsPath(), []byte(testLegacyAccountsYAML), 0644)
c.Assert(err, jc.ErrorIsNil)
accounts, err := jujuclient.ReadAccountsFile(jujuclient.JujuAccountsPath())
c.Assert(err, jc.ErrorIsNil)
migratedData, err := os.ReadFile(jujuclient.JujuAccountsPath())
c.Assert(err, jc.ErrorIsNil)
migratedAccounts, err := jujuclient.ParseAccounts(migratedData)
c.Assert(err, jc.ErrorIsNil)
c.Assert(string(migratedData), jc.DeepEquals, testAccountsYAML[1:])
c.Assert(migratedAccounts, jc.DeepEquals, accounts)
}
func writeTestAccountsFile(c *gc.C) {
err := jujuclient.WriteAccountsFile(testControllerAccounts)
c.Assert(err, jc.ErrorIsNil)
}
func (s *AccountsFileSuite) TestParseAccounts(c *gc.C) {
accounts, err := jujuclient.ParseAccounts([]byte(testAccountsYAML))
c.Assert(err, jc.ErrorIsNil)
c.Assert(accounts, jc.DeepEquals, testControllerAccounts)
}
func (s *AccountsFileSuite) TestParseAccountMetadataError(c *gc.C) {
accounts, err := jujuclient.ParseAccounts([]byte("fail me now"))
c.Assert(err, gc.ErrorMatches,
"cannot unmarshal accounts: yaml: unmarshal errors:"+
"\n line 1: cannot unmarshal !!str `fail me...` into "+
"jujuclient.accountsCollection",
)
c.Assert(accounts, gc.IsNil)
}