localstack

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 23, 2020 License: MIT Imports: 13 Imported by: 3

README

Gnomock Localstack

Gnomock Localstack is a Gnomock preset for running tests against AWS services locally, powered by Localstack project. It allows to setup a number of supported AWS services locally, run tests against them, and tear them down easily.

See Localstack documentation for more details.

Testing against local S3
package localstack_test

import (
	"bytes"
	"encoding/json"
	"fmt"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
	"github.com/orlangure/gnomock"
	"github.com/orlangure/gnomock/preset/localstack"
)

func ExamplePreset_s3() {
	p := localstack.Preset(localstack.WithServices(localstack.S3))
	c, _ := gnomock.Start(p)

	defer func() { _ = gnomock.Stop(c) }()

	s3Endpoint := fmt.Sprintf("http://%s/", c.Address(localstack.APIPort))
	config := &aws.Config{
		Region:           aws.String("us-east-1"),
		Endpoint:         aws.String(s3Endpoint),
		S3ForcePathStyle: aws.Bool(true),
		Credentials:      credentials.NewStaticCredentials("a", "b", "c"),
	}
	sess, _ := session.NewSession(config)
	svc := s3.New(sess)

	_, _ = svc.CreateBucket(&s3.CreateBucketInput{
		Bucket: aws.String("foo"),
	})

	out, _ := svc.ListObjectsV2(&s3.ListObjectsV2Input{
		Bucket: aws.String("foo"),
	})
	fmt.Println("keys before:", *out.KeyCount)

	_, _ = svc.PutObject(&s3.PutObjectInput{
		Body:   bytes.NewReader([]byte("this is a file")),
		Key:    aws.String("file"),
		Bucket: aws.String("foo"),
	})

	out, _ = svc.ListObjectsV2(&s3.ListObjectsV2Input{
		Bucket: aws.String("foo"),
	})
	fmt.Println("keys after:", *out.KeyCount)

	// Output:
	// keys before: 0
	// keys after: 1
}
Testing against local SQS+SNS
package localstack_test

import (
	"bytes"
	"encoding/json"
	"fmt"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/sns"
	"github.com/aws/aws-sdk-go/service/sqs"
	"github.com/orlangure/gnomock"
	"github.com/orlangure/gnomock/preset/localstack"
)

func ExamplePreset_sqs_sns() {
	p := localstack.Preset(
		localstack.WithServices(localstack.SNS, localstack.SQS),
	)
	c, _ := gnomock.Start(p)

	defer func() { _ = gnomock.Stop(c) }()

	endpoint := fmt.Sprintf("http://%s", c.Address(localstack.APIPort))

	sess, _ := session.NewSession(&aws.Config{
		Region:      aws.String("us-east-1"),
		Endpoint:    aws.String(endpoint),
		Credentials: credentials.NewStaticCredentials("a", "b", "c"),
	})

	sqsService := sqs.New(sess)
	snsService := sns.New(sess)

	_, _ = sqsService.CreateQueue(&sqs.CreateQueueInput{
		QueueName: aws.String("my_queue"),
	})

	_, _ = snsService.CreateTopic(&sns.CreateTopicInput{
		Name: aws.String("my_topic"),
	})

	queues, _ := sqsService.ListQueues(&sqs.ListQueuesInput{})
	fmt.Println("queues:", len(queues.QueueUrls))

	queueURL := queues.QueueUrls[0]

	topics, _ := snsService.ListTopics(&sns.ListTopicsInput{})
	fmt.Println("topics:", len(topics.Topics))

	topic := topics.Topics[0]

	_, _ = snsService.Subscribe(&sns.SubscribeInput{
		Protocol: aws.String("sqs"),
		Endpoint: queueURL,
		TopicArn: topic.TopicArn,
	})

	_, _ = snsService.Publish(&sns.PublishInput{
		TopicArn: topic.TopicArn,
		Message:  aws.String("foobar"),
	})

	messages, _ := sqsService.ReceiveMessage(&sqs.ReceiveMessageInput{
		QueueUrl: queueURL,
	})
	fmt.Println("messages:", len(messages.Messages))

	var msg map[string]string

	_ = json.Unmarshal([]byte(*messages.Messages[0].Body), &msg)
	fmt.Println("message:", msg["Message"])

	// Output:
	// queues: 1
	// topics: 1
	// messages: 1
	// message: foobar
}

Documentation

Overview

Package localstack provides a Gnomock Preset for localstack project (https://github.com/localstack/localstack). It allows to easily setup local AWS stack for testing

Index

Constants

View Source
const (

	// APIPort should be used to configure AWS SDK endpoint
	APIPort = "api"
)

Variables

This section is empty.

Functions

func Preset

func Preset(opts ...Option) gnomock.Preset

Preset creates a new localstack preset to use with gnomock.Start. See package docs for a list of exposed ports and services. It is legal to not provide any services using WithServices options, but in such case a new localstack container will be useless.

This Preset cannot be used with localstack image prior to 0.11.0

Types

type Option

type Option func(*P)

Option is an optional configuration of this Gnomock preset. Use available Options to configure the container

func WithS3Files

func WithS3Files(path string) Option

WithS3Files sets up S3 service running in localstack with the contents of `path` directory. The first level children of `path` must be directories, their names will be used to create buckets. Below them, all the files in any other directories, these files will be uploaded as-is.

For example, if you put your test files in testdata/my-bucket/dir/, Gnomock will create "my-bucket" for you, and pull "dir" with all its contents into this bucket.

This function does nothing if you don't provide localstack.S3 as one of the services in WithServices

func WithServices

func WithServices(services ...Service) Option

WithServices selects localstack services to spin up. It is OK to not select any services, but in such case the container will be useless

type P

type P struct {
	Services []Service `json:"services"`
	S3Path   string    `json:"s3_path"`
}

P is a Gnomock Preset localstack implementation

func (*P) Image

func (p *P) Image() string

Image returns an image that should be pulled to create this container

func (*P) Options

func (p *P) Options() []gnomock.Option

Options returns a list of options to configure this container

func (*P) Ports

func (p *P) Ports() gnomock.NamedPorts

Ports returns ports that should be used to access this container

type Service

type Service string

Service represents an AWS service that can be setup using localstack

const (
	APIGateway       Service = "apigateway"
	CloudFormation   Service = "cloudformation"
	CloudWatch       Service = "cloudwatch"
	CloudWatchLogs   Service = "logs"
	CloudWatchEvents Service = "events"
	DynamoDB         Service = "dynamodb"
	DynamoDBStreams  Service = "dynamodbstreams"
	EC2              Service = "ec2"
	ES               Service = "es"
	Firehose         Service = "firehose"
	IAM              Service = "iam"
	Kinesis          Service = "kinesis"
	KMS              Service = "kms"
	Lambda           Service = "lambda"
	Redshift         Service = "redshift"
	Route53          Service = "route53"
	S3               Service = "s3"
	SecretsManager   Service = "secretsmanager"
	SES              Service = "ses"
	SNS              Service = "sns"
	SQS              Service = "sqs"
	SSM              Service = "ssm"
	STS              Service = "sts"
	StepFunctions    Service = "stepfunctions"
)

These services are available in this Preset

func (*Service) UnmarshalJSON

func (s *Service) UnmarshalJSON(bs []byte) error

UnmarshalJSON allows to unmarshal string into Service type

Jump to

Keyboard shortcuts

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