multus-cni/pkg/netutils/netutils_test.go
Miguel Duarte Barroso fb31217e2c thick-plugin: refactor multus
Multus is refactored as a thick plugin, featuring 2 main components:
  - a server listening to a unix domain socket, running in a pod
  - a shim, a binary on the host that will send JSON requests built from
    its environment / stdin values to the aforementioned server.

The pod where the multus daemon is running must share the host's PID
namespace.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

react to maintainers review

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

thick, deployment: update the daemonset spec

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

thick, config: validate the cni config passed by the runtime

Without this patch, we're blindly trusting anything sent by the server.
This way, we assure the requests arriving at the multus controller are
valid before hand.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

thick: model client / server config

Also add a new command line parameter on the multus controller, pointing
it to the server configuration.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

SQUASH candidate, thick, config: cleanup the configuration

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

multus: use args.args instead of an env variable

CNI is already filling the args structure; we should consume that
rather than rely on the environment variables.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

unit tests: remove weird tests that check an impossible scenario

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

docs, thick: document the thick plugin variant

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>

thick, server, multus: re-use common types

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
2022-04-06 00:34:52 +09:00

1025 lines
26 KiB
Go

// Copyright (c) 2019 Multus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package netutils
import (
"encoding/json"
"net"
"testing"
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types/020"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/containernetworking/plugins/pkg/testutils"
"github.com/vishvananda/netlink"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestNetutils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "netutils")
}
// helper function
func testAddRoute(link netlink.Link, ip net.IP, mask net.IPMask, gw net.IP) error {
dst := &net.IPNet{
IP: ip,
Mask: mask,
}
route := netlink.Route{LinkIndex: link.Attrs().Index, Dst: dst, Gw: gw}
return netlink.RouteAdd(&route)
}
func testAddAddr(link netlink.Link, ip net.IP, mask net.IPMask) error {
return netlink.AddrAdd(link, &netlink.Addr{IPNet: &net.IPNet{IP: ip, Mask: mask}})
}
func testGetResultFromCache(data []byte) []byte {
var cachedInfo map[string]interface{}
ExpectWithOffset(1, json.Unmarshal(data, &cachedInfo)).NotTo(HaveOccurred())
// try to get result
_, ok := cachedInfo["result"]
ExpectWithOffset(1, ok).To(BeTrue())
resultJSON, ok := cachedInfo["result"].(map[string]interface{})
ExpectWithOffset(1, ok).To(BeTrue())
resultByte, err := json.Marshal(resultJSON)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
return resultByte
}
func test020ResultHasIPv4DefaultRoute(data []byte) bool {
resultRaw, err := types020.NewResult(data)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
result, err := types020.GetResult(resultRaw)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
for _, r := range result.IP4.Routes {
if r.Dst.String() == "0.0.0.0/0" {
return true
}
}
return false
}
func test020ResultHasIPv6DefaultRoute(data []byte) bool {
resultRaw, err := types020.NewResult(data)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
result, err := types020.GetResult(resultRaw)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
for _, r := range result.IP6.Routes {
if r.Dst.String() == "::/0" {
return true
}
}
return false
}
func testResultHasIPv4DefaultRoute(data []byte) bool {
resultRaw, err := current.NewResult(data)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
result, err := current.GetResult(resultRaw)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
for _, r := range result.Routes {
if r.Dst.String() == "0.0.0.0/0" {
return true
}
}
return false
}
func testResultHasIPv6DefaultRoute(data []byte) bool {
resultRaw, err := current.NewResult(data)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
result, err := current.GetResult(resultRaw)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
for _, r := range result.Routes {
if r.Dst.String() == "::/0" {
return true
}
}
return false
}
var _ = Describe("netutil netlink function testing", func() {
const IFNAME string = "dummy0"
var IFMAC net.HardwareAddr = net.HardwareAddr([]byte{0x02, 0x66, 0x7d, 0xe3, 0x14, 0x1c})
var originalNS ns.NetNS
var targetNS ns.NetNS
BeforeEach(func() {
// Create a new NetNS so we don't modify the host
var err error
originalNS, err = testutils.NewNS()
Expect(err).NotTo(HaveOccurred())
targetNS, err = testutils.NewNS()
Expect(err).NotTo(HaveOccurred())
Expect(targetNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()
Expect(netlink.LinkAdd(&netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Name: IFNAME,
HardwareAddr: IFMAC,
Index: 10,
},
})).Should(Succeed())
_, err = netlink.LinkByName(IFNAME)
Expect(err).NotTo(HaveOccurred())
return nil
})).Should(Succeed())
})
AfterEach(func() {
Expect(originalNS.Close()).To(Succeed())
})
Context("test DeleteDefaultGW", func() {
It("verify default gateway is removed", func() {
Expect(targetNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()
link, err := netlink.LinkByName(IFNAME)
Expect(err).NotTo(HaveOccurred())
Expect(netlink.LinkSetUp(link)).NotTo(HaveOccurred())
// addr 10.0.0.2/24
Expect(testAddAddr(link, net.IPv4(10, 0, 0, 2), net.CIDRMask(24, 32))).Should(Succeed())
// add default gateway into IFNAME
Expect(testAddRoute(link,
net.IPv4(0, 0, 0, 0), net.CIDRMask(0, 0),
net.IPv4(10, 0, 0, 1))).Should(Succeed())
//"dst": "10.0.0.0/16"
Expect(testAddRoute(link,
net.IPv4(10, 0, 0, 0), net.CIDRMask(16, 32),
net.IPv4(10, 0, 0, 1))).Should(Succeed())
return nil
})).Should(Succeed())
args := &skel.CmdArgs{
ContainerID: "dummy",
Netns: targetNS.Path(),
IfName: IFNAME,
}
Expect(originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()
Expect(DeleteDefaultGW(args.Netns, IFNAME)).Should(Succeed())
return nil
})).Should(Succeed())
})
})
Context("test SetDefaultGW", func() {
It("verify default gateway is removed", func() {
Expect(targetNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()
link, err := netlink.LinkByName(IFNAME)
Expect(err).NotTo(HaveOccurred())
Expect(netlink.LinkSetUp(link)).Should(Succeed())
// addr 10.0.0.2/24
Expect(testAddAddr(link, net.IPv4(10, 0, 0, 2), net.CIDRMask(24, 32))).Should(Succeed())
//"dst": "10.0.0.0/16"
Expect(testAddRoute(link,
net.IPv4(10, 0, 0, 0), net.CIDRMask(16, 32),
net.IPv4(10, 0, 0, 1))).Should(Succeed())
return nil
})).Should(Succeed())
args := &skel.CmdArgs{
ContainerID: "dummy",
Netns: targetNS.Path(),
IfName: IFNAME,
}
Expect(originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()
Expect(SetDefaultGW(args.Netns, IFNAME, []net.IP{net.ParseIP("10.0.0.1")})).Should(Succeed())
return nil
})).Should(Succeed())
})
})
})
var _ = Describe("netutil cnicache function testing", func() {
Context("test DeleteDefaultGWCache", func() {
It("verify ipv4 default gateway is removed from CNI 0.1.0/0.2.0 results", func() {
origResult := []byte(`{
"kind": "cniCacheV1",
"result": {
"dns": {},
"ip4": {
"ip": "10.1.1.103/24",
"routes": [
{
"dst": "20.0.0.0/24",
"gw": "10.1.1.1"
},
{
"dst": "0.0.0.0/0",
"gw": "10.1.1.1"
},
{
"dst": "30.0.0.0/24",
"gw": "10.1.1.1"
}
]
},
"ip6": {
"ip": "10::1:1:103/64",
"routes": [
{
"dst": "20::0:0:0/56",
"gw": "10::1:1:1"
},
{
"dst": "::0/0",
"gw": "10::1:1:1"
},
{
"dst": "30::0:0:0/64",
"gw": "10::1:1:1"
}
]
}
}
}`)
newResult, err := deleteDefaultGWCacheBytes(origResult, true, false)
Expect(err).NotTo(HaveOccurred())
Expect(test020ResultHasIPv4DefaultRoute(testGetResultFromCache(newResult))).To(BeFalse())
Expect(test020ResultHasIPv6DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
// Simplified CNI Cache with 010/020 Result
type CNICacheResult020 struct {
Kind string `json:"kind"`
Result struct {
IP4 struct {
IP string `json:"ip"`
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"ip4"`
IP6 struct {
IP string `json:"ip"`
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"ip6"`
} `json:"result"`
}
result := CNICacheResult020{}
Expect(json.Unmarshal(newResult, &result)).NotTo(HaveOccurred())
Expect(len(result.Result.IP4.Routes)).To(Equal(2))
Expect(len(result.Result.IP6.Routes)).To(Equal(3))
})
It("verify ipv6 default gateway is removed from CNI 0.1.0/0.2.0 results", func() {
origResult := []byte(`{
"kind": "cniCacheV1",
"result": {
"dns": {},
"ip4": {
"ip": "10.1.1.103/24",
"routes": [
{
"dst": "20.0.0.0/24",
"gw": "10.1.1.1"
},
{
"dst": "0.0.0.0/0",
"gw": "10.1.1.1"
},
{
"dst": "30.0.0.0/24",
"gw": "10.1.1.1"
}
]
},
"ip6": {
"ip": "10::1:1:103/64",
"routes": [
{
"dst": "20::0:0:0/56",
"gw": "10::1:1:1"
},
{
"dst": "::0/0",
"gw": "10::1:1:1"
},
{
"dst": "30::0:0:0/64",
"gw": "10::1:1:1"
}
]
}
}
}`)
newResult, err := deleteDefaultGWCacheBytes(origResult, false, true)
Expect(err).NotTo(HaveOccurred())
Expect(test020ResultHasIPv4DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
Expect(test020ResultHasIPv6DefaultRoute(testGetResultFromCache(newResult))).To(BeFalse())
// Simplified CNI Cache with 010/020 Result
type CNICacheResult020 struct {
Kind string `json:"kind"`
Result struct {
IP4 struct {
IP string `json:"ip"`
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"ip4"`
IP6 struct {
IP string `json:"ip"`
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"ip6"`
} `json:"result"`
}
result := CNICacheResult020{}
Expect(json.Unmarshal(newResult, &result)).NotTo(HaveOccurred())
Expect(len(result.Result.IP4.Routes)).To(Equal(3))
Expect(len(result.Result.IP6.Routes)).To(Equal(2))
})
It("verify ipv4 default gateway is removed from CNI 0.3.0/0.3.1/0.4.0 results", func() {
origResult := []byte(`{
"kind": "cniCacheV1",
"result": {
"cniVersion": "0.3.1",
"dns": {},
"interfaces": [
{
"mac": "0a:c2:e6:3d:45:17",
"name": "net1",
"sandbox": "/var/run/netns/bb74fcb9-989a-4589-b2df-ddd0384a8ee5"
}
],
"ips": [
{
"address": "10.1.1.103/24",
"interface": 0,
"version": "4"
},
{
"address": "10::1:1:103/64",
"interface": 0,
"version": "6"
}
],
"routes": [
{
"dst": "20.0.0.0/24",
"gw": "10.1.1.1"
},
{
"dst": "0.0.0.0/0",
"gw": "10.1.1.1"
},
{
"dst": "30.0.0.0/24",
"gw": "10.1.1.1"
},
{
"dst": "20::0:0:0/56",
"gw": "10::1:1:1"
},
{
"dst": "::0/0",
"gw": "10::1:1:1"
},
{
"dst": "30::0:0:0/64",
"gw": "10::1:1:1"
}
]
}
}`)
newResult, err := deleteDefaultGWCacheBytes(origResult, true, false)
Expect(err).NotTo(HaveOccurred())
Expect(testResultHasIPv4DefaultRoute(testGetResultFromCache(newResult))).To(BeFalse())
Expect(testResultHasIPv6DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
// Simplified CNI Cache with 0.3.0/0.3.1/0.4.0 Result
type CNICacheResult030_040 struct {
Kind string `json:"kind"`
Result struct {
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"result"`
}
result := CNICacheResult030_040{}
Expect(json.Unmarshal(newResult, &result)).NotTo(HaveOccurred())
Expect(len(result.Result.Routes)).To(Equal(5))
})
It("verify ipv6 default gateway is removed from CNI 0.3.0/0.3.1/0.4.0 results", func() {
origResult := []byte(`{
"kind": "cniCacheV1",
"result": {
"cniVersion": "0.3.1",
"dns": {},
"interfaces": [
{
"mac": "0a:c2:e6:3d:45:17",
"name": "net1",
"sandbox": "/var/run/netns/bb74fcb9-989a-4589-b2df-ddd0384a8ee5"
}
],
"ips": [
{
"address": "10.1.1.103/24",
"interface": 0,
"version": "4"
},
{
"address": "10::1:1:103/64",
"interface": 0,
"version": "6"
}
],
"routes": [
{
"dst": "20.0.0.0/24",
"gw": "10.1.1.1"
},
{
"dst": "0.0.0.0/0",
"gw": "10.1.1.1"
},
{
"dst": "30.0.0.0/24",
"gw": "10.1.1.1"
},
{
"dst": "20::0:0:0/56",
"gw": "10::1:1:1"
},
{
"dst": "::0/0",
"gw": "10::1:1:1"
},
{
"dst": "30::0:0:0/64",
"gw": "10::1:1:1"
}
]
}
}`)
newResult, err := deleteDefaultGWCacheBytes(origResult, false, true)
Expect(err).NotTo(HaveOccurred())
Expect(testResultHasIPv4DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
Expect(testResultHasIPv6DefaultRoute(testGetResultFromCache(newResult))).To(BeFalse())
// Simplified CNI Cache with 0.3.0/0.3.1/0.4.0 Result
type CNICacheResult030_040 struct {
Kind string `json:"kind"`
Result struct {
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"result"`
}
result := CNICacheResult030_040{}
Expect(json.Unmarshal(newResult, &result)).NotTo(HaveOccurred())
Expect(len(result.Result.Routes)).To(Equal(5))
})
It("verify ipv4 default gateway is added to CNI 0.1.0/0.2.0 results without routes", func() {
origResult := []byte(`{
"kind": "cniCacheV1",
"result": {
"dns": {},
"ip4": {
"ip": "10.1.1.103/24"
},
"ip6": {
"ip": "10::1:1:103/64",
"routes": [
{
"dst": "20::0:0:0/56",
"gw": "10::1:1:1"
},
{
"dst": "::0/0",
"gw": "10::1:1:1"
},
{
"dst": "30::0:0:0/64",
"gw": "10::1:1:1"
}
]
}
}
}`)
newResult, err := addDefaultGWCacheBytes(origResult, []net.IP{net.ParseIP("10.1.1.1")})
Expect(err).NotTo(HaveOccurred())
Expect(test020ResultHasIPv4DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
Expect(test020ResultHasIPv6DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
// Simplified CNI Cache with 010/020 Result
type CNICacheResult020 struct {
Kind string `json:"kind"`
Result struct {
IP4 struct {
IP string `json:"ip"`
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"ip4"`
IP6 struct {
IP string `json:"ip"`
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"ip6"`
} `json:"result"`
}
result := CNICacheResult020{}
Expect(json.Unmarshal(newResult, &result)).NotTo(HaveOccurred())
Expect(len(result.Result.IP4.Routes)).To(Equal(1))
})
It("verify ipv4 default gateway is added to CNI 0.1.0/0.2.0 results", func() {
origResult := []byte(`{
"kind": "cniCacheV1",
"result": {
"dns": {},
"ip4": {
"ip": "10.1.1.103/24",
"routes": [
{
"dst": "20.0.0.0/24",
"gw": "10.1.1.1"
},
{
"dst": "30.0.0.0/24",
"gw": "10.1.1.1"
}
]
},
"ip6": {
"ip": "10::1:1:103/64",
"routes": [
{
"dst": "20::0:0:0/56",
"gw": "10::1:1:1"
},
{
"dst": "::0/0",
"gw": "10::1:1:1"
},
{
"dst": "30::0:0:0/64",
"gw": "10::1:1:1"
}
]
}
}
}`)
newResult, err := addDefaultGWCacheBytes(origResult, []net.IP{net.ParseIP("10.1.1.1")})
Expect(err).NotTo(HaveOccurred())
Expect(test020ResultHasIPv4DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
Expect(test020ResultHasIPv6DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
// Simplified CNI Cache with 010/020 Result
type CNICacheResult020 struct {
Kind string `json:"kind"`
Result struct {
IP4 struct {
IP string `json:"ip"`
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"ip4"`
IP6 struct {
IP string `json:"ip"`
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"ip6"`
} `json:"result"`
}
result := CNICacheResult020{}
Expect(json.Unmarshal(newResult, &result)).NotTo(HaveOccurred())
Expect(len(result.Result.IP4.Routes)).To(Equal(3))
})
It("verify ipv6 default gateway is added to CNI 0.1.0/0.2.0 results without routes", func() {
origResult := []byte(`{
"kind": "cniCacheV1",
"result": {
"dns": {},
"ip4": {
"ip": "10.1.1.103/24",
"routes": [
{
"dst": "20.0.0.0/24",
"gw": "10.1.1.1"
},
{
"dst": "0.0.0.0/0",
"gw": "10.1.1.1"
},
{
"dst": "30.0.0.0/24",
"gw": "10.1.1.1"
}
]
},
"ip6": {
"ip": "10::1:1:103/64"
}
}
}`)
newResult, err := addDefaultGWCacheBytes(origResult, []net.IP{net.ParseIP("10::1:1:1")})
Expect(err).NotTo(HaveOccurred())
Expect(test020ResultHasIPv4DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
Expect(test020ResultHasIPv6DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
// Simplified CNI Cache with 010/020 Result
type CNICacheResult020 struct {
Kind string `json:"kind"`
Result struct {
IP4 struct {
IP string `json:"ip"`
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"ip4"`
IP6 struct {
IP string `json:"ip"`
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"ip6"`
} `json:"result"`
}
result := CNICacheResult020{}
Expect(json.Unmarshal(newResult, &result)).NotTo(HaveOccurred())
Expect(len(result.Result.IP6.Routes)).To(Equal(1))
})
It("verify ipv6 default gateway is added to CNI 0.1.0/0.2.0 results", func() {
origResult := []byte(`{
"kind": "cniCacheV1",
"result": {
"dns": {},
"ip4": {
"ip": "10.1.1.103/24",
"routes": [
{
"dst": "20.0.0.0/24",
"gw": "10.1.1.1"
},
{
"dst": "0.0.0.0/0",
"gw": "10.1.1.1"
},
{
"dst": "30.0.0.0/24",
"gw": "10.1.1.1"
}
]
},
"ip6": {
"ip": "10::1:1:103/64",
"routes": [
{
"dst": "20::0:0:0/56",
"gw": "10::1:1:1"
},
{
"dst": "30::0:0:0/64",
"gw": "10::1:1:1"
}
]
}
}
}`)
newResult, err := addDefaultGWCacheBytes(origResult, []net.IP{net.ParseIP("10::1:1:1")})
Expect(err).NotTo(HaveOccurred())
Expect(test020ResultHasIPv4DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
Expect(test020ResultHasIPv6DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
// Simplified CNI Cache with 010/020 Result
type CNICacheResult020 struct {
Kind string `json:"kind"`
Result struct {
IP4 struct {
IP string `json:"ip"`
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"ip4"`
IP6 struct {
IP string `json:"ip"`
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"ip6"`
} `json:"result"`
}
result := CNICacheResult020{}
Expect(json.Unmarshal(newResult, &result)).NotTo(HaveOccurred())
Expect(len(result.Result.IP6.Routes)).To(Equal(3))
})
It("verify ipv4 default gateway is added to CNI 0.3.0/0.3.1/0.4.0 results without routes", func() {
origResult := []byte(`{
"kind": "cniCacheV1",
"result": {
"cniVersion": "0.3.1",
"dns": {},
"interfaces": [
{
"mac": "0a:c2:e6:3d:45:17",
"name": "net1",
"sandbox": "/var/run/netns/bb74fcb9-989a-4589-b2df-ddd0384a8ee5"
}
],
"ips": [
{
"address": "10.1.1.103/24",
"interface": 0,
"version": "4"
},
{
"address": "10::1:1:103/64",
"interface": 0,
"version": "6"
}
]
}
}`)
newResult, err := addDefaultGWCacheBytes(origResult, []net.IP{net.ParseIP("10.1.1.1")})
Expect(err).NotTo(HaveOccurred())
Expect(testResultHasIPv4DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
Expect(testResultHasIPv6DefaultRoute(testGetResultFromCache(newResult))).To(BeFalse())
// Simplified CNI Cache with 0.3.0/0.3.1/0.4.0 Result
type CNICacheResult030_040 struct {
Kind string `json:"kind"`
Result struct {
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"result"`
}
result := CNICacheResult030_040{}
Expect(json.Unmarshal(newResult, &result)).NotTo(HaveOccurred())
Expect(len(result.Result.Routes)).To(Equal(1))
})
It("verify ipv4 default gateway is added to CNI 0.3.0/0.3.1/0.4.0 results", func() {
origResult := []byte(`{
"kind": "cniCacheV1",
"result": {
"cniVersion": "0.3.1",
"dns": {},
"interfaces": [
{
"mac": "0a:c2:e6:3d:45:17",
"name": "net1",
"sandbox": "/var/run/netns/bb74fcb9-989a-4589-b2df-ddd0384a8ee5"
}
],
"ips": [
{
"address": "10.1.1.103/24",
"interface": 0,
"version": "4"
},
{
"address": "10::1:1:103/64",
"interface": 0,
"version": "6"
}
],
"routes": [
{
"dst": "20.0.0.0/24",
"gw": "10.1.1.1"
},
{
"dst": "30.0.0.0/24",
"gw": "10.1.1.1"
},
{
"dst": "20::0:0:0/56",
"gw": "10::1:1:1"
},
{
"dst": "30::0:0:0/64",
"gw": "10::1:1:1"
}
]
}
}`)
newResult, err := addDefaultGWCacheBytes(origResult, []net.IP{net.ParseIP("10.1.1.1")})
Expect(err).NotTo(HaveOccurred())
Expect(testResultHasIPv4DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
Expect(testResultHasIPv6DefaultRoute(testGetResultFromCache(newResult))).To(BeFalse())
// Simplified CNI Cache with 0.3.0/0.3.1/0.4.0 Result
type CNICacheResult030_040 struct {
Kind string `json:"kind"`
Result struct {
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"result"`
}
result := CNICacheResult030_040{}
Expect(json.Unmarshal(newResult, &result)).NotTo(HaveOccurred())
Expect(len(result.Result.Routes)).To(Equal(5))
})
It("verify ipv6 default gateway is added to CNI 0.3.0/0.3.1/0.4.0 results without routes", func() {
origResult := []byte(`{
"kind": "cniCacheV1",
"result": {
"cniVersion": "0.3.1",
"dns": {},
"interfaces": [
{
"mac": "0a:c2:e6:3d:45:17",
"name": "net1",
"sandbox": "/var/run/netns/bb74fcb9-989a-4589-b2df-ddd0384a8ee5"
}
],
"ips": [
{
"address": "10.1.1.103/24",
"interface": 0,
"version": "4"
},
{
"address": "10::1:1:103/64",
"interface": 0,
"version": "6"
}
]
}
}`)
newResult, err := addDefaultGWCacheBytes(origResult, []net.IP{net.ParseIP("10::1:1:1")})
Expect(err).NotTo(HaveOccurred())
Expect(testResultHasIPv4DefaultRoute(testGetResultFromCache(newResult))).To(BeFalse())
Expect(testResultHasIPv6DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
// Simplified CNI Cache with 0.3.0/0.3.1/0.4.0 Result
type CNICacheResult030_040 struct {
Kind string `json:"kind"`
Result struct {
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"result"`
}
result := CNICacheResult030_040{}
Expect(json.Unmarshal(newResult, &result)).NotTo(HaveOccurred())
Expect(len(result.Result.Routes)).To(Equal(1))
})
It("verify ipv6 default gateway is added to CNI 0.3.0/0.3.1/0.4.0 results", func() {
origResult := []byte(`{
"kind": "cniCacheV1",
"result": {
"cniVersion": "0.3.1",
"dns": {},
"interfaces": [
{
"mac": "0a:c2:e6:3d:45:17",
"name": "net1",
"sandbox": "/var/run/netns/bb74fcb9-989a-4589-b2df-ddd0384a8ee5"
}
],
"ips": [
{
"address": "10.1.1.103/24",
"interface": 0,
"version": "4"
},
{
"address": "10::1:1:103/64",
"interface": 0,
"version": "6"
}
],
"routes": [
{
"dst": "20.0.0.0/24",
"gw": "10.1.1.1"
},
{
"dst": "30.0.0.0/24",
"gw": "10.1.1.1"
},
{
"dst": "0.0.0.0/0",
"gw": "10.1.1.1"
},
{
"dst": "20::0:0:0/56",
"gw": "10::1:1:1"
},
{
"dst": "30::0:0:0/64",
"gw": "10::1:1:1"
}
]
}
}`)
newResult, err := addDefaultGWCacheBytes(origResult, []net.IP{net.ParseIP("10::1:1:1")})
Expect(err).NotTo(HaveOccurred())
Expect(testResultHasIPv4DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
Expect(testResultHasIPv6DefaultRoute(testGetResultFromCache(newResult))).To(BeTrue())
// Simplified CNI Cache with 0.3.0/0.3.1/0.4.0 Result
type CNICacheResult030_040 struct {
Kind string `json:"kind"`
Result struct {
Routes []struct {
Dst string `json:"dst"`
Gw string `json:"gw"`
} `json:"routes"`
} `json:"result"`
}
result := CNICacheResult030_040{}
Expect(json.Unmarshal(newResult, &result)).NotTo(HaveOccurred())
Expect(len(result.Result.Routes)).To(Equal(6))
})
})
})