Skip to content

Commit

Permalink
support using fake file system for unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Sch <sebassch@gmail.com>
  • Loading branch information
SchSeba committed Jun 2, 2024
1 parent ecfd5bd commit ad8b976
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
13 changes: 11 additions & 2 deletions pkg/infoprovider/vhostNetInfoProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ import (
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/types"
)

var (
// HostNet variable for vhost-net device path
// used to check path for unit-tests
HostNet = "/dev/vhost-net"
// HostTun variable for tun device path
// used to check path for unit-tests
HostTun = "/dev/net/tun"
)

/*
VhostNetInfoProvider wraps any DeviceInfoProvider and adds a vhost-net device
*/
Expand All @@ -39,7 +48,7 @@ func NewVhostNetInfoProvider() types.DeviceInfoProvider {

// VhostNetDeviceExist returns true if /dev/vhost-net exists
func VhostNetDeviceExist() bool {
_, err := os.Stat("/dev/vhost-net")
_, err := os.Stat(HostNet)
return err == nil
}

Expand All @@ -57,7 +66,7 @@ func getVhostNetDeviceSpec() []*pluginapi.DeviceSpec {

// TunDeviceExist returns true if /dev/net/tun exists
func tunDeviceExist() bool {
_, err := os.Stat("/dev/net/tun")
_, err := os.Stat(HostTun)
return err == nil
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/infoprovider/vhostNetInfoProvider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
package infoprovider_test

import (
"path/filepath"

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

pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"

"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/infoprovider"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/utils"
)

var _ = Describe("vdpaInfoProvider", func() {
Expand All @@ -35,6 +39,18 @@ var _ = Describe("vdpaInfoProvider", func() {
})
Describe("GetDeviceSpecs", func() {
It("should return correct specs for vhost net device", func() {
fakeFs := &utils.FakeFilesystem{
Dirs: []string{"/dev/net/"},
Files: map[string][]byte{
"/dev/vhost-net": nil,
"/dev/net/tun": nil},
}
defer fakeFs.Use()()
//nolint: gocritic
infoprovider.HostNet = filepath.Join(fakeFs.RootDir, "/dev/vhost-net")
//nolint: gocritic
infoprovider.HostTun = filepath.Join(fakeFs.RootDir, "/dev/net/tun")

dip := infoprovider.NewVhostNetInfoProvider()
Expect(dip.GetDeviceSpecs()).To(Equal([]*pluginapi.DeviceSpec{
{
Expand Down
13 changes: 12 additions & 1 deletion pkg/netdevice/pciNetDevice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
package netdevice_test

import (
"path/filepath"
"testing"

"github.com/jaypipes/ghw"
"github.com/jaypipes/pcidb"
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"

"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/factory"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/infoprovider"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/netdevice"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/types"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/types/mocks"
Expand Down Expand Up @@ -252,7 +254,11 @@ var _ = Describe("PciNetDevice", func() {
"sys/bus/pci/devices/0000:00:00.1",
"sys/kernel/iommu_groups/0",
"sys/bus/pci/drivers/vfio-pci",
// "dev/vhost-net", FIXME: /dev folder is not mocked
"dev/net",
},
Files: map[string][]byte{
"/dev/vhost-net": nil,
"/dev/net/tun": nil,
},
Symlinks: map[string]string{
"sys/bus/pci/devices/0000:00:00.1/iommu_group": "../../../../kernel/iommu_groups/0",
Expand All @@ -266,6 +272,11 @@ var _ = Describe("PciNetDevice", func() {
defer fs.Use()()
utils.SetDefaultMockNetlinkProvider()

//nolint: gocritic
infoprovider.HostNet = filepath.Join(fs.RootDir, "/dev/vhost-net")
//nolint: gocritic
infoprovider.HostTun = filepath.Join(fs.RootDir, "/dev/net/tun")

dev, err := netdevice.NewPciNetDevice(in, f, rc, vhost_net_selector_index)

Expect(dev.GetDriver()).To(Equal("vfio-pci"))
Expand Down
1 change: 1 addition & 0 deletions pkg/utils/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func writePciIds(fs *FakeFilesystem) {
if err != nil {
panic(fmt.Errorf("error reading testdata pci file working directory %s: %s", pciIdsPath, err.Error()))
}

//nolint: gomnd
err = os.WriteFile(path.Join(fs.RootDir, "usr/share/hwdata/pci.ids"), pciData, 0600)
if err != nil {
Expand Down

0 comments on commit ad8b976

Please sign in to comment.