nurse/protocols/redis/container_test.go

54 lines
1.3 KiB
Go
Raw Permalink Normal View History

2022-04-28 16:35:02 +00:00
package redis_test
import (
"context"
"fmt"
"testing"
"time"
tc "github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
2022-04-28 16:35:02 +00:00
2022-09-22 09:46:36 +00:00
"code.icb4dc0.de/prskr/nurse/config"
2022-04-28 16:35:02 +00:00
)
func PrepareRedisContainer(tb testing.TB) *config.Server {
tb.Helper()
const redisPort = "6379/tcp"
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
2022-04-28 16:35:02 +00:00
tb.Cleanup(cancel)
container, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{
ContainerRequest: tc.ContainerRequest{
2022-04-28 16:35:02 +00:00
Image: "docker.io/redis:alpine",
ExposedPorts: []string{redisPort},
SkipReaper: true,
AutoRemove: true,
WaitingFor: wait.ForListeningPort(redisPort),
2022-04-28 16:35:02 +00:00
},
Started: true,
Logger: tc.TestLogger(tb),
2022-04-28 16:35:02 +00:00
})
if err != nil {
tb.Fatalf("tc.GenericContainer() err = %v", err)
2022-04-28 16:35:02 +00:00
}
tb.Cleanup(func() {
if err := container.Terminate(context.Background()); err != nil {
tb.Errorf("container.Terminate() err = %v", err)
}
})
ep, err := container.PortEndpoint(ctx, redisPort, "redis")
if err != nil {
tb.Fatalf("container.PortEndpoint() err = %v", err)
}
srv := new(config.Server)
if err := srv.UnmarshalURL(fmt.Sprintf("%s/0?MaxRetries=3", ep)); err != nil {
2022-04-28 16:35:02 +00:00
tb.Fatalf("config.ParseFromURL() err = %v", err)
}
return srv
}