package adapter_test import ( "flag" "fmt" "log" "net" "net/url" "os" "testing" "time" "github.com/ory/dockertest/v3" "github.com/ory/dockertest/v3/docker" "github.com/pressly/goose/v3" ) var ( dbDSN = os.Getenv("TESTS_DB_DSN") redisConnectionString = os.Getenv("TESTS_REDIS_CONNECTION_STRING") ) func TestMain(m *testing.M) { setUpGoose() // https://github.com/golang/go/blob/7cfa7d69259590319524c3715df4a39b39924bc3/src/testing/testing.go#L224 flag.Parse() // all required envs are set, there is no need to use dockertest if (dbDSN != "" && redisConnectionString != "") || testing.Short() { 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 dbDSN == "" { resource, dsn := newPostgres(pool) dbDSN = dsn.String() resources = append(resources, resource) } if redisConnectionString == "" { resource, connString := newRedis(pool) redisConnectionString = connString.String() resources = append(resources, resource) } code := m.Run() for _, r := range resources { _ = pool.Purge(r) } os.Exit(code) } func setUpGoose() { goose.SetBaseFS(os.DirFS("../../migrations")) goose.SetLogger(goose.NopLogger()) if err := goose.SetDialect("postgres"); err != nil { log.Fatalf("goose - couldn't set dialect: %s", err) } } 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() resource, err := pool.RunWithOptions(&dockertest.RunOptions{ Repository: "postgres", Tag: "14.9", 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", } }) if err != nil { log.Fatalf("couldn't run postgres: %s", err) } _ = resource.Expire(120) dsn.Host = getHostPort(resource, "5432/tcp") return resource, dsn } 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.2.2", }, 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 } 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)) }