Skip to content

Commit

Permalink
Resolve sql testing race issues (#1332)
Browse files Browse the repository at this point in the history
Signed-off-by: aeneasr <aeneas@ory.sh>
  • Loading branch information
aeneasr authored Apr 2, 2019
1 parent 7c4b6d4 commit 22c0487
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 60 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ test-resetdb:
docker run --rm --name hydra_test_database_postgres -p 3445:5432 -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=hydra -d postgres:9.6

# Runs tests in short mode, without database adapters
.PHONY: test-short
test-short:
.PHONY: quicktest
quicktest:
go test -failfast -short ./...

# Formats the code
Expand Down
25 changes: 6 additions & 19 deletions consent/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ import (
"testing"
"time"

"github.com/ory/hydra/client"
"github.com/ory/hydra/oauth2"

"github.com/jmoiron/sqlx"
"github.com/spf13/viper"

Expand Down Expand Up @@ -74,16 +71,7 @@ func TestMain(m *testing.M) {
func createSQL(db *sqlx.DB) driver.Registry {
conf := internal.NewConfigurationWithDefaults()
reg := internal.NewRegistrySQL(conf, db)

if _, err := reg.ClientManager().(*client.SQLManager).CreateSchemas(); err != nil {
panic(err)
}

if _, err := reg.ConsentManager().(*SQLManager).CreateSchemas(); err != nil {
panic(err)
}

if _, err := reg.OAuth2Storage().(*oauth2.FositeSQLStore).CreateSchemas(); err != nil {
if _, err := reg.CreateSchemas(); err != nil {
panic(err)
}

Expand All @@ -96,17 +84,16 @@ func TestManagers(t *testing.T) {
regs["memory"] = internal.NewRegistry(conf)

if !testing.Short() {
var p, m *sqlx.DB
dockertest.Parallel([]func(){
func() {
m.Lock()
regs["postgres"] = createSQL(connectToPostgres(t))
m.Unlock()
p = connectToPostgres(t)
}, func() {
m.Lock()
regs["mysql"] = createSQL(connectToMySQL(t))
m.Unlock()
m = connectToMySQL(t)
},
})
regs["postgres"] = createSQL(p)
regs["mysql"] = createSQL(m)
}

for k, m := range regs {
Expand Down
17 changes: 9 additions & 8 deletions driver/registry_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,22 @@ func (m *RegistrySQL) DB() *sqlx.DB {
func (m *RegistrySQL) CreateSchemas() (int, error) {
var total int

// Ensure dependencies exist
_, _, _, _ = m.ClientManager(), m.ConsentManager(), m.KeyManager(), m.OAuth2Storage()

for _, s := range []schemaCreator{
m.km.(schemaCreator),
m.cm.(schemaCreator),
m.com.(schemaCreator),
m.fs.(schemaCreator),
m.Logger().Debugf("Applying %s SQL migrations...", m.db.DriverName())
for k, s := range []schemaCreator{
m.KeyManager().(schemaCreator),
m.ClientManager().(schemaCreator),
m.ConsentManager().(schemaCreator),
m.OAuth2Storage().(schemaCreator),
} {
m.Logger().Debugf("Applying %s SQL migrations for manager: %T (%d)", m.db.DriverName(), s, k)
if c, err := s.CreateSchemas(); err != nil {
return c, err
} else {
m.Logger().Debugf("Successfully applied %d %s SQL migrations from manager: %T (%d)", c, m.db.DriverName(), s, k)
total += c
}
}
m.Logger().Debugf("Applied %d %s SQL migrations", total, m.db.DriverName())

return total, nil
}
Expand Down
22 changes: 6 additions & 16 deletions oauth2/fosite_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package oauth2_test
import (
"context"
"flag"
"sync"
"testing"

"github.com/ory/hydra/x"
Expand All @@ -34,7 +33,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/ory/hydra/client"
"github.com/ory/hydra/consent"
"github.com/ory/hydra/driver/configuration"

"github.com/ory/hydra/driver"
Expand All @@ -46,8 +44,6 @@ import (

var registries = make(map[string]driver.Registry)

var m sync.Mutex

func TestMain(m *testing.M) {
flag.Parse()
runner := dockertest.Register()
Expand All @@ -70,11 +66,7 @@ func connectToMySQL(t *testing.T) *sqlx.DB {

func connectSQL(t *testing.T, conf *configuration.ViperProvider, db *sqlx.DB) driver.Registry {
reg := internal.NewRegistrySQL(conf, db)
_, err := reg.ClientManager().(*client.SQLManager).CreateSchemas()
require.NoError(t, err)
_, err = reg.ConsentManager().(*consent.SQLManager).CreateSchemas()
require.NoError(t, err)
_, err = reg.OAuth2Storage().(*FositeSQLStore).CreateSchemas()
_, err := reg.CreateSchemas()
require.NoError(t, err)
return reg
}
Expand All @@ -87,19 +79,17 @@ func TestManagers(t *testing.T) {
registries["memory"] = reg

if !testing.Short() {
var p, m *sqlx.DB
dockertest.Parallel([]func(){
func() {
m.Lock()

registries["postgres"] = connectSQL(t, conf, connectToPG(t))
m.Unlock()
p = connectToPG(t)
},
func() {
m.Lock()
registries["mysql"] = connectSQL(t, conf, connectToMySQL(t))
m.Unlock()
m = connectToMySQL(t)
},
})
registries["postgres"] = connectSQL(t, conf, p)
registries["mysql"] = connectSQL(t, conf, m)
}

for k, store := range registries {
Expand Down
29 changes: 14 additions & 15 deletions oauth2/oauth2_auth_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
"testing"
"time"

"github.com/jmoiron/sqlx"

"github.com/ory/hydra/x"
"github.com/ory/x/sqlcon/dockertest"

Expand Down Expand Up @@ -96,33 +98,30 @@ type clientCreator interface {
// - [x] If `id_token_hint` is handled properly
// - [x] What happens if `id_token_hint` does not match the value from the handled authentication request ("accept login")
func TestAuthCodeWithDefaultStrategy(t *testing.T) {
var mutex sync.Mutex
conf := internal.NewConfigurationWithDefaults()
regs := map[string]driver.Registry{
"memory": internal.NewRegistry(conf),
}

if !testing.Short() {
var p, m *sqlx.DB
dockertest.Parallel([]func(){
func() {
r := internal.NewRegistrySQL(conf, connectToPG(t))
_, err := r.CreateSchemas()
require.NoError(t, err)

mutex.Lock()
regs["postgres"] = r
mutex.Unlock()
p = connectToPG(t)
},
func() {
r := internal.NewRegistrySQL(conf, connectToMySQL(t))
_, err := r.CreateSchemas()
require.NoError(t, err)

mutex.Lock()
regs["mysql"] = r
mutex.Unlock()
m = connectToMySQL(t)
},
})
pr := internal.NewRegistrySQL(conf, p)
_, err := pr.CreateSchemas()
require.NoError(t, err)
regs["postgres"] = pr

mr := internal.NewRegistrySQL(conf, m)
_, err = mr.CreateSchemas()
require.NoError(t, err)
regs["mysql"] = mr
}

for km, reg := range regs {
Expand Down

0 comments on commit 22c0487

Please sign in to comment.