/
dynamotest.go
141 lines (122 loc) · 3.26 KB
/
dynamotest.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
package dynamotest
import (
"fmt"
"log"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
func SetupDynamoServer() (*DynamoServerInfo, error) {
info := DynamoServerInfo{
TableName: os.Getenv("DONUTDB_DYNAMODB_TEST_TABLE_NAME"),
Addr: os.Getenv("DONUTDB_DYNAMODB_TEST_ADDR"),
Region: os.Getenv("DONUTDB_DYNAMODB_TEST_REGION"),
}
// if set, test will try to start local dynamo db jar
dynamoLocalDir := os.Getenv("DONUTDB_DYNAMODB_LOCAL_DIR")
var cleanups []func()
info.Cleanup = func() {
for _, cleanupFunc := range cleanups {
cleanupFunc()
}
}
if dynamoLocalDir != "" {
port := 8000
// get a random port to listen on
l, err := net.Listen("tcp", ":0")
if err == nil {
port = l.Addr().(*net.TCPAddr).Port
l.Close()
}
log.Printf("Starting local dyamodb server on %d", port)
cmd := exec.Command("java", "-Djava.library.path="+filepath.Join(dynamoLocalDir, "DynamoDBLocal_lib"), "-jar", filepath.Join(dynamoLocalDir, "DynamoDBLocal.jar"), "-sharedDb", "-inMemory", "-port", strconv.Itoa(port))
err = cmd.Start()
if err != nil {
return nil, err
}
cleanups = append(cleanups, func() {
cmd.Process.Kill()
})
if info.Addr == "" {
info.Addr = fmt.Sprintf("http://localhost:%d", port)
}
if info.Region == "" {
info.Region = "us-east-2"
}
os.Setenv("AWS_ACCESS_KEY_ID", "fakeMyKeyId")
os.Setenv("AWS_SECRET_ACCESS_KEY", "fakeSecretAccessKey")
deadline := time.Now().Add(5 * time.Second)
var connectOK bool
for time.Now().Before(deadline) {
resp, err := http.Get(info.Addr)
if err != nil {
time.Sleep(1 * time.Millisecond)
continue
}
resp.Body.Close()
connectOK = true
break
}
if !connectOK {
return nil, fmt.Errorf("Failed to conncet to test dynamodb server within deadline")
}
}
if info.Region == "" {
return nil, fmt.Errorf("Missing required environment variables to connect to dynamodb (either local or remote)")
}
sess := session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: &info.Region,
Endpoint: &info.Addr,
MaxRetries: aws.Int(0),
// LogLevel: aws.LogLevel(aws.LogDebug),
// Logger: aws.NewDefaultLogger(),
},
}))
info.DB = dynamodb.New(sess)
if info.TableName == "" {
info.TableName = fmt.Sprintf("donutdb-test-%d", time.Now().UnixNano())
_, err := info.DB.CreateTable(&dynamodb.CreateTableInput{
TableName: &info.TableName,
AttributeDefinitions: []*dynamodb.AttributeDefinition{
{
AttributeName: aws.String("hash_key"),
AttributeType: aws.String("S"),
},
{
AttributeName: aws.String("range_key"),
AttributeType: aws.String("N"),
},
},
KeySchema: []*dynamodb.KeySchemaElement{
{
AttributeName: aws.String("hash_key"),
KeyType: aws.String("HASH"),
},
{
AttributeName: aws.String("range_key"),
KeyType: aws.String("RANGE"),
},
},
BillingMode: aws.String("PAY_PER_REQUEST"),
})
if err != nil {
return nil, err
}
}
return &info, nil
}
type DynamoServerInfo struct {
Region string
Addr string
TableName string
Cleanup func()
DB *dynamodb.DynamoDB
}