forked from juju/description
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudcredential_test.go
224 lines (203 loc) · 5.99 KB
/
cloudcredential_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
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/names/v5"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
)
type CloudCredentialSerializationSuite struct {
SerializationSuite
}
var _ = gc.Suite(&CloudCredentialSerializationSuite{})
func (s *CloudCredentialSerializationSuite) SetUpTest(c *gc.C) {
s.SerializationSuite.SetUpTest(c)
s.importName = "cloudCredential"
s.importFunc = func(m map[string]interface{}) (interface{}, error) {
return importCloudCredential(m)
}
s.testFields = func(m map[string]interface{}) {
m["owner"] = ""
m["cloud"] = ""
m["name"] = ""
m["auth-type"] = ""
}
}
func (s *CloudCredentialSerializationSuite) TestMissingOwner(c *gc.C) {
testMap := s.makeMap(1)
delete(testMap, "owner")
_, err := importCloudCredential(testMap)
c.Check(err.Error(), gc.Equals, "cloudCredential v1 schema check failed: owner: expected string, got nothing")
}
func (s *CloudCredentialSerializationSuite) TestMissingCloud(c *gc.C) {
testMap := s.makeMap(1)
delete(testMap, "cloud")
_, err := importCloudCredential(testMap)
c.Check(err.Error(), gc.Equals, "cloudCredential v1 schema check failed: cloud: expected string, got nothing")
}
func (s *CloudCredentialSerializationSuite) TestMissingName(c *gc.C) {
testMap := s.makeMap(1)
delete(testMap, "name")
_, err := importCloudCredential(testMap)
c.Check(err.Error(), gc.Equals, "cloudCredential v1 schema check failed: name: expected string, got nothing")
}
func (s *CloudCredentialSerializationSuite) TestMissingAuthType(c *gc.C) {
testMap := s.makeMap(1)
delete(testMap, "auth-type")
_, err := importCloudCredential(testMap)
c.Check(err.Error(), gc.Equals, "cloudCredential v1 schema check failed: auth-type: expected string, got nothing")
}
func (*CloudCredentialSerializationSuite) allArgs() CloudCredentialArgs {
return CloudCredentialArgs{
Owner: names.NewUserTag("me"),
Cloud: names.NewCloudTag("altostratus"),
Name: "creds",
AuthType: "fuzzy",
Attributes: map[string]string{
"key": "value",
},
}
}
func (s *CloudCredentialSerializationSuite) TestAllArgs(c *gc.C) {
args := s.allArgs()
creds := newCloudCredential(args)
c.Check(creds.Owner(), gc.Equals, args.Owner.Id())
c.Check(creds.Cloud(), gc.Equals, args.Cloud.Id())
c.Check(creds.Name(), gc.Equals, args.Name)
c.Check(creds.AuthType(), gc.Equals, args.AuthType)
c.Check(creds.Attributes(), jc.DeepEquals, args.Attributes)
}
func (s *CloudCredentialSerializationSuite) TestParsingSerializedData(c *gc.C) {
args := s.allArgs()
initial := newCloudCredential(args)
bytes, err := yaml.Marshal(initial)
c.Assert(err, jc.ErrorIsNil)
var source map[string]interface{}
err = yaml.Unmarshal(bytes, &source)
c.Assert(err, jc.ErrorIsNil)
imported, err := importCloudCredential(source)
c.Assert(err, jc.ErrorIsNil)
c.Assert(imported, jc.DeepEquals, initial)
}
func (s *CloudCredentialSerializationSuite) TestV2MigrationSteps(c *gc.C) {
tests := []struct {
InitialSource map[string]interface{}
PostAuthType string
PostAttributes map[string]string
}{
{
InitialSource: map[string]interface{}{
"version": 1,
"owner": "wallyworld",
"cloud": "k8s",
"name": "ipv6rockz",
"auth-type": "oauth2withcert",
"attributes": map[string]string{
"ClientCertificateData": "aa=",
"ClientKeyData": "aa=",
},
},
PostAuthType: "clientcertificate",
PostAttributes: map[string]string{
"ClientCertificateData": "aa=",
"ClientKeyData": "aa=",
},
},
{
InitialSource: map[string]interface{}{
"version": 1,
"owner": "wallyworld",
"cloud": "k8s",
"name": "ipv6rockz",
"auth-type": "oauth2withcert",
"attributes": map[string]string{
"Token": "aa=",
},
},
PostAuthType: "oauth2",
PostAttributes: map[string]string{
"Token": "aa=",
},
},
{
InitialSource: map[string]interface{}{
"version": 1,
"owner": "wallyworld",
"cloud": "k8s",
"name": "ipv6rockz",
"auth-type": "certificate",
"attributes": map[string]string{
"Token": "aa=",
"ClientCertificateData": "aa=",
},
},
PostAuthType: "oauth2",
PostAttributes: map[string]string{
"Token": "aa=",
},
},
{
InitialSource: map[string]interface{}{
"version": 1,
"owner": "wallyworld",
"cloud": "k8s",
"name": "ipv6rockz",
"auth-type": "certificate",
"attributes": map[string]string{
"ClientCertificateData": "aa=",
"ClientKeyData": "aa=",
},
},
PostAuthType: "certificate",
PostAttributes: map[string]string{
"ClientCertificateData": "aa=",
"ClientKeyData": "aa=",
},
},
{
InitialSource: map[string]interface{}{
"version": 1,
"owner": "wallyworld",
"cloud": "k8s",
"name": "ipv6rockz",
"auth-type": "certificate",
},
PostAuthType: "certificate",
PostAttributes: nil,
},
}
for _, test := range tests {
cred, err := importCloudCredential(test.InitialSource)
c.Assert(err, jc.ErrorIsNil)
c.Assert(cred.AuthType(), gc.Equals, test.PostAuthType)
c.Assert(cred.Attributes(), jc.DeepEquals, test.PostAttributes)
}
}
func (s *CloudCredentialSerializationSuite) TestV1MigrationToLatest(c *gc.C) {
InitialSource := map[string]interface{}{
"version": 1,
"owner": "wallyworld",
"cloud": "k8s",
"name": "ipv6rockz",
"auth-type": "certificate",
"attributes": map[string]string{
"ClientCertificateData": "aa=",
"ClientKeyData": "aa=",
},
}
cred, err := importCloudCredential(InitialSource)
c.Assert(err, jc.ErrorIsNil)
c.Assert(cred.Version, gc.Equals, 2)
c.Assert(cred, gc.DeepEquals, &cloudCredential{
Version: 2,
Owner_: "wallyworld",
Cloud_: "k8s",
Name_: "ipv6rockz",
AuthType_: "certificate",
Attributes_: map[string]string{
"ClientCertificateData": "aa=",
"ClientKeyData": "aa=",
},
})
}