Skip to content
Permalink
Browse files

Adds cursory unit test for default-route and cleans lint (and one err…

…ant test)
  • Loading branch information...
dougbtv committed Oct 28, 2019
1 parent 3a9dd7e commit 8c76fd62e0e93605eb323becf5a4adcac023d033
Showing with 60 additions and 4 deletions.
  1. +55 −0 k8sclient/k8sclient_test.go
  2. +3 −3 multus/multus_test.go
  3. +2 −1 netutils/netutils.go
@@ -203,6 +203,61 @@ var _ = Describe("k8sclient operations", func() {
Expect(err).To(MatchError("parsePodNetworkAnnotation: failed to parse pod Network Attachment Selection Annotation JSON format: invalid character 'a' looking for beginning of value"))
})

It("can set the default-gateway on an additional interface", func() {
fakePod := testutils.NewFakePod("testpod", `[
{"name":"net1"},
{
"name":"net2",
"default-route": ["192.168.2.2"]
},
{
"name":"net3",
"namespace":"other-ns"
}
]`, "")
args := &skel.CmdArgs{
Args: fmt.Sprintf("K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s", fakePod.ObjectMeta.Name, fakePod.ObjectMeta.Namespace),
}

fKubeClient := testutils.NewFakeKubeClient()
fKubeClient.AddPod(fakePod)
fKubeClient.AddNetConfig(fakePod.ObjectMeta.Namespace, "net1", `{
"name": "net1",
"type": "mynet",
"cniVersion": "0.2.0"
}`)
fKubeClient.AddNetConfig(fakePod.ObjectMeta.Namespace, "net2", `{
"name": "net2",
"type": "mynet2",
"cniVersion": "0.2.0"
}`)
fKubeClient.AddNetConfig("other-ns", "net3", `{
"name": "net3",
"type": "mynet3",
"cniVersion": "0.2.0"
}`)

kubeClient, err := GetK8sClient("", fKubeClient)
Expect(err).NotTo(HaveOccurred())
k8sArgs, err := GetK8sArgs(args)
Expect(err).NotTo(HaveOccurred())
pod, err := kubeClient.GetPod(string(k8sArgs.K8S_POD_NAMESPACE), string(k8sArgs.K8S_POD_NAME))
networks, err := GetPodNetwork(pod)
Expect(err).NotTo(HaveOccurred())
delegates, err := GetNetworkDelegates(kubeClient, pod, networks, tmpDir, false)
Expect(err).NotTo(HaveOccurred())
Expect(fKubeClient.PodCount).To(Equal(1))
Expect(fKubeClient.NetCount).To(Equal(3))

Expect(len(delegates)).To(Equal(3))
Expect(delegates[0].Conf.Name).To(Equal("net1"))
Expect(delegates[0].Conf.Type).To(Equal("mynet"))
Expect(delegates[1].Conf.Name).To(Equal("net2"))
Expect(delegates[1].Conf.Type).To(Equal("mynet2"))
Expect(delegates[2].Conf.Name).To(Equal("net3"))
Expect(delegates[2].Conf.Type).To(Equal("mynet3"))
})

It("retrieves delegates from kubernetes using on-disk config files", func() {
fakePod := testutils.NewFakePod("testpod", "net1,net2", "")
args := &skel.CmdArgs{
@@ -1169,7 +1169,7 @@ var _ = Describe("multus operations", func() {
_, err = cmdAdd(args, fExec, nil)
Expect(fExec.addIndex).To(Equal(2))
Expect(fExec.delIndex).To(Equal(2))
Expect(err).To(MatchError("Multus: error adding pod to network \"other1\": delegateAdd: error invoking DelegateAdd - \"other-plugin\": expected plugin failure"))
Expect(err).To(MatchError("Multus: error adding pod to network \"other1\": delegateAdd: error invoking DelegateAdd - \"other-plugin\": error in getting result from AddNetwork: expected plugin failure"))

// Cleanup default network file.
if _, errStat := os.Stat(configPath); errStat == nil {
@@ -1283,7 +1283,7 @@ var _ = Describe("multus operations", func() {
fExec := &fakeExec{}
expectedResult1 := &current.Result{
CNIVersion: resultCNIVersion,
IPs: []*current.IPConfig{&current.IPConfig{
IPs: []*current.IPConfig{{
Address: *testhelpers.EnsureCIDR("1.1.1.2/24"),
},
},
@@ -1328,7 +1328,7 @@ var _ = Describe("multus operations", func() {
fExec.addPlugin(nil, "eth0", expectedConf1, expectedResult1, nil)
fExec.addPlugin(nil, "net1", expectedNet1, &current.Result{
CNIVersion: "0.3.1",
IPs: []*current.IPConfig{&current.IPConfig{
IPs: []*current.IPConfig{{
Address: *testhelpers.EnsureCIDR("1.1.1.3/24"),
},
},
@@ -23,6 +23,7 @@ import (
"github.com/intel/multus-cni/logging"
"github.com/vishvananda/netlink"
"net"
"strings"
)

// DeleteDefaultGW removes the default gateway from marked interfaces.
@@ -98,7 +99,7 @@ func SetDefaultGW(args *skel.CmdArgs, ifName string, gateways []net.IP, res *cni

// Set a correct CIDR depending on IP type
_, dstipnet, _ := net.ParseCIDR("::0/0")
if gw.To4 != nil {
if strings.Count(gw.String(), ":") < 2 {
_, dstipnet, _ = net.ParseCIDR("0.0.0.0/0")
}
newResultDefaultRoutes = append(newResultDefaultRoutes, &cnitypes.Route{Dst: *dstipnet, GW: gw})

0 comments on commit 8c76fd6

Please sign in to comment.
You can’t perform that action at this time.