-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsprofile_test.go
160 lines (128 loc) · 3.53 KB
/
awsprofile_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
package awsprofile_test
import (
"errors"
"fmt"
"log"
"os"
"testing"
"github.com/youyo/awsprofile"
)
func ExampleAwsProfile_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)
}
if err := os.Setenv("AWS_CONFIG_FILE", "./tests/.aws/config"); err != nil {
log.Fatal(err)
}
// New
awsProfile := awsprofile.New()
// Parse AWS_SHARED_CREDENTIALS_FILE and AWS_CONFIG_FILE
if err := awsProfile.Parse(); err != nil {
log.Fatal(err)
}
// Get profile names
profiles, err := awsProfile.ProfileNames()
if err != nil {
log.Fatal(err)
}
fmt.Println(profiles)
// Output: [default foo foobar bar barbar]
}
func ExampleAwsProfile_GetCredentials() {
// if you use non-default configuration file path
if err := os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "./tests/.aws/credentials"); err != nil {
log.Fatal(err)
}
if err := os.Setenv("AWS_CONFIG_FILE", "./tests/.aws/config"); err != nil {
log.Fatal(err)
}
// New
awsProfile := awsprofile.New()
// Parse AWS_SHARED_CREDENTIALS_FILE and AWS_CONFIG_FILE
if err := awsProfile.Parse(); err != nil {
log.Fatal(err)
}
credentials := awsProfile.GetCredentials()
profiles, err := credentials.ProfileNames()
if err != nil {
log.Fatal(err)
}
fmt.Println(profiles)
// Output: [default foo foobar]
}
func ExampleAwsProfile_GetConfigs() {
// if you use non-default configuration file path
if err := os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "./tests/.aws/credentials"); err != nil {
log.Fatal(err)
}
if err := os.Setenv("AWS_CONFIG_FILE", "./tests/.aws/config"); err != nil {
log.Fatal(err)
}
// New
awsProfile := awsprofile.New()
// Parse AWS_SHARED_CREDENTIALS_FILE and AWS_CONFIG_FILE
if err := awsProfile.Parse(); err != nil {
log.Fatal(err)
}
configs := awsProfile.GetConfigs()
profiles, err := configs.ProfileNames()
if err != nil {
log.Fatal(err)
}
fmt.Println(profiles)
// Output: [default bar barbar]
}
func TestAwsProfile_Parse(t *testing.T) {
// if you use non-default configuration file path
if err := os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "./tests/.aws/credentials"); err != nil {
log.Fatal(err)
}
if err := os.Setenv("AWS_CONFIG_FILE", "./tests/.aws/config"); err != nil {
log.Fatal(err)
}
// New
awsProfile := awsprofile.New()
// Parse AWS_SHARED_CREDENTIALS_FILE and AWS_CONFIG_FILE
if err := awsProfile.Parse(); err != nil {
log.Fatal(err)
}
}
func TestAwsProfile_IsCredential(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()
ok, _ := awsProfile.IsCredential("fooooo")
if ok {
log.Fatal(errors.New("Unexpected"))
}
ok, cred := awsProfile.IsCredential("foo")
if !ok {
log.Fatal(errors.New("Unmatched profile"))
}
if ok {
if cred.GetAwsAccessKeyID() != "ACCESS-2-XXXXXXXXXXXXX" {
t.Fatal(errors.New("Unmatched AwsAccessKeyID"))
}
}
}
func TestAwsProfile_IsConfig(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()
ok, _ := awsProfile.IsCredential("fooooo")
if ok {
log.Fatal(errors.New("Unexpected"))
}
ok, config := awsProfile.IsConfig("bar")
if !ok {
log.Fatal(errors.New("Unmatched profile"))
}
if ok {
if config.GetRoleArn() != "arn:aws:iam::xxxxxxxxxxxx:role/bar" {
t.Fatal(errors.New("Unmatched RoleArn"))
}
}
}