Skip to content

Commit

Permalink
test: use isolated databases to parallelize all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik committed May 30, 2022
1 parent 62b2ad2 commit bc09032
Show file tree
Hide file tree
Showing 36 changed files with 939 additions and 153 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules/
coverage.txt
dist/
**/*.sqlite
**/*.sqlite-journal
.vscode/
9 changes: 6 additions & 3 deletions cmd/expand/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/ory/x/cmdx"

"github.com/stretchr/testify/assert"

"github.com/ory/keto/cmd/client"
Expand All @@ -18,12 +17,16 @@ func TestExpandCommand(t *testing.T) {

t.Run("case=unknown tuple", func(t *testing.T) {
t.Run("format=JSON", func(t *testing.T) {
stdOut := ts.Cmd.ExecNoErr(t, "access", nspace.Name, "object", "--"+cmdx.FlagFormat, string(cmdx.FormatJSON))
stdOut := ts.Cmd.ExecNoErr(t,
"access", nspace.Name,
"object", "--"+cmdx.FlagFormat, string(cmdx.FormatJSON))
assert.Equal(t, "null\n", stdOut)
})

t.Run("format=default", func(t *testing.T) {
stdOut := ts.Cmd.ExecNoErr(t, "access", nspace.Name, "object", "--"+cmdx.FlagFormat, string(cmdx.FormatDefault))
stdOut := ts.Cmd.ExecNoErr(t,
"access", nspace.Name,
"object", "--"+cmdx.FlagFormat, string(cmdx.FormatDefault))
assert.Contains(t, stdOut, "empty tree")
})
})
Expand Down
3 changes: 1 addition & 2 deletions cmd/migrate/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"fmt"
"strconv"

"github.com/ory/keto/internal/driver"

"github.com/ory/x/cmdx"
"github.com/ory/x/flagx"
"github.com/ory/x/popx"
"github.com/spf13/cobra"

"github.com/ory/keto/internal/driver"
"github.com/ory/keto/ketoctx"
)

Expand Down
22 changes: 17 additions & 5 deletions cmd/migrate/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func assertNoneApplied(t *testing.T, status string) {
}

