/
dynamodb_test.go
212 lines (186 loc) · 4.33 KB
/
dynamodb_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
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
package dynamodb_test
import (
"flag"
"log"
"testing"
"time"
"github.com/crowdmob/goamz/aws"
"github.com/stretchr/testify/suite"
"github.com/nabeken/goamz-dynamodb"
)
const timeout = 3 * time.Minute
var (
integration = flag.Bool("integration", false, "Enable integration tests against DynamoDB server")
provider = flag.String("provider", "local", "Specify a DynamoDB provider. Default: local. [local|dynalite|amazon]")
)
var (
dummyRegion = map[string]aws.Region{
"local": aws.Region{DynamoDBEndpoint: "http://127.0.0.1:8000"},
"dynalite": aws.Region{DynamoDBEndpoint: "http://127.0.0.1:4567"},
"amazon": aws.USEast,
}
dummyAuth = aws.Auth{AccessKey: "DUMMY_KEY", SecretKey: "DUMMY_SECRET"}
)
type actionHandler func(done chan struct{})
func handleAction(action actionHandler) (done chan struct{}) {
done = make(chan struct{})
go func() {
for {
select {
case <-done:
return
default:
action(done)
}
}
}()
return done
}
type DynamoDBCommonSuite struct {
Table *dynamodb.Table
TableOption *dynamodb.TableOption
CreateNewTable bool
c *dynamodb.Client
t *testing.T
}
// TearDownSuite implements suite.TearDownAllSuite interface.
func (s *DynamoDBCommonSuite) TearDownSuite() {
// Ensure that the table does not exist
s.DeleteTable()
}
// TearDownTest implements suite.TearDownTestSuite interface.
func (s *DynamoDBCommonSuite) TearDownTest() {
s.DeleteAllItems()
}
// DeleteAllItems deletes all items in the table
func (s *DynamoDBCommonSuite) DeleteAllItems() {
sret, serr := s.c.Scan(s.Table.Name, nil)
if serr != nil {
s.t.Error(serr)
return
}
if sret.Count == 0 {
return
}
key := map[string]dynamodb.AttributeValue{}
for _, ks := range s.Table.KeySchema {
for i := range sret.Items {
if v, ok := sret.Items[i][ks.AttributeName]; ok {
key[ks.AttributeName] = v
}
}
}
if _, err := s.c.DeleteItem(s.Table.Name, key, nil); err != nil {
s.t.Error(err)
return
}
}
func (s *DynamoDBCommonSuite) CreateTable() {
_, err := s.c.CreateTable(s.Table, s.TableOption)
if err != nil {
s.t.Error(err)
return
}
s.WaitUntilStatus(dynamodb.TableStatusActive)
}
func (s *DynamoDBCommonSuite) DeleteTable() {
// check whether the table exists
ret, err := s.c.ListTables(nil)
if err != nil {
s.t.Error(err)
return
} else {
if !findTableByName(ret.TableNames, s.Table.Name) {
return
}
}
// Delete the table and wait
s.c.DeleteTable(s.Table.Name)
timeoutChan := time.After(timeout)
done := handleAction(func(done chan struct{}) {
ret, err := s.c.ListTables(nil)
if err != nil {
s.t.Error(err)
close(done)
return
}
if findTableByName(ret.TableNames, s.Table.Name) {
time.Sleep(5 * time.Second)
} else {
close(done)
}
})
select {
case <-done:
break
case <-timeoutChan:
close(done)
s.t.Error("Expect the table to be deleted but timed out")
}
}
func (s *DynamoDBCommonSuite) WaitUntilStatus(status dynamodb.TableStatus) {
// We should wait until the table is in specified status because a real DynamoDB has some delay for ready
timeoutChan := time.After(timeout)
done := handleAction(func(done chan struct{}) {
desc, err := s.c.DescribeTable(s.Table.Name)
if err != nil {
s.t.Error(err)
close(done)
return
}
if desc.Table.TableStatus == status {
close(done)
} else {
time.Sleep(5 * time.Second)
}
})
select {
case <-done:
break
case <-timeoutChan:
close(done)
s.t.Errorf("Expect a status to be %s, but timed out", status)
}
}
func (s *DynamoDBCommonSuite) SetupDB() {
if !*integration {
s.t.Skip("Integration tests are disabled")
}
s.t.Logf("Performing Integration tests on %s...", *provider)
var auth aws.Auth
if *provider == "amazon" {
s.t.Log("Using REAL AMAZON SERVER")
awsAuth, err := aws.EnvAuth()
if err != nil {
log.Fatal(err)
}
auth = awsAuth
} else {
auth = dummyAuth
}
s.c = &dynamodb.Client{
Auth: auth,
Region: dummyRegion[*provider],
}
// Ensure that the table does not exist
s.DeleteTable()
if s.CreateNewTable {
s.CreateTable()
}
}
func findTableByName(tables []string, name string) bool {
for _, t := range tables {
if t == name {
return true
}
}
return false
}
func doIntegrationTest(t *testing.T, suites ...suite.TestingSuite) {
if !*integration {
t.Skip("Test against amazon not enabled.")
}
for i := range suites {
suite.Run(t, suites[i])
}
}