localstack

package module
v1.0.97 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2024 License: Apache-2.0 Imports: 49 Imported by: 0

README

go-localstack

Test codecov CodeQL Go Report Card PkgGoDev License

Go Wrapper for using localstack in go testing

Installation

Please make sure that you have Docker installed.

go get github.com/elgohr/go-localstack

Usage

With SDK V2 (using EndpointResolverV2). Please have a look at resolvers for a complete list of resolvers.

func ExampleLocalstackSdkV2EndpointResolverV2(t *testing.T) {
    l, err := localstack.NewInstance()
    if err != nil {
        t.Fatalf("Could not connect to Docker %v", err)
    }
    if err := l.Start(); err != nil {
        t.Fatalf("Could not start localstack %v", err)
    }
    t.Cleanup(func() {
        if err := l.Stop(); err != nil {
            t.Fatalf("Could not stop localstack %v", err)
        }
    })
    
    cfg, err := config.LoadDefaultConfig(ctx,
        config.WithRegion("us-east-1"),
        config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("dummy", "dummy", "dummy")),
    )
    if err != nil {
        t.Fatalf("Could not get config %v", err)
    }
    resolver := localstack.NewDynamoDbResolverV2(i)
    client := dynamodb.NewFromConfig(cfg, dynamodb.WithEndpointResolverV2(resolver))
	
    myTestWithV2Client(client)
}

With SDK V2 (using EndpointResolverV1)

func ExampleLocalstackSdkV2(t *testing.T) {
    l, err := localstack.NewInstance()
    if err != nil {
        t.Fatalf("Could not connect to Docker %v", err)
    }
    if err := l.Start(); err != nil {
        t.Fatalf("Could not start localstack %v", err)
    }
    t.Cleanup(func() {
        if err := l.Stop(); err != nil {
            t.Fatalf("Could not stop localstack %v", err)
        }
	})
    
    cfg, err := config.LoadDefaultConfig(ctx,
        config.WithRegion("us-east-1"),
        config.WithEndpointResolverWithOptions(aws.EndpointResolverWithOptionsFunc(func(_, _ string, _ ...interface{}) (aws.Endpoint, error) {
            return aws.Endpoint{
			    PartitionID:       "aws", 
			    URL:               l.EndpointV2(localstack.SQS), 
			    SigningRegion:     "us-east-1", 
			    HostnameImmutable: true,
		    }, nil
        })),
        config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("dummy", "dummy", "dummy")),
    )
    if err != nil {
        t.Fatalf("Could not get config %v", err)
    }
    
    myTestWithV2(cfg)
}

With SDK V1

func TestWithLocalStack(t *testing.T) {
    l, err := localstack.NewInstance()
    if err != nil {
        t.Fatalf("Could not connect to Docker %v", err)
    }
    if err := l.Start(); err != nil {
        t.Fatalf("Could not start localstack %v", err)
    }
    t.Cleanup(func() {
        if err := l.Stop(); err != nil {
            t.Fatalf("Could not stop localstack %v", err)
        }
    })

    myTestWith(&aws.Config{
        Credentials: credentials.NewStaticCredentials("not", "empty", ""),
        DisableSSL:  aws.Bool(true),
        Region:      aws.String(endpoints.UsWest1RegionID),
        Endpoint:    aws.String(l.Endpoint(localstack.SQS)),
    })
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FixedPort = Service{Name: "all", Port: "4566/tcp"}

	CloudFormation   = Service{Name: "cloudformation", Port: "4581/tcp"}
	CloudWatch       = Service{Name: "cloudwatch", Port: "4582/tcp"}
	CloudWatchLogs   = Service{Name: "cloudwatchlogs", Port: "4586/tcp"}
	CloudWatchEvents = Service{Name: "cloudwatchevents", Port: "4587/tcp"}
	DynamoDB         = Service{Name: "dynamoDB", Port: "4569/tcp"}
	DynamoDBStreams  = Service{Name: "dynamoDBStreams", Port: "4570/tcp"}
	EC2              = Service{Name: "ec2", Port: "4597/tcp"}
	ES               = Service{Name: "es", Port: "4578/tcp"}
	Firehose         = Service{Name: "firehose", Port: "4573/tcp"}
	IAM              = Service{Name: "iam", Port: "4593/tcp"}
	Kinesis          = Service{Name: "kinesis", Port: "4568/tcp"}
	Lambda           = Service{Name: "lambda", Port: "4574/tcp"}
	Redshift         = Service{Name: "redshift", Port: "4577/tcp"}
	Route53          = Service{Name: "route53", Port: "4580/tcp"}
	S3               = Service{Name: "s3", Port: "4572/tcp"}
	SecretsManager   = Service{Name: "secretsmanager", Port: "4584/tcp"}
	SES              = Service{Name: "ses", Port: "4579/tcp"}
	SNS              = Service{Name: "sns", Port: "4575/tcp"}
	SQS              = Service{Name: "sqs", Port: "4576/tcp"}
	SSM              = Service{Name: "ssm", Port: "4583/tcp"}
	STS              = Service{Name: "sts", Port: "4592/tcp"}
	StepFunctions    = Service{Name: "stepfunctions", Port: "4585/tcp"}
)

