aboutsummaryrefslogtreecommitdiffstats
path: root/internal/pkg/ovn/ovn_test.go
blob: 312a26a863cfc4278700909f6d1d445452093fa0 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package ovn

import (
	"fmt"
	"testing"

	"github.com/urfave/cli"
	fakeexec "k8s.io/utils/exec/testing"

	"k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes/fake"
	"ovn4nfv-k8s-plugin/internal/pkg/config"
	"ovn4nfv-k8s-plugin/internal/pkg/factory"
	ovntest "ovn4nfv-k8s-plugin/internal/pkg/testing"
	"ovn4nfv-k8s-plugin/internal/pkg/util"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

func TestOvn(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "OVN/Pod Test Suite")
}

var _ = AfterSuite(func() {
})

var _ = Describe("Add logical Port", func() {
	var app *cli.App

	BeforeEach(func() {

		app = cli.NewApp()
		app.Name = "test"
		app.Flags = config.Flags
	})

	It("tests Pod", func() {
		app.Action = func(ctx *cli.Context) error {
			const (
				gwIP         string = "10.1.1.1"
				gwCIDR       string = gwIP + "/24"
				netName      string = "ovn-prot-net"
				portName     string = "_ok_net0"
				macIPAddress string = "0a:00:00:00:00:01 192.168.1.3"
			)
			fakeCmds := ovntest.AddFakeCmd(nil, &ovntest.ExpectedCmd{
				Cmd:    "ovn-nbctl --timeout=15 --data=bare --no-heading --columns=name find logical_switch " + "name=" + netName,
				Output: netName,
			})
			fakeCmds = ovntest.AddFakeCmdsNoOutputNoError(fakeCmds, []string{
				"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",
			})

			fakeCmds = ovntest.AddFakeCmd(fakeCmds, &ovntest.ExpectedCmd{
				Cmd:    "ovn-nbctl --timeout=15 get logical_switch_port " + portName + " dynamic_addresses",
				Output: macIPAddress,
			})
			fakeCmds = ovntest.AddFakeCmd(fakeCmds, &ovntest.ExpectedCmd{
				Cmd:    "ovn-nbctl --timeout=15 --if-exists get logical_switch " + netName + " external_ids:gateway_ip",
				Output: gwCIDR,
			})

			fexec := &fakeexec.FakeExec{
				CommandScript: fakeCmds,
				LookPathFunc: func(file string) (string, error) {
					return fmt.Sprintf("/fake-bin/%s", file), nil
				},
			}

			err := util.SetExec(fexec)
			Expect(err).NotTo(HaveOccurred())

			fakeClient := &fake.Clientset{}
			var fakeWatchFactory factory.WatchFactory

			ovnController := NewOvnController(fakeClient, &fakeWatchFactory)
			Expect(err).NotTo(HaveOccurred())
			var (
				okPod = v1.Pod{
					TypeMeta: metav1.TypeMeta{
						Kind:       "Pod",
						APIVersion: "v1",
					},
					ObjectMeta: metav1.ObjectMeta{
						Name:        "ok",
						Annotations: map[string]string{"ovnNetwork": "[{ \"name\": \"ovn-prot-net\", \"interface\": \"net0\" , \"defaultGateway\": \"true\"}]"},
					},
					Spec: v1.PodSpec{
						Containers: []v1.Container{
							{
								Name: "by-name",
							},
							{},
						},
					},
				}
			)

			ovnController.addLogicalPort(&okPod)
			Expect(fexec.CommandCalls).To(Equal(len(fakeCmds)))

			return nil
		}

		err := app.Run([]string{app.Name})
		Expect(err).NotTo(HaveOccurred())
	})

	It("tests Pod provider", func() {
		app.Action = func(ctx *cli.Context) error {
			const (
				gwIP         string = "10.1.1.1"
				gwCIDR       string = gwIP + "/24"
				netName      string = "ovn-prot-net"
				portName     string = "_ok_net0"
				macIPAddress string = "0a:00:00:00:00:01 192.168.1.3/24"
			)
			fakeCmds := ovntest.AddFakeCmd(nil, &ovntest.ExpectedCmd{
				Cmd:    "ovn-nbctl --timeout=15 --data=bare --no-heading --columns=name find logical_switch " + "name=" + netName,
				Output: netName,
			})

			fakeCmds = ovntest.AddFakeCmdsNoOutputNoError(fakeCmds, []string{
				"ovn-nbctl --timeout=15 --may-exist lsp-add " + netName + " " + portName + " -- lsp-set-addresses " + portName + " " + macIPAddress + " -- --if-exists clear logical_switch_port " + portName + " dynamic_addresses",
			})

			fakeCmds = ovntest.AddFakeCmd(fakeCmds, &ovntest.ExpectedCmd{
				Cmd:    "ovn-nbctl --timeout=15 get logical_switch_port " + portName + " addresses",
				Output: macIPAddress,
			})

			fexec := &fakeexec.FakeExec{
				CommandScript: fakeCmds,
				LookPathFunc: func(file string) (string, error) {
					return fmt.Sprintf("/fake-bin/%s", file), nil
				},
			}
			err := util.SetExec(fexec)
			Expect(err).NotTo(HaveOccurred())

			_, err = config.InitConfig(ctx, fexec, nil)
			Expect(err).NotTo(HaveOccurred())

			fakeClient := &fake.Clientset{}
			var fakeWatchFactory factory.WatchFactory

			ovnController := NewOvnController(fakeClient, &fakeWatchFactory)
			var (
				okPod = v1.Pod{
					TypeMeta: metav1.TypeMeta{
						Kind:       "Pod",
						APIVersion: "v1",
					},
					ObjectMeta: metav1.ObjectMeta{
						Name:        "ok",
						Annotations: map[string]string{"ovnNetwork": "[{ \"name\": \"ovn-prot-net\", \"interface\": \"net0\", \"netType\": \"provider\", \"ipAddress\": \"192.168.1.3/24\", \"macAddress\": \"0a:00:00:00:00:01\" }]"},
					},
					Spec: v1.PodSpec{
						Containers: []v1.Container{
							{
								Name: "by-name",
							},
							{},
						},
					},
				}
			)
			ovnController.addLogicalPort(&okPod)
			Expect(fexec.CommandCalls).To(Equal(len(fakeCmds)))

			return nil
		}

		err := app.Run([]string{app.Name})
		Expect(err).NotTo(HaveOccurred())
	})

})