forked from k8snetworkplumbingwg/sriov-network-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for some functions in SriovInterface
Signed-off-by: Yury Kulazhenkov <[email protected]>
- Loading branch information
1 parent
5410c12
commit 2b49a6a
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |