59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/ory/dockertest/v3"
|
|
"github.com/pkg/errors"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
var client redis.UniversalClient
|
|
|
|
func TestMain(m *testing.M) {
|
|
// uses a sensible default on windows (tcp/http) and linux/osx (socket)
|
|
pool, err := dockertest.NewPool("")
|
|
if err != nil {
|
|
log.Fatalf("%+v", errors.WithStack(err))
|
|
}
|
|
|
|
// uses pool to try to connect to Docker
|
|
err = pool.Client.Ping()
|
|
if err != nil {
|
|
log.Fatalf("%+v", errors.WithStack(err))
|
|
}
|
|
|
|
// pulls an image, creates a container based on it and runs it
|
|
resource, err := pool.Run("redis", "alpine3.17", []string{})
|
|
if err != nil {
|
|
log.Fatalf("%+v", errors.WithStack(err))
|
|
}
|
|
|
|
if err := pool.Retry(func() error {
|
|
client = redis.NewUniversalClient(&redis.UniversalOptions{
|
|
Addrs: []string{resource.GetHostPort("6379/tcp")},
|
|
})
|
|
|
|
ctx := context.Background()
|
|
|
|
if cmd := client.Ping(ctx); cmd.Err() != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
return nil
|
|
}); err != nil {
|
|
log.Fatalf("%+v", errors.WithStack(err))
|
|
}
|
|
|
|
code := m.Run()
|
|
|
|
if err := pool.Purge(resource); err != nil {
|
|
log.Fatalf("%+v", errors.WithStack(err))
|
|
}
|
|
|
|
os.Exit(code)
|
|
}
|