2023-07-10 06:16:03 +00:00
|
|
|
package adapter_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ory/dockertest/v3"
|
|
|
|
"github.com/ory/dockertest/v3/docker"
|
|
|
|
"github.com/pressly/goose/v3"
|
|
|
|
)
|
|
|
|
|
2023-07-13 04:22:52 +00:00
|
|
|
const (
|
|
|
|
envDBDSN = "TESTS_DB_DSN"
|
|
|
|
envRedisConnectionString = "TESTS_REDIS_CONNECTION_STRING"
|
|
|
|
)
|
2023-07-10 06:16:03 +00:00
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goose.SetBaseFS(os.DirFS("../../migrations"))
|
|
|
|
goose.SetLogger(goose.NopLogger())
|
|
|
|
|
|
|
|
_, isDBDSNPresent := os.LookupEnv(envDBDSN)
|
2023-07-13 04:22:52 +00:00
|
|
|
_, isRedisConnectionStringPresent := os.LookupEnv(envRedisConnectionString)
|
2023-07-10 06:16:03 +00:00
|
|
|
|
|
|
|
// all required envs are set, there is no need to use dockertest
|
2023-07-13 04:22:52 +00:00
|
|
|
if isDBDSNPresent && isRedisConnectionStringPresent {
|
2023-07-10 06:16:03 +00:00
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := dockertest.NewPool("")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("couldn't construct pool: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pool.MaxWait = 30 * time.Second
|
|
|
|
if err = pool.Client.Ping(); err != nil {
|
|
|
|
log.Fatalf("couldn't connect to Docker: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var resources []*dockertest.Resource
|
|
|
|
|
|
|
|
if !isDBDSNPresent {
|
|
|
|
resource, dsn := newPostgres(pool)
|
|
|
|
_ = os.Setenv(envDBDSN, dsn.String())
|
|
|
|
resources = append(resources, resource)
|
|
|
|
}
|
|
|
|
|
2023-07-13 04:22:52 +00:00
|
|
|
if !isRedisConnectionStringPresent {
|
|
|
|
resource, connString := newRedis(pool)
|
|
|
|
_ = os.Setenv(envRedisConnectionString, connString.String())
|
|
|
|
resources = append(resources, resource)
|
|
|
|
}
|
|
|
|
|
2023-07-10 06:16:03 +00:00
|
|
|
code := m.Run()
|
|
|
|
|
|
|
|
for _, r := range resources {
|
|
|
|
_ = pool.Purge(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPostgres(pool *dockertest.Pool) (*dockertest.Resource, *url.URL) {
|
|
|
|
q := url.Values{}
|
|
|
|
q.Add("sslmode", "disable")
|
|
|
|
dsn := &url.URL{
|
|
|
|
Scheme: "postgres",
|
|
|
|
User: url.UserPassword("postgres", "postgres"),
|
|
|
|
Path: "twhelpdb",
|
|
|
|
RawQuery: q.Encode(),
|
|
|
|
}
|
|
|
|
|
|
|
|
pw, _ := dsn.User.Password()
|
|
|
|
|
2023-07-13 04:22:52 +00:00
|
|
|
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
|
2023-07-10 06:16:03 +00:00
|
|
|
Repository: "postgres",
|
|
|
|
Tag: "14.8",
|
|
|
|
Env: []string{
|
|
|
|
fmt.Sprintf("POSTGRES_USER=%s", dsn.User.Username()),
|
|
|
|
fmt.Sprintf("POSTGRES_PASSWORD=%s", pw),
|
|
|
|
fmt.Sprintf("POSTGRES_DB=%s", dsn.Path),
|
|
|
|
},
|
|
|
|
}, func(config *docker.HostConfig) {
|
|
|
|
config.AutoRemove = true
|
|
|
|
config.RestartPolicy = docker.RestartPolicy{
|
|
|
|
Name: "no",
|
|
|
|
}
|
|
|
|
})
|
2023-07-13 04:22:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("couldn't run postgres: %s", err)
|
2023-07-10 06:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ = resource.Expire(120)
|
|
|
|
|
|
|
|
dsn.Host = getHostPort(resource, "5432/tcp")
|
|
|
|
|
|
|
|
return resource, dsn
|
|
|
|
}
|
|
|
|
|
2023-07-13 04:22:52 +00:00
|
|
|
func newRedis(pool *dockertest.Pool) (*dockertest.Resource, *url.URL) {
|
|
|
|
connectionString := &url.URL{
|
|
|
|
Scheme: "redis",
|
|
|
|
Path: "0",
|
|
|
|
}
|
|
|
|
|
|
|
|
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
|
|
|
|
Repository: "redis",
|
|
|
|
Tag: "7.0.11",
|
|
|
|
}, func(config *docker.HostConfig) {
|
|
|
|
config.AutoRemove = true
|
|
|
|
config.RestartPolicy = docker.RestartPolicy{
|
|
|
|
Name: "no",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("couldn't run redis: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = resource.Expire(120)
|
|
|
|
|
|
|
|
connectionString.Host = getHostPort(resource, "6379/tcp")
|
|
|
|
|
|
|
|
return resource, connectionString
|
|
|
|
}
|
|
|
|
|
2023-07-10 06:16:03 +00:00
|
|
|
func getHostPort(resource *dockertest.Resource, id string) string {
|
|
|
|
dockerURL := os.Getenv("DOCKER_HOST")
|
|
|
|
if dockerURL == "" {
|
|
|
|
return resource.GetHostPort(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(dockerURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("couldn't parse DOCKER_HOST: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return net.JoinHostPort(u.Hostname(), resource.GetPort(id))
|
|
|
|
}
|