Skip to content

Commit

Permalink
Merge pull request #2 from beeceej/fix/dont-vendor-anything
Browse files Browse the repository at this point in the history
Fix/dont vendor anything
  • Loading branch information
beeceej authored Jan 19, 2019
2 parents 2b199da + 0ad4058 commit ec463f9
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 122 deletions.
10 changes: 3 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
language: go
env:
- DEP_VERSION=0.5.0"
before_install:
- curl -L -s https://github.com/golang/dep/releases/download/v${DEP_VERSION}/dep-linux-amd64 -o $GOPATH/bin/dep
- chmod +x $GOPATH/bin/dep
install:
- dep ensure -v
- go get ./...
go:
- '1.x'
os:
- linux
matrix:
fast_finish: true
script:
- go test ./... -race -coverprofile=coverage.txt -covermode=atomic
- make get-tools
- make test

after_success:
- bash <(curl -s https://codecov.io/bash)
61 changes: 0 additions & 61 deletions Gopkg.lock

This file was deleted.

42 changes: 0 additions & 42 deletions Gopkg.toml

This file was deleted.

8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.PHONY: get-tools
get-tools:
@echo "+ $@: retrieving build/test dependencies "
@go get -u -v github.com/google/go-cmp/...

test:
@echo 'run `make get-tools` if you are unable to run this test`'
@go test ./... -race -coverprofile=coverage.txt -covermode=atomic
38 changes: 31 additions & 7 deletions inflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,57 @@ type (
KeyPath string
)

// ObjectKeyFunc is a function which generates the string to be used as the object key.
type ObjectKeyFunc func() (string, error)

func defaultObjectKeyFunc() (string, error) {
var (
u uuid.UUID
err error
)
if u, err = uuid.NewV4(); err != nil {
return "", err
}
return u.String(), nil
}

// Inflight is a structure which provides an interface to retrieving and writing data to s3,
// it doesn't care about what data you're writing, just provides an easy way to get to it
type Inflight struct {
s3iface.S3API
Bucket Bucket
KeyPath KeyPath

// ObjectKeyFunc will be called when Inflight#Write(io.ReadSeeker) is invoked.
// The data will be given the name that this function generates.
ObjectKeyFunc ObjectKeyFunc
}

// NewInflight Creates a reference to an Inflight struct
func NewInflight(bucket Bucket, keypath KeyPath, s3 s3iface.S3API) *Inflight {
return &Inflight{
Bucket: bucket,
KeyPath: keypath,
S3API: s3,
Bucket: bucket,
KeyPath: keypath,
S3API: s3,
ObjectKeyFunc: ObjectKeyFunc(defaultObjectKeyFunc),
}
}

// Write will take the data given and attempt to put it in S3
// It then will return the S3 URI back to the caller so that the data may be passed between callers
func (i *Inflight) Write(data io.ReadSeeker) (*Ref, error) {
ref := &Ref{
func (i *Inflight) Write(data io.ReadSeeker) (ref *Ref, err error) {
objID, err := i.ObjectKeyFunc()
if err != nil {
return nil, backoff.Permanent(err)
}

ref = &Ref{
Bucket: string(i.Bucket),
Path: string(i.KeyPath),
Object: uuid.NewV4().String(),
Object: objID,
}

err := backoff.Retry(
err = backoff.Retry(
i.tryWriteToS3(data, ref.Object),
backoff.NewExponentialBackOff(),
)
Expand Down
52 changes: 47 additions & 5 deletions inflight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"bytes"
"errors"
"io/ioutil"
"reflect"
"testing"

"github.com/google/go-cmp/cmp"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/awserr"

Expand Down Expand Up @@ -57,14 +58,15 @@ func TestNewInflightGivenBucketAndKeyExpectCorrectValues(t *testing.T) {
givenKeyPath := KeyPath("key/path")

expected := &Inflight{
Bucket: Bucket("bucket"),
KeyPath: KeyPath("key/path"),
S3API: s3,
Bucket: Bucket("bucket"),
KeyPath: KeyPath("key/path"),
S3API: s3,
ObjectKeyFunc: ObjectKeyFunc(defaultObjectKeyFunc),
}

actual := NewInflight(givenBucket, givenKeyPath, s3)

if !reflect.DeepEqual(expected, actual) {
if cmp.Equal(expected, actual) {
t.Fail()
}
}
Expand Down Expand Up @@ -168,3 +170,43 @@ func TestWriteGivenSomeBytesExpectRetryableErrorThenIdentifierReturned(t *testin
t.Fail()
}
}

func TestWriteGivenSomeBytesButUUIDReturnsErrorExpectPermanentError(t *testing.T) {
givenBytes := []byte("hi")
givenBucket := Bucket("a_bucket")

givenKeyPath := KeyPath("a/key/path")
s3 := &mocks3PutObjectRequestRetryableErrorExpectSuccessAfterSecondAttempt{
givenBytes: givenBytes,
}

inflight := NewInflight(givenBucket, givenKeyPath, s3)
inflight.ObjectKeyFunc = func() (string, error) {
return "", errors.New("")
}

_, err := inflight.Write(bytes.NewReader(givenBytes))
if err == nil {
t.Fail()
}
}

func TestWriteGivenSomeBytesButUUIDReturnsErrorExpectStringFromGenerator(t *testing.T) {
givenBytes := []byte("hi")
givenBucket := Bucket("a_bucket")

givenKeyPath := KeyPath("a/key/path")
s3 := &mocks3PutObjectRequestRetryableErrorExpectSuccessAfterSecondAttempt{
givenBytes: givenBytes,
}

inflight := NewInflight(givenBucket, givenKeyPath, s3)
inflight.ObjectKeyFunc = func() (string, error) {
return "from_the_func", nil
}

ref, err := inflight.Write(bytes.NewReader(givenBytes))
if err != nil && ref.Object != "from_the_func" {
t.Fail()
}
}

0 comments on commit ec463f9

Please sign in to comment.