Skip to content

Commit

Permalink
DevInfoFile: add unit tests
Browse files Browse the repository at this point in the history
Two unit tests are added:
netResourcePool_test verifies that the appropriate nadutils functions
are called with the correct arguments on
netResourcePool.{Store,Clean}DeviceInfoFile()

server_test.go verifies that the
resourcePool.{Store,Clean}DeviceInfoFile() functions are actually called
when the server starts and stops.

Signed-off-by: Adrian Moreno <[email protected]>
  • Loading branch information
amorenoz committed Nov 30, 2020
1 parent 78a6ebf commit 2601529
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
54 changes: 54 additions & 0 deletions pkg/netdevice/netResourcePool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package netdevice_test

import (
"fmt"
nettypes "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/factory"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/netdevice"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/types"
Expand All @@ -24,6 +26,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/stretchr/testify/mock"
)

var _ = Describe("NetResourcePool", func() {
Expand Down Expand Up @@ -144,4 +147,55 @@ var _ = Describe("NetResourcePool", func() {
})
})
})
Describe("Saving and Cleaning DevInfo files ", func() {
Context("for valid pci devices", func() {
rc := &types.ResourceConfig{
ResourceName: "fakeResource",
ResourcePrefix: "fakeOrg.io",
SelectorObj: &types.NetDeviceSelectors{
IsRdma: true,
},
}
nadutils := &mocks.NadUtils{}
nadutils.On("SaveDeviceInfoFile", "fakeOrg.io/fakeResource", "fake1", Anything).
Return(func(rName, id string, devInfo *nettypes.DeviceInfo) error {
if devInfo.Type != nettypes.DeviceInfoTypePCI || devInfo.Pci == nil || devInfo.Pci.PciAddress != "0000:01:00.1" {
return fmt.Errorf("wrong device info")
}
return nil
})
nadutils.On("SaveDeviceInfoFile", "fakeOrg.io/fakeResource", "fake2", Anything).
Return(func(rName, id string, devInfo *nettypes.DeviceInfo) error {
if devInfo.Type != nettypes.DeviceInfoTypePCI || devInfo.Pci == nil || devInfo.Pci.PciAddress != "0000:01:00.2" {
return fmt.Errorf("wrong device info")
}
return nil
})
nadutils.On("CleanDeviceInfoFile", "fakeOrg.io/fakeResource", "fake1").Return(nil)
nadutils.On("CleanDeviceInfoFile", "fakeOrg.io/fakeResource", "fake2").Return(nil)

devs := map[string]*v1beta1.Device{}
fake1 := &mocks.PciNetDevice{}
fake1.On("GetPciAddr").Return("0000:01:00.1")
fake2 := &mocks.PciNetDevice{}
fake2.On("GetPciAddr").Return("0000:01:00.2")
pcis := map[string]types.PciDevice{"fake1": fake1, "fake2": fake2}
rp := netdevice.NewNetResourcePool(nadutils, rc, devs, pcis)

err_store := rp.StoreDeviceInfoFile("fakeOrg.io")
err_clean := rp.CleanDeviceInfoFile("fakeOrg.io")

It("should call nadutils to create a well formatted DeviceInfo object", func() {
nadutils.AssertCalled(GinkgoT(), "SaveDeviceInfoFile", "fakeOrg.io/fakeResource", "fake1", Anything)
nadutils.AssertCalled(GinkgoT(), "SaveDeviceInfoFile", "fakeOrg.io/fakeResource", "fake2", Anything)
Expect(err_store).ToNot(HaveOccurred())
})
It("should call nadutils to clean the DeviceInfo objects", func() {
fmt.Printf("%+v", nadutils.Calls)
nadutils.AssertCalled(GinkgoT(), "CleanDeviceInfoFile", "fakeOrg.io/fakeResource", "fake1")
nadutils.AssertCalled(GinkgoT(), "CleanDeviceInfoFile", "fakeOrg.io/fakeResource", "fake2")
Expect(err_clean).ToNot(HaveOccurred())
})
})
})
})
8 changes: 6 additions & 2 deletions pkg/resources/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ var _ = Describe("Server", func() {
rp := mocks.ResourcePool{}
rp.On("Probe").Return(false)
rp.On("GetResourceName").Return("fakename")
rp.On("StoreDeviceInfoFile", "fakeprefix").Return(nil)
rp.On("CleanDeviceInfoFile", "fakeprefix").Return(nil)

// Use faked dir as socket dir
types.SockDir = fs.RootDir
Expand All @@ -71,9 +73,9 @@ var _ = Describe("Server", func() {

if shouldRunServer {
if shouldEnablePluginWatch {
rp.On("StoreDeviceInfoFile", "fakeprefix").Return(nil)
rp.On("CleanDeviceInfoFile", "fakeprefix").Return(nil)
rs.Start()
rp.AssertCalled(GinkgoT(), "CleanDeviceInfoFile", "fakeprefix")
rp.AssertCalled(GinkgoT(), "StoreDeviceInfoFile", "fakeprefix")
} else {
os.MkdirAll(pluginapi.DevicePluginPath, 0755)
registrationServer.start()
Expand Down Expand Up @@ -171,6 +173,7 @@ var _ = Describe("Server", func() {
rp.On("CleanDeviceInfoFile", "fake").Return(nil)
err := rs.Stop()
Expect(err).NotTo(HaveOccurred())
rp.AssertCalled(GinkgoT(), "CleanDeviceInfoFile", "fake")
}()
Eventually(rs.termSignal, time.Second*10).Should(Receive())
Eventually(rs.stopWatcher, time.Second*10).Should(Receive())
Expand Down Expand Up @@ -205,6 +208,7 @@ var _ = Describe("Server", func() {
rp.On("CleanDeviceInfoFile", "fake").Return(nil)
err := rs.Stop()
Expect(err).NotTo(HaveOccurred())
rp.AssertCalled(GinkgoT(), "CleanDeviceInfoFile", "fake")
}()
Eventually(rs.termSignal, time.Second*10).Should(Receive())

Expand Down

0 comments on commit 2601529

Please sign in to comment.