-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package endpoint | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"bou.ke/monkey" | ||
Check failure on line 8 in pkg/watchers/endpoint/watcher_test.go
|
||
"github.com/microsoft/retina/pkg/log" | ||
"github.com/stretchr/testify/require" | ||
"github.com/vishvananda/netlink" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func TestName(t *testing.T) { | ||
_, err := log.SetupZapLogger(log.GetDefaultLogOpts()) | ||
require.NoError(t, err) | ||
w := NewWatcher() | ||
require.Equal(t, watcherName, w.Name()) | ||
} | ||
|
||
func TestStart(t *testing.T) { | ||
// Monkey patch netlink.LinkSubscribeWithOptions with our fakeLinkSubscribe function to simulate netlink events. | ||
// TIL: https://bou.ke/blog/monkey-patching-in-go/ | ||
// Should be fine for testing purposes (?) | ||
patch := monkey.Patch(netlink.LinkSubscribeWithOptions, fakeLinkSubscribe) | ||
Check failure on line 26 in pkg/watchers/endpoint/watcher_test.go
|
||
defer patch.Unpatch() | ||
|
||
_, err := log.SetupZapLogger(log.GetDefaultLogOpts()) | ||
require.NoError(t, err) | ||
|
||
w := NewWatcher() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
doneCh := make(chan error, 1) | ||
go func() { | ||
doneCh <- w.Start(ctx) | ||
}() | ||
|
||
// Wait briefly to allow the fake event to be sent. | ||
time.Sleep(50 * time.Millisecond) | ||
|
||
// Cancel the context to stop the watcher. | ||
cancel() | ||
|
||
// Wait for Start to finish. | ||
err = <-doneCh | ||
require.NoError(t, err, "Start should exit without error") | ||
} | ||
|
||
// fakeLinkSubscribe simulates netlink events by sending a fake event after a short delay. | ||
func fakeLinkSubscribe(netlinkEvCh chan<- netlink.LinkUpdate, done <-chan struct{}, _ netlink.LinkSubscribeOptions) error { | ||
Check failure on line 54 in pkg/watchers/endpoint/watcher_test.go
|
||
go func() { | ||
time.Sleep(10 * time.Millisecond) | ||
fakeVethCreatedEvent := netlink.LinkUpdate{ | ||
Check failure on line 57 in pkg/watchers/endpoint/watcher_test.go
|
||
Header: unix.NlMsghdr{ | ||
Check failure on line 58 in pkg/watchers/endpoint/watcher_test.go
|
||
Type: unix.RTM_NEWLINK, | ||
Check failure on line 59 in pkg/watchers/endpoint/watcher_test.go
|
||
}, | ||
Link: &netlink.Veth{ | ||
LinkAttrs: netlink.LinkAttrs{ | ||
Name: "veth0", | ||
Index: 1, | ||
OperState: netlink.OperUp, | ||
}, | ||
}, | ||
} | ||
netlinkEvCh <- fakeVethCreatedEvent | ||
fakeVethDeletedEvent := netlink.LinkUpdate{ | ||
Check failure on line 70 in pkg/watchers/endpoint/watcher_test.go
|
||
Header: unix.NlMsghdr{ | ||
Check failure on line 71 in pkg/watchers/endpoint/watcher_test.go
|
||
Type: unix.RTM_DELLINK, | ||
Check failure on line 72 in pkg/watchers/endpoint/watcher_test.go
|
||
}, | ||
Link: &netlink.Veth{ | ||
LinkAttrs: netlink.LinkAttrs{ | ||
Name: "veth0", | ||
Index: 1, | ||
OperState: netlink.OperDown, | ||
}, | ||
}, | ||
} | ||
netlinkEvCh <- fakeVethDeletedEvent | ||
<-done | ||
}() | ||
return nil | ||
} |