Skip to content

Commit

Permalink
alerting: Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Aug 18, 2021
1 parent 432d1f3 commit 36e62c7
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
61 changes: 61 additions & 0 deletions journal/alerting/alerts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package alerting

import (
"encoding/json"
"github.com/filecoin-project/lotus/journal"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"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)
}
75 changes: 75 additions & 0 deletions journal/mockjournal/journal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions journal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 36e62c7

Please sign in to comment.