watcher: add tests for EventWatcher

This commit is contained in:
Paul Stemmet 2022-12-09 12:33:33 +00:00
parent 196bdde70f
commit 256351013b
Signed by: Paul Stemmet
GPG Key ID: EDEA539F594E7E75
1 changed files with 129 additions and 0 deletions

129
watcher/watcher_test.go Normal file
View File

@ -0,0 +1,129 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package watcher_test
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"git.st8l.com/luxolus/kdnotify/config"
mfifo "git.st8l.com/luxolus/kdnotify/fifo/mock"
n "git.st8l.com/luxolus/kdnotify/schema/notify"
"git.st8l.com/luxolus/kdnotify/schema/notify/state"
"git.st8l.com/luxolus/kdnotify/schema/notify/ty"
"git.st8l.com/luxolus/kdnotify/watcher"
mwatcher "git.st8l.com/luxolus/kdnotify/watcher/mock"
)
func Test_EventWatcher(t *testing.T) {
t.Parallel()
cases := []struct {
Name string
Events []n.VrrpMessage
Expected []n.VrrpMessage
ShouldError bool
HandlerFunc func(t *testing.T, expected, actual *n.VrrpMessage) bool
}{
{
Name: "NoEvents",
},
{
Name: "InputEqualOutput",
Events: []n.VrrpMessage{
{
Message: n.Message{
Type: ty.INSTANCE,
State: state.MASTER,
Instance: "buzz_instance",
},
},
{
Message: n.Message{
Type: ty.GROUP,
State: state.BACKUP,
Instance: "wuz_group",
},
},
{
Message: n.Message{
Type: ty.INSTANCE,
State: state.STOP,
Instance: "blah_instance",
},
},
},
Expected: []n.VrrpMessage{
{
Message: n.Message{
Type: ty.INSTANCE,
State: state.MASTER,
Instance: "buzz_instance",
},
},
{
Message: n.Message{
Type: ty.GROUP,
State: state.BACKUP,
Instance: "wuz_group",
},
},
{
Message: n.Message{
Type: ty.INSTANCE,
State: state.STOP,
Instance: "blah_instance",
},
},
},
},
}
for i, test := range cases {
test := test // Avoid closure capture by ref
tname := fmt.Sprintf("%s@%d:%d", test.Name, i+1, len(cases))
t.Run(tname, func(t *testing.T) {
t.Parallel()
hfunc := test.HandlerFunc
if hfunc == nil {
hfunc = CompareVrrpMessage
}
if test.ShouldError {
hfunc = func(t *testing.T, expected, actual *n.VrrpMessage) bool {
return !hfunc(t, expected, actual)
}
}
events := mfifo.NewMockEvents(test.Events...)
watcher := watcher.NewWatcher(mkTestCxt(), &events)
handler := mwatcher.MockHandler{
T: t,
Expected: test.Expected,
HandlerFunc: hfunc,
}
watcher.Watch(&handler)
})
}
}
func CompareVrrpMessage(t *testing.T, expected, actual *n.VrrpMessage) bool {
return assert.Equal(t, *expected, *actual)
}
func mkTestCxt() *config.LibCxt {
return &config.LibCxt{
Context: context.TODO(),
Logger: zap.NewNop(),
}
}