@@ -14,22 +14,86 @@ import (
14
14
"github.com/stretchr/testify/assert"
15
15
)
16
16
17
- func TestClientSuccessfulConnection (t * testing.T ) {
18
- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
19
- b := []byte (`{}` )
20
- w .WriteHeader (http .StatusOK )
21
- w .Write (b )
22
- }))
23
-
24
- svc := lambda .New (session .Must (session .NewSession (& aws.Config {
25
- Endpoint : aws .String (ts .URL ),
26
- Region : aws .String ("fake-moon-1" ),
27
- Credentials : credentials .NewStaticCredentials ("akid" , "secret" , "noop" )})))
17
+ func TestAWS (t * testing.T ) {
18
+ // Runs a suite of tests against two different methods of registering
19
+ // handlers on an AWS client.
20
+
21
+ type test func (* testing.T , * lambda.Lambda )
22
+ tests := []struct {
23
+ name string
24
+ test test
25
+ failConn bool
26
+ }{
27
+ {"failed connection" , testClientFailedConnection , true },
28
+ {"successful connection" , testClientSuccessfulConnection , false },
29
+ {"without segment" , testClientWithoutSegment , false },
30
+ }
28
31
29
- ctx , root := BeginSegment (context .Background (), "Test" )
32
+ onClient := func (s * session.Session ) * lambda.Lambda {
33
+ svc := lambda .New (s )
34
+ AWS (svc .Client )
35
+ return svc
36
+ }
30
37
31
- AWS (svc .Client )
38
+ onSession := func (s * session.Session ) * lambda.Lambda {
39
+ return lambda .New (AWSSession (s ))
40
+ }
41
+
42
+ const whitelist = "../resources/AWSWhitelist.json"
43
+
44
+ onClientWithWhitelist := func (s * session.Session ) * lambda.Lambda {
45
+ svc := lambda .New (s )
46
+ AWSWithWhitelist (svc .Client , whitelist )
47
+ return svc
48
+ }
49
+
50
+ onSessionWithWhitelist := func (s * session.Session ) * lambda.Lambda {
51
+ return lambda .New (AWSSessionWithWhitelist (s , whitelist ))
52
+ }
53
+
54
+ type constructor func (* session.Session ) * lambda.Lambda
55
+ constructors := []struct {
56
+ name string
57
+ constructor constructor
58
+ }{
59
+ {"AWS()" , onClient },
60
+ {"AWSSession()" , onSession },
61
+ {"AWSWithWhitelist()" , onClientWithWhitelist },
62
+ {"AWSSessionWithWhitelist()" , onSessionWithWhitelist },
63
+ }
64
+
65
+ // Run all combinations of constructors + tests.
66
+ for _ , cons := range constructors {
67
+ t .Run (cons .name , func (t * testing.T ) {
68
+ for _ , test := range tests {
69
+ t .Run (test .name , func (t * testing.T ) {
70
+ test .test (t , cons .constructor (fakeSession (t , test .failConn )))
71
+ })
72
+ }
73
+ })
74
+ }
75
+ }
32
76
77
+ func fakeSession (t * testing.T , failConn bool ) * session.Session {
78
+ cfg := & aws.Config {
79
+ Region : aws .String ("fake-moon-1" ),
80
+ Credentials : credentials .NewStaticCredentials ("akid" , "secret" , "noop" ),
81
+ }
82
+ if ! failConn {
83
+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
84
+ b := []byte (`{}` )
85
+ w .WriteHeader (http .StatusOK )
86
+ w .Write (b )
87
+ }))
88
+ cfg .Endpoint = aws .String (ts .URL )
89
+ }
90
+ s , err := session .NewSession (cfg )
91
+ assert .NoError (t , err )
92
+ return s
93
+ }
94
+
95
+ func testClientSuccessfulConnection (t * testing.T , svc * lambda.Lambda ) {
96
+ ctx , root := BeginSegment (context .Background (), "Test" )
33
97
_ , err := svc .ListFunctionsWithContext (ctx , & lambda.ListFunctionsInput {})
34
98
root .Close (nil )
35
99
assert .NoError (t , err )
@@ -76,15 +140,8 @@ func TestClientSuccessfulConnection(t *testing.T) {
76
140
}
77
141
}
78
142
79
- func TestClientFailedConnection (t * testing.T ) {
80
- svc := lambda .New (session .Must (session .NewSession (& aws.Config {
81
- Region : aws .String ("fake-moon-1" ),
82
- Credentials : credentials .NewStaticCredentials ("akid" , "secret" , "noop" )})))
83
-
143
+ func testClientFailedConnection (t * testing.T , svc * lambda.Lambda ) {
84
144
ctx , root := BeginSegment (context .Background (), "Test" )
85
-
86
- AWS (svc .Client )
87
-
88
145
_ , err := svc .ListFunctionsWithContext (ctx , & lambda.ListFunctionsInput {})
89
146
root .Close (nil )
90
147
assert .Error (t , err )
@@ -116,24 +173,11 @@ func TestClientFailedConnection(t *testing.T) {
116
173
assert .NotEmpty (t , connectSubseg .Subsegments )
117
174
}
118
175
119
- func TestClientWithoutSegment (t * testing.T ) {
176
+ func testClientWithoutSegment (t * testing.T , svc * lambda. Lambda ) {
120
177
Configure (Config {ContextMissingStrategy : & TestContextMissingStrategy {}})
121
178
defer ResetConfig ()
122
- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
123
- b := []byte (`{}` )
124
- w .WriteHeader (http .StatusOK )
125
- w .Write (b )
126
- }))
127
-
128
- svc := lambda .New (session .Must (session .NewSession (& aws.Config {
129
- Endpoint : aws .String (ts .URL ),
130
- Region : aws .String ("fake-moon-1" ),
131
- Credentials : credentials .NewStaticCredentials ("akid" , "secret" , "noop" )})))
132
179
133
180
ctx := context .Background ()
134
-
135
- AWS (svc .Client )
136
-
137
181
_ , err := svc .ListFunctionsWithContext (ctx , & lambda.ListFunctionsInput {})
138
182
assert .NoError (t , err )
139
183
}
0 commit comments