-
Notifications
You must be signed in to change notification settings - Fork 508
/
Copy pathcredentials_test.go
136 lines (112 loc) · 3.94 KB
/
credentials_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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuclient_test
import (
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/cloud"
"github.com/juju/juju/internal/testing"
"github.com/juju/juju/jujuclient"
)
type CredentialsSuite struct {
testing.FakeJujuXDGDataHomeSuite
store jujuclient.CredentialStore
cloudName string
credentials cloud.CloudCredential
}
var _ = gc.Suite(&CredentialsSuite{})
func (s *CredentialsSuite) SetUpTest(c *gc.C) {
s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
s.store = jujuclient.NewFileCredentialStore()
s.cloudName = "testcloud"
s.credentials = cloud.CloudCredential{
DefaultCredential: "peter",
DefaultRegion: "east",
AuthCredentials: map[string]cloud.Credential{
"peter": cloud.NewCredential(cloud.AccessKeyAuthType, nil),
"paul": cloud.NewCredential(cloud.AccessKeyAuthType, nil),
},
}
}
func (s *CredentialsSuite) TestCredentialForCloudNoFile(c *gc.C) {
found, err := s.store.CredentialForCloud(s.cloudName)
c.Assert(err, gc.ErrorMatches, "credentials for cloud testcloud not found")
c.Assert(found, gc.IsNil)
}
func (s *CredentialsSuite) TestCredentialForCloudNoneExists(c *gc.C) {
writeTestCredentialsFile(c)
found, err := s.store.CredentialForCloud(s.cloudName)
c.Assert(err, gc.ErrorMatches, "credentials for cloud testcloud not found")
c.Assert(found, gc.IsNil)
}
func (s *CredentialsSuite) TestCredentialForCloud(c *gc.C) {
name := firstTestCloudName(c)
found, err := s.store.CredentialForCloud(name)
c.Assert(err, jc.ErrorIsNil)
expected := s.getCredentials(c)[name]
c.Assert(found, gc.DeepEquals, &expected)
}
func (s *CredentialsSuite) TestUpdateCredentialAddFirst(c *gc.C) {
err := s.store.UpdateCredential(s.cloudName, s.credentials)
c.Assert(err, jc.ErrorIsNil)
s.assertUpdateSucceeded(c)
}
func (s *CredentialsSuite) TestUpdateCredentialAddNew(c *gc.C) {
s.assertCredentialsNotExists(c)
err := s.store.UpdateCredential(s.cloudName, s.credentials)
c.Assert(err, jc.ErrorIsNil)
s.assertUpdateSucceeded(c)
}
func (s *CredentialsSuite) TestUpdateCredential(c *gc.C) {
s.cloudName = firstTestCloudName(c)
err := s.store.UpdateCredential(s.cloudName, s.credentials)
c.Assert(err, jc.ErrorIsNil)
s.assertUpdateSucceeded(c)
}
func (s *CredentialsSuite) TestUpdateCredentialRemovesDefaultIfNecessary(c *gc.C) {
s.cloudName = firstTestCloudName(c)
store := jujuclient.NewFileCredentialStore()
err := store.UpdateCredential(s.cloudName, s.credentials)
c.Assert(err, jc.ErrorIsNil)
newCreds := s.credentials
// "peter" is the default credential
delete(newCreds.AuthCredentials, "peter")
err = store.UpdateCredential(s.cloudName, newCreds)
c.Assert(err, jc.ErrorIsNil)
creds, err := store.AllCredentials()
c.Assert(err, jc.ErrorIsNil)
c.Assert(creds[s.cloudName].DefaultCredential, gc.Equals, "")
}
func (s *CredentialsSuite) TestUpdateCredentialRemovesCloudWhenNoCredentialLeft(c *gc.C) {
s.cloudName = firstTestCloudName(c)
store := jujuclient.NewFileCredentialStore()
err := store.UpdateCredential(s.cloudName, s.credentials)
c.Assert(err, jc.ErrorIsNil)
// delete all
err = store.UpdateCredential(s.cloudName, cloud.CloudCredential{})
c.Assert(err, jc.ErrorIsNil)
creds, err := store.AllCredentials()
c.Assert(err, jc.ErrorIsNil)
_, exists := creds[s.cloudName]
c.Assert(exists, jc.IsFalse)
}
func (s *CredentialsSuite) assertCredentialsNotExists(c *gc.C) {
all := writeTestCredentialsFile(c)
_, exists := all[s.cloudName]
c.Assert(exists, jc.IsFalse)
}
func (s *CredentialsSuite) assertUpdateSucceeded(c *gc.C) {
c.Assert(s.getCredentials(c)[s.cloudName], gc.DeepEquals, s.credentials)
}
func (s *CredentialsSuite) getCredentials(c *gc.C) map[string]cloud.CloudCredential {
credentials, err := s.store.AllCredentials()
c.Assert(err, jc.ErrorIsNil)
return credentials
}
func firstTestCloudName(c *gc.C) string {
all := writeTestCredentialsFile(c)
for key := range all {
return key
}
return ""
}