Skip to content

Commit

Permalink
test: add migration tests (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored Oct 4, 2021
1 parent b4dea9c commit 3b946d0
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/docs/cli/keto-migrate-up.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Migrate the database up
### Synopsis

Run this command on a fresh SQL installation and when you upgrade Ory Keto from
version v0.7.0-alpha.0.pre.0 and later.
version v0.7.0 and later.

It is recommended to run this command close to the SQL instance (e.g. same
subnet) instead of over the public internet. This decreases risk of failure and
Expand Down
6 changes: 1 addition & 5 deletions docs/versions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
[
"v0.7",
"v0.6",
"v0.5"
]
["v0.7", "v0.6", "v0.5"]
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ require (
github.com/ory/herodot v0.9.6
github.com/ory/jsonschema/v3 v3.0.3
github.com/ory/keto/proto v0.0.0-00010101000000-000000000000
github.com/ory/x v0.0.287
github.com/ory/x v0.0.289
github.com/pelletier/go-toml v1.8.1
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/pkg/errors v0.9.1
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
github.com/go-bindata/go-bindata v3.1.1+incompatible h1:tR4f0e4VTO7LK6B2YWyAoVEzG9ByG1wrXB4TL9+jiYg=
github.com/go-bindata/go-bindata v3.1.1+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
Expand Down Expand Up @@ -428,7 +427,6 @@ github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4er
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -867,8 +865,8 @@ github.com/ory/x v0.0.93/go.mod h1:lfcTaGXpTZs7IEQAW00r9EtTCOxD//SiP5uWtNiz31g=
github.com/ory/x v0.0.110/go.mod h1:DJfkE3GdakhshNhw4zlKoRaL/ozg/lcTahA9OCih2BE=
github.com/ory/x v0.0.127/go.mod h1:FwUujfFuCj5d+xgLn4fGMYPnzriR5bdAIulFXMtnK0M=
github.com/ory/x v0.0.205/go.mod h1:A1s4iwmFIppRXZLF3J9GGWeY/HpREVm0Dk5z/787iek=
github.com/ory/x v0.0.287 h1:ubXxLsEOCaIX37KJnr/VgyfHQOQleNO9fmlqhcXK7hg=
github.com/ory/x v0.0.287/go.mod h1:APpShLyJcVzKw1kTgrHI+j/L9YM+8BRjHlcYObc7C1U=
github.com/ory/x v0.0.289 h1:JuPILxVQS0NnHcbmt6HbFGYlrQwPHidqFDRXWfduQH4=
github.com/ory/x v0.0.289/go.mod h1:APpShLyJcVzKw1kTgrHI+j/L9YM+8BRjHlcYObc7C1U=
github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=
github.com/parnurzeal/gorequest v0.2.15/go.mod h1:3Kh2QUMJoqw3icWAecsyzkpY7UzRfDhbRdTjtNwNiUE=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
Expand Down
110 changes: 110 additions & 0 deletions internal/persistence/sql/migrations/migratest/migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package migratest

import (
"context"
"os"
"testing"
"time"

"github.com/gobuffalo/pop/v5"

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

"github.com/gofrs/uuid"
"github.com/ory/x/fsx"
"github.com/ory/x/logrusx"
"github.com/ory/x/networkx"
"github.com/ory/x/popx"
"github.com/ory/x/sqlcon"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ory/keto/internal/driver"
"github.com/ory/keto/internal/persistence/sql"
"github.com/ory/keto/internal/relationtuple"
"github.com/ory/keto/internal/x/dbx"
)

func TestMigrations(t *testing.T) {
const debugOnDisk = false

for _, db := range dbx.GetDSNs(t, debugOnDisk) {
t.Run("dsn="+db.Name, func(t *testing.T) {
db.MigrateUp, db.MigrateDown = false, false

ctx := context.Background()
l := logrusx.New("", "", logrusx.ForceLevel(logrus.DebugLevel))

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

tm := popx.NewTestMigrator(t, c, fsx.Merge(networkx.Migrations, os.DirFS("../sql")), os.DirFS("./testdata"), l)
// cleanup first
require.NoError(t, tm.Down(ctx, -1))

t.Run("suite=up", func(t *testing.T) {
require.NoError(t, tm.Up(ctx))
})

t.Run("suite=fixtures", func(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("SELECT * FROM keto_namespace").Exec()), sqlcon.ErrNoSuchTable)
})

t.Run("table=relation tuples", func(t *testing.T) {
reg := driver.NewTestRegistry(t, db)
require.NoError(t,
reg.Config().Set(config.KeyNamespaces, []*namespace.Namespace{{ID: 1, Name: "foo"}}))

p, err := sql.NewPersister(reg, uuid.Must(uuid.FromString("77fdc5e0-2260-49da-8aae-c36ba255d05b")))
require.NoError(t, err)
rts, next, err := p.GetRelationTuples(context.Background(), &relationtuple.RelationQuery{Namespace: "foo"})
require.NoError(t, err)
assert.Equal(t, "", next)

for _, rt := range []*relationtuple.InternalRelationTuple{
{
Namespace: "foo",
Object: "object",
Relation: "relation",
Subject: &relationtuple.SubjectID{ID: "user"},
},
{
Namespace: "foo",
Object: "object",
Relation: "relation",
Subject: &relationtuple.SubjectSet{
Namespace: "foo",
Object: "s_object",
Relation: "s_relation",
},
},
} {
assert.Contains(t, rts, rt)
}
})
})

t.Run("suite=down", func(t *testing.T) {
if debugOnDisk && db.Name == "sqlite" {
t.SkipNow()
}
require.NoError(t, tm.Down(ctx, -1))
})
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO keto_namespace (id, schema_version) VALUES (1, 1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
INSERT INTO keto_relation_tuples (shard_id, nid, namespace_id, object, relation, subject_id, subject_set_namespace_id,
subject_set_object, subject_set_relation, commit_time)
VALUES ('7a9d6df4-3b60-4cae-996e-d15d78b0fc36', '77fdc5e0-2260-49da-8aae-c36ba255d05b', 1, 'object', 'relation', 'user',
NULL, NULL, NULL, '2021-09-30 15:50:52');

INSERT INTO keto_relation_tuples (shard_id, nid, namespace_id, object, relation, subject_id, subject_set_namespace_id,
subject_set_object, subject_set_relation, commit_time)
VALUES ('1457d171-4473-4c77-bd88-0809529c599f', '77fdc5e0-2260-49da-8aae-c36ba255d05b', 1, 'object', 'relation', NULL,
1, 's_object', 's_relation', '2021-09-30 15:50:52');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- DROP TABLE statement only, nothing to insert

0 comments on commit 3b946d0

Please sign in to comment.