Debian Codegraph @

networking_bagpipe/tests/unit/bagpipe_bgp/ipvpn/mpls_ovs_dataplane.py

raw
1 # Copyright 2017 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 # NOTE(tmorin): unit tests for system interactions aren't that useful, which
16 # is why you will find very few things here. The code is is expected to be
17 # covered by tempest and fullstack test jobstores
18
19 from unittest import mock
20
21 from networking_bagpipe.bagpipe_bgp.common import dataplane_utils
22 from networking_bagpipe.bagpipe_bgp.vpn.ipvpn import mpls_ovs_dataplane
23 from networking_bagpipe.tests.unit.bagpipe_bgp import base as t
24
25 INSTANCE_ID = 77
26
27 LOCAL_IP = "1.1.1.1"
28 REMOTE_PE1 = "2.2.2.2"
29 REMOTE_PE2 = "3.3.3.3"
30
31 NH1 = mpls_ovs_dataplane.NextHop(1, LOCAL_IP, None, 0)
32 NH2 = mpls_ovs_dataplane.NextHop(2, LOCAL_IP, None, 1)
33
34 REMOTE_PREFIX1 = "11.0.0.2"
35
36
37 class TestMPLSOVSDataplaneDriver(t.TestCase):
38
39 def _test_priority_for_prefix(self, prefix_list):
40 # assuming that prefix_list is a list of prefixes of increasing length
41 # check that each prefixes has a priority higher than the previous one
42 previous_prio = 0
43 previous_prefix = ""
44 for prefix in prefix_list:
45 prio = mpls_ovs_dataplane._priority_from_prefix(prefix)
46 self.assertTrue(prio > previous_prio,
47 ("%s should have a priority higher than %s, "
48 "but 0x%x !> 0x%x") % (prefix, previous_prefix,
49 prio, previous_prio))
50 previous_prio = prio
51 previous_prefix = prefix
52
53 def test_priority_for_prefix_v4(self):
54 self._test_priority_for_prefix(
55 ["0.0.0.0/0", "1.1.0.0/16", "2.2.2.0/24", "3.3.3.3/32"])
56
57 def test_priority_for_prefix_v6(self):
58 self._test_priority_for_prefix(
59 ["::0/0", "2001:db8:8:4::2/64", "::1/128"])
60
61 def test_fallback_priority(self):
62 self.assertLess(mpls_ovs_dataplane.FALLBACK_PRIORITY,
63 mpls_ovs_dataplane._priority_from_prefix("0.0.0.0/0"))
64
65
66 class TestNextHopGroupManager(t.TestCase):
67
68 def setUp(self):
69 super().setUp()
70
71 self.bridge = mock.Mock(spec=dataplane_utils.OVSBridgeWithGroups)
72 self.manager = mpls_ovs_dataplane.NextHopGroupManager(self.bridge,
73 None,
74 None,
75 None)
76
77 def test_get_nexthop_group(self):
78 nh_g1 = self.manager.get_object("2.2.2.2", NH1)
79
80 self.bridge.add_group.assert_called_once_with(
81 group_id=nh_g1,
82 type='select',
83 selection_method=None,
84 selection_method_param=None,
85 fields=None)
86
87 self.bridge.add_group.reset_mock()
88
89 nh_g2 = self.manager.get_object("2.2.2.2", NH2)
90 self.bridge.add_group.assert_not_called()
91
92 self.assertEqual(nh_g1, nh_g2)
93
94 self.bridge.add_group.reset_mock()
95
96 nh_g3 = self.manager.get_object("3.3.3.3", NH1)
97 self.bridge.add_group.assert_called_once()
98
99 self.assertNotEqual(nh_g3, nh_g1)
100
101 self.assertTrue(len(self.manager.infos()))
102
103 def test_free_object(self):
104 nh_g1 = self.manager.get_object("2.2.2.2", NH1)
105 self.manager.get_object("2.2.2.2", NH2)
106 nh_g2 = self.manager.get_object("3.3.3.3", NH1)
107
108 self.bridge.add_group.reset_mock()
109
110 self.manager.free_object("2.2.2.2", NH1)
111 self.bridge.delete_group.assert_not_called()
112 nh_g1bis = self.manager.get_object("2.2.2.2")
113 self.assertEqual(nh_g1bis, nh_g1)
114
115 self.manager.free_object("2.2.2.2", NH2)
116 self.bridge.delete_group.assert_called_once_with(nh_g1)
117
118 self.bridge.delete_group.reset_mock()
119
120 self.manager.free_object("3.3.3.3", NH1)
121 self.bridge.delete_group.assert_called_once_with(nh_g2)
122
123
124 class FakeBridgeMockSpec(dataplane_utils.OVSBridgeWithGroups,
125 dataplane_utils.OVSExtendedBridge):
126 pass
127
128
129 class FakeNLRI:
130
131 def __init__(self, ip):
132 self.ip = ip
133
134
135 class TestMPLSOVSVRFSDataplane(t.TestCase):
136
137 def setUp(self):
138 super().setUp()
139
140 self.bridge = mock.Mock(spec=FakeBridgeMockSpec)
141
142 self.nh_group_mgr = mock.Mock(
143 spec=mpls_ovs_dataplane.NextHopGroupManager
144 )
145 self.nh_group_mgr.bridge = self.bridge
146 self.nh_group_mgr.get_object.side_effect = [None, 0, 0]
147
148 self.dp_driver = mock.Mock(
149 spec=mpls_ovs_dataplane.MPLSOVSDataplaneDriver
150 )
151 self.dp_driver.bridge = self.bridge
152 self.dp_driver.nh_group_mgr = self.nh_group_mgr
153 self.dp_driver.vxlan_encap = False
154 self.dp_driver.vrf_table = 3
155 self.dp_driver.config = mock.Mock()
156 self.dp_driver.config.arp_responder = False
157 self.dp_driver.get_local_address.return_value = LOCAL_IP
158 self.dp_driver.mpls_in_port.return_value = 1
159
160 self.label = 99
161
162 self.dataplane = mpls_ovs_dataplane.MPLSOVSVRFDataplane(
163 self.dp_driver, INSTANCE_ID, "foo_external_instance_id",
164 "10.0.0.1", "24", instance_label=self.label)
165 self.dataplane._mtu_fixup = mock.Mock()
166 self.dataplane._match_label_action = mock.Mock(return_value="")
167 self.dataplane._match_output_action = mock.Mock(return_value="")
168
169 def test_setup_dataplane_for_remote_endpoint(self):
170 self.dataplane.setup_dataplane_for_remote_endpoint(
171 REMOTE_PREFIX1, REMOTE_PE1, 42, FakeNLRI(REMOTE_PREFIX1), None)
172
173 expected_nh = mpls_ovs_dataplane.NextHop(42, REMOTE_PE1, None, 0)
174 self.nh_group_mgr.get_object.assert_called_with(
175 REMOTE_PREFIX1, (INSTANCE_ID, mock.ANY), buckets=mock.ANY)
176 nh1 = self.nh_group_mgr.get_object.call_args_list[1][0][1][1]
177 self.assertEqual(expected_nh, nh1)
178
179 self.bridge.add_flow.assert_called_once_with(
180 table=3,
181 cookie=mock.ANY,
182 priority=mock.ANY,
183 nw_dst=REMOTE_PREFIX1,
184 actions="group:0")
185
186 self.nh_group_mgr.get_object.reset_mock()
187 self.bridge.add_flow.reset_mock()
188
189 self.dataplane.setup_dataplane_for_remote_endpoint(
190 REMOTE_PREFIX1, REMOTE_PE2, 43, FakeNLRI(REMOTE_PREFIX1), None, 1)
191
192 self.nh_group_mgr.get_object.assert_called_with(
193 REMOTE_PREFIX1)
194
195 self.bridge.add_flow.assert_not_called()
196 self.bridge.insert_bucket.assert_called_once_with(
197 group_id=0,
198 bucket_id=1,
199 command_bucket_id="last",
200 actions=mock.ANY)