5ea01d1d0aaaa236cbd54243f86e8cabd164ac30
[ovn4nfv-k8s-plugin.git] / internal / pkg / ovn / ovn_test.go
1 package ovn
2
3 import (
4         "fmt"
5         "testing"
6
7         "github.com/urfave/cli"
8         fakeexec "k8s.io/utils/exec/testing"
9
10         "k8s.io/api/core/v1"
11         metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12         "ovn4nfv-k8s-plugin/internal/pkg/config"
13         ovntest "ovn4nfv-k8s-plugin/internal/pkg/testing"
14
15         . "github.com/onsi/ginkgo"
16         . "github.com/onsi/gomega"
17 )
18
19 func TestOvn(t *testing.T) {
20         RegisterFailHandler(Fail)
21         RunSpecs(t, "OVN/Pod Test Suite")
22 }
23
24 var _ = AfterSuite(func() {
25 })
26
27 var _ = Describe("Add logical Port", func() {
28         var app *cli.App
29
30         BeforeEach(func() {
31                 app = cli.NewApp()
32                 app.Name = "test"
33                 app.Flags = config.Flags
34
35         })
36
37         It("tests Pod", func() {
38                 app.Action = func(ctx *cli.Context) error {
39                         const (
40                                 gwIP         string = "10.1.1.1"
41                                 gwCIDR       string = gwIP + "/24"
42                                 netName      string = "ovn-prot-net"
43                                 portName     string = "_ok_net0"
44                                 macIPAddress string = "0a:00:00:00:00:01 192.168.1.3"
45                         )
46                         fakeCmds := ovntest.AddFakeCmd(nil, &ovntest.ExpectedCmd{
47                                 Cmd:    "ovn-nbctl --timeout=15 --data=bare --no-heading --columns=name find logical_switch " + "name=" + netName,
48                                 Output: netName,
49                         })
50                         fakeCmds = ovntest.AddFakeCmdsNoOutputNoError(fakeCmds, []string{
51                                 "ovn-nbctl --timeout=15 --wait=sb -- --may-exist lsp-add " + netName + " " + portName + " -- lsp-set-addresses " + portName + " dynamic -- set logical_switch_port " + portName + " external-ids:namespace= external-ids:logical_switch=" + netName + " external-ids:pod=true",
52                         })
53
54                         fakeCmds = ovntest.AddFakeCmd(fakeCmds, &ovntest.ExpectedCmd{
55                                 Cmd:    "ovn-nbctl --timeout=15 get logical_switch_port " + portName + " dynamic_addresses",
56                                 Output: macIPAddress,
57                         })
58                         fakeCmds = ovntest.AddFakeCmd(fakeCmds, &ovntest.ExpectedCmd{
59                                 Cmd:    "ovn-nbctl --timeout=15 --if-exists get logical_switch " + netName + " external_ids:gateway_ip",
60                                 Output: gwCIDR,
61                         })
62
63                         fexec := &fakeexec.FakeExec{
64                                 CommandScript: fakeCmds,
65                                 LookPathFunc: func(file string) (string, error) {
66                                         return fmt.Sprintf("/fake-bin/%s", file), nil
67                                 },
68                         }
69                         oldSetupOvnUtils := SetupOvnUtils
70                         // as we are exiting, revert ConfigureInterface back  at end of function
71                         defer func() { SetupOvnUtils = oldSetupOvnUtils }()
72                         SetupOvnUtils = func() error {
73                                 return nil
74                         }
75                         ovnController, err := NewOvnController(fexec)
76                         Expect(err).NotTo(HaveOccurred())
77
78                         var (
79                                 okPod = v1.Pod{
80                                         TypeMeta: metav1.TypeMeta{
81                                                 Kind:       "Pod",
82                                                 APIVersion: "v1",
83                                         },
84                                         ObjectMeta: metav1.ObjectMeta{
85                                                 Name: "ok",
86                                         },
87                                         Spec: v1.PodSpec{
88                                                 Containers: []v1.Container{
89                                                         {
90                                                                 Name: "by-name",
91                                                         },
92                                                         {},
93                                                 },
94                                         },
95                                 }
96                         )
97                         a := []map[string]interface{}{{"name": "ovn-prot-net", "interface": "net0"}}
98                         ovnController.AddLogicalPorts(&okPod, a)
99                         Expect(fexec.CommandCalls).To(Equal(len(fakeCmds)))
100
101                         return nil
102                 }
103
104                 err := app.Run([]string{app.Name})
105                 Expect(err).NotTo(HaveOccurred())
106         })
107
108         It("tests Pod provider", func() {
109                 app.Action = func(ctx *cli.Context) error {
110                         const (
111                                 gwIP         string = "10.1.1.1"
112                                 gwCIDR       string = gwIP + "/24"
113                                 netName      string = "ovn-prot-net"
114                                 portName     string = "_ok_net0"
115                                 macIPAddress string = "0a:00:00:00:00:01 192.168.1.3/24"
116                         )
117                         fakeCmds := ovntest.AddFakeCmd(nil, &ovntest.ExpectedCmd{
118                                 Cmd:    "ovn-nbctl --timeout=15 --data=bare --no-heading --columns=name find logical_switch " + "name=" + netName,
119                                 Output: netName,
120                         })
121
122                         fakeCmds = ovntest.AddFakeCmdsNoOutputNoError(fakeCmds, []string{
123                                 "ovn-nbctl --timeout=15 --may-exist lsp-add " + netName + " " + portName + " -- lsp-set-addresses " + portName + " " + macIPAddress + " -- --if-exists clear logical_switch_port " + portName + " dynamic_addresses" + " -- set logical_switch_port " + portName + " external-ids:namespace= external-ids:logical_switch=" + netName + " external-ids:pod=true",
124                         })
125
126                         fakeCmds = ovntest.AddFakeCmd(fakeCmds, &ovntest.ExpectedCmd{
127                                 Cmd:    "ovn-nbctl --timeout=15 get logical_switch_port " + portName + " addresses",
128                                 Output: macIPAddress,
129                         })
130
131                         fakeCmds = ovntest.AddFakeCmd(fakeCmds, &ovntest.ExpectedCmd{
132                                 Cmd:    "ovn-nbctl --timeout=15 --if-exists get logical_switch " + netName + " external_ids:gateway_ip",
133                                 Output: gwCIDR,
134                         })
135
136                         fexec := &fakeexec.FakeExec{
137                                 CommandScript: fakeCmds,
138                                 LookPathFunc: func(file string) (string, error) {
139                                         return fmt.Sprintf("/fake-bin/%s", file), nil
140                                 },
141                         }
142                         oldSetupOvnUtils := SetupOvnUtils
143                         // as we are exiting, revert ConfigureInterface back  at end of function
144                         defer func() { SetupOvnUtils = oldSetupOvnUtils }()
145                         SetupOvnUtils = func() error {
146                                 return nil
147                         }
148                         ovnController, err := NewOvnController(fexec)
149                         Expect(err).NotTo(HaveOccurred())
150                         var (
151                                 okPod = v1.Pod{
152                                         TypeMeta: metav1.TypeMeta{
153                                                 Kind:       "Pod",
154                                                 APIVersion: "v1",
155                                         },
156                                         ObjectMeta: metav1.ObjectMeta{
157                                                 Name: "ok",
158                                         },
159                                         Spec: v1.PodSpec{
160                                                 Containers: []v1.Container{
161                                                         {
162                                                                 Name: "by-name",
163                                                         },
164                                                         {},
165                                                 },
166                                         },
167                                 }
168                         )
169                         a := []map[string]interface{}{{"name": "ovn-prot-net", "interface": "net0", "netType": "provider", "ipAddress": "192.168.1.3/24", "macAddress": "0a:00:00:00:00:01"}}
170                         ovnController.AddLogicalPorts(&okPod, a)
171                         Expect(fexec.CommandCalls).To(Equal(len(fakeCmds)))
172
173                         return nil
174                 }
175
176                 err := app.Run([]string{app.Name})
177                 Expect(err).NotTo(HaveOccurred())
178         })
179
180 })