Skip to content

Commit

Permalink
Add tests for some functions in SriovInterface
Browse files Browse the repository at this point in the history
Signed-off-by: Yury Kulazhenkov <[email protected]>
  • Loading branch information
ykulazhenkov committed Jan 23, 2024
1 parent 5410c12 commit 2b49a6a
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
96 changes: 96 additions & 0 deletions pkg/host/internal/sriov/sriov_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package sriov

import (
"fmt"
"strconv"
"syscall"

"github.com/golang/mock/gomock"
"github.com/vishvananda/netlink"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

netlinkMockPkg "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/lib/netlink/mock"
hostMockPkg "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/mock"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types"
"github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/fakefilesystem"
"github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/helpers"
)

var _ = Describe("SRIOV", func() {
var (
s types.SriovInterface
libMock *netlinkMockPkg.MockNetlinkLib
hostMock *hostMockPkg.MockHostManagerInterface

testCtrl *gomock.Controller

testError = fmt.Errorf("test")
)
BeforeEach(func() {
testCtrl = gomock.NewController(GinkgoT())
libMock = netlinkMockPkg.NewMockNetlinkLib(testCtrl)
hostMock = hostMockPkg.NewMockHostManagerInterface(testCtrl)
s = New(nil, hostMock, hostMock, hostMock, libMock)
})

AfterEach(func() {
testCtrl.Finish()
})

Context("SetSriovNumVfs", func() {
It("set", func() {
helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{
Dirs: []string{"/sys/bus/pci/devices/0000:d8:00.0"},
Files: map[string][]byte{"/sys/bus/pci/devices/0000:d8:00.0/sriov_numvfs": {}},
})
Expect(s.SetSriovNumVfs("0000:d8:00.0", 5)).NotTo(HaveOccurred())
helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/devices/0000:d8:00.0/sriov_numvfs", strconv.Itoa(5))
})
It("fail - no such device", func() {
Expect(s.SetSriovNumVfs("0000:d8:00.0", 5)).To(HaveOccurred())
})
})

Context("GetNicSriovMode", func() {
It("devlink returns info", func() {
libMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(
&netlink.DevlinkDevice{Attrs: netlink.DevlinkDevAttrs{Eswitch: netlink.DevlinkDevEswitchAttr{Mode: "switchdev"}}},
nil)
mode, err := s.GetNicSriovMode("0000:d8:00.0")
Expect(err).NotTo(HaveOccurred())
Expect(mode).To(Equal("switchdev"))
})
It("devlink returns error", func() {
libMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(nil, testError)
_, err := s.GetNicSriovMode("0000:d8:00.0")
Expect(err).To(MatchError(testError))
})
It("devlink not supported - fail to get name", func() {
libMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(nil, syscall.ENODEV)
mode, err := s.GetNicSriovMode("0000:d8:00.0")
Expect(err).NotTo(HaveOccurred())
Expect(mode).To(BeEmpty())
})
})

Context("SetNicSriovMode", func() {
It("set", func() {
testDev := &netlink.DevlinkDevice{}
libMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(&netlink.DevlinkDevice{}, nil)
libMock.EXPECT().DevLinkSetEswitchMode(testDev, "legacy").Return(nil)
Expect(s.SetNicSriovMode("0000:d8:00.0", "legacy")).NotTo(HaveOccurred())
})
It("fail to get dev", func() {
libMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(nil, testError)
Expect(s.SetNicSriovMode("0000:d8:00.0", "legacy")).To(MatchError(testError))
})
It("fail to set mode", func() {
testDev := &netlink.DevlinkDevice{}
libMock.EXPECT().DevLinkGetDeviceByName("pci", "0000:d8:00.0").Return(&netlink.DevlinkDevice{}, nil)
libMock.EXPECT().DevLinkSetEswitchMode(testDev, "legacy").Return(testError)
Expect(s.SetNicSriovMode("0000:d8:00.0", "legacy")).To(MatchError(testError))
})
})
})
21 changes: 21 additions & 0 deletions pkg/host/internal/sriov/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package sriov

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"go.uber.org/zap/zapcore"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

func TestSriov(t *testing.T) {
log.SetLogger(zap.New(
zap.WriteTo(GinkgoWriter),
zap.Level(zapcore.Level(-2)),
zap.UseDevMode(true)))
RegisterFailHandler(Fail)
RunSpecs(t, "Package SRIOV Suite")
}

0 comments on commit 2b49a6a

Please sign in to comment.