2022-04-28 16:35:02 +00:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
|
|
|
|
"github.com/baez90/nurse/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
func PrepareRedisContainer(tb testing.TB) *config.Server {
|
|
|
|
tb.Helper()
|
|
|
|
const redisPort = "6379/tcp"
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
|
|
|
|
tb.Cleanup(cancel)
|
|
|
|
|
|
|
|
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
|
|
|
ContainerRequest: testcontainers.ContainerRequest{
|
|
|
|
Image: "docker.io/redis:alpine",
|
|
|
|
ExposedPorts: []string{redisPort},
|
2022-05-08 09:00:22 +00:00
|
|
|
SkipReaper: true,
|
|
|
|
AutoRemove: true,
|
2022-04-28 16:35:02 +00:00
|
|
|
},
|
|
|
|
Started: true,
|
|
|
|
Logger: testcontainers.TestLogger(tb),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
tb.Fatalf("testcontainers.GenericContainer() err = %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
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
|
|
|
|
}
|