Supported AWS/localstack services

View Source
var AvailableServices = map[Service]struct{}{
	FixedPort:        {},
	CloudFormation:   {},
	CloudWatch:       {},
	CloudWatchLogs:   {},
	CloudWatchEvents: {},
	DynamoDB:         {},
	DynamoDBStreams:  {},
	EC2:              {},
	ES:               {},
	Firehose:         {},
	IAM:              {},
	Kinesis:          {},
	Lambda:           {},
	Redshift:         {},
	Route53:          {},
	S3:               {},
	SecretsManager:   {},
	SES:              {},
	SNS:              {},
	SQS:              {},
	SSM:              {},
	STS:              {},
	StepFunctions:    {},
}

AvailableServices provides a map of all services for faster searches

Functions

This section is empty.

Types

type CloudformationResolverV2 added in v1.0.92

type CloudformationResolverV2 struct {
	// contains filtered or unexported fields
}

func NewCloudformationResolverV2 added in v1.0.92

func NewCloudformationResolverV2(i *Instance) *CloudformationResolverV2

NewCloudformationResolverV2 resolves the services ResolverV2 endpoint

func (*CloudformationResolverV2) ResolveEndpoint added in v1.0.92

type CloudwatchEventsResolverV2 added in v1.0.92

type CloudwatchEventsResolverV2 struct {
	// contains filtered or unexported fields
}

func NewCloudwatchEventsResolverV2 added in v1.0.92

func NewCloudwatchEventsResolverV2(i *Instance) *CloudwatchEventsResolverV2

NewCloudwatchEventsResolverV2 resolves the services ResolverV2 endpoint

func (*CloudwatchEventsResolverV2) ResolveEndpoint added in v1.0.92

type CloudwatchLogsResolverV2 added in v1.0.92

type CloudwatchLogsResolverV2 struct {
	// contains filtered or unexported fields
}

func NewCloudwatchLogsResolverV2 added in v1.0.92

func NewCloudwatchLogsResolverV2(i *Instance) *CloudwatchLogsResolverV2

NewCloudwatchLogsResolverV2 resolves the services ResolverV2 endpoint

func (*CloudwatchLogsResolverV2) ResolveEndpoint added in v1.0.92

type CloudwatchResolverV2 added in v1.0.92

type CloudwatchResolverV2 struct {
	// contains filtered or unexported fields
}

func NewCloudwatchResolverV2 added in v1.0.92

func NewCloudwatchResolverV2(i *Instance) *CloudwatchResolverV2

NewCloudwatchResolverV2 resolves the services ResolverV2 endpoint

func (*CloudwatchResolverV2) ResolveEndpoint added in v1.0.92

type DynamoDbResolver added in v1.0.92

type DynamoDbResolver struct {
	// contains filtered or unexported fields
}

func NewDynamoDbResolverV2 added in v1.0.92

func NewDynamoDbResolverV2(i *Instance) *DynamoDbResolver

NewDynamoDbResolverV2 resolves the services ResolverV2 endpoint

func (*DynamoDbResolver) ResolveEndpoint added in v1.0.92

type DynamoDbStreamsResolverV2 added in v1.0.92

type DynamoDbStreamsResolverV2 struct {
	// contains filtered or unexported fields
}

func NewDynamoDbStreamsResolverV2 added in v1.0.92

func NewDynamoDbStreamsResolverV2(i *Instance) *DynamoDbStreamsResolverV2

NewDynamoDbStreamsResolverV2 resolves the services ResolverV2 endpoint

func (*DynamoDbStreamsResolverV2) ResolveEndpoint added in v1.0.92

type Ec2ResolverV2 added in v1.0.92

type Ec2ResolverV2 struct {
	// contains filtered or unexported fields
}

func NewEc2ResolverV2 added in v1.0.92

