Skip to content
This repository has been archived by the owner on Jun 18, 2022. It is now read-only.

Download DynamoDB archive, add tests, fix bug on Windows #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Created by .ignore support plugin (hsz.mobi)
### Go template
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# external packages folder
vendor/

# dynamodb archive chore
dynamodb_local_latest.tar.gz
LICENSE.txt
README.txt
DynamoDBLocal_lib
DynamoDBLocal.jar
third_party_licenses
Binary file removed DynamoDBLocal.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/antlr-runtime-4.1.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/aws-java-sdk-1.9.16.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/commons-cli-1.2.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/commons-lang3-3.x.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jackson-annotations-2.3.2.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jackson-core-2.3.2.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jackson-databind-2.3.2.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jetty-ajp-8.1.12.v20130726.jar
Binary file not shown.
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jetty-client-8.1.12.v20130726.jar
Binary file not shown.
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jetty-deploy-8.1.12.v20130726.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jetty-http-8.1.12.v20130726.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jetty-io-8.1.12.v20130726.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jetty-jmx-8.1.12.v20130726.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jetty-jndi-8.1.12.v20130726.jar
Binary file not shown.
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jetty-plus-8.1.12.v20130726.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jetty-policy-8.1.12.v20130726.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jetty-rewrite-8.1.12.v20130726.jar
Binary file not shown.
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jetty-server-8.1.12.v20130726.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jetty-start.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jetty-util-8.1.12.v20130726.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jetty-webapp-8.1.12.v20130726.jar
Binary file not shown.
Binary file not shown.
Binary file removed DynamoDBLocal_lib/jetty-xml-8.1.12.v20130726.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/joda-time-2.3.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/libsqlite4java-linux-amd64.so
Binary file not shown.
Binary file removed DynamoDBLocal_lib/libsqlite4java-linux-i386.so
Binary file not shown.
Binary file removed DynamoDBLocal_lib/libsqlite4java-osx-10.4.jnilib
Binary file not shown.
Binary file removed DynamoDBLocal_lib/libsqlite4java-osx-ppc.jnilib
Binary file not shown.
Binary file removed DynamoDBLocal_lib/libsqlite4java-osx.jnilib
Binary file not shown.
Binary file removed DynamoDBLocal_lib/log4j-1.2.17.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/servlet-api-3.0.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/spdy-core-8.1.12.v20130726.jar
Binary file not shown.
Binary file removed DynamoDBLocal_lib/spdy-jetty-8.1.12.v20130726.jar
Binary file not shown.
Binary file not shown.
Binary file removed DynamoDBLocal_lib/sqlite4java-win32-x64.dll
Binary file not shown.
Binary file removed DynamoDBLocal_lib/sqlite4java-win32-x86.dll
Binary file not shown.
Binary file removed DynamoDBLocal_lib/sqlite4java.jar
Binary file not shown.
96 changes: 89 additions & 7 deletions db.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package dynamodbtest

import (
"archive/tar"
"bufio"
"compress/gzip"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/exec"
"strings"
"sync/atomic"
"syscall"
"time"
)

Expand All @@ -29,16 +31,97 @@ type DB struct {
cmd *exec.Cmd
}

func read(mpath string) (*os.File, error) {
f, err := os.OpenFile(mpath, os.O_RDONLY, 0444)
if err != nil {
return f, err
}
return f, nil
}

func overwrite(mpath string) (*os.File, error) {
f, err := os.OpenFile(mpath, os.O_RDWR|os.O_TRUNC, 0777)
if err != nil {
f, err = os.Create(mpath)
if err != nil {
return f, err
}
}
return f, nil
}

func untarIt(basepath, mpath string) {
fr, err := read(mpath)
defer fr.Close()
if err != nil {
panic(err)
}
gr, err := gzip.NewReader(fr)
defer gr.Close()
if err != nil {
panic(err)
}
tr := tar.NewReader(gr)
for {
hdr, err := tr.Next()
if err == io.EOF {
// end of tar archive
break
}
if err != nil {
panic(err)
}
path := hdr.Name
switch hdr.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(basepath+path, os.FileMode(hdr.Mode)); err != nil {
panic(err)
}
case tar.TypeReg:
ow, err := overwrite(basepath + path)
defer ow.Close()
if err != nil {
panic(err)
}
if _, err := io.Copy(ow, tr); err != nil {
panic(err)
}
default:
fmt.Printf("Can't: %c, %s\n", hdr.Typeflag, path)
}
}
}

// New returns a started DynamoDB local instance
func New() (*DB, error) {
port := newPort()
addr := fmt.Sprintf("localhost:%d", port)
// if $GOPATH is composed of multiple paths, use the first one (fix for godep)
gopath := strings.Split(os.Getenv("GOPATH"), ":")[0]
gopath := os.Getenv("GOPATH")
if gopath == "" {
return nil, ErrGopath
}
path := gopath + "/src/github.com/groupme/dynamodbtest/"
path := gopath + "/src/github.com/miltador/dynamodbtest/"
archivePath := path + "dynamodb_local_latest.tar.gz"
if _, err := os.Stat(archivePath); os.IsNotExist(err) {
response, err := http.Get("https://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz")
if err != nil {
panic(err)
}
defer response.Body.Close()

f, err := os.Create(archivePath)
if err != nil {
panic(err)
}
defer f.Close()

io.Copy(f, response.Body)
}
if _, err := os.Stat(path + "DynamoDbLocal_lib/"); os.IsNotExist(err) {
untarIt(path, archivePath)
}

db := &DB{
addr: addr,
cmd: exec.Command(
Expand Down Expand Up @@ -78,7 +161,7 @@ func New() (*DB, error) {
go func() {
// periodically check if connectable
ticker := time.NewTicker(time.Millisecond * 10)
for _ = range ticker.C {
for range ticker.C {
c, err := net.Dial("tcp", db.addr)
if c != nil {
c.Close()
Expand All @@ -99,8 +182,7 @@ func New() (*DB, error) {
}

func (db *DB) Close() error {
db.cmd.Process.Signal(syscall.SIGINT)
return db.cmd.Wait()
return db.cmd.Process.Kill()
}

func (db *DB) URL() string {
Expand Down
26 changes: 26 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dynamodbtest

import (
"testing"
)

func TestAll(t *testing.T) {
// Log output to aid debugging
LogOutput = true

// Start a new test process
db, err := New()
if err != nil {
t.Fatal(err)
}

url := db.URL()
if url != "http://localhost:8000" {
t.Error("URL is not correct")
}

_err := db.Close()
if _err != nil {
t.Fatal(_err)
}
}