-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredentials_test.go
130 lines (106 loc) · 3.17 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
package awsprofile_test
import (
"errors"
"fmt"
"log"
"os"
"testing"
"github.com/youyo/awsprofile"
)
func ExampleCredentials_ProfileNames() {
// if you use non-default configuration file path
if err := os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "./tests/.aws/credentials"); err != nil {
log.Fatal(err)
}
// New
creds := awsprofile.NewCredentials()
// get credentials file path
file, err := awsprofile.GetCredentialsPath()
if err != nil {
log.Fatal(err)
}
// Parse AWS_SHARED_CREDENTIALS_FILE
if err := creds.Parse(file); err != nil {
log.Fatal(err)
}
// Get profile names
profiles, err := creds.ProfileNames()
if err != nil {
log.Fatal(err)
}
fmt.Println(profiles)
// Output: [default foo foobar]
}
func TestGetCredentialsPath(t *testing.T) {
if err := os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "./tests/.aws/credentials"); err != nil {
t.Fatal(err)
}
file, err := awsprofile.GetCredentialsPath()
if err != nil {
t.Error(err)
}
if file != "./tests/.aws/credentials" {
t.Error("file", file)
t.Error("expect", "./tests/.aws/credentials")
t.Fatal("credentials file path unmatched")
}
}
func TestCredentials_Parse(t *testing.T) {
if err := os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "./tests/.aws/credentials"); err != nil {
t.Fatal(err)
}
creds := awsprofile.NewCredentials()
file, err := awsprofile.GetCredentialsPath()
if err != nil {
t.Fatal(err)
}
if err = creds.Parse(file); err != nil {
t.Fatal(err)
}
}
func TestCredentials_GetAwsAccessKeyID(t *testing.T) {
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "./tests/.aws/credentials")
creds := awsprofile.NewCredentials()
file, _ := awsprofile.GetCredentialsPath()
creds.Parse(file)
if awsAccessKeyID, err := creds.GetAwsAccessKeyID("foo"); err != nil {
t.Fatal(err)
} else if awsAccessKeyID != "ACCESS-2-XXXXXXXXXXXXX" {
t.Fatal(errors.New("Unmatched AwsAccessKeyID"))
}
}
func TestCredentials_GetAwsSecretAccessKey(t *testing.T) {
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "./tests/.aws/credentials")
creds := awsprofile.NewCredentials()
file, _ := awsprofile.GetCredentialsPath()
creds.Parse(file)
if awsSecretAccessKey, err := creds.GetAwsSecretAccessKey("foo"); err != nil {
t.Fatal(err)
} else if awsSecretAccessKey != "SECRET-2-XXXXXXXXXXXXX" {
t.Fatal(errors.New("Unmatched AwsSecretAccessKey"))
}
}
func TestCredential_GetAwsAccessKeyID(t *testing.T) {
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "./tests/.aws/credentials")
os.Setenv("AWS_CONFIG_FILE", "./tests/.aws/config")
awsProfile := awsprofile.New()
awsProfile.Parse()
if ok, cred := awsProfile.IsCredential("foo"); ok {
awsAccessKeyID := cred.GetAwsAccessKeyID()
if awsAccessKeyID != "ACCESS-2-XXXXXXXXXXXXX" {
t.Fatal(errors.New("Unmatched AwsAccessKeyID"))
}
}
}
func TestCredential_GetAwsSecretAccessKey(t *testing.T) {
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "./tests/.aws/credentials")
os.Setenv("AWS_CONFIG_FILE", "./tests/.aws/config")
awsProfile := awsprofile.New()
awsProfile.Parse()
if ok, cred := awsProfile.IsCredential("foo"); ok {
awsSecretAccessKey := cred.GetAwsSecretAccessKey()
if awsSecretAccessKey != "SECRET-2-XXXXXXXXXXXXX" {
t.Fatal(errors.New("Unmatched AwsSecretAccessKey"))
}
}
}