From 8f32968f73e84f731bbfea4256e3a43e7d7f031c Mon Sep 17 00:00:00 2001 From: Nico Schieder Date: Sat, 6 Mar 2021 11:34:24 +0100 Subject: [PATCH] Fix nil-pointer check Signed-off-by: Nico Schieder --- plugins/ipam/static/main.go | 7 ++-- plugins/ipam/static/static_test.go | 54 +++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/plugins/ipam/static/main.go b/plugins/ipam/static/main.go index 49e0dc00..d7eaeab1 100644 --- a/plugins/ipam/static/main.go +++ b/plugins/ipam/static/main.go @@ -143,6 +143,9 @@ func LoadIPAMConfig(bytes []byte, envArgs string) (*IPAMConfig, string, error) { if err := json.Unmarshal(bytes, &n); err != nil { return nil, "", err } + if n.IPAM == nil { + return nil, "", fmt.Errorf("IPAM config missing 'ipam' key") + } // load IP from CNI_ARGS if envArgs != "" { @@ -203,10 +206,6 @@ func LoadIPAMConfig(bytes []byte, envArgs string) (*IPAMConfig, string, error) { } } - if n.IPAM == nil { - return nil, "", fmt.Errorf("IPAM config missing 'ipam' key") - } - // Validate all ranges numV4 := 0 numV6 := 0 diff --git a/plugins/ipam/static/static_test.go b/plugins/ipam/static/static_test.go index f75d6342..deb1bbad 100644 --- a/plugins/ipam/static/static_test.go +++ b/plugins/ipam/static/static_test.go @@ -21,7 +21,7 @@ import ( "github.com/containernetworking/cni/pkg/skel" "github.com/containernetworking/cni/pkg/types" - "github.com/containernetworking/cni/pkg/types/100" + types100 "github.com/containernetworking/cni/pkg/types/100" "github.com/containernetworking/plugins/pkg/testutils" . "github.com/onsi/ginkgo" @@ -494,6 +494,58 @@ var _ = Describe("static Operations", func() { }) Expect(err).NotTo(HaveOccurred()) }) + + It(fmt.Sprintf("[%s] is returning an error on missing ipam key when args are set", ver), func() { + const ifname string = "eth0" + const nspath string = "/some/where" + conf := fmt.Sprintf(`{ + "cniVersion": "%s", + "name": "mynet", + "type": "ipvlan", + "master": "foo0" + }`, ver) + + args := &skel.CmdArgs{ + ContainerID: "dummy", + Netns: nspath, + IfName: ifname, + StdinData: []byte(conf), + Args: "IP=10.10.0.1/24;GATEWAY=10.10.0.254", + } + + // Allocate the IP + _, _, err := testutils.CmdAddWithArgs(args, func() error { + return cmdAdd(args) + }) + Expect(err).Should(MatchError("IPAM config missing 'ipam' key")) + }) + + It(fmt.Sprintf("[%s] is returning an error on missing ipam key when runtimeConfig is set", ver), func() { + const ifname string = "eth0" + const nspath string = "/some/where" + conf := fmt.Sprintf(`{ + "cniVersion": "%s", + "name": "mynet", + "type": "ipvlan", + "master": "foo0", + "runtimeConfig": { + "ips": ["10.10.0.1/24"] + } + }`, ver) + + args := &skel.CmdArgs{ + ContainerID: "dummy", + Netns: nspath, + IfName: ifname, + StdinData: []byte(conf), + } + + // Allocate the IP + _, _, err := testutils.CmdAddWithArgs(args, func() error { + return cmdAdd(args) + }) + Expect(err).Should(MatchError("IPAM config missing 'ipam' key")) + }) } })