This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
forked from felixb/swamp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
profile_writer_test.go
105 lines (82 loc) · 2.85 KB
/
profile_writer_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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
"path/filepath"
"testing"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/stretchr/testify/assert"
)
func TestProfileWriter_NewProfileWriterWithDefaults(t *testing.T) {
pw, err := NewProfileWriter()
assert.NoError(t, err)
assert.NotNil(t, pw)
usr, _ := user.Current()
awsPath := filepath.Join(usr.HomeDir, ".aws")
credPath := filepath.Join(awsPath, "credentials")
assert.Equal(t, awsPath+"/", pw.awsPath)
assert.Equal(t, credPath, pw.credentialsPath)
}
func TestProfileWriter_NewProfileWriterWithEnvironment(t *testing.T) {
awsPath := "/tmp/foo/"
credPath := "/tmp/foo/.aws-secrets"
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", credPath)
defer os.Clearenv()
pw, err := NewProfileWriter()
assert.NoError(t, err)
assert.NotNil(t, pw)
assert.Equal(t, awsPath, pw.awsPath)
assert.Equal(t, credPath, pw.credentialsPath)
}
func TestProfileWriter_WriteProfile(t *testing.T) {
credPath := path.Join(os.TempDir(), "swamp-test.ini")
os.Remove(credPath)
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", credPath)
defer os.Clearenv()
defer os.Remove(credPath)
profileName := "some-profile"
region := "some-region"
creds := &sts.Credentials{}
creds.SetAccessKeyId("some-access-key")
creds.SetSecretAccessKey("some-secret-access-key")
creds.SetSessionToken("some-session-token")
pw, _ := NewProfileWriter()
pw.WriteProfile(creds, &profileName, ®ion)
b, err := ioutil.ReadFile(credPath)
assert.NoError(t, err)
content := string(b)
assert.Regexp(t, `^\[some-profile\]\n.*`, content)
assertKeyValue(t, "region", "some-region", content)
assertKeyValue(t, "aws_access_key_id", "some-access-key", content)
assertKeyValue(t, "aws_secret_access_key", "some-secret-access-key", content)
assertKeyValue(t, "aws_session_token", "some-session-token", content)
}
func TestProfileWriter_WriteProfileWoRegion(t *testing.T) {
credPath := path.Join(os.TempDir(), "swamp-test.ini")
os.Remove(credPath)
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", credPath)
defer os.Clearenv()
defer os.Remove(credPath)
profileName := "some-profile"
region := ""
creds := &sts.Credentials{}
creds.SetAccessKeyId("some-access-key")
creds.SetSecretAccessKey("some-secret-access-key")
creds.SetSessionToken("some-session-token")
pw, _ := NewProfileWriter()
pw.WriteProfile(creds, &profileName, ®ion)
b, err := ioutil.ReadFile(credPath)
assert.NoError(t, err)
content := string(b)
assert.Regexp(t, `^\[some-profile\]\n.*`, content)
assert.NotRegexp(t, `region`, content)
assertKeyValue(t, "aws_access_key_id", "some-access-key", content)
assertKeyValue(t, "aws_secret_access_key", "some-secret-access-key", content)
assertKeyValue(t, "aws_session_token", "some-session-token", content)
}
func assertKeyValue(t *testing.T, key, value, content string) {
assert.Regexp(t, fmt.Sprintf(`\n%s\s*=\s*%s\n.*`, key, value), content)
}