-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
112 lines (102 loc) · 3.09 KB
/
main_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
package main
import (
"bufio"
"bytes"
"reflect"
"strings"
"testing"
)
var testFileContent = []string{
"[test_profile]",
"aws_access_key_id = test_access_key_id",
"aws_secret_access_key = test_secret_access_key",
"aws_session_token = test_session_token",
"",
"[test_profile2]",
"aws_access_key_id = test_access_key_id2",
"aws_secret_access_key = test_secret_access_key2",
"aws_session_token = test_session_token2",
"",
"[test_profile3]",
"aws_access_key_id = test_access_key_id3",
"aws_secret_access_key = test_secret_access_key3",
"aws_session_token = test_session_token3",
}
func TestRemoveProfile(t *testing.T) {
expectedFileContent := []string{
"[test_profile]",
"aws_access_key_id = test_access_key_id",
"aws_secret_access_key = test_secret_access_key",
"aws_session_token = test_session_token",
"",
"[test_profile3]",
"aws_access_key_id = test_access_key_id3",
"aws_secret_access_key = test_secret_access_key3",
"aws_session_token = test_session_token3",
}
newCredentials := removeProfile("test_profile2", testFileContent)
if !reflect.DeepEqual(newCredentials, expectedFileContent) {
t.Errorf("Expected %v, got %v", expectedFileContent, newCredentials)
}
}
func TestFileContainsProfile(t *testing.T) {
fileContent := []string{
"[test_profile]",
"aws_access_key_id = test_access_key_id",
"[test_profile2]",
"aws_access_key_id = test_access_key_id2",
"[test_profile3]",
"aws_access_key_id = test_access_key_id3",
}
buff := bytes.Buffer{}
for _, line := range fileContent {
buff.WriteString(line)
buff.WriteString("\n")
}
expected := true
actual := fileContainsProfile("test_profile2", strings.Split(buff.String(), "\n"))
if expected != actual {
t.Errorf("Expected %v, got %v", expected, actual)
}
}
func TestAddProfile(t *testing.T) {
buffer := bytes.Buffer{}
for _, line := range testFileContent {
buffer.WriteString(line)
buffer.WriteString("\n")
}
credentials := credentials{
AccessKeyId: "test_access_key_id",
SecretAccessKey: "test_secret_access_key",
SessionToken: "test_session_token",
}
profile := "test_profile4"
w := bufio.NewWriter(&buffer)
appendCredentials(credentials, profile, w)
w.Flush()
if !fileContainsProfile(profile, strings.Split(buffer.String(), "\n")) {
t.Errorf("Expected to see %v, got %v", profile, buffer.String())
}
}
func TestFormatCredentials(t *testing.T) {
uglyCredentials := []string{
"[test_profile]",
"aws_access_key_id = test_access_key_id",
"aws_secret_access_key = test_secret_access_key",
"aws_session_token = test_session_token",
"", "", "", "",
"[test_profile2]",
"aws_access_key_id = test_access_key_id2",
"aws_secret_access_key = test_secret_access_key2",
"aws_session_token = test_session_token2",
"", "", "", "",
"[test_profile3]",
"aws_access_key_id = test_access_key_id3",
"aws_secret_access_key = test_secret_access_key3",
"aws_session_token = test_session_token3",
}
formattedCredentials := formatCredentials(uglyCredentials)
if !reflect.DeepEqual(testFileContent, formattedCredentials) {
t.Errorf("Expected %v, got %v", testFileContent, formattedCredentials)
}
}