Skip to content

Commit

Permalink
test: significantly reduce persister run time
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Feb 26, 2022
1 parent 4e87c3b commit 64ecfc2
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 108 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ test/e2e/.bin
pkged.go
coverage.*
schema.sql

heap_profiler/
goroutine_dump/
inflight_trace_dump/
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ require (
github.com/ory/kratos-client-go v0.6.3-alpha.1
github.com/ory/mail/v3 v3.0.0
github.com/ory/nosurf v1.2.7
github.com/ory/x v0.0.348
github.com/ory/x v0.0.351
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/pkg/errors v0.9.1
github.com/pquerna/otp v1.3.0
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ github.com/envoyproxy/protoc-gen-validate v0.6.1/go.mod h1:txg5va2Qkip90uYoSKH+n
github.com/envoyproxy/protoc-gen-validate v0.6.2 h1:JiO+kJTpmYGjEodY7O1Zk8oZcNz1+f30UtwtXoFUPzE=
github.com/envoyproxy/protoc-gen-validate v0.6.2/go.mod h1:2t7qjJNvHPx8IjnBOzl9E9/baC+qXE/TeeyBRzgJDws=
github.com/etcd-io/gofail v0.0.0-20190801230047-ad7f989257ca/go.mod h1:49H/RkXP8pKaZy4h0d+NW16rSLhyVBt4o6VLJbmOqDE=
github.com/evanphx/json-patch v4.9.0+incompatible h1:kLcOMZeuLAJvL2BPWLMIj5oaZQobrkAqrL+WFZwQses=
github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
Expand Down Expand Up @@ -1804,8 +1805,8 @@ github.com/ory/x v0.0.205/go.mod h1:A1s4iwmFIppRXZLF3J9GGWeY/HpREVm0Dk5z/787iek=
github.com/ory/x v0.0.250/go.mod h1:jUJaVptu+geeqlb9SyQCogTKj5ztSDIF6APkhbKtwLc=
github.com/ory/x v0.0.272/go.mod h1:1TTPgJGQutrhI2OnwdrTIHE9ITSf4MpzXFzA/ncTGRc=
github.com/ory/x v0.0.288/go.mod h1:APpShLyJcVzKw1kTgrHI+j/L9YM+8BRjHlcYObc7C1U=
github.com/ory/x v0.0.348 h1:Z2wbEvSpTindtjKTTrd3grIlWbBtvW2udYG5ZjTZHTo=
github.com/ory/x v0.0.348/go.mod h1:Ddbu3ecSaNDgxdntdD1gDu3ALG5fWR5AwUB1ILeBUNE=
github.com/ory/x v0.0.351 h1:RkiK5MH7rCm461SmHvGJopHWGToTkGLaC8VOrTis6cM=
github.com/ory/x v0.0.351/go.mod h1:zuNjBKtyxFpKnDG6q/1QP0pqQv840P/Dw5JbdY7lNTU=
github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
Expand Down
25 changes: 0 additions & 25 deletions persistence/sql/persister_credentials_test.go

This file was deleted.

59 changes: 0 additions & 59 deletions persistence/sql/persister_identity_test.go

This file was deleted.

97 changes: 77 additions & 20 deletions persistence/sql/persister_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package sql_test

import (
"bytes"
"context"
"fmt"
"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/schema"
"github.com/ory/x/sqlxx"
"github.com/ory/x/urlx"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -94,9 +97,10 @@ func createCleanDatabases(t *testing.T) map[string]*driver.RegistryDefault {
var l sync.Mutex
if !testing.Short() {
funcs := map[string]func(t testing.TB) string{
"postgres": dockertest.RunTestPostgreSQL,
"mysql": dockertest.RunTestMySQL,
"cockroach": dockertest.RunTestCockroachDB}
"postgres": dockertest.RunTestPostgreSQL,
// "mysql": dockertest.RunTestMySQL,
"cockroach": dockertest.NewLocalTestCRDBServer,
}

var wg sync.WaitGroup
wg.Add(len(funcs))
Expand All @@ -117,30 +121,40 @@ func createCleanDatabases(t *testing.T) map[string]*driver.RegistryDefault {
t.Logf("sqlite: %s", sqlite)

ps := make(map[string]*driver.RegistryDefault, len(conns))
var wg sync.WaitGroup
wg.Add(len(conns))
for name, dsn := range conns {
_, reg := internal.NewRegistryDefaultWithDSN(t, dsn)
p := reg.Persister().(*sql.Persister)
go func(name, dsn string) {
defer wg.Done()
t.Logf("Connecting to %s", name)
_, reg := internal.NewRegistryDefaultWithDSN(t, dsn)
p := reg.Persister().(*sql.Persister)

_ = os.Remove("migrations/schema.sql")
xsql.CleanSQL(t, p.Connection(context.Background()))
t.Cleanup(func() {
xsql.CleanSQL(t, p.Connection(context.Background()))
t.Logf("Cleaning up %s", name)
_ = os.Remove("migrations/schema.sql")
})
xsql.CleanSQL(t, p.Connection(context.Background()))
t.Cleanup(func() {
xsql.CleanSQL(t, p.Connection(context.Background()))
_ = os.Remove("migrations/schema.sql")
})

pop.SetLogger(pl(t))
require.NoError(t, p.MigrateUp(context.Background()))
status, err := p.MigrationStatus(context.Background())
require.NoError(t, err)
require.False(t, status.HasPending())
t.Logf("Applying %s migrations", name)
pop.SetLogger(pl(t))
require.NoError(t, p.MigrateUp(context.Background()))
t.Logf("%s migrations applied", name)
status, err := p.MigrationStatus(context.Background())
require.NoError(t, err)
require.False(t, status.HasPending())

var b bytes.Buffer
require.NoError(t, status.Write(&b))
t.Logf("%s", b.String())
l.Lock()
ps[name] = reg
l.Unlock()

ps[name] = reg
t.Logf("Database %s initialized successfully", name)
}(name, dsn)
}

wg.Wait()
return ps
}

Expand All @@ -150,10 +164,53 @@ func TestPersister(t *testing.T) {

for name, reg := range conns {
t.Run(fmt.Sprintf("database=%s", name), func(t *testing.T) {
t.Parallel()

_, p := testhelpers.NewNetwork(t, ctx, reg.Persister())
conf := reg.Config(context.Background())

t.Logf("DSN: %s", conf.DSN())

// This test must remain the first test in the test suite!
t.Run("racy identity creation", func(t *testing.T) {
defaultSchema := schema.Schema{
ID: config.DefaultIdentityTraitsSchemaID,
URL: urlx.ParseOrPanic("file://./stub/identity.schema.json"),
RawURL: "file://./stub/identity.schema.json",
}

var wg sync.WaitGroup
testhelpers.SetDefaultIdentitySchema(reg.Config(context.Background()), defaultSchema.RawURL)
_, ps := testhelpers.NewNetwork(t, ctx, reg.Persister())

for i := 0; i < 10; i++ {
wg.Add(1)
// capture i
ii := i
go func() {
defer wg.Done()

id := ri.NewIdentity("")
id.SetCredentials(ri.CredentialsTypePassword, ri.Credentials{
Type: ri.CredentialsTypePassword,
Identifiers: []string{fmt.Sprintf("racy identity %d", ii)},
Config: sqlxx.JSONRawMessage(`{"foo":"bar"}`),
})
id.Traits = ri.Traits("{}")

require.NoError(t, ps.CreateIdentity(context.Background(), id))
}()
}

wg.Wait()
})

t.Run("case=credentials types", func(t *testing.T) {
for _, ct := range []ri.CredentialsType{ri.CredentialsTypeOIDC, ri.CredentialsTypePassword} {
require.NoError(t, p.(*sql.Persister).Connection(context.Background()).Where("name = ?", ct).First(&ri.CredentialsTypeTable{}))
}
})

t.Run("contract=identity.TestPool", func(t *testing.T) {
pop.SetLogger(pl(t))
identity.TestPool(ctx, conf, p)(t)
Expand Down

0 comments on commit 64ecfc2

Please sign in to comment.