diff --git a/journal/alerting/alerts_test.go b/journal/alerting/alerts_test.go new file mode 100644 index 000000000..46ab4bbbf --- /dev/null +++ b/journal/alerting/alerts_test.go @@ -0,0 +1,61 @@ +package alerting + +import ( + "encoding/json" + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/require" + + "github.com/filecoin-project/lotus/journal" + "github.com/filecoin-project/lotus/journal/mockjournal" +) + +func TestAlerting(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + j := mockjournal.NewMockJournal(mockCtrl) + + a := NewAlertingSystem(j) + + j.EXPECT().RegisterEventType("s1", "b1").Return(journal.EventType{System: "s1", Event: "b1"}) + al1 := a.AddAlertType("s1", "b1") + + j.EXPECT().RegisterEventType("s2", "b2").Return(journal.EventType{System: "s2", Event: "b2"}) + al2 := a.AddAlertType("s2", "b2") + + l := a.GetAlerts() + require.Len(t, l, 2) + require.Equal(t, al1, l[0].Type) + require.Equal(t, al2, l[1].Type) + + for _, alert := range l { + require.False(t, alert.Active) + require.Nil(t, alert.LastActive) + require.Nil(t, alert.LastResolved) + } + + j.EXPECT().RecordEvent(a.alerts[al1].journalType, gomock.Any()) + a.Raise(al1, "test") + + for _, alert := range l { // check for no magic mutations + require.False(t, alert.Active) + require.Nil(t, alert.LastActive) + require.Nil(t, alert.LastResolved) + } + + l = a.GetAlerts() + require.Len(t, l, 2) + require.Equal(t, al1, l[0].Type) + require.Equal(t, al2, l[1].Type) + + require.True(t, l[0].Active) + require.NotNil(t, l[0].LastActive) + require.Equal(t, "raised", l[0].LastActive.Type) + require.Equal(t, json.RawMessage(`"test"`), l[0].LastActive.Message) + require.Nil(t, l[0].LastResolved) + + require.False(t, l[1].Active) + require.Nil(t, l[1].LastActive) + require.Nil(t, l[1].LastResolved) +} diff --git a/journal/mockjournal/journal.go b/journal/mockjournal/journal.go new file mode 100644 index 000000000..3668923e5 --- /dev/null +++ b/journal/mockjournal/journal.go @@ -0,0 +1,75 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/filecoin-project/lotus/journal (interfaces: Journal) + +// Package mockjournal is a generated GoMock package. +package mockjournal + +import ( + reflect "reflect" + + journal "github.com/filecoin-project/lotus/journal" + gomock "github.com/golang/mock/gomock" +) + +// MockJournal is a mock of Journal interface. +type MockJournal struct { + ctrl *gomock.Controller + recorder *MockJournalMockRecorder +} + +// MockJournalMockRecorder is the mock recorder for MockJournal. +type MockJournalMockRecorder struct { + mock *MockJournal +} + +// NewMockJournal creates a new mock instance. +func NewMockJournal(ctrl *gomock.Controller) *MockJournal { + mock := &MockJournal{ctrl: ctrl} + mock.recorder = &MockJournalMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockJournal) EXPECT() *MockJournalMockRecorder { + return m.recorder +} + +// Close mocks base method. +func (m *MockJournal) Close() error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Close") + ret0, _ := ret[0].(error) + return ret0 +} + +// Close indicates an expected call of Close. +func (mr *MockJournalMockRecorder) Close() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockJournal)(nil).Close)) +} + +// RecordEvent mocks base method. +func (m *MockJournal) RecordEvent(arg0 journal.EventType, arg1 func() interface{}) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "RecordEvent", arg0, arg1) +} + +// RecordEvent indicates an expected call of RecordEvent. +func (mr *MockJournalMockRecorder) RecordEvent(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RecordEvent", reflect.TypeOf((*MockJournal)(nil).RecordEvent), arg0, arg1) +} + +// RegisterEventType mocks base method. +func (m *MockJournal) RegisterEventType(arg0, arg1 string) journal.EventType { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RegisterEventType", arg0, arg1) + ret0, _ := ret[0].(journal.EventType) + return ret0 +} + +// RegisterEventType indicates an expected call of RegisterEventType. +func (mr *MockJournalMockRecorder) RegisterEventType(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegisterEventType", reflect.TypeOf((*MockJournal)(nil).RegisterEventType), arg0, arg1) +} diff --git a/journal/types.go b/journal/types.go index 7381a4dc9..af21607bf 100644 --- a/journal/types.go +++ b/journal/types.go @@ -65,6 +65,8 @@ func (et EventType) Enabled() bool { return et.safe && et.enabled } +//go:generate go run github.com/golang/mock/mockgen -destination=mockjournal/journal.go -package=mockjournal . Journal + // Journal represents an audit trail of system actions. // // Every entry is tagged with a timestamp, a system name, and an event name.