refactor: set up postgres only once for tests
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Dawid Wysokiński 2023-07-10 08:16:03 +02:00
parent c7efe7a240
commit 3722b0098f
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
2 changed files with 107 additions and 79 deletions

View File

@ -0,0 +1,106 @@
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"
)
const envDBDSN = "TESTS_DB_DSN"
func TestMain(m *testing.M) {
goose.SetBaseFS(os.DirFS("../../migrations"))
goose.SetLogger(goose.NopLogger())
_, isDBDSNPresent := os.LookupEnv(envDBDSN)
// all required envs are set, there is no need to use dockertest
if isDBDSNPresent {
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)
}
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()
resource, runErr := pool.RunWithOptions(&dockertest.RunOptions{
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",
}
})
if runErr != nil {
log.Fatalf("couldn't run postgres: %s", runErr)
}
_ = resource.Expire(120)
dsn.Host = getHostPort(resource, "5432/tcp")
return resource, dsn
}
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))
}

View File

@ -4,12 +4,8 @@ import (
"context"
"database/sql"
"errors"
"fmt"
"net"
"net/url"
"os"
"strings"
"sync"
"testing"
"time"
"unicode"
@ -18,10 +14,7 @@ import (
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
"github.com/cenkalti/backoff/v4"
"github.com/google/uuid"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"github.com/pressly/goose/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dbfixture"
@ -29,65 +22,13 @@ import (
"github.com/uptrace/bun/driver/pgdriver"
)
var (
gooseSetUpOnce = sync.Once{}
)
func newBunDB(tb testing.TB) *bun.DB {
tb.Helper()
if dsn, ok := os.LookupEnv("TESTS_DB_DSN"); ok {
return newBunDBWithDSN(tb, dsn)
}
q := url.Values{}
q.Add("sslmode", "disable")
dsn := &url.URL{
Scheme: "postgres",
User: url.UserPassword("postgres", "postgres"),
Path: "twhelp",
RawQuery: q.Encode(),
}
pool, err := dockertest.NewPool("")
require.NoError(tb, err, "couldn't connect to docker")
pool.MaxWait = 20 * time.Second
pw, _ := dsn.User.Password()
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
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",
}
})
require.NoError(tb, err, "couldn't start resource")
tb.Cleanup(func() {
_ = pool.Purge(resource)
})
assert.NoError(tb, resource.Expire(60))
dsn.Host = getHostPort(tb, resource, "5432/tcp")
return newBunDBWithDSN(tb, dsn.String())
}
func newBunDBWithDSN(tb testing.TB, dsn string) *bun.DB {
tb.Helper()
schema := generateSchema()
sqldb := sql.OpenDB(
pgdriver.NewConnector(
pgdriver.WithDSN(dsn),
pgdriver.WithDSN(os.Getenv(envDBDSN)),
pgdriver.WithConnParams(map[string]interface{}{
"search_path": schema,
}),
@ -108,28 +49,9 @@ func newBunDBWithDSN(tb testing.TB, dsn string) *bun.DB {
return bunDB
}
func getHostPort(tb testing.TB, resource *dockertest.Resource, id string) string {
tb.Helper()
dockerURL := os.Getenv("DOCKER_HOST")
if dockerURL == "" {
return resource.GetHostPort(id)
}
u, err := url.Parse(dockerURL)
require.NoError(tb, err)
return net.JoinHostPort(u.Hostname(), resource.GetPort(id))
}
func runMigrations(tb testing.TB, db *sql.DB) {
tb.Helper()
gooseSetUpOnce.Do(func() {
goose.SetBaseFS(os.DirFS("../../migrations"))
goose.SetLogger(goose.NopLogger())
})
require.NoError(tb, goose.Up(db, ""))
}