func NewEc2ResolverV2(i *Instance) *Ec2ResolverV2

NewEc2ResolverV2 resolves the services ResolverV2 endpoint

func (*Ec2ResolverV2) ResolveEndpoint added in v1.0.92

type ElasticSearchResolverV2 added in v1.0.92

type ElasticSearchResolverV2 struct {
	// contains filtered or unexported fields
}

func NewElasticSearchResolverV2 added in v1.0.92

func NewElasticSearchResolverV2(i *Instance) *ElasticSearchResolverV2

NewElasticSearchResolverV2 resolves the services ResolverV2 endpoint

func (*ElasticSearchResolverV2) ResolveEndpoint added in v1.0.92

type FirehoseResolverV2 added in v1.0.92

type FirehoseResolverV2 struct {
	// contains filtered or unexported fields
}

func NewFirehoseResolverV2 added in v1.0.92

func NewFirehoseResolverV2(i *Instance) *FirehoseResolverV2

NewFirehoseResolverV2 resolves the services ResolverV2 endpoint

func (*FirehoseResolverV2) ResolveEndpoint added in v1.0.92

type IamResolverV2 added in v1.0.92

type IamResolverV2 struct {
	// contains filtered or unexported fields
}

func NewIamResolverV2 added in v1.0.92

func NewIamResolverV2(i *Instance) *IamResolverV2

NewIamResolverV2 resolves the services ResolverV2 endpoint

func (*IamResolverV2) ResolveEndpoint added in v1.0.92

type Instance

type Instance struct {
	// contains filtered or unexported fields
}

Instance manages the localstack

func NewInstance

func NewInstance(opts ...InstanceOption) (*Instance, error)

NewInstance creates a new Instance Fails when Docker is not reachable

func NewInstanceCtx added in v1.0.3

func NewInstanceCtx(ctx context.Context, opts ...InstanceOption) (*Instance, error)

NewInstanceCtx is NewInstance, but with Context

func (*Instance) Endpoint

func (i *Instance) Endpoint(service Service) string

Endpoint returns the endpoint for the given service Endpoints are allocated dynamically (to avoid blocked ports), but are fix after starting the instance

func (*Instance) EndpointV2

func (i *Instance) EndpointV2(service Service) string

EndpointV2 returns the endpoint for the given service when used by aws-sdk-v2 Endpoints are allocated dynamically (to avoid blocked ports), but are fix after starting the instance

func (*Instance) Start

func (i *Instance) Start(services ...Service) error

Start starts the localstack

func (*Instance) StartWithContext

func (i *Instance) StartWithContext(ctx context.Context, services ...Service) error

StartWithContext starts the localstack and ends it when the context is done. Deprecated: Use Start/Stop instead, as shutdown is not reliable

func (*Instance) Stop

func (i *Instance) Stop() error

Stop stops the localstack

type InstanceOption

type InstanceOption func(i *Instance)

InstanceOption is an option that controls the behaviour of localstack.

func WithClientFromEnv

func WithClientFromEnv() (InstanceOption, error)

WithClientFromEnv configures the instance to use a client that respects environment variables.

func WithClientFromEnvCtx added in v1.0.3

func WithClientFromEnvCtx(ctx context.Context) (InstanceOption, error)

WithClientFromEnvCtx like WithClientFromEnv but with context

func WithLabels

func WithLabels(labels map[string]string) InstanceOption

WithLabels configures the labels that will be applied on the instance.

func WithLogger

func WithLogger(logger *logrus.Logger) InstanceOption

WithLogger configures the instance to use the specified logger.

func WithTimeout

func WithTimeout(timeout time.Duration) InstanceOption

WithTimeout configures the timeout for terminating the localstack instance. This was invented to prevent orphaned containers after panics. The default timeout is set to 5 minutes.

func WithVersion

func WithVersion(version string) InstanceOption

WithVersion configures the instance to use a specific version of localstack. Must be a valid version string or "latest".

type KinesisResolverV2 added in v1.0.92

type KinesisResolverV2 struct {
	// contains filtered or unexported fields
}

func NewKinesisResolverV2 added in v1.0.92

func NewKinesisResolverV2(i *Instance) *KinesisResolverV2

NewKinesisResolverV2 resolves the services ResolverV2 endpoint

func (*KinesisResolverV2) ResolveEndpoint added in v1.0.92

type LambdaResolverV2 added in v1.0.92

type LambdaResolverV2 struct {
	// contains filtered or unexported fields
}

