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
|
package dynamodb_test
import (
"flag"
"testing"
"time"
"github.com/goamz/goamz/aws"
"github.com/goamz/goamz/dynamodb"
. "gopkg.in/check.v1"
)
const TIMEOUT = 3 * time.Minute
var amazon = flag.Bool("amazon", false, "Enable tests against dynamodb")
var local = flag.Bool("local", true, "Use DynamoDB local on 8080 instead of real server on us-east.")
var dynamodb_region aws.Region
var dynamodb_auth aws.Auth
type DynamoDBTest struct {
server *dynamodb.Server
aws.Region // Exports Region
TableDescriptionT dynamodb.TableDescriptionT
table *dynamodb.Table
}
// Delete all items in the table
func (s *DynamoDBTest) TearDownTest(c *C) {
pk, err := s.TableDescriptionT.BuildPrimaryKey()
if err != nil {
c.Fatal(err)
}
attrs, err := s.table.Scan(nil)
if err != nil {
c.Fatal(err)
}
for _, a := range attrs {
key := &dynamodb.Key{
HashKey: a[pk.KeyAttribute.Name].Value,
}
if pk.HasRange() {
key.RangeKey = a[pk.RangeAttribute.Name].Value
}
if ok, err := s.table.DeleteItem(key); !ok {
c.Fatal(err)
}
}
}
func (s *DynamoDBTest) TearDownSuite(c *C) {
// return immediately in the case of calling c.Skip() in SetUpSuite()
if s.server == nil {
return
}
// check whether the table exists
if tables, err := s.server.ListTables(); err != nil {
c.Fatal(err)
} else {
if !findTableByName(tables, s.TableDescriptionT.TableName) {
return
}
}
// Delete the table and wait
if _, err := s.server.DeleteTable(s.TableDescriptionT); err != nil {
c.Fatal(err)
}
done := make(chan bool)
timeout := time.After(TIMEOUT)
go func() {
for {
select {
case <-done:
return
default:
tables, err := s.server.ListTables()
if err != nil {
c.Fatal(err)
}
if findTableByName(tables, s.TableDescriptionT.TableName) {
time.Sleep(5 * time.Second)
} else {
done <- true
return
}
}
}
}()
select {
case <-done:
break
case <-timeout:
c.Error("Expect the table to be deleted but timed out")
close(done)
}
}
func (s *DynamoDBTest) WaitUntilStatus(c *C, status string) {
// We should wait until the table is in specified status because a real DynamoDB has some delay for ready
done := make(chan bool)
timeout := time.After(TIMEOUT)
go func() {
for {
select {
case <-done:
return
default:
desc, err := s.table.DescribeTable()
if err != nil {
c.Fatal(err)
}
if desc.TableStatus == status {
done <- true
return
}
time.Sleep(5 * time.Second)
}
}
}()
select {
case <-done:
break
case <-timeout:
c.Errorf("Expect a status to be %s, but timed out", status)
close(done)
}
}
func setUpAuth(c *C) {
if !*amazon {
c.Skip("Test against amazon not enabled.")
}
if *local {
c.Log("Using local server")
dynamodb_region = aws.Region{
DynamoDBEndpoint: "http://127.0.0.1:8000",
DynamoDBStreamsEndpoint: "http://127.0.0.1:8000",
}
dynamodb_auth = aws.Auth{AccessKey: "DUMMY_KEY", SecretKey: "DUMMY_SECRET"}
} else {
c.Log("Using REAL AMAZON SERVER")
dynamodb_region = aws.USEast
auth, err := aws.EnvAuth()
if err != nil {
c.Fatal(err)
}
dynamodb_auth = auth
}
}
func findTableByName(tables []string, name string) bool {
for _, t := range tables {
if t == name {
return true
}
}
return false
}
func Test(t *testing.T) {
TestingT(t)
}
|