From d762c319a19a19c04d3523f3a3b51d94cedfcf02 Mon Sep 17 00:00:00 2001 From: Sebastian Sch Date: Thu, 16 May 2024 20:19:09 +0300 Subject: [PATCH] Fix the cache save before we send a pointer to the netConf object so the marshal function didn't parse all the information needed Signed-off-by: Sebastian Sch --- cmd/sriov/main.go | 2 +- pkg/utils/utils.go | 6 ++++-- pkg/utils/utils_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/cmd/sriov/main.go b/cmd/sriov/main.go index c31ee5b54..cfb7b78ea 100644 --- a/cmd/sriov/main.go +++ b/cmd/sriov/main.go @@ -195,7 +195,7 @@ func cmdAdd(args *skel.CmdArgs) error { "func", "cmdAdd", "config.DefaultCNIDir", config.DefaultCNIDir, "netConf", netConf) - if err = utils.SaveNetConf(args.ContainerID, config.DefaultCNIDir, args.IfName, netConf); err != nil { + if err = utils.SaveNetConf(args.ContainerID, config.DefaultCNIDir, args.IfName, *netConf); err != nil { return fmt.Errorf("error saving NetConf %q", err) } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 81bcf70f3..6d91a5c18 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -10,6 +10,8 @@ import ( "strconv" "strings" "time" + + sriovtypes "github.com/k8snetworkplumbingwg/sriov-cni/pkg/types" ) var ( @@ -238,8 +240,8 @@ func HasDpdkDriver(pciAddr string) (bool, error) { // SaveNetConf takes in container ID, data dir and Pod interface name as string and a json encoded struct Conf // and save this Conf in data dir -func SaveNetConf(cid, dataDir, podIfName string, conf interface{}) error { - netConfBytes, err := json.Marshal(conf) +func SaveNetConf(cid, dataDir, podIfName string, netConf sriovtypes.NetConf) error { + netConfBytes, err := json.Marshal(netConf) if err != nil { return fmt.Errorf("error serializing delegate netconf: %v", err) } diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 273f02e91..5f8b0b43e 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -1,8 +1,11 @@ package utils import ( + "encoding/json" "errors" "net" + "os" + "path/filepath" "time" . "github.com/onsi/ginkgo/v2" @@ -10,6 +13,7 @@ import ( "github.com/vishvananda/netlink" + sriovtypes "github.com/k8snetworkplumbingwg/sriov-cni/pkg/types" mocks_utils "github.com/k8snetworkplumbingwg/sriov-cni/pkg/utils/mocks" ) @@ -184,4 +188,28 @@ var _ = Describe("Utils", func() { Expect(err).ToNot(HaveOccurred()) }) }) + + Context("Checking SaveNetConf function", func() { + var tmpDir string + + BeforeEach(func() { + var err error + tmpDir, err = os.MkdirTemp("", "sriov") + Expect(err).ToNot(HaveOccurred()) + }) + It("should save all the netConf struct to the cache file", func() { + netconf := &sriovtypes.NetConf{DeviceID: "0000:af:06.0"} + err := SaveNetConf("test", tmpDir, "net1", *netconf) + Expect(err).ToNot(HaveOccurred()) + + data, err := os.ReadFile(filepath.Join(tmpDir, "test-net1")) + Expect(err).ToNot(HaveOccurred()) + + newNetConf := &sriovtypes.NetConf{} + err = json.Unmarshal(data, newNetConf) + Expect(err).ToNot(HaveOccurred()) + + Expect(netconf.DeviceID).To(Equal(newNetConf.DeviceID)) + }) + }) })