func NewLambdaResolverV2 added in v1.0.92

func NewLambdaResolverV2(i *Instance) *LambdaResolverV2

NewLambdaResolverV2 resolves the services ResolverV2 endpoint

func (*LambdaResolverV2) ResolveEndpoint added in v1.0.92

type RedshiftResolverV2 added in v1.0.92

type RedshiftResolverV2 struct {
	// contains filtered or unexported fields
}

func NewRedshiftResolverV2 added in v1.0.92

func NewRedshiftResolverV2(i *Instance) *RedshiftResolverV2

NewRedshiftResolverV2 resolves the services ResolverV2 endpoint

func (*RedshiftResolverV2) ResolveEndpoint added in v1.0.92

type Route53ResolverV2 added in v1.0.92

type Route53ResolverV2 struct {
	// contains filtered or unexported fields
}

func NewRoute53ResolverV2 added in v1.0.92

func NewRoute53ResolverV2(i *Instance) *Route53ResolverV2

NewRoute53ResolverV2 resolves the services ResolverV2 endpoint

func (*Route53ResolverV2) ResolveEndpoint added in v1.0.92

type S3ResolverV2 added in v1.0.92

type S3ResolverV2 struct {
	// contains filtered or unexported fields
}

func NewS3ResolverV2 added in v1.0.92

func NewS3ResolverV2(i *Instance) *S3ResolverV2

NewS3ResolverV2 resolves the services ResolverV2 endpoint

func (*S3ResolverV2) ResolveEndpoint added in v1.0.92

type SecretsManagerResolverV2 added in v1.0.92

type SecretsManagerResolverV2 struct {
	// contains filtered or unexported fields
}

func NewSecretsManagerResolverV2 added in v1.0.92

func NewSecretsManagerResolverV2(i *Instance) *SecretsManagerResolverV2

NewSecretsManagerResolverV2 resolves the services ResolverV2 endpoint

func (*SecretsManagerResolverV2) ResolveEndpoint added in v1.0.92

type Service

type Service struct {
	Name string
	Port string
}

Service represents an AWS service

type SesResolverV2 added in v1.0.92

type SesResolverV2 struct {
	// contains filtered or unexported fields
}

func NewSesResolverV2 added in v1.0.92

func NewSesResolverV2(i *Instance) *SesResolverV2

NewSesResolverV2 resolves the services ResolverV2 endpoint

func (*SesResolverV2) ResolveEndpoint added in v1.0.92

type SnsResolverV2 added in v1.0.92

type SnsResolverV2 struct {
	// contains filtered or unexported fields
}

func NewSnsResolverV2 added in v1.0.92

func NewSnsResolverV2(i *Instance) *SnsResolverV2

NewSnsResolverV2 resolves the services ResolverV2 endpoint

func (*SnsResolverV2) ResolveEndpoint added in v1.0.92

type SqsResolverV2 added in v1.0.92

type SqsResolverV2 struct {
	// contains filtered or unexported fields
}

func NewSqsResolverV2 added in v1.0.92

func NewSqsResolverV2(i *Instance) *SqsResolverV2

NewSqsResolverV2 resolves the services ResolverV2 endpoint

func (*SqsResolverV2) ResolveEndpoint added in v1.0.92

type SsmResolverV2 added in v1.0.92

type SsmResolverV2 struct {
	// contains filtered or unexported fields
}

func NewSsmResolverV2 added in v1.0.92

func NewSsmResolverV2(i *Instance) *SsmResolverV2

NewSsmResolverV2 resolves the services ResolverV2 endpoint

func (*SsmResolverV2) ResolveEndpoint added in v1.0.92

type StepFunctionsResolverV2 added in v1.0.92

type StepFunctionsResolverV2 struct {
	// contains filtered or unexported fields
}

func NewStepFunctionsResolverV2 added in v1.0.92

func NewStepFunctionsResolverV2(i *Instance) *StepFunctionsResolverV2

NewStepFunctionsResolverV2 resolves the services ResolverV2 endpoint

func (*StepFunctionsResolverV2) ResolveEndpoint added in v1.0.92

type StsResolverV2 added in v1.0.92

type StsResolverV2 struct {
	// contains filtered or unexported fields
}

func NewStsResolverV2 added in v1.0.92

func NewStsResolverV2(i *Instance) *StsResolverV2

NewStsResolverV2 resolves the services ResolverV2 endpoint

func (*StsResolverV2) ResolveEndpoint added in v1.0.92

Directories

Path Synopsis
internalfakes
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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