forked from felixb/swamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile_writer_test.go
107 lines (85 loc) · 2.93 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
106
107
package main
import (
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sts/types"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"os/user"
"path"
"path/filepath"
"testing"
)
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 := &types.Credentials{
AccessKeyId: aws.String("some-access-key"),
SecretAccessKey: aws.String("some-secret-access-key"),
SessionToken: aws.String("some-session-token"),
}
pw, _ := NewProfileWriter()
pw.WriteProfile(creds, profileName, region)
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 := &types.Credentials{
AccessKeyId: aws.String("some-access-key"),
SecretAccessKey: aws.String("some-secret-access-key"),
SessionToken: aws.String("some-session-token"),
}
pw, _ := NewProfileWriter()
pw.WriteProfile(creds, profileName, region)
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)
}