s3

package module
v0.0.0-...-f8f7c41 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 31, 2018 License: MIT Imports: 9 Imported by: 0

README

Godoc Reference Build Status Go Report Card

AWS S3 unit testing library

S3 makes it possible to write AWS S3 unit tests and integrate them into the Go development work-flow using the go test CLI.

Install: go get github.com/aead/s3

Run S3 tests
  1. Install minio S3 server: go get -u github.com/minio/minio
  2. Setup TLS:
    • openssl ecparam -genkey -name prime256v1 | openssl ec -out ~/.minio/certs/private.key
    • openssl req -new -x509 -days 3650 -key ~/.minio/certs/private.key -out ~/.minio/certs/public.crt -subj "/C=US/ST=state/L=location/O=organization/CN=domain"
  3. Run S3 server: minio server <your-dir>
  4. Run S3 tests: go test -v -short github.com/aead/s3 -args -access=your-access-key -secret=your-secret-key -insecure
Write S3 tests
import (
    "bytes"
    "crypto/tls"
    "net/http"
    "testing"

    "github.com/aead/s3"
    "github.com/minio/minio-go"
    "github.com/minio/minio-go/pkg/encrypt"
)

func TestEncryptedPut(t *testing.T) {
        if err := s3.Parse(); err != nil {
		t.Fatal(err)
	}

	client, err := minio.New(s3.Endpoint, s3.AccessKey, s3.SecretKey, true)
	if err != nil {
		t.Fatalf("Failed to create client: %v", err)
	}
	client.SetCustomTransport(&http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: s3.Insecure},
	})

	bucket := s3.BucketName("test-encrypted-put")
	if remove, err := s3.MakeBucket(bucket, client.BucketExists, client.MakeBucket, client.RemoveBucket); err != nil {
		t.Fatalf("Failed to create bucket '%s': %s", bucket, err)
	} else {
		defer remove(t)
	}

	object, data, password := "object-1", make([]byte, 5*1024*1024), "my-password"
	encryption := encrypt.DefaultPBKDF([]byte(password), []byte(bucket+object))
	options := minio.PutObjectOptions{
		ServerSideEncryption: encryption,
	}

	if _, err = client.PutObject(bucket, object, bytes.NewReader(data), int64(len(data)), options); err != nil {
		t.Fatalf("Failed to upload object '%s/%s': %s", bucket, object, err)
	}
	s3.RemoveObject(bucket, object, client.RemoveObject, t)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Endpoint is the S3 endpoint. Specified either through the '-server' CLI argument
	// or through the 'SERVER_ENDPOINT' env. variable. The default is 'localhost:9000'
	Endpoint string
	// AccessKey is the S3 access-key for the specified endpoint. Specified either through
	// the '-access' CLI argument or through the 'ACCESS_KEY' env. variable.
	AccessKey string
	// SecretKey is the S3 secret-key for the specified endpoint. Specified either through
	// the '-secret' CLI argument or through the 'SECRET_KEY' env. variable.
	SecretKey string
	// Insecure allows TLS to endpoints without a valid signed TLS certificate.
	// Particually useful for local servers. Can be set using the '-insecure' CLI flag.
	Insecure bool
	// NoTLS disables TLS. All client requests will be made of plain HTTP/TCP connections.
	// Tests which require TLS will be skipped.
	NoTLS bool
	// Size is the size of objects for single-part operations in bytes. It is set by the '-size' CLI flag.
	Size int64
	// MultipartSize is the size of objects for multi-part operations in bytes. It is set by the '-sizeMultipart' CLI flag.
	MultipartSize int64
)

Functions

func BucketName

func BucketName(prefix string) string

BucketName returns a bucket name with the given prefix and a random hex suffix.

func ErrorCode

func ErrorCode(err error) (string, bool)

ErrorCode returns the response code as string if the err is a minio.ErrorResponse. It returns a boolean flag indicating whether the provided error is a minio.ErrorResponse.

func ErrorMessage

func ErrorMessage(err error) (string, bool)

ErrorMessage returns the response message as string if the err is a minio.ErrorResponse. It returns a boolean flag indicating whether the provided error is a minio.ErrorResponse.

func MakeBucket

func MakeBucket(bucket string, exists func(string) (bool, error), make func(string, string) error, remove func(string) error) (func(testing.TB), error)

MakeBucket checks whether the bucket exists, if not creates it and returns a function which removes the bucket if it was created successfully.

It simplifies code that should cleanup created objects and buckets.

func Parse

func Parse() error

Parse parses the command line arguments. It returns an error if no server, access-key or secret-key is provided and also no env. variables for the missing arguments are exported.

It is save to call Parse() multiple times.

func RemoveBucket

func RemoveBucket(bucket string, remove func(string) error, t testing.TB)

RemoveBucket removes the bucket using the remove function. If the remove function returns a error RemoveBucket() fails the test.

It simplifies code that should cleanup created objects and buckets.

func RemoveObject

func RemoveObject(bucket, object string, remove func(bucket, object string) error, t testing.TB)

RemoveObject removes the object at the bucket using the remove function. If the remove function returns a error RemoveObject() fails the test.

It simplifies code that should cleanup created objects and buckets.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL