Skip to content

Commit

Permalink
tests: more robust test db connections
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed May 16, 2022
1 parent cb8509d commit 38c3533
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ require (
github.com/go-openapi/strfmt v0.20.3
github.com/go-openapi/swag v0.19.15
github.com/go-openapi/validate v0.20.3
github.com/go-sql-driver/mysql v1.6.0
github.com/gobuffalo/pop/v6 v6.0.1
github.com/gofrs/uuid v4.1.0+incompatible
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645
github.com/instana/testify v1.6.2-0.20200721153833-94b1851f4d65
github.com/julienschmidt/httprouter v1.3.0
github.com/luna-duclos/instrumentedsql v1.1.3
github.com/mikefarah/yq/v4 v4.19.1
Expand Down Expand Up @@ -96,7 +99,6 @@ require (
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/loads v0.20.2 // indirect
github.com/go-openapi/spec v0.20.3 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/gobuffalo/envy v1.10.1 // indirect
github.com/gobuffalo/fizz v1.14.0 // indirect
Expand Down
16 changes: 8 additions & 8 deletions internal/persistence/sql/migrations/migratest/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,23 @@ func TestMigrations(t *testing.T) {
ctx := context.Background()
l := logrusx.New("", "", logrusx.ForceLevel(logrus.DebugLevel))

var c *pop.Connection
var conn *pop.Connection
var err error
c, err = pop.NewConnection(&pop.ConnectionDetails{URL: db.Conn})
conn, err = pop.NewConnection(&pop.ConnectionDetails{URL: db.Conn})
require.NoError(t, err)
require.NoError(t, c.Open())
t.Cleanup(func() { c.Close() })
for i := 0; i < 120; i++ {
if err := c.Store.(interface{ Ping() error }).Ping(); err == nil {
require.NoError(t, conn.Open())
if err := dbx.Ping(conn); err == nil {
t.Cleanup(func() { conn.Close() })
break
}
time.Sleep(time.Second)
}
require.NoError(t, c.Store.(interface{ Ping() error }).Ping())
require.NoError(t, dbx.Ping(conn))

tm, err := popx.NewMigrationBox(
fsx.Merge(sql.Migrations, networkx.Migrations),
popx.NewMigrator(c, l, nil, 1*time.Minute),
popx.NewMigrator(conn, l, nil, 1*time.Minute),
popx.WithGoMigrations(uuidmapping.Migrations),
withTestdata(t, os.DirFS("./testdata")),
)
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestMigrations(t *testing.T) {

t.Run("table=legacy namespaces", func(t *testing.T) {
// as they are legacy, we expect them to be actually dropped
assert.ErrorIs(t, sqlcon.HandleError(c.RawQuery(
assert.ErrorIs(t, sqlcon.HandleError(conn.RawQuery(
"SELECT * FROM keto_namespace",
).Exec()), sqlcon.ErrNoSuchTable)
})
Expand Down
64 changes: 50 additions & 14 deletions internal/x/dbx/dsn_testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"github.com/go-sql-driver/mysql"
"github.com/gobuffalo/pop/v6"
Expand Down Expand Up @@ -56,22 +57,48 @@ func dbName(_ string) string {
return fmt.Sprintf("testdb_%x", buf)
}

func createDB(t testing.TB, dsn string) (err error) {
func openAndPing(url string) (conn *pop.Connection, err error) {
if conn, err = pop.NewConnection(&pop.ConnectionDetails{URL: url}); err != nil {
return nil, fmt.Errorf("failed to connect to %q: %w", url, err)
}
for i := 0; i < 120; i++ {
fmt.Println("trying to open connection to", url)
if err := conn.Open(); err != nil {
// return nil, fmt.Errorf("failed to open connection: %w", err)
time.Sleep(1 * time.Second)
continue
}
if err := Ping(conn); err == nil {
break
}
time.Sleep(1 * time.Second)
}
if err := Ping(conn); err != nil {
return nil, fmt.Errorf("failed to ping: %w", err)
}
return conn, nil
}

func createDB(t testing.TB, url string, dbName string) (err error) {
var conn *pop.Connection

if conn, err = pop.NewConnection(&pop.ConnectionDetails{URL: dsn}); err != nil {
return fmt.Errorf("failed to connect to %q: %w", dsn, err)
if conn, err = openAndPing(url); err != nil {
return fmt.Errorf("failed to connect to %q: %w", url, err)
}
if err = pop.CreateDB(conn); err != nil {
return fmt.Errorf("failed to create db in %q: %w", dsn, err)

if err := conn.RawQuery("CREATE DATABASE " + dbName).Exec(); err != nil {
return fmt.Errorf("failed to create db %q in %q: %w", dbName, url, err)
}

t.Cleanup(func() {
if err = pop.DropDB(conn); err != nil {
t.Log(err)
if err := conn.RawQuery("DROP DATABASE " + dbName).Exec(); err != nil {
t.Logf("could not drop database %q in %q: %v", dbName, url, err)
}
conn.Close()
})

return

}

func GetDSNs(t testing.TB, debugSqliteOnDisk bool) []*DsnT {
Expand All @@ -88,26 +115,31 @@ func GetDSNs(t testing.TB, debugSqliteOnDisk bool) []*DsnT {

if !testing.Short() {
var mysql, postgres, cockroach string
db := dbName(t.Name())
testDB := dbName(t.Name())

dockertest.Parallel([]func(){
func() {
mysql = withDbName(dockertest.RunTestMySQL(t), db)
if err := createDB(t, mysql); err != nil {
url := dockertest.RunTestMySQL(t)
time.Sleep(1 * time.Second)
if err := createDB(t, url, testDB); err != nil {
t.Fatal(err)
}
mysql = withDbName(url, testDB)
},
func() {
postgres = withDbName(dockertest.RunTestPostgreSQL(t), db)
if err := createDB(t, postgres); err != nil {
url := dockertest.RunTestPostgreSQL(t)
if err := createDB(t, url, testDB); err != nil {
t.Fatal(err)
}
postgres = withDbName(url, testDB)
},
func() {
cockroach = withDbName(dockertest.RunTestCockroachDB(t), db)
if err := createDB(t, cockroach); err != nil {
url := dockertest.RunTestCockroachDB(t)
// time.Sleep(1 * time.Second)
if err := createDB(t, url, testDB); err != nil {
t.Fatal(err)
}
cockroach = withDbName(url, testDB)
},
})

Expand Down Expand Up @@ -187,3 +219,7 @@ func ConfigFile(t testing.TB, values map[string]interface{}) string {

return fn
}

type pinger interface{ Ping() error }

func Ping(conn *pop.Connection) error { return conn.Store.(pinger).Ping() }

0 comments on commit 38c3533

Please sign in to comment.