watcher: add tests for EventWatcher
This commit is contained in:
parent
196bdde70f
commit
256351013b
|
@ -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(),
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue