-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
in unit tests we cannow spin up loclastack containers
- Loading branch information
Adam Bezecny
committed
Oct 30, 2023
1 parent
b481362
commit 7ef561f
Showing
2 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |