Skip to content

Commit

Permalink
Unit test for WaitForCarrier
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Haller <[email protected]>
Signed-off-by: Thomas Haller <[email protected]>
  • Loading branch information
zeeke and thom311 committed Jul 9, 2024
1 parent 0a80b6d commit 4c8f82e
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions pkg/utils/packet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package utils

import (
"sync/atomic"
"time"

mocks_utils "github.com/k8snetworkplumbingwg/sriov-cni/pkg/utils/mocks"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/mock"

"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
)

var _ = Describe("Packets", func() {

Context("WaitForCarrier", func() {
It("should wait until the link has IFF_UP flag", func() {
DeferCleanup(func(old NetlinkManager) { netLinkLib = old }, netLinkLib)

mockedNetLink := &mocks_utils.NetlinkManager{}
netLinkLib = mockedNetLink

rawFlagsAtomic := new(uint32)
*rawFlagsAtomic = unix.IFF_UP

fakeLink := &FakeLink{LinkAttrs: netlink.LinkAttrs{
Index: 1000,
Name: "dummylink",
RawFlags: atomic.LoadUint32(rawFlagsAtomic),
}}

mockedNetLink.On("LinkByName", "dummylink").Return(fakeLink, nil).Run(func(args mock.Arguments) {
fakeLink.RawFlags = atomic.LoadUint32(rawFlagsAtomic)
})

hasCarrier := make(chan bool)
go func() {
hasCarrier <- WaitForCarrier("dummylink", 5*time.Second)
}()

Consistently(hasCarrier, "100ms").ShouldNot(Receive())

go func() {
atomic.StoreUint32(rawFlagsAtomic, unix.IFF_UP|unix.IFF_RUNNING)
}()

Eventually(hasCarrier, "300ms").Should(Receive())
})
})
})

0 comments on commit 4c8f82e

Please sign in to comment.