aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni_test.go
blob: e1190ca3a86f2bb796912ae9dbb339967fd1a9aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// +build linux

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"
)

func TestAddMultipleInterfaces(t *testing.T) {
	oldConfigureInterface := app.ConfigureInterface
	// as we are exiting, revert ConfigureInterface back  at end of function
	defer func() { app.ConfigureInterface = oldConfigureInterface }()
	app.ConfigureInterface = func(args *skel.CmdArgs, namespace, podName, macAddress, ipAddress, gatewayIP, interfaceName, defaultGateway string, mtu int) ([]*current.Interface, error) {
		return []*current.Interface{
			{
				Name:    "pod",
				Mac:     "0a:00:00:00:00:0c",
				Sandbox: "102103104",
			}}, 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 {
		t.Errorf("Failed addMultipleInterfaces %+v", ovnAnnotation)
	}
	ovnAnnotation = "[{\"ip_address\":\"172.16.24.2/24\", \"mac_address\":\"0a:00:00:00:00:0c\", \"gateway_ip\": \"172.16.24.1\"}] "
	result = addMultipleInterfaces(args, ovnAnnotation, "default", "pod")
	if result != nil {
		t.Errorf("Failed addMultipleInterfaces %+v", ovnAnnotation)
	}
	ovnAnnotation = "[{\"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)
	}
	ovnAnnotation = "[{\"ip_address\":\"172.16.24.2/24\", \"mac_address\":\"0a:00:00:00:00:0c\", \"gateway_ip\": \"172.16.24.1\",\"interface\":\"net0\"}, {\"ip_address\":\"172.16.25.2/24\", \"mac_address\":\"0a:00:00:00:00:0d\", \"gateway_ip\": \"172.16.25.1\",\"interface\":\"net1\"}]"
	result = addMultipleInterfaces(args, ovnAnnotation, "default", "pod")
	if result == nil {
		t.Errorf("Failed addMultipleInterfaces %+v", ovnAnnotation)
	}
	ovnAnnotation = "[{\"ip_address\":\"172.16.24.2/24\", \"mac_address\":\"0a:00:00:00:00:0c\", \"gateway_ip\": \"172.16.24.1\",\"interface\":\"net0\", \"defaultGateway\":\"true\"}, {\"ip_address\":\"172.16.25.2/24\", \"mac_address\":\"0a:00:00:00:00:0d\", \"gateway_ip\": \"172.16.25.1\",\"interface\":\"net1\"}]"
	result = addMultipleInterfaces(args, ovnAnnotation, "default", "pod")
	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")
        }

}