-
Notifications
You must be signed in to change notification settings - Fork 508
/
Copy pathcredentialsfile_test.go
95 lines (80 loc) · 2.52 KB
/
credentialsfile_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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuclient_test
import (
"os"
"github.com/juju/errors"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
"github.com/juju/juju/cloud"
"github.com/juju/juju/internal/testing"
"github.com/juju/juju/juju/osenv"
"github.com/juju/juju/jujuclient"
)
type CredentialsFileSuite struct {
testing.FakeJujuXDGDataHomeSuite
}
var _ = gc.Suite(&CredentialsFileSuite{})
const testCredentialsYAML = `
credentials:
aws:
default-credential: peter
default-region: us-west-2
paul:
auth-type: access-key
access-key: paul-key
secret-key: paul-secret
peter:
auth-type: access-key
access-key: key
secret-key: secret
aws-gov:
fbi:
auth-type: access-key
access-key: key
secret-key: secret
`
func (s *CredentialsFileSuite) TestWriteFile(c *gc.C) {
writeTestCredentialsFile(c)
data, err := os.ReadFile(osenv.JujuXDGDataHomePath("credentials.yaml"))
c.Assert(err, jc.ErrorIsNil)
var original map[string]interface{}
err = yaml.Unmarshal([]byte(testCredentialsYAML), &original)
c.Assert(err, jc.ErrorIsNil)
var written map[string]interface{}
err = yaml.Unmarshal(data, &written)
c.Assert(err, jc.ErrorIsNil)
c.Assert(written, gc.DeepEquals, original)
}
func (s *CredentialsFileSuite) TestReadNoFile(c *gc.C) {
credentials, err := jujuclient.ReadCredentialsFile("nohere.yaml")
c.Assert(err, jc.ErrorIsNil)
c.Assert(credentials, gc.NotNil)
}
func (s *CredentialsFileSuite) TestReadEmptyFile(c *gc.C) {
err := os.WriteFile(osenv.JujuXDGDataHomePath("credentials.yaml"), []byte(""), 0600)
c.Assert(err, jc.ErrorIsNil)
credentialstore := jujuclient.NewFileCredentialStore()
_, err = credentialstore.CredentialForCloud("foo")
c.Assert(err, jc.ErrorIs, errors.NotFound)
}
func parseCredentials(c *gc.C) *cloud.CredentialCollection {
credentials, err := cloud.ParseCredentialCollection([]byte(testCredentialsYAML))
c.Assert(err, jc.ErrorIsNil)
return credentials
}
func writeTestCredentialsFile(c *gc.C) map[string]cloud.CloudCredential {
credentials := parseCredentials(c)
err := jujuclient.WriteCredentialsFile(credentials)
c.Assert(err, jc.ErrorIsNil)
allCredentials := make(map[string]cloud.CloudCredential)
names := credentials.CloudNames()
for _, cloudName := range names {
cred, err := credentials.CloudCredential(cloudName)
c.Assert(err, jc.ErrorIsNil)
c.Assert(cred, gc.NotNil)
allCredentials[cloudName] = *cred
}
return allCredentials
}