Skip to content

Commit

Permalink
feat: localstack support added
Browse files Browse the repository at this point in the history
in unit tests we cannow spin up loclastack containers
  • Loading branch information
Adam Bezecny committed Oct 30, 2023
1 parent b481362 commit 7ef561f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
65 changes: 64 additions & 1 deletion test/localstack.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,66 @@
package test

// TBD, first we need to have aws package, v2
import (
"context"
"fmt"
"github.com/rs/zerolog/log"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"time"
)

const (
LocalstackImage = "localstack/localstack:latest"
LocalstackPort = "4566"
)

type LocalstackContainer struct {
Container testcontainers.Container
Context context.Context
URI string
}

func SetupLocalstack(ctx context.Context) *LocalstackContainer {
exposedPort := fmt.Sprintf("%s/tcp", LocalstackPort)
req := testcontainers.ContainerRequest{
Image: LocalstackImage,
ExposedPorts: []string{exposedPort},
WaitingFor: wait.ForAll(
wait.ForLog("Ready.").WithStartupTimeout(20*time.Second),
wait.ForListeningPort(LocalstackPort),
).WithDeadline(1 * time.Minute),
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
log.Fatal().Err(err).Msg("cannot start localstack container")
}

hostIP, err := container.Host(ctx)
if err != nil {
log.Fatal().Err(err).Msg("cannot get container host for localstack")
}

mappedPort, err := container.MappedPort(ctx, LocalstackPort)
if err != nil {
log.Fatal().Err(err).Msg("cannot get mapped port for localstack")
}

uri := fmt.Sprintf("http://%s:%s", hostIP, mappedPort.Port())
log.Info().Msgf("Localstack connection string: %s", uri)

return &LocalstackContainer{
Container: container,
Context: ctx,
URI: uri,
}
}

func (l *LocalstackContainer) TeardownLocalstack() {
if err := l.Container.Terminate(l.Context); err != nil {
log.Error().Err(err).Msg("cannot close localstack")
}
}
13 changes: 13 additions & 0 deletions test/localstack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package test_test

import (
"context"
"github.com/hrsupersport/hrnogomet-backend-kit/test"
"testing"
)

func TestCreateLocalstackContainer(t *testing.T) {
ctx := context.Background()
c := test.SetupLocalstack(ctx)
defer c.TeardownLocalstack()
}

0 comments on commit 7ef561f

Please sign in to comment.