/
migrate_test.go
170 lines (137 loc) · 4.33 KB
/
migrate_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package migrate
import (
"bytes"
"context"
"strings"
"testing"
"github.com/ory/x/dbal"
"github.com/ory/keto/internal/x/dbx"
"github.com/ory/x/cmdx"
"github.com/ory/x/configx"
"github.com/sirupsen/logrus/hooks/test"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ory/keto/internal/driver"
"github.com/ory/keto/internal/driver/config"
"github.com/ory/keto/internal/namespace"
)
func assertAllApplied(t *testing.T, status string) {
assert.NotContains(t, status, "Pending")
assert.Contains(t, status, "Applied")
}
func assertNoneApplied(t *testing.T, status string) {
assert.Contains(t, status, "Pending")
assert.NotContains(t, status, "Applied")
}
func TestMigrate(t *testing.T) {
t.Parallel()
nspaces := []*namespace.Namespace{
{
Name: "default",
},
{
Name: "other",
},
}
newCmd := func(ctx context.Context, persistentArgs ...string) *cmdx.CommandExecuter {
return &cmdx.CommandExecuter{
New: func() *cobra.Command {
cmd := newMigrateCmd(nil)
configx.RegisterFlags(cmd.PersistentFlags())
return cmd
},
Ctx: ctx,
PersistentArgs: persistentArgs,
}
}
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))
t.Cleanup(cancel)
cf := dbx.ConfigFile(t, map[string]interface{}{
config.KeyDSN: dsn.Conn,
config.KeyNamespaces: nspaces,
"log.level": "debug",
})
cmd := newCmd(ctx, "-c", cf)
out := cmd.ExecNoErr(t, "up", "--"+FlagYes)
assert.Contains(t, out, "All migrations are already applied, there is nothing to do.")
})
})
} 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)
cf := dbx.ConfigFile(t, map[string]interface{}{
config.KeyDSN: dsn.Conn,
config.KeyNamespaces: nspaces,
"log.level": "debug",
})
cmd := newCmd(ctx, "-c", cf)
t.Run("case=aborts on no", func(t *testing.T) {
stdOut, stdErr, err := cmd.Exec(bytes.NewBufferString("n\n"), "up")
require.NoError(t, err, "%s %s", stdOut, stdErr)
assert.Contains(t, stdOut, "Pending", "%s %s", stdOut, stdErr)
assert.NotContains(t, stdOut, "Applied", "%s %s", stdOut, stdErr)
assert.Contains(t, stdOut, "Aborting", "%s %s", stdOut, stdErr)
})
t.Run("case=applies on yes input", func(t *testing.T) {
stdOut, stdErr, err := cmd.Exec(bytes.NewBufferString("y\n"), "up")
require.NoError(t, err, "%s %s", stdOut, stdErr)
t.Cleanup(func() {
// migrate all down
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?")
require.Len(t, parts, 2)
assertNoneApplied(t, parts[0])
assertAllApplied(t, parts[1])
})
t.Run("case=applies on yes flag", func(t *testing.T) {
out := cmd.ExecNoErr(t, "up", "--"+FlagYes)
t.Cleanup(func() {
// migrate all down
t.Logf("cleanup:\n%s\n", cmd.ExecNoErr(t, "down", "0", "--"+FlagYes))
})
parts := strings.Split(out, "Applying migrations...")
require.Len(t, parts, 2)
assertNoneApplied(t, parts[0])
assertAllApplied(t, parts[1])
})
})
}
}
}
func TestUpAndDown(t *testing.T) {
t.Parallel()
const debugOnDisk = false
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cmd := &cmdx.CommandExecuter{
New: func() *cobra.Command {
cmd := newMigrateCmd(nil)
configx.RegisterFlags(cmd.PersistentFlags())
return cmd
},
Ctx: ctx,
}
for _, dsn := range dbx.GetDSNs(t, debugOnDisk) {
dsn := dsn
t.Run("dsn="+dsn.Name, func(t *testing.T) {
cf := dbx.ConfigFile(t, map[string]interface{}{
config.KeyDSN: dsn.Conn,
config.KeyNamespaces: []*namespace.Namespace{},
})
t.Log(cmd.ExecNoErr(t, "up", "-c", cf, "--"+FlagYes))
t.Log(cmd.ExecNoErr(t, "down", "0", "-c", cf, "--"+FlagYes))
})
}
}