func TestMigrate(t *testing.T) {
t.Parallel()

nspaces := []*namespace.Namespace{
{
Name: "default",
Expand All @@ -57,8 +59,11 @@ func TestMigrate(t *testing.T) {
}

for _, dsn := range dbx.GetDSNs(t, false) {
dsn := dsn
if dbal.IsMemorySQLite(dsn.Conn) {
t.Run("dsn=memory", func(t *testing.T) {
t.Parallel()

t.Run("case=auto migrates", func(t *testing.T) {
hook := &test.Hook{}
ctx, cancel := context.WithCancel(context.WithValue(context.Background(), driver.LogrusHookContextKey, hook))
Expand All @@ -78,6 +83,8 @@ func TestMigrate(t *testing.T) {
})
} else {
t.Run("dsn="+dsn.Name, func(t *testing.T) {
t.Parallel()

hook := &test.Hook{}
ctx, cancel := context.WithCancel(context.WithValue(context.Background(), driver.LogrusHookContextKey, hook))
t.Cleanup(cancel)
Expand Down Expand Up @@ -105,7 +112,7 @@ func TestMigrate(t *testing.T) {

t.Cleanup(func() {
// migrate all down
t.Log(cmd.ExecNoErr(t, "down", "0", "--"+FlagYes))
t.Logf("cleanup:\n%s\n", cmd.ExecNoErr(t, "down", "0", "--"+FlagYes))
})

parts := strings.Split(stdOut, "Are you sure that you want to apply this migration?")
Expand All @@ -120,7 +127,7 @@ func TestMigrate(t *testing.T) {

t.Cleanup(func() {
// migrate all down
t.Log(cmd.ExecNoErr(t, "down", "0", "--"+FlagYes))
t.Logf("cleanup:\n%s\n", cmd.ExecNoErr(t, "down", "0", "--"+FlagYes))
})

parts := strings.Split(out, "Applying migrations...")
Expand All @@ -135,6 +142,8 @@ func TestMigrate(t *testing.T) {
}

func TestUpAndDown(t *testing.T) {
t.Parallel()

const debugOnDisk = false

ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -149,9 +158,12 @@ func TestUpAndDown(t *testing.T) {
Ctx: ctx,
}
for _, dsn := range dbx.GetDSNs(t, debugOnDisk) {
cf := dbx.ConfigFile(t, map[string]interface{}{config.KeyDSN: dsn.Conn})
dsn := dsn
t.Run("dsn="+dsn.Name, func(t *testing.T) {
cf := dbx.ConfigFile(t, map[string]interface{}{config.KeyDSN: dsn.Conn})

t.Log(cmd.ExecNoErr(t, "up", "-c", cf, "--"+FlagYes))
t.Log(cmd.ExecNoErr(t, "down", "0", "-c", cf, "--"+FlagYes))
t.Log(cmd.ExecNoErr(t, "up", "-c", cf, "--"+FlagYes))
t.Log(cmd.ExecNoErr(t, "down", "0", "-c", cf, "--"+FlagYes))
})
}
}
5 changes: 2 additions & 3 deletions cmd/migrate/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package migrate
import (
"fmt"

"github.com/ory/keto/internal/driver"

"github.com/ory/x/cmdx"
"github.com/ory/x/popx"
"github.com/spf13/cobra"

"github.com/ory/keto/internal/driver"
"github.com/ory/keto/ketoctx"
)

Expand All @@ -21,7 +20,7 @@ func newStatusCmd(opts []ketoctx.Option) *cobra.Command {
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()

reg, err := driver.NewDefaultRegistry(cmd.Context(), cmd.Flags(), true, opts...)
reg, err := driver.NewDefaultRegistry(ctx, cmd.Flags(), true, opts...)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/migrate/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package migrate
import (
"fmt"

"github.com/ory/keto/internal/driver"

"github.com/ory/x/cmdx"
"github.com/ory/x/flagx"
"github.com/ory/x/popx"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/ory/keto/internal/driver"
"github.com/ory/keto/ketoctx"
)

Expand All @@ -35,7 +34,7 @@ Before running this command on an existing database, create a back up!
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()

reg, err := driver.NewDefaultRegistry(cmd.Context(), cmd.Flags(), true, opts...)
reg, err := driver.NewDefaultRegistry(ctx, cmd.Flags(), true, opts...)
if err != nil {
return err
}
Expand Down
17 changes: 12 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ 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
Expand All @@ -37,7 +38,7 @@ require (
github.com/ory/herodot v0.9.13
github.com/ory/jsonschema/v3 v3.0.7
github.com/ory/keto/proto v0.0.0-00010101000000-000000000000
github.com/ory/x v0.0.375
github.com/ory/x v0.0.389
github.com/pelletier/go-toml v1.9.4
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/pkg/errors v0.9.1
Expand All @@ -52,7 +53,7 @@ require (
github.com/tidwall/sjson v1.2.4
github.com/urfave/negroni v1.0.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.30.0
go.opentelemetry.io/otel v1.6.3
go.opentelemetry.io/otel v1.7.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
google.golang.org/grpc v1.45.0
google.golang.org/protobuf v1.28.0
Expand Down Expand Up @@ -96,7 +97,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 All @@ -116,6 +116,7 @@ require (
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.0.0 // indirect
github.com/hashicorp/go-retryablehttp v0.7.0 // indirect
Expand Down Expand Up @@ -156,6 +157,7 @@ require (
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.1.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/openzipkin/zipkin-go v0.4.0 // indirect
github.com/ory/dockertest/v3 v3.8.1 // indirect
github.com/ory/go-acc v0.2.6 // indirect
github.com/ory/viper v1.7.5 // indirect
Expand Down Expand Up @@ -191,10 +193,15 @@ require (
go.opentelemetry.io/contrib/propagators/jaeger v1.4.0 // indirect
go.opentelemetry.io/contrib/samplers/jaegerremote v0.0.0-20220314184135-32895002a444 // indirect
go.opentelemetry.io/otel/exporters/jaeger v1.5.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.6.3 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.6.3 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.6.3 // indirect
go.opentelemetry.io/otel/exporters/zipkin v1.7.0 // indirect
go.opentelemetry.io/otel/internal/metric v0.27.0 // indirect
go.opentelemetry.io/otel/metric v0.27.0 // indirect
go.opentelemetry.io/otel/sdk v1.6.3 // indirect
go.opentelemetry.io/otel/trace v1.6.3 // indirect
go.opentelemetry.io/otel/sdk v1.7.0 // indirect
go.opentelemetry.io/otel/trace v1.7.0 // indirect
go.opentelemetry.io/proto/otlp v0.15.0 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/mod v0.5.1 // indirect
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
Expand Down
Loading

0 comments on commit bc09032

Please sign in to comment.