forked from aws/eks-anywhere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreds_test.go
116 lines (100 loc) Β· 3.36 KB
/
creds_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
package aws_test
import (
"os"
"strings"
"testing"
. "github.com/onsi/gomega"
"github.com/aws/eks-anywhere/pkg/aws"
)
const (
credentialsFile = "testdata/valid_credentials"
certificatesFile = "testdata/valid_certificates"
)
func TestAwsCredentialsFile(t *testing.T) {
tt := newAwsTest(t)
t.Setenv(aws.EksaAwsCredentialsFileKey, credentialsFile)
_, err := aws.AwsCredentialsFile()
tt.Expect(err).To(Succeed())
}
func TestAwsCredentialsFileEnvNotSet(t *testing.T) {
tt := newAwsTest(t)
os.Unsetenv(aws.EksaAwsCredentialsFileKey)
_, err := aws.AwsCredentialsFile()
tt.Expect(err).To((MatchError(ContainSubstring("env 'EKSA_AWS_CREDENTIALS_FILE' is not set or is empty"))))
}
func TestAwsCredentialsFileNotExists(t *testing.T) {
tt := newAwsTest(t)
t.Setenv(aws.EksaAwsCredentialsFileKey, "testdata/not_exists_credentials")
_, err := aws.AwsCredentialsFile()
tt.Expect(err).To((MatchError(ContainSubstring("file 'testdata/not_exists_credentials' does not exist"))))
}
func TestAwsCABundlesFile(t *testing.T) {
tt := newAwsTest(t)
t.Setenv(aws.EksaAwsCABundlesFileKey, certificatesFile)
_, err := aws.AwsCABundlesFile()
tt.Expect(err).To(Succeed())
}
func TestAwsCABundlesFileEnvNotSet(t *testing.T) {
tt := newAwsTest(t)
os.Unsetenv(aws.EksaAwsCABundlesFileKey)
_, err := aws.AwsCABundlesFile()
tt.Expect(err).To((MatchError(ContainSubstring("env 'EKSA_AWS_CA_BUNDLES_FILE' is not set or is empty"))))
}
func TestAwsCABundlesFileNotExists(t *testing.T) {
tt := newAwsTest(t)
t.Setenv(aws.EksaAwsCABundlesFileKey, "testdata/not_exists_certificates")
_, err := aws.AwsCABundlesFile()
tt.Expect(err).To((MatchError(ContainSubstring("file 'testdata/not_exists_certificates' does not exist"))))
}
func TestEncodeFileFromEnv(t *testing.T) {
tt := newAwsTest(t)
t.Setenv(aws.EksaAwsCredentialsFileKey, credentialsFile)
strB64, err := aws.EncodeFileFromEnv(aws.EksaAwsCredentialsFileKey)
tt.Expect(err).To(Succeed())
tt.Expect(strB64).To(Equal("WzEuMi4zLjRdCmF3c19hY2Nlc3Nfa2V5X2lkID0gQUJDREVGR0hJSktMTU5PUFFSMlQKYXdzX3NlY3JldF9hY2Nlc3Nfa2V5ID0gQWZTRDdzWXovVEJadHprUmVCbDZQdXVJU3pKMld0TmtlZVB3K25OekoKcmVnaW9uID0gc25vdwoKWzEuMi4zLjVdCmF3c19hY2Nlc3Nfa2V5X2lkID0gQUJDREVGR0hJSktMTU5PUFFSMlQKYXdzX3NlY3JldF9hY2Nlc3Nfa2V5ID0gQWZTRDdzWXovVEJadHprUmVCbDZQdXVJU3pKMld0TmtlZVB3K25OekoKcmVnaW9uID0gc25vdw=="))
}
func TestParseDeviceIPsFromFile(t *testing.T) {
tests := []struct {
name string
creds string
want []string
wantErr string
}{
{
name: "validate creds",
creds: `[1.2.3.4]
aws_access_key_id = ABCDEFGHIJKLMNOPQR2T
aws_secret_access_key = AfSD7sYz/TBZtzkReBl6PuuISzJ2WtNkeePw+nNzJ
region = snow
[1.2.3.5]
aws_access_key_id = ABCDEFGHIJKLMNOPQR2T
aws_secret_access_key = AfSD7sYz/TBZtzkReBl6PuuISzJ2WtNkeePw+nNzJ
region = snow`,
want: []string{
"1.2.3.4",
"1.2.3.5",
},
wantErr: "",
},
{
name: "no ip in profile",
creds: `[invalid profile]
aws_access_key_id = ABCDEFGHIJKLMNOPQR2T
aws_secret_access_key = AfSD7sYz/TBZtzkReBl6PuuISzJ2WtNkeePw+nNzJ
region = snow`,
wantErr: "no ip address profile found",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := newAwsTest(t)
got, err := aws.ParseDeviceIPs(strings.NewReader(tt.creds))
if tt.wantErr == "" {
g.Expect(err).To(Succeed())
g.Expect(got).To(Equal(tt.want))
} else {
g.Expect(err).To(MatchError(ContainSubstring(tt.wantErr)))
}
})
}
}