zitadel/internal/command/debug_events_test.go
Tim Möhlmann 3aba942162
feat: add debug events API (#8533)
# Which Problems Are Solved

Add a debug API which allows pushing a set of events to be reduced in a
dedicated projection.
The events can carry a sleep duration which simulates a slow query
during projection handling.

# How the Problems Are Solved

- `CreateDebugEvents` allows pushing multiple events which simulate the
lifecycle of a resource. Each event has a `projectionSleep` field, which
issues a `pg_sleep()` statement query in the projection handler :
  - Add
  - Change
  - Remove
- `ListDebugEventsStates` list the current state of the projection,
optionally with a Trigger
- `GetDebugEventsStateByID` get the current state of the aggregate ID in
the projection, optionally with a Trigger


# Additional Changes

- none

# Additional Context

-  Allows reproduction of https://github.com/zitadel/zitadel/issues/8517
2024-09-11 08:24:00 +00:00

341 lines
7.8 KiB
Go

package command
import (
"io"
"testing"
"time"
"github.com/muhlemmer/gu"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/repository/debug_events"
"github.com/zitadel/zitadel/internal/zerrors"
)
func TestCommands_CreateDebugEvents(t *testing.T) {
ctx := authz.NewMockContextWithPermissions("instance1", "org1", "user1", nil)
type fields struct {
eventstore func(*testing.T) *eventstore.Eventstore
}
type args struct {
dbe *DebugEvents
}
tests := []struct {
name string
fields fields
args args
want *domain.ObjectDetails
wantErr error
}{
{
name: "filter error",
fields: fields{
eventstore: expectEventstore(
expectFilterError(io.ErrClosedPipe),
),
},
args: args{&DebugEvents{
AggregateID: "dbg1",
Events: []DebugEvent{
DebugEventAdded{
ProjectionSleep: time.Millisecond,
Blob: gu.Ptr("a"),
},
},
}},
wantErr: io.ErrClosedPipe,
},
{
name: "already exists",
fields: fields{
eventstore: expectEventstore(
expectFilter(
eventFromEventPusher(
debug_events.NewAddedEvent(
ctx, debug_events.NewAggregate("dbg1", "instance1"),
time.Millisecond, gu.Ptr("a"),
),
),
),
),
},
args: args{&DebugEvents{
AggregateID: "dbg1",
Events: []DebugEvent{
DebugEventAdded{
ProjectionSleep: time.Millisecond,
Blob: gu.Ptr("a"),
},
},
}},
wantErr: zerrors.ThrowAlreadyExists(nil, "COMMAND-Aex6j", "debug aggregate already exists"),
},
{
name: "double added event, already exists",
fields: fields{
eventstore: expectEventstore(
expectFilter(),
),
},
args: args{&DebugEvents{
AggregateID: "dbg1",
Events: []DebugEvent{
DebugEventAdded{
ProjectionSleep: time.Millisecond,
Blob: gu.Ptr("a"),
},
DebugEventAdded{
ProjectionSleep: time.Millisecond,
Blob: gu.Ptr("a"),
},
},
}},
wantErr: zerrors.ThrowAlreadyExists(nil, "COMMAND-Aex6j", "debug aggregate already exists"),
},
{
name: "changed event, not found",
fields: fields{
eventstore: expectEventstore(
expectFilter(),
),
},
args: args{&DebugEvents{
AggregateID: "dbg1",
Events: []DebugEvent{
DebugEventChanged{
ProjectionSleep: time.Millisecond,
Blob: gu.Ptr("a"),
},
},
}},
wantErr: zerrors.ThrowNotFound(nil, "COMMAND-Thie6", "debug aggregate not found"),
},
{
name: "removed event, not found",
fields: fields{
eventstore: expectEventstore(
expectFilter(),
),
},
args: args{&DebugEvents{
AggregateID: "dbg1",
Events: []DebugEvent{
DebugEventRemoved{
ProjectionSleep: time.Millisecond,
},
},
}},
wantErr: zerrors.ThrowNotFound(nil, "COMMAND-Ohna9", "debug aggregate not found"),
},
{
name: "changed after removed event, not found",
fields: fields{
eventstore: expectEventstore(
expectFilter(
eventFromEventPusher(
debug_events.NewAddedEvent(
ctx, debug_events.NewAggregate("dbg1", "instance1"),
time.Millisecond, gu.Ptr("a"),
),
),
),
),
},
args: args{&DebugEvents{
AggregateID: "dbg1",
Events: []DebugEvent{
DebugEventRemoved{
ProjectionSleep: time.Millisecond,
},
DebugEventChanged{
ProjectionSleep: time.Millisecond,
Blob: gu.Ptr("a"),
},
},
}},
wantErr: zerrors.ThrowNotFound(nil, "COMMAND-Thie6", "debug aggregate not found"),
},
{
name: "double removed event, not found",
fields: fields{
eventstore: expectEventstore(
expectFilter(
eventFromEventPusher(
debug_events.NewAddedEvent(
ctx, debug_events.NewAggregate("dbg1", "instance1"),
time.Millisecond, gu.Ptr("a"),
),
),
),
),
},
args: args{&DebugEvents{
AggregateID: "dbg1",
Events: []DebugEvent{
DebugEventRemoved{
ProjectionSleep: time.Millisecond,
},
DebugEventRemoved{
ProjectionSleep: time.Millisecond,
},
},
}},
wantErr: zerrors.ThrowNotFound(nil, "COMMAND-Ohna9", "debug aggregate not found"),
},
{
name: "added, ok",
fields: fields{
eventstore: expectEventstore(
expectFilter(),
expectPush(
debug_events.NewAddedEvent(
ctx, debug_events.NewAggregate("dbg1", "instance1"),
time.Millisecond, gu.Ptr("a"),
),
),
),
},
args: args{&DebugEvents{
AggregateID: "dbg1",
Events: []DebugEvent{
DebugEventAdded{
ProjectionSleep: time.Millisecond,
Blob: gu.Ptr("a"),
},
},
}},
want: &domain.ObjectDetails{
ResourceOwner: "instance1",
},
},
{
name: "changed, ok",
fields: fields{
eventstore: expectEventstore(
expectFilter(
eventFromEventPusher(
debug_events.NewAddedEvent(
ctx, debug_events.NewAggregate("dbg1", "instance1"),
time.Millisecond, gu.Ptr("a"),
),
),
),
expectPush(
debug_events.NewChangedEvent(
ctx, debug_events.NewAggregate("dbg1", "instance1"),
time.Millisecond, gu.Ptr("b"),
),
),
),
},
args: args{&DebugEvents{
AggregateID: "dbg1",
Events: []DebugEvent{
DebugEventChanged{
ProjectionSleep: time.Millisecond,
Blob: gu.Ptr("b"),
},
},
}},
want: &domain.ObjectDetails{
ResourceOwner: "instance1",
},
},
{
name: "removed, ok",
fields: fields{
eventstore: expectEventstore(
expectFilter(
eventFromEventPusher(
debug_events.NewAddedEvent(
ctx, debug_events.NewAggregate("dbg1", "instance1"),
time.Millisecond, gu.Ptr("a"),
),
),
),
expectPush(
debug_events.NewRemovedEvent(
ctx, debug_events.NewAggregate("dbg1", "instance1"),
time.Millisecond,
),
),
),
},
args: args{&DebugEvents{
AggregateID: "dbg1",
Events: []DebugEvent{
DebugEventRemoved{
ProjectionSleep: time.Millisecond,
},
},
}},
want: &domain.ObjectDetails{
ResourceOwner: "instance1",
},
},
{
name: "added, changed, changed, removed ok",
fields: fields{
eventstore: expectEventstore(
expectFilter(),
expectPush(
debug_events.NewAddedEvent(
ctx, debug_events.NewAggregate("dbg1", "instance1"),
time.Millisecond, gu.Ptr("a"),
),
debug_events.NewChangedEvent(
ctx, debug_events.NewAggregate("dbg1", "instance1"),
time.Millisecond, gu.Ptr("b"),
),
debug_events.NewChangedEvent(
ctx, debug_events.NewAggregate("dbg1", "instance1"),
time.Millisecond, gu.Ptr("c"),
),
debug_events.NewRemovedEvent(
ctx, debug_events.NewAggregate("dbg1", "instance1"),
time.Millisecond,
),
),
),
},
args: args{&DebugEvents{
AggregateID: "dbg1",
Events: []DebugEvent{
DebugEventAdded{
ProjectionSleep: time.Millisecond,
Blob: gu.Ptr("a"),
},
DebugEventChanged{
ProjectionSleep: time.Millisecond,
Blob: gu.Ptr("b"),
},
DebugEventChanged{
ProjectionSleep: time.Millisecond,
Blob: gu.Ptr("c"),
},
DebugEventRemoved{
ProjectionSleep: time.Millisecond,
},
},
}},
want: &domain.ObjectDetails{
ResourceOwner: "instance1",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Commands{
eventstore: tt.fields.eventstore(t),
}
got, err := c.CreateDebugEvents(ctx, tt.args.dbe)
require.ErrorIs(t, err, tt.wantErr)
assert.Equal(t, tt.want, got)
})
}
}