-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathdynamodb_test.go
More file actions
261 lines (228 loc) · 7.79 KB
/
dynamodb_test.go
File metadata and controls
261 lines (228 loc) · 7.79 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package dynamodb_test
import (
"context"
"log"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
awsdynamodb "github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/philippgille/gokv/dynamodb"
"github.com/philippgille/gokv/encoding"
"github.com/philippgille/gokv/test"
)
// For "DynamoDB local" Docker container.
// See https://hub.docker.com/r/amazon/dynamodb-local/.
var customEndpoint = "http://localhost:8000"
// TestConnection only tests the connection to the local DynamoDB, allowing to work on connection options with `go test -run TestConnection .` for example.
func TestConnection(t *testing.T) {
if !checkConnection() {
t.Skip("No connection to DynamoDB could be established. Probably not running in a proper test environment.")
}
}
// TestClient tests if reading from, writing to and deleting from the store works properly.
// A struct is used as value. See TestTypes() for a test that is simpler but tests all types.
//
// Note: This test is only executed if the initial connection to DynamoDB works.
func TestClient(t *testing.T) {
if !checkConnection() {
t.Skip("No connection to DynamoDB could be established. Probably not running in a proper test environment.")
}
// Test with JSON
t.Run("JSON", func(t *testing.T) {
client := createClient(t, encoding.JSON)
test.TestStore(client, t)
})
// Test with gob
t.Run("gob", func(t *testing.T) {
client := createClient(t, encoding.Gob)
test.TestStore(client, t)
})
}
// TestTypes tests if setting and getting values works with all Go types.
//
// Note: This test is only executed if the initial connection to DynamoDB works.
func TestTypes(t *testing.T) {
if !checkConnection() {
t.Skip("No connection to DynamoDB could be established. Probably not running in a proper test environment.")
}
// Test with JSON
t.Run("JSON", func(t *testing.T) {
client := createClient(t, encoding.JSON)
test.TestTypes(client, t)
})
// Test with gob
t.Run("gob", func(t *testing.T) {
client := createClient(t, encoding.Gob)
test.TestTypes(client, t)
})
}
// TestClientConcurrent launches a bunch of goroutines that concurrently work with the DynamoDB client.
//
// Note: This test is only executed if the initial connection to DynamoDB works.
func TestClientConcurrent(t *testing.T) {
if !checkConnection() {
t.Skip("No connection to DynamoDB could be established. Probably not running in a proper test environment.")
}
client := createClient(t, encoding.JSON)
goroutineCount := 1000
test.TestConcurrentInteractions(t, goroutineCount, client)
}
// TestErrors tests some error cases.
//
// Note: This test is only executed if the initial connection to DynamoDB works.
func TestErrors(t *testing.T) {
if !checkConnection() {
t.Skip("No connection to DynamoDB could be established. Probably not running in a proper test environment.")
}
// Test empty key
client := createClient(t, encoding.JSON)
err := client.Set("", "bar")
if err == nil {
t.Error("Expected an error")
}
_, err = client.Get("", new(string))
if err == nil {
t.Error("Expected an error")
}
err = client.Delete("")
if err == nil {
t.Error("Expected an error")
}
// Test client creation with bad options
options := dynamodb.Options{
AWSaccessKeyID: "foo",
}
_, err = dynamodb.NewClient(options)
if err.Error() != "When passing credentials via options, you need to set BOTH AWSaccessKeyID AND AWSsecretAccessKey" {
t.Error("An error was expected, but didn't occur.")
}
options = dynamodb.Options{
AWSsecretAccessKey: "foo",
}
_, err = dynamodb.NewClient(options)
if err.Error() != "When passing credentials via options, you need to set BOTH AWSaccessKeyID AND AWSsecretAccessKey" {
t.Error("An error was expected, but didn't occur.")
}
// Bad credentials on actual AWS DynamoDB endpoint (no custom endpoint for local Docker container)
options = dynamodb.Options{
AWSaccessKeyID: "foo",
AWSsecretAccessKey: "bar",
Region: endpoints.UsWest2RegionID,
}
_, err = dynamodb.NewClient(options)
if strings.Index(err.Error(), "UnrecognizedClientException: The security token included in the request is invalid.") != 0 {
t.Errorf("An UnrecognizedClientException was expected, but it seems like it didn't occur. Instead, the error was: %v", err)
}
}
// TestNil tests the behaviour when passing nil or pointers to nil values to some methods.
//
// Note: This test is only executed if the initial connection to DynamoDB works.
func TestNil(t *testing.T) {
if !checkConnection() {
t.Skip("No connection to DynamoDB could be established. Probably not running in a proper test environment.")
}
// Test setting nil
t.Run("set nil with JSON marshalling", func(t *testing.T) {
client := createClient(t, encoding.JSON)
err := client.Set("foo", nil)
if err == nil {
t.Error("Expected an error")
}
})
t.Run("set nil with Gob marshalling", func(t *testing.T) {
client := createClient(t, encoding.Gob)
err := client.Set("foo", nil)
if err == nil {
t.Error("Expected an error")
}
})
// Test passing nil or pointer to nil value for retrieval
createTest := func(codec encoding.Codec) func(t *testing.T) {
return func(t *testing.T) {
client := createClient(t, codec)
// Prep
err := client.Set("foo", test.Foo{Bar: "baz"})
if err != nil {
t.Error(err)
}
_, err = client.Get("foo", nil) // actually nil
if err == nil {
t.Error("An error was expected")
}
var i any // actually nil
_, err = client.Get("foo", i)
if err == nil {
t.Error("An error was expected")
}
var valPtr *test.Foo // nil value
_, err = client.Get("foo", valPtr)
if err == nil {
t.Error("An error was expected")
}
}
}
t.Run("get with nil / nil value parameter", createTest(encoding.JSON))
t.Run("get with nil / nil value parameter", createTest(encoding.Gob))
}
// TestClose tests if the close method returns any errors.
//
// Note: This test is only executed if the initial connection to DynamoDB works.
func TestClose(t *testing.T) {
if !checkConnection() {
t.Skip("No connection to DynamoDB could be established. Probably not running in a proper test environment.")
}
client := createClient(t, encoding.JSON)
err := client.Close()
if err != nil {
t.Error(err)
}
}
// checkConnection returns true if a connection could be made, false otherwise.
func checkConnection() bool {
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{
CredentialsChainVerboseErrors: aws.Bool(true),
// Local DynamoDB requires credentials, and with the shared config disabled these must be explicitly provided.
Credentials: credentials.NewStaticCredentials("foo", "foo", ""),
Endpoint: aws.String(customEndpoint),
Region: aws.String(endpoints.EuCentral1RegionID),
},
Profile: session.DefaultSharedConfigProfile,
SharedConfigState: session.SharedConfigDisable,
})
if err != nil {
log.Printf("An error occurred during testing the connection to the server: %v\n", err)
return false
}
svc := awsdynamodb.New(sess)
timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
limit := int64(1)
listTablesInput := awsdynamodb.ListTablesInput{
Limit: &limit,
}
_, err = svc.ListTablesWithContext(timeoutCtx, &listTablesInput)
if err != nil {
log.Printf("An error occurred during testing the connection to the server: %v\n", err)
return false
}
return true
}
func createClient(t *testing.T, codec encoding.Codec) dynamodb.Client {
options := dynamodb.Options{
Region: endpoints.EuCentral1RegionID,
AWSaccessKeyID: "foo",
AWSsecretAccessKey: "foo",
CustomEndpoint: customEndpoint,
Codec: codec,
}
client, err := dynamodb.NewClient(options)
if err != nil {
t.Fatal(err)
}
return client
}