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 ¶
Examples ¶
Constants ¶
const (
// APIPort should be used to configure AWS SDK endpoint
APIPort = "api"
)
Variables ¶
This section is empty.
Functions ¶
func 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
Example (S3) ¶
package main
import (
"bytes"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/orlangure/gnomock"
localstack "github.com/orlangure/gnomock-localstack"
)
func main() {
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
Example (Sqs_sns) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"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"
localstack "github.com/orlangure/gnomock-localstack"
)
func main() {
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
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 ¶ added in v0.1.1
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 ¶
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 ¶ added in v0.3.1
P is a Gnomock Preset localstack implementation
func (*P) Image ¶ added in v0.3.1
Image returns an image that should be pulled to create this container
func (*P) Ports ¶ added in v0.3.1
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 ¶ added in v0.3.1
UnmarshalJSON allows to unmarshal string into Service type