Skip to content

Commit

Permalink
test: add initial e2e test (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored Dec 21, 2020
1 parent 185ee1e commit dc5d3c9
Show file tree
Hide file tree
Showing 43 changed files with 1,123 additions and 565 deletions.
10 changes: 7 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ jobs:
image: circleci/golang:1.15
environment:
TEST_DATABASE_POSTGRESQL: postgres://test:test@localhost:5432/keto?sslmode=disable
TEST_DATABASE_MYSQL: root:test@(localhost:3306)/mysql?parseTime=true
TEST_DATABASE_MYSQL: mysql://root:test@(localhost:3306)/mysql?parseTime=true&multiStatements=true
TEST_DATABASE_COCKROACHDB: cockroach://root@localhost:26257/defaultdb?sslmode=disable
-
image: postgres:9.5
image: postgres:11.8
environment:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
Expand All @@ -27,6 +28,9 @@ jobs:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: test
-
image: cockroachdb/cockroach:v20.1.0
command: start --insecure
working_directory: /go/src/github.com/ory/keto
steps:
- checkout
Expand All @@ -41,7 +45,7 @@ jobs:

# Tests
-
run: go test -tags sqlite -race -short -v $(go list ./... | grep -v cmd)
run: go test -tags sqlite -race -short -v ./...
-
run: go-acc -o coverage.txt ./... -- -v -tags sqlite

Expand Down
79 changes: 53 additions & 26 deletions .schema/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,35 +197,62 @@
},
"serve": {
"type": "object",
"title": "HTTP REST API",
"additionalProperties": false,
"properties": {
"port": {
"type": "integer",
"default": 4456,
"title": "Port",
"description": "The port to listen on.",
"minimum": 1,
"maximum": 65535,
"examples": [
4456
]
},
"host": {
"type": "string",
"default": "",
"examples": [
"localhost",
"127.0.0.1"
],
"title": "Host",
"description": "The network interface to listen on."
},
"cors": {
"$ref": "#/definitions/cors"
"rest": {
"type": "object",
"title": "HTTP REST API",
"additionalProperties": false,
"properties": {
"port": {
"type": "integer",
"default": 4466,
"title": "Port",
"description": "The port to listen on.",
"minimum": 1,
"maximum": 65535
},
"host": {
"type": "string",
"default": "",
"examples": [
"localhost",
"127.0.0.1"
],
"title": "Host",
"description": "The network interface to listen on."
},
"cors": {
"$ref": "#/definitions/cors"
},
"tls": {
"$ref": "#/definitions/tlsx"
}
}
},
"tls": {
"$ref": "#/definitions/tlsx"
"grpc": {
"type": "object",
"additionalProperties": false,
"properties": {
"port": {
"type": "integer",
"default": 4467,
"title": "Port",
"description": "The port to listen on.",
"minimum": 1,
"maximum": 65535
},
"host": {
"type": "string",
"default": "",
"examples": [
"localhost",
"127.0.0.1"
],
"title": "Host",
"description": "The network interface to listen on."
}
}
}
}
},
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,11 @@ buf-lint: deps
#
.PHONY: buf
buf: buf-lint buf-gen

.PHONY: reset-testdb
reset-testdb:
source scripts/test-resetdb.sh

.PHONY: test-e2e
test-e2e:
go test -tags sqlite -failfast -v ./internal/e2e
40 changes: 40 additions & 0 deletions cmd/migrate/down.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package migrate

import (
"fmt"
"strconv"

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

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

func newDownCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "down <steps>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

steps, err := strconv.ParseInt(args[0], 0, 0)
if err != nil {
// return this error so it gets printed along the usage
return fmt.Errorf("malformed argument %s for <steps>: %+v", args[0], err)
}

reg, err := driver.NewDefaultRegistry(ctx, cmd.Flags())
if err != nil {
return err
}
if err := reg.Migrator().MigrateDown(ctx, int(steps)); err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could apply down migrations: %+v\n", err)
return cmdx.FailSilently(cmd)
}

return nil
},
}

return cmd
}
16 changes: 12 additions & 4 deletions cmd/migrate/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ package migrate

import "github.com/spf13/cobra"

var migrateCmd = &cobra.Command{
Use: "migrate",
func newMigrateCmd() *cobra.Command {
return &cobra.Command{
Use: "migrate",
}
}

func RegisterCommandRecursive(parent *cobra.Command) {
migrateCmd.AddCommand(newStatusCmd(), newUpCmd())
func RegisterCommandsRecursive(parent *cobra.Command) {
migrateCmd := newMigrateCmd()

migrateCmd.AddCommand(
newStatusCmd(),
newUpCmd(),
newDownCmd(),
)

parent.AddCommand(migrateCmd)
}
10 changes: 5 additions & 5 deletions cmd/migrate/status.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package migrate

import (
"context"
"fmt"

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

"github.com/ory/keto/internal/driver"
Expand All @@ -15,10 +13,12 @@ func newStatusCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
RunE: func(cmd *cobra.Command, _ []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := cmd.Context()

reg := driver.NewDefaultRegistry(ctx, logrusx.New("keto", "test"), cmd.Flags(), "test", "adf", "today")
reg, err := driver.NewDefaultRegistry(ctx, cmd.Flags())
if err != nil {
return err
}
if err := reg.Migrator().MigrationStatus(ctx, cmd.OutOrStdout()); err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not get migration status: %+v\n", err)
return cmdx.FailSilently(cmd)
Expand Down
26 changes: 21 additions & 5 deletions cmd/migrate/up.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
package migrate

import (
"context"
"fmt"

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

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

const FlagYes = "yes"

func newUpCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "up",
RunE: func(cmd *cobra.Command, _ []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := cmd.Context()

reg, err := driver.NewDefaultRegistry(ctx, cmd.Flags())
if err != nil {
return err
}
if err := reg.Migrator().MigrationStatus(ctx, cmd.OutOrStdout()); err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not get migration status: %+v\n", err)
return cmdx.FailSilently(cmd)
}

if !flagx.MustGetBool(cmd, FlagYes) && !cmdx.AskForConfirmation("Do you want to apply above planned migrations?", cmd.InOrStdin(), cmd.OutOrStdout()) {
_, _ = fmt.Fprintln(cmd.OutOrStdout(), "Aborting")
return nil
}

reg := driver.NewDefaultRegistry(ctx, logrusx.New("keto", "test"), cmd.Flags(), "test", "adf", "today")
if err := reg.Migrator().MigrateUp(ctx); err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not apply migrations: %+v\n", err)
return cmdx.FailSilently(cmd)
}
return nil
},
}

cmd.Flags().BoolP(FlagYes, "y", false, "yes to all questions, no user input required")

return cmd
}
50 changes: 50 additions & 0 deletions cmd/namespace/migrate_down.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package namespace

import (
"fmt"

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

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

func NewMigrateDownCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "down <namespace-name> <steps>",
Short: "Migrate a namespace down.",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

reg, err := driver.NewDefaultRegistry(ctx, cmd.Flags())
if err != nil {
return err
}

nm, err := reg.Config().NamespaceManager()
if err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not initialize the namespace manager: %+v\n", err)
return cmdx.FailSilently(cmd)
}

n, err := nm.GetNamespace(ctx, args[0])
if err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not find the namespace with name \"%s\": %+v\n", args[0], err)
return cmdx.FailSilently(cmd)
}

if err := reg.NamespaceMigrator().MigrateNamespaceDown(ctx, n, 0); err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not apply namespace migration: %+v\n", err)
return cmdx.FailSilently(cmd)
}

return nil
},
}

registerYesFlag(cmd.Flags())
registerPackageFlags(cmd.Flags())

return cmd
}
25 changes: 16 additions & 9 deletions cmd/namespace/migrate.go → cmd/namespace/migrate_up.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
package namespace

import (
"context"
"errors"
"fmt"

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

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

func NewMigrateCmd() *cobra.Command {
func NewMigrateUpCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "migrate <namespaces.yml>",
Use: "up <namespace-name>",
Short: "Migrate a namespace up.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := cmd.Context()

reg := driver.NewDefaultRegistry(ctx, logrusx.New("keto", "master"), cmd.Flags(), "master", "local", "today")

n, err := validateNamespaceFile(cmd, args[0])
reg, err := driver.NewDefaultRegistry(ctx, cmd.Flags())
if err != nil {
return err
}

nm, err := reg.Config().NamespaceManager()
if err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not initialize the namespace manager: %+v\n", err)
return cmdx.FailSilently(cmd)
}

n, err := nm.GetNamespace(ctx, args[0])
if err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not find the namespace with name \"%s\": %+v\n", args[0], err)
return cmdx.FailSilently(cmd)
}

status, err := reg.NamespaceMigrator().NamespaceStatus(ctx, n.ID)
if err != nil {
if !errors.Is(err, persistence.ErrNamespaceUnknown) {
Expand Down
Loading

0 comments on commit dc5d3c9

Please sign in to comment.