aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni_test.go')
-rw-r--r--cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni_test.go72
1 files changed, 71 insertions, 1 deletions
diff --git a/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni_test.go b/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni_test.go
index d5b7b6b..e1190ca 100644
--- a/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni_test.go
+++ b/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni_test.go
@@ -3,9 +3,14 @@
package main
import (
+ "encoding/json"
+
"github.com/containernetworking/cni/pkg/skel"
+ "github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
+ "net"
"ovn4nfv-k8s-plugin/cmd/ovn4nfvk8s-cni/app"
+ "reflect"
"testing"
)
@@ -21,13 +26,19 @@ func TestAddMultipleInterfaces(t *testing.T) {
Sandbox: "102103104",
}}, nil
}
- args := &skel.CmdArgs{"102103104", "default", "eth0", "", "", nil}
+ oldConfigureRoute := app.ConfigureRoute
+ defer func() { app.ConfigureRoute = oldConfigureRoute }()
+ app.ConfigureRoute = func(args *skel.CmdArgs, dst, gw, dev string) error {
+ return nil
+ }
+ args := &skel.CmdArgs{"102103104", "default", "eth0", "", "", nil}
ovnAnnotation := "[{\"ip_address\":\"172.16.24.2/24\", \"mac_address\":\"0a:00:00:00:00:0c\", \"gateway_ip\": \"172.16.24.1\",\"interface\":\"net0\"}] "
result := addMultipleInterfaces(args, ovnAnnotation, "default", "pod")
if result == nil {
t.Errorf("Failed addMultipleInterfaces %+v", ovnAnnotation)
}
+ resultSave := result
ovnAnnotation = "[{\"ip_address\":\"172.16.24.2/24\", \"mac_address\":\"0a:00:00:00:00:0c\", \"gateway_ip\": \"172.16.24.1\",\"defaultGateway\":\"true\",\"interface\":\"net0\"}] "
result = addMultipleInterfaces(args, ovnAnnotation, "default", "pod")
if result == nil {
@@ -53,4 +64,63 @@ func TestAddMultipleInterfaces(t *testing.T) {
if result == nil {
t.Errorf("Failed addMultipleInterfaces %+v", ovnAnnotation)
}
+ // Test add route feature
+ ovnRoutesAnnotation := "[{ \"dst\": \"172.16.29.0/24\", \"gw\": \"172.16.24.1\", \"dev\": \"eth0\" }]"
+ result = addRoutes(args, ovnRoutesAnnotation, resultSave)
+ if result == nil {
+ t.Errorf("Failed addRoutes %+v", ovnRoutesAnnotation)
+ }
+
+ ovnRoutesAnnotation = "[{ \"dst\": \"172.16.30.0/24\", \"gw\": \"172.16.25.1\", \"dev\": \"eth0\"}, { \"dst\": \"172.16.31.0/24\", \"gw\": \"172.16.26.1\", \"dev\": \"eth1\" }]"
+ result = addRoutes(args, ovnRoutesAnnotation, resultSave)
+ if result == nil {
+ t.Errorf("Failed addRoutes %+v", ovnRoutesAnnotation)
+ }
+ newResult, err := current.NewResultFromResult(result)
+ if err != nil {
+ t.Errorf("Failed addMultipleInterfaces %+v", newResult)
+ }
+ addr1, addrNet1, _ := net.ParseCIDR("172.16.24.2/24")
+ addr2, addrNet2, _ := net.ParseCIDR("172.16.29.0/24")
+ addr3, addrNet3, _ := net.ParseCIDR("172.16.30.0/24")
+ addr4, addrNet4, _ := net.ParseCIDR("172.16.31.0/24")
+ expectedResult := &current.Result{
+ CNIVersion: "0.3.1",
+ Interfaces: []*current.Interface{
+ {
+ Name: "pod",
+ Mac: "0a:00:00:00:00:0c",
+ Sandbox: "102103104",
+ },
+ },
+ IPs: []*current.IPConfig{
+ {
+ Version: "4",
+ Interface: current.Int(1),
+ Address: net.IPNet{IP: addr1, Mask: addrNet1.Mask},
+ Gateway: net.ParseIP("172.16.24.1"),
+ },
+ },
+ Routes: []*types.Route{
+ {
+ Dst: net.IPNet{IP: addr2, Mask: addrNet2.Mask},
+ GW: net.ParseIP("172.16.24.1"),
+ },
+ {
+ Dst: net.IPNet{IP: addr3, Mask: addrNet3.Mask},
+ GW: net.ParseIP("172.16.25.1"),
+ },
+ {
+ Dst: net.IPNet{IP: addr4, Mask: addrNet4.Mask},
+ GW: net.ParseIP("172.16.26.1"),
+ },
+ },
+ DNS: types.DNS{},
+ }
+ jsonBytes1, err := json.Marshal(newResult.Routes)
+ jsonBytes2, err := json.Marshal(expectedResult.Routes)
+ if !reflect.DeepEqual(jsonBytes1, jsonBytes2) {
+ t.Errorf("Routes are not correct")
+ }
+
}