Skip to content

Commit

Permalink
Merge pull request #561 from SchSeba/fix_unittest
Browse files Browse the repository at this point in the history
allow to run unit-tests inside a container
  • Loading branch information
adrianchiris authored Jun 2, 2024
2 parents bd0a56d + 103ce99 commit 3ffbe14
Show file tree
Hide file tree
Showing 6 changed files with 36,731 additions and 27 deletions.
24 changes: 17 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,25 @@ $(BUILDDIR)/$(BINARY_NAME): $(GOFILES) | $(BUILDDIR)
@cd $(BASE)/cmd/$(BINARY_NAME) && CGO_ENABLED=0 go build $(LDFLAGS) -o $(BUILDDIR)/$(BINARY_NAME) -tags no_openssl -v

$(GOLINT): | $(BASE) ; $(info building golint...)
$Q go install golang.org/x/lint/golint@latest
$(call go-install-tool,$(GOLINT),golang.org/x/lint/golint@latest)

$(GOCOVMERGE): | $(BASE) ; $(info building gocovmerge...)
$Q go install github.com/wadey/gocovmerge@latest
$(call go-install-tool,$(GOCOVMERGE),github.com/wadey/gocovmerge@latest)

$(GOCOV): | $(BASE) ; $(info building gocov...)
$Q go install github.com/axw/gocov/gocov@v1.1.0
$(call go-install-tool,$(GOCOV),github.com/axw/gocov/gocov@v1.1.0)

$(GCOV2LCOV): | $(BASE) ; $(info building gcov2lcov...)
$Q go install github.com/jandelgado/gcov2lcov@latest
$(call go-install-tool,$(GCOV2LCOV),github.com/jandelgado/gcov2lcov@latest)

$(GOCOVXML): | $(BASE) ; $(info building gocov-xml...)
$Q go install github.com/AlekSi/gocov-xml@latest
$(call go-install-tool,$(GOCOVXML),github.com/AlekSi/gocov-xml@latest)

$(GO2XUNIT): | $(BASE) ; $(info building go2xunit...)
$Q go install github.com/tebeka/go2xunit@latest
$(call go-install-tool,$(GO2XUNIT),github.com/tebeka/go2xunit@latest)

$(GOMOCKERY): | $(BASE) ; $(info building go2xunit...)
$Q go install github.com/vektra/mockery/v2@latest
$(call go-install-tool,$(GOMOCKERY),github.com/vektra/mockery/v2@latest)

TEST_TARGETS := test-default test-bench test-short test-verbose test-race
.PHONY: $(TEST_TARGETS) test-xml check test tests
Expand Down Expand Up @@ -166,3 +166,13 @@ mockery: | $(BASE) $(GOMOCKERY) ; $(info Running mockery...) @ ## Run golint on
help: ; @ ## Display this help message
@grep -E '^[ a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'


# go-install-tool will 'go install' any package $2 and install it to $1.
define go-install-tool
@[ -f $(1) ] || { \
set -e ;\
echo "Downloading $(2)" ;\
GOBIN=$(GOBIN) go install -mod=mod $(2) ;\
}
endef
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
Loading

0 comments on commit 3ffbe14

Please sign in to comment.