Skip to content

Commit

Permalink
add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
nddq committed Feb 3, 2025
1 parent 103bdc8 commit 083b08c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 18 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ require (
)

require (
bou.ke/monkey v1.0.2
github.com/Azure/azure-container-networking/zapai v0.0.3
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
cel.dev/expr v0.15.0 h1:O1jzfJCQBfL5BFoYktaxwIhuttaQPsVWerH9/EEKx0w=
cel.dev/expr v0.15.0/go.mod h1:TRSuuV7DlVCE/uwv5QbAiW/v8l5O8C4eEPHeu7gf7Sg=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
Expand Down
18 changes: 0 additions & 18 deletions pkg/watchers/endpoint/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ func NewWatcher() *Watcher {
return w
}

type key struct {
name string
hardwareAddr string
// Network namespace for linux.
// Compartment ID for windows.
netNsID int
}

type cache map[key]interface{}

type EndpointEvent struct {
// Type is the type of the event.
Type EventType
Expand Down Expand Up @@ -70,11 +60,3 @@ func (e EventType) String() string {
return "unknown"
}
}

func (c cache) deepcopy() cache {
copy := make(cache)
for k, v := range c {
copy[k] = v
}
return copy
}
86 changes: 86 additions & 0 deletions pkg/watchers/endpoint/watcher_test.go
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

View workflow job for this annotation

GitHub Actions / Lint (linux, arm64)

could not import bou.ke/monkey (-: # bou.ke/monkey

Check failure on line 8 in pkg/watchers/endpoint/watcher_test.go

View workflow job for this annotation

GitHub Actions / Lint (windows, arm64)

could not import bou.ke/monkey (-: # bou.ke/monkey
"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

View workflow job for this annotation

GitHub Actions / Lint (windows, amd64)

undefined: netlink.LinkSubscribeWithOptions

Check failure on line 26 in pkg/watchers/endpoint/watcher_test.go

View workflow job for this annotation

GitHub Actions / Lint (windows, arm64)

undefined: netlink.LinkSubscribeWithOptions (typecheck)
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

View workflow job for this annotation

GitHub Actions / Lint (windows, amd64)

undefined: netlink.LinkUpdate

Check failure on line 54 in pkg/watchers/endpoint/watcher_test.go

View workflow job for this annotation

GitHub Actions / Lint (windows, amd64)

undefined: netlink.LinkSubscribeOptions

Check failure on line 54 in pkg/watchers/endpoint/watcher_test.go

View workflow job for this annotation

GitHub Actions / Lint (windows, arm64)

undefined: netlink.LinkUpdate (typecheck)
go func() {
time.Sleep(10 * time.Millisecond)
fakeVethCreatedEvent := netlink.LinkUpdate{

Check failure on line 57 in pkg/watchers/endpoint/watcher_test.go

View workflow job for this annotation

GitHub Actions / Lint (windows, amd64)

undefined: netlink.LinkUpdate

Check failure on line 57 in pkg/watchers/endpoint/watcher_test.go

View workflow job for this annotation

GitHub Actions / Lint (windows, arm64)

undefined: netlink.LinkUpdate (typecheck)
Header: unix.NlMsghdr{

Check failure on line 58 in pkg/watchers/endpoint/watcher_test.go

View workflow job for this annotation

GitHub Actions / Lint (windows, amd64)

undefined: unix.NlMsghdr

Check failure on line 58 in pkg/watchers/endpoint/watcher_test.go

View workflow job for this annotation

GitHub Actions / Lint (windows, arm64)

undefined: unix.NlMsghdr (typecheck)
Type: unix.RTM_NEWLINK,

Check failure on line 59 in pkg/watchers/endpoint/watcher_test.go

View workflow job for this annotation

GitHub Actions / Lint (windows, amd64)

undefined: unix.RTM_NEWLINK

Check failure on line 59 in pkg/watchers/endpoint/watcher_test.go

View workflow job for this annotation

GitHub Actions / Lint (windows, arm64)

undefined: unix.RTM_NEWLINK (typecheck)
},
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

View workflow job for this annotation

GitHub Actions / Lint (windows, amd64)

undefined: netlink.LinkUpdate

Check failure on line 70 in pkg/watchers/endpoint/watcher_test.go

View workflow job for this annotation

GitHub Actions / Lint (windows, arm64)

undefined: netlink.LinkUpdate (typecheck)
Header: unix.NlMsghdr{

Check failure on line 71 in pkg/watchers/endpoint/watcher_test.go

View workflow job for this annotation

GitHub Actions / Lint (windows, amd64)

undefined: unix.NlMsghdr

Check failure on line 71 in pkg/watchers/endpoint/watcher_test.go

View workflow job for this annotation

GitHub Actions / Lint (windows, arm64)

undefined: unix.NlMsghdr (typecheck)
Type: unix.RTM_DELLINK,

Check failure on line 72 in pkg/watchers/endpoint/watcher_test.go

View workflow job for this annotation

GitHub Actions / Lint (windows, amd64)

undefined: unix.RTM_DELLINK (typecheck)

Check failure on line 72 in pkg/watchers/endpoint/watcher_test.go

View workflow job for this annotation

GitHub Actions / Lint (windows, arm64)

undefined: unix.RTM_DELLINK (typecheck)
},
Link: &netlink.Veth{
LinkAttrs: netlink.LinkAttrs{
Name: "veth0",
Index: 1,
OperState: netlink.OperDown,
},
},
}
netlinkEvCh <- fakeVethDeletedEvent
<-done
}()
return nil
}

0 comments on commit 083b08c

Please sign in to comment.