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 Jun 3, 2024
1 parent 9515cc2 commit 4779c1b
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions pkg/utils/packet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package utils

import (
"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"
"sync/atomic"
)

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

ginkgo.Context("WaitForCarrier", func() {
ginkgo.It("should wait until the link has IFF_UP flag", func() {
ginkgo.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)
}()

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

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

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

0 comments on commit 4779c1b

Please sign in to comment.