7 "github.com/urfave/cli"
8 fakeexec "k8s.io/utils/exec/testing"
11 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12 "ovn4nfv-k8s-plugin/internal/pkg/config"
13 ovntest "ovn4nfv-k8s-plugin/internal/pkg/testing"
15 . "github.com/onsi/ginkgo"
16 . "github.com/onsi/gomega"
19 func TestOvn(t *testing.T) {
20 RegisterFailHandler(Fail)
21 RunSpecs(t, "OVN/Pod Test Suite")
24 var _ = AfterSuite(func() {
27 var _ = Describe("Add logical Port", func() {
33 app.Flags = config.Flags
37 It("tests Pod", func() {
38 app.Action = func(ctx *cli.Context) error {
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"
46 fakeCmds := ovntest.AddFakeCmd(nil, &ovntest.ExpectedCmd{
47 Cmd: "ovn-nbctl --timeout=15 --data=bare --no-heading --columns=name find logical_switch " + "name=" + netName,
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",
54 fakeCmds = ovntest.AddFakeCmd(fakeCmds, &ovntest.ExpectedCmd{
55 Cmd: "ovn-nbctl --timeout=15 get logical_switch_port " + portName + " dynamic_addresses",
58 fakeCmds = ovntest.AddFakeCmd(fakeCmds, &ovntest.ExpectedCmd{
59 Cmd: "ovn-nbctl --timeout=15 --if-exists get logical_switch " + netName + " external_ids:gateway_ip",
63 fexec := &fakeexec.FakeExec{
64 CommandScript: fakeCmds,
65 LookPathFunc: func(file string) (string, error) {
66 return fmt.Sprintf("/fake-bin/%s", file), nil
69 oldSetupOvnUtils := SetupOvnUtils
70 // as we are exiting, revert ConfigureInterface back at end of function
71 defer func() { SetupOvnUtils = oldSetupOvnUtils }()
72 SetupOvnUtils = func() error {
75 ovnController, err := NewOvnController(fexec)
76 Expect(err).NotTo(HaveOccurred())
80 TypeMeta: metav1.TypeMeta{
84 ObjectMeta: metav1.ObjectMeta{
88 Containers: []v1.Container{
97 a := []map[string]interface{}{{"name": "ovn-prot-net", "interface": "net0"}}
98 ovnController.AddLogicalPorts(&okPod, a)
99 Expect(fexec.CommandCalls).To(Equal(len(fakeCmds)))
104 err := app.Run([]string{app.Name})
105 Expect(err).NotTo(HaveOccurred())
108 It("tests Pod provider", func() {
109 app.Action = func(ctx *cli.Context) error {
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"
117 fakeCmds := ovntest.AddFakeCmd(nil, &ovntest.ExpectedCmd{
118 Cmd: "ovn-nbctl --timeout=15 --data=bare --no-heading --columns=name find logical_switch " + "name=" + netName,
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",
126 fakeCmds = ovntest.AddFakeCmd(fakeCmds, &ovntest.ExpectedCmd{
127 Cmd: "ovn-nbctl --timeout=15 get logical_switch_port " + portName + " addresses",
128 Output: macIPAddress,
131 fakeCmds = ovntest.AddFakeCmd(fakeCmds, &ovntest.ExpectedCmd{
132 Cmd: "ovn-nbctl --timeout=15 --if-exists get logical_switch " + netName + " external_ids:gateway_ip",
136 fexec := &fakeexec.FakeExec{
137 CommandScript: fakeCmds,
138 LookPathFunc: func(file string) (string, error) {
139 return fmt.Sprintf("/fake-bin/%s", file), nil
142 oldSetupOvnUtils := SetupOvnUtils
143 // as we are exiting, revert ConfigureInterface back at end of function
144 defer func() { SetupOvnUtils = oldSetupOvnUtils }()
145 SetupOvnUtils = func() error {
148 ovnController, err := NewOvnController(fexec)
149 Expect(err).NotTo(HaveOccurred())
152 TypeMeta: metav1.TypeMeta{
156 ObjectMeta: metav1.ObjectMeta{
160 Containers: []v1.Container{
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)))
176 err := app.Run([]string{app.Name})
177 Expect(err).NotTo(HaveOccurred())