localstack

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2020 License: Unlicense Imports: 13 Imported by: 0

Documentation

Overview

This package was written to help writing tests with Localstack. (https://github.com/localstack/localstack) It uses libraries that help create and manage a Localstack docker container for your go tests.

Requirements

Go v1.11.0 or higher
Docker (Tested on version 19.03.0-rc Community Edition)
Example (S3)

In this example we initialise Localstack with the S3 service enabled, create a bucket, upload a file to that bucket, then download that file from s3 and output it's content.

For complete testing examples, see the examples in the github.com repository. https://github.com/nichobbs/go_localstack/tree/master/examples

// LOCALSTACK: A reference to the Localstack object
var LOCALSTACK *Localstack

// Create the S3 Service definition
s3Service, _ := NewLocalstackService("s3")

// Gather up all service definitions in a single collection.
// (Only one in this case.)
LocalstackServices := &LocalstackServiceCollection{
	*s3Service,
}

// Initialise Localstack.  Here Localstack is created and
// is ready to go.
var err error
LOCALSTACK, err = NewLocalstack(LocalstackServices)
if err != nil {
	log.Fatal(fmt.Sprintf("Unable to create the instance: %s", err))
}
if LOCALSTACK == nil {
	log.Fatal("LOCALSTACK was nil.")
}

// Make sure we Destroy Localstack.  This method handles
// stopping and removing the docker container.
//nolint:errcheck
defer LOCALSTACK.Destroy()

// Here we start the code to interact with S3
svc := s3.New(LOCALSTACK.CreateAWSSession())

// Create Bucket
input := &s3.CreateBucketInput{
	Bucket: aws.String("examplebucket"),
	CreateBucketConfiguration: &s3.CreateBucketConfiguration{
		LocationConstraint: aws.String("us-east-1"),
	},
}

_, err = svc.CreateBucket(input)
if err != nil {
	log.Fatal(err)
}

//Upload File
uploader := s3manager.NewUploader(LOCALSTACK.CreateAWSSession())
_, err = uploader.Upload(&s3manager.UploadInput{
	Bucket: aws.String("examplebucket"),
	Key:    aws.String("examplefile"),
	Body:   strings.NewReader("Hello World"),
})
if err != nil {
	log.Fatal(err)
}

// Download the file
getObjectInput := &s3.GetObjectInput{
	Bucket: aws.String("examplebucket"),
	Key:    aws.String("examplefile"),
}

result, err := svc.GetObject(getObjectInput)
if err != nil {
	log.Fatal(err)
}

// Read the contents of the file.
text, err := ioutil.ReadAll(result.Body)

if err != nil {
	log.Fatal(err)
}

// Print the contents of the file out.
fmt.Println(string(text))
Output:

Hello World

Index

Examples

Constants

View Source
const LocalstackRepository string = "localstack/localstack"

LocalstackRepository is the Localstack Docker repository

View Source
const LocalstackTag string = "0.11.5"

LocalstackTag is the last tested version of the Localstack Docker repository

Variables

This section is empty.

Functions

This section is empty.

Types

type DockerWrapper

DockerWrapper is used to abstract docker to make testing easier. Each method of this interface simply wraps functionality that already exists in the Client object of the github.com/ory/dockertest/docker library.

type Localstack

type Localstack struct {
	// Resource is a pointer to the dockertest.Resource
	// object that is the localstack docker container.
	// (https://godoc.org/github.com/ory/dockertest#Resource)
	Resource *dockertest.Resource
	// Services is a pointer to a collection of service definitions
	// that are being requested from this particular instance of Localstack.
	Services *LocalstackServiceCollection
}

Localstack is a structure used to control the lifecycle of the Localstack Docker container.

func NewLocalstack

func NewLocalstack(services *LocalstackServiceCollection) (*Localstack, error)

NewLocalstack creates a new Localstack docker container based on the latest version.

func NewNamedPersistentLocalstack

func NewNamedPersistentLocalstack(services *LocalstackServiceCollection, name, data string) (*Localstack, error)

func NewPersistentLocalstack

func NewPersistentLocalstack(services *LocalstackServiceCollection, data string) (*Localstack, error)

func NewPersistentSpecificLocalstack

func NewPersistentSpecificLocalstack(services *LocalstackServiceCollection, name, repository, tag, data string) (*Localstack, error)

func NewSpecificLocalstack

func NewSpecificLocalstack(services *LocalstackServiceCollection, name, repository, tag string) (*Localstack, error)

NewSpecificLocalstack creates a new Localstack docker container based on the given name, repository, and tag given. NOTE: The Docker image used should be a Localstack image. The behaviour is unknown otherwise. This method is provided to allow special situations like using a tag other than latest or when referencing an internal Localstack image.

func (*Localstack) CreateAWSSession

func (ls *Localstack) CreateAWSSession() *session.Session

CreateAWSSession should be used to make sure that your AWS SDK traffic is routing to Localstack correctly.

func (*Localstack) Destroy

func (ls *Localstack) Destroy() error

Destroy simply shuts down and cleans up the Localstack container out of docker.

func (Localstack) EndpointFor

func (ls Localstack) EndpointFor(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error)

EndpointResolver is necessary to route traffic to AWS services in your code to the Localstack endpoints.

type LocalstackService

type LocalstackService struct {
	// The name of the AWS Service. (I.E. "s3" or "apigateway")
	Name string
	// Protocol is the network protocol used for communication.
	Protocol string
	// Port is the port used when communicating with the service in the
	// Localstack instance.
	Port int
}

LocalstackService defines a particular AWS service requested for a Localstack instances. Note: You shold not create an instance of LocastackService directly. See: NewLocalstackService

func NewLocalstackService

func NewLocalstackService(name string) (*LocalstackService, error)

NewLocalstackService returns a new pointer to an instance of LocalstackService given the name of the service provided. Note: The name must match an aws service from this list (https://docs.aws.amazon.com/cli/latest/reference/#available-services) and be a supported service by Localstack.

func (*LocalstackService) Equals

func (service *LocalstackService) Equals(rhs *LocalstackService) bool

Equals returns wether two pointers to a LocalstackService are equal.

func (*LocalstackService) GetNamePort

func (service *LocalstackService) GetNamePort() string

GetNameProtocol returns the protocol string (eg. s3:1234) used by Docker.

func (*LocalstackService) GetPortProtocol

func (service *LocalstackService) GetPortProtocol() string

GetPortProtocol returns the protocol string (eg. 1234/tcp) used by Docker.

type LocalstackServiceCollection

type LocalstackServiceCollection []LocalstackService

LocalstackServiceCollection represents a collection of LocalstackService objects.

func (*LocalstackServiceCollection) Contains

func (collection *LocalstackServiceCollection) Contains(name string) bool

func (*LocalstackServiceCollection) GetServiceMap

func (collection *LocalstackServiceCollection) GetServiceMap() string

GetServiceMap returns a comma delimited string of all the AWS service names in the collection.

func (LocalstackServiceCollection) Len

func (collection LocalstackServiceCollection) Len() int

Len returns the number of items in the collection.

func (LocalstackServiceCollection) Less

func (collection LocalstackServiceCollection) Less(i, j int) bool

Less compares two items in the collection. This returns true if the instance at i is less than the instance at j. Otherwise it will return false.

func (*LocalstackServiceCollection) Sort

Sort simply sorts the collection based on the names of the defined services. The collection returned is a pointer to the calling collection.

func (LocalstackServiceCollection) Swap

func (collection LocalstackServiceCollection) Swap(i, j int)

Swap will swap two items in the collection.

Jump to

Keyboard shortcuts

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