Skip to content

Commit 254ac08

Browse files
committed
feat: assert audit logs
1 parent 429c1a9 commit 254ac08

File tree

5 files changed

+126
-44
lines changed

5 files changed

+126
-44
lines changed

ferro/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (a *App) Run(args []string) int {
152152
}},
153153
},
154154
Action: func(ctx context.Context, cmd *cli.Command) error {
155-
runCmd := run.CommandMigrateAudit{
155+
runCmd := run.CommandAudit{
156156
Driver: cmd.String("driver"),
157157
Set: cmd.String("set"),
158158
N: uint(cmd.Uint("n")),

ferro/run/runner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type CommandMigrateDown struct {
6565
Version string
6666
}
6767

68-
type CommandMigrateAudit struct {
68+
type CommandAudit struct {
6969
Command
7070

7171
Driver string
@@ -238,7 +238,7 @@ func (r *Runner) ExecuteMigrateStatus(ctx context.Context, cmd *CommandMigrateSt
238238
})
239239
}
240240

241-
func (r *Runner) ExecuteMigrateAudit(ctx context.Context, cmd *CommandMigrateAudit) (*MigrateAuditResult, error) {
241+
func (r *Runner) ExecuteMigrateAudit(ctx context.Context, cmd *CommandAudit) (*MigrateAuditResult, error) {
242242
cfg, err := r.UseConfig()
243243
if err != nil {
244244
return nil, err

ferro/run/runner_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ spec:
9191
_, err := runner.ExecuteMigrateUp(ctx, cmd)
9292
expecto.NoErr(t, "[migrate.up] runner execution", err)
9393

94-
_, err = runner.ExecuteMigrateAudit(ctx, &CommandMigrateAudit{
94+
_, err = runner.ExecuteMigrateAudit(ctx, &CommandAudit{
9595
Driver: db.clientDriverName,
9696
Set: "public",
9797
N: 0,

ferro/spec/action_migrate_up_test.go

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import (
55
)
66

77
func TestActionMigrateUp(t *testing.T) {
8-
cli, teardown := NewTestCLI(t)
9-
defer teardown()
8+
cli, teardown := NewTestCLI(t)
9+
defer teardown()
1010

11-
dbTeardown := cli.RandomDatabase()
12-
defer dbTeardown()
11+
dbTeardown := cli.RandomDatabase()
12+
defer dbTeardown()
1313

14-
cli.Files(
15-
"set.fyml",
16-
`
14+
cli.Files(
15+
"set.fyml",
16+
`
1717
apiVersion: migrations/v1
1818
kind: MigrationSet
1919
metadata:
@@ -24,8 +24,8 @@ spec:
2424
migrations:
2525
- create_animals
2626
`,
27-
"01_create_animals.fyml",
28-
`
27+
"01_create_animals.fyml",
28+
`
2929
apiVersion: migrations/v1
3030
kind: Migration
3131
metadata:
@@ -38,43 +38,63 @@ spec:
3838
down:
3939
sql: DROP TABLE animals;
4040
`,
41-
)
41+
)
4242

43-
cli.AssertRun("migrate", "status", "--set", "public", "--driver", "test")
44-
cli.AssertOutputContains(t, "pending v1 create_animals")
45-
cli.ResetAllOutputs()
43+
cli.AssertRun("migrate", "status", "--set", "public", "--driver", "test")
44+
cli.AssertOutputContains(t, "pending v1 create_animals")
45+
cli.ResetAllOutputs()
4646

47-
cli.AssertRun("migrate", "up", "--set", "public", "--driver", "test")
47+
cli.AssertRun("migrate", "up", "--set", "public", "--driver", "test")
4848
cli.AssertOutputNotContains(t, "No pending migrations")
49-
cli.AssertOutputContains(t, "Applied successfully")
50-
cli.ResetAllOutputs()
49+
cli.AssertOutputContains(t, "Applied successfully")
50+
cli.ResetAllOutputs()
5151

52-
cli.AssertRun("migrate", "up", "--set", "public", "--driver", "test")
52+
cli.AssertRun("migrate", "up", "--set", "public", "--driver", "test")
5353
cli.AssertOutputContains(t, "No pending migrations")
54-
cli.ResetAllOutputs()
54+
cli.ResetAllOutputs()
5555

56-
cli.AssertRun("migrate", "status", "--set", "public", "--driver", "test")
57-
cli.AssertOutputContains(t, "completed v1 create_animals")
58-
cli.ResetAllOutputs()
59-
// cli.AssertSchemaMigrationTable(t, "public", "v1")
60-
// TODO:
61-
// - count records in schema migration table
62-
// - read records in schema migration table
63-
// - check if table exists
64-
// - check if table does not exists
65-
// - fix english with asserts Contains? NotContain? be consistent
56+
cli.AssertRun("migrate", "status", "--set", "public", "--driver", "test")
57+
cli.AssertOutputContains(t, "completed v1 create_animals")
58+
cli.ResetAllOutputs()
6659

67-
// cli.AssertRecordsCount("test", "public", 0)
68-
// ...
69-
// cli.AssertRecordsCount("test", "public", 1)
60+
audit := cli.Audit("test", "public")
61+
audit.AssertCount(2)
62+
audit.Assert(0, auditLog{
63+
ID: 1,
64+
Event: "migration.up.started",
65+
Data: map[string]any{
66+
"migration": "create_animals",
67+
"set": "public",
68+
"version": "v1",
69+
},
70+
Metadata: map[string]any{},
71+
})
72+
audit.Assert(1, auditLog{
73+
ID: 2,
74+
Event: "migration.up.completed",
75+
Data: map[string]any{
76+
"migration": "create_animals",
77+
"set": "public",
78+
"version": "v1",
79+
},
80+
Metadata: map[string]any{},
81+
})
82+
// TODO:
83+
// - check if table exists
84+
// - check if table does not exists
85+
// - fix english with asserts Contains? NotContain? be consistent
7086

71-
// audit := cli.UseAudit("test", "public")
72-
// ...
73-
// audit.AssertEvent(1, "pending")
87+
// cli.AssertRecordsCount("test", "public", 0)
88+
// ...
89+
// cli.AssertRecordsCount("test", "public", 1)
7490

75-
// query := cli.UseQuery("test", "public")
76-
// query.AssertTableExists("animals")
77-
// query.AssertCount("table", 1)
91+
// audit := cli.UseAudit("test", "public")
92+
// ...
93+
// audit.AssertEvent(1, "pending")
94+
95+
// query := cli.UseQuery("test", "public")
96+
// query.AssertTableExists("animals")
97+
// query.AssertCount("table", 1)
7898
}
7999

80100
// func TestActionMigrateUpWithError(t *testing.T) {

ferro/spec/helpers_test.go

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"html/template"
78
"path/filepath"
9+
"reflect"
810
"strings"
911
"testing"
1012

@@ -14,8 +16,10 @@ import (
1416
"github.com/ohkrab/krab/ferro"
1517
"github.com/ohkrab/krab/ferro/config"
1618
"github.com/ohkrab/krab/ferro/plugin"
19+
"github.com/ohkrab/krab/ferro/run"
1720
"github.com/ohkrab/krab/fmtx"
1821
"github.com/ohkrab/krab/plugins"
22+
"github.com/ohkrab/krab/tpls"
1923
"github.com/spf13/afero"
2024
"github.com/stretchr/testify/assert"
2125
"github.com/wzshiming/ctc"
@@ -32,6 +36,11 @@ type cliMock struct {
3236
T *testing.T
3337
}
3438

39+
type assertAudit struct {
40+
result *run.MigrateAuditResult
41+
T *testing.T
42+
}
43+
3544
func NewTestCLI(t *testing.T) (*cliMock, func()) {
3645
dir := t.TempDir()
3746
osfs := afero.NewOsFs()
@@ -221,8 +230,61 @@ func (m *cliMock) ResetDriverOutputs() {
221230
m.connection.recorder = []string{}
222231
}
223232

224-
type versionGeneratorMock struct{}
233+
func (m *cliMock) Runner() *run.Runner {
234+
templates := tpls.New(template.FuncMap{})
235+
registry := plugins.New()
236+
registry.RegisterAll()
237+
filesystem := config.NewFilesystem(m.app.Dir)
238+
return run.New(filesystem, templates, registry, m.app.Logger)
239+
}
240+
241+
func (m *cliMock) Audit(driver string, set string) *assertAudit {
242+
result, err := m.Runner().ExecuteMigrateAudit(context.Background(), &run.CommandAudit{
243+
Driver: driver,
244+
Set: set,
245+
N: 0,
246+
FullView: false,
247+
})
248+
if err != nil {
249+
m.T.Fatalf("can read audit: %v", err)
250+
}
251+
return &assertAudit{
252+
T: m.T,
253+
result: result,
254+
}
255+
}
225256

226-
func (g *versionGeneratorMock) Next() string {
227-
return "20230101"
257+
func (a *assertAudit) AssertCount(count int) {
258+
if count != len(a.result.Logs) {
259+
a.T.Fatalf("expect to have %d audit logs, but got %d", count, len(a.result.Logs))
260+
}
261+
}
262+
263+
type auditLog struct {
264+
ID int64
265+
Event string
266+
Data map[string]any
267+
Metadata map[string]any
268+
}
269+
270+
func (a *assertAudit) Assert(index int, log auditLog) {
271+
if index >= len(a.result.Logs) {
272+
a.T.Fatalf("no enough logs in audit")
273+
}
274+
275+
got := a.result.Logs[index]
276+
want := plugin.DriverAuditLog{
277+
ID: log.ID,
278+
AppliedAt: got.AppliedAt,
279+
Event: log.Event,
280+
Data: log.Data,
281+
Metadata: log.Metadata,
282+
}
283+
284+
if !reflect.DeepEqual(got, want) {
285+
a.T.Logf("audit log[%d] is not same", index)
286+
a.T.Logf(" got:%v", got)
287+
a.T.Logf(" want:%v", want)
288+
a.T.Fail()
289+
}
228290
}

0 commit comments

Comments
 (0)