| 1 |
# Copyright 2018 Orange |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
from unittest import mock |
| 16 |
|
| 17 |
from networking_bagpipe.bagpipe_bgp.common import dataplane_utils |
| 18 |
from networking_bagpipe.bagpipe_bgp.vpn import dataplane_drivers as dp_drivers |
| 19 |
from networking_bagpipe.bagpipe_bgp.vpn.evpn import ovs |
| 20 |
from networking_bagpipe.tests.unit.bagpipe_bgp import base as t |
| 21 |
|
| 22 |
from neutron.agent.common import ovs_lib |
| 23 |
from neutron.plugins.ml2.drivers.openvswitch.agent.openflow.native import \ |
| 24 |
br_tun |
| 25 |
|
| 26 |
from neutron_lib.plugins.ml2 import ovs_constants as ovs_const |
| 27 |
|
| 28 |
LOCAL_IP = "1.2.3.4" |
| 29 |
MAC1 = "01:00:de:ad:be:ef" |
| 30 |
MAC2 = "01:00:fe:ed:f0:0d" |
| 31 |
|
| 32 |
|
| 33 |
class TestTunnelManager(t.TestCase): |
| 34 |
|
| 35 |
def setUp(self): |
| 36 |
super().setUp() |
| 37 |
|
| 38 |
self.bridge = mock.Mock(spec=br_tun.OVSTunnelBridge) |
| 39 |
self.manager = ovs.TunnelManager(self.bridge, LOCAL_IP) |
| 40 |
|
| 41 |
def test_get_tunnel(self): |
| 42 |
t1, _ = self.manager.get_object("2.2.2.2", "A") |
| 43 |
self.bridge.add_tunnel_port.assert_called_once_with(mock.ANY, |
| 44 |
"2.2.2.2", |
| 45 |
LOCAL_IP, |
| 46 |
mock.ANY) |
| 47 |
self.bridge.setup_tunnel_port.assert_called_once_with(mock.ANY, t1) |
| 48 |
|
| 49 |
self.bridge.add_tunnel_port.reset_mock() |
| 50 |
self.bridge.setup_tunnel_port.reset_mock() |
| 51 |
|
| 52 |
t2, _ = self.manager.get_object("2.2.2.2", "B") |
| 53 |
self.bridge.add_tunnel_port.assert_not_called() |
| 54 |
self.bridge.setup_tunnel_port.assert_not_called() |
| 55 |
|
| 56 |
self.assertEqual(t1, t2) |
| 57 |
|
| 58 |
self.bridge.add_tunnel_port.reset_mock() |
| 59 |
self.bridge.setup_tunnel_port.reset_mock() |
| 60 |
|
| 61 |
t3, _ = self.manager.get_object("3.3.3.3", "A") |
| 62 |
self.bridge.add_tunnel_port.assert_called_once() |
| 63 |
self.bridge.setup_tunnel_port.assert_called_once() |
| 64 |
|
| 65 |
self.assertEqual(t3, t1) |
| 66 |
|
| 67 |
self.assertTrue(len(self.manager.infos())) |
| 68 |
|
| 69 |
def test_free_object(self): |
| 70 |
t1, _ = self.manager.get_object("2.2.2.2", "A") |
| 71 |
self.manager.get_object("2.2.2.2", "B") |
| 72 |
t2, _ = self.manager.get_object("3.3.3.3", "A") |
| 73 |
|
| 74 |
self.bridge.add_tunnel_port.reset_mock() |
| 75 |
self.bridge.delete_port.reset_mock() |
| 76 |
self.bridge.setup_tunnel_port.reset_mock() |
| 77 |
|
| 78 |
self.manager.free_object("2.2.2.2", "A") |
| 79 |
self.bridge.delete_port.assert_not_called() |
| 80 |
t1bis = self.manager.find_object("2.2.2.2") |
| 81 |
self.assertEqual(t1bis, t1) |
| 82 |
|
| 83 |
self.bridge.add_tunnel_port.reset_mock() |
| 84 |
self.bridge.delete_port.reset_mock() |
| 85 |
self.bridge.setup_tunnel_port.reset_mock() |
| 86 |
|
| 87 |
self.manager.free_object("2.2.2.2", "B") |
| 88 |
self.bridge.delete_port.assert_called_once_with(t1) |
| 89 |
|
| 90 |
self.bridge.add_tunnel_port.reset_mock() |
| 91 |
self.bridge.delete_port.reset_mock() |
| 92 |
self.bridge.setup_tunnel_port.reset_mock() |
| 93 |
|
| 94 |
self.manager.free_object("3.3.3.3", "A") |
| 95 |
self.bridge.delete_port.assert_called_once_with(t2) |
| 96 |
|
| 97 |
|
| 98 |
class FakeBridgeMockSpec(dataplane_utils.OVSBridgeWithGroups, |
| 99 |
br_tun.OVSTunnelBridge, |
| 100 |
ovs_lib.OVSBridge): |
| 101 |
pass |
| 102 |
|
| 103 |
|
| 104 |
class FakeNLRI: |
| 105 |
|
| 106 |
def __init__(self, ip): |
| 107 |
self.ip = ip |
| 108 |
|
| 109 |
|
| 110 |
class TestOVSEVIDataplane(t.TestCase): |
| 111 |
|
| 112 |
def setUp(self): |
| 113 |
super().setUp() |
| 114 |
|
| 115 |
self.bridge = mock.Mock(spec=FakeBridgeMockSpec) |
| 116 |
self.tunnel_mgr = mock.Mock(spec=ovs.TunnelManager) |
| 117 |
self.tunnel_mgr.get_object.return_value = ("TUNNEL1", None) |
| 118 |
self.tunnel_mgr.find_object.return_value = "TUNNEL1" |
| 119 |
|
| 120 |
self.dp_driver = mock.Mock(spec=dp_drivers.DataplaneDriver) |
| 121 |
self.dp_driver.bridge = self.bridge |
| 122 |
self.dp_driver.tunnel_mgr = self.tunnel_mgr |
| 123 |
self.dp_driver.config = {} |
| 124 |
self.dp_driver.get_local_address.return_value = LOCAL_IP |
| 125 |
|
| 126 |
self.dataplane = ovs.OVSEVIDataplane( |
| 127 |
self.dp_driver, 77, "foo_external_instance_id", |
| 128 |
None, None, instance_label=99) |
| 129 |
self.vlan = 99 |
| 130 |
|
| 131 |
self.dataplane.vif_plugged("MAC1", "10.0.0.1", |
| 132 |
{'vlan': self.vlan}, |
| 133 |
None, None) |
| 134 |
self.bridge.add_flow.assert_called_once_with( |
| 135 |
table=ovs_const.VXLAN_TUN_TO_LV, |
| 136 |
priority=mock.ANY, |
| 137 |
tun_id=99, |
| 138 |
actions=mock.ANY) |
| 139 |
|
| 140 |
def test_setup_dataplane_for_remote_endpoint__local(self): |
| 141 |
self.dataplane.setup_dataplane_for_remote_endpoint( |
| 142 |
MAC1, LOCAL_IP, 42, FakeNLRI("11.0.0.1"), None) |
| 143 |
|
| 144 |
self.tunnel_mgr.get_object.assert_not_called() |
| 145 |
|
| 146 |
def test_setup_dataplane_for_remote_endpoint(self): |
| 147 |
self.dataplane.setup_dataplane_for_remote_endpoint( |
| 148 |
MAC1, "2.2.2.2", 42, FakeNLRI("11.0.0.1"), None) |
| 149 |
|
| 150 |
self.tunnel_mgr.get_object.assert_called_once_with( |
| 151 |
"2.2.2.2", (77, (42, MAC1))) |
| 152 |
|
| 153 |
self.bridge.add_flow.assert_called_with( |
| 154 |
table=ovs_const.UCAST_TO_TUN, |
| 155 |
priority=mock.ANY, |
| 156 |
dl_vlan=self.vlan, |
| 157 |
dl_dst=MAC1, |
| 158 |
actions=mock.ANY) |
| 159 |
|
| 160 |
def test_remove_dataplane_for_remote_endpoint__local(self): |
| 161 |
self.dataplane.remove_dataplane_for_remote_endpoint( |
| 162 |
MAC1, LOCAL_IP, 42, FakeNLRI("11.0.0.1")) |
| 163 |
|
| 164 |
self.bridge.delete_unicast_to_tun.assert_called_with(self.vlan, MAC1) |
| 165 |
|
| 166 |
self.tunnel_mgr.free_object.assert_not_called() |
| 167 |
|
| 168 |
def test_remove_dataplane_for_remote_endpoint(self): |
| 169 |
self.dataplane.remove_dataplane_for_remote_endpoint( |
| 170 |
MAC1, "2.2.2.2", 42, FakeNLRI("11.0.0.1")) |
| 171 |
|
| 172 |
self.bridge.delete_unicast_to_tun.assert_called_with(self.vlan, MAC1) |
| 173 |
|
| 174 |
self.tunnel_mgr.free_object.assert_called_with( |
| 175 |
"2.2.2.2", (77, (42, MAC1))) |
| 176 |
|
| 177 |
def test_add_dataplane_for_bum_endpoint__local(self): |
| 178 |
self.dataplane.add_dataplane_for_bum_endpoint(LOCAL_IP, 45, |
| 179 |
None, None) |
| 180 |
|
| 181 |
self.tunnel_mgr.get_object.assert_not_called() |
| 182 |
|
| 183 |
def test_add_dataplane_for_bum_endpoint(self): |
| 184 |
self.dataplane.add_dataplane_for_bum_endpoint("2.2.2.2", 45, |
| 185 |
None, None) |
| 186 |
|
| 187 |
self.tunnel_mgr.get_object.assert_called_with( |
| 188 |
"2.2.2.2", (77, (45, "flood"))) |
| 189 |
|
| 190 |
def test_remove_dataplane_for_bum_endpoint__local(self): |
| 191 |
self.dataplane.add_dataplane_for_bum_endpoint(LOCAL_IP, 45, |
| 192 |
None, None) |
| 193 |
self.tunnel_mgr.free_object.reset_mock() |
| 194 |
|
| 195 |
self.dataplane.remove_dataplane_for_bum_endpoint(LOCAL_IP, 45, None) |
| 196 |
|
| 197 |
self.tunnel_mgr.free_object.assert_not_called() |
| 198 |
|
| 199 |
def test_remove_dataplane_for_bum_endpoint(self): |
| 200 |
self.dataplane.add_dataplane_for_bum_endpoint("2.2.2.2", 45, |
| 201 |
None, None) |
| 202 |
self.tunnel_mgr.free_object.reset_mock() |
| 203 |
|
| 204 |
self.dataplane.remove_dataplane_for_bum_endpoint("2.2.2.2", 45, None) |
| 205 |
|
| 206 |
self.tunnel_mgr.free_object.assert_called_with( |
| 207 |
"2.2.2.2", (77, (45, "flood"))) |