Debian Codegraph @

neutron_taas/tests/unit/services/taas/test_tap_mirror_plugin.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import contextlib
14
15 import testtools
16 from unittest import mock
17
18 from neutron.tests.unit import testlib_api
19 from neutron_lib import context
20 from neutron_lib.exceptions import taas as taas_exc
21 from neutron_lib import rpc as n_rpc
22 from neutron_lib.utils import net as n_utils
23 from oslo_utils import uuidutils
24
25 from neutron_taas.services.taas import tap_mirror_plugin
26
27
28 class TestTapMirrorPlugin(testlib_api.SqlTestCase):
29 def setUp(self):
30 super().setUp()
31 mock.patch.object(n_rpc, 'Connection', spec=object).start()
32
33 self.driver = mock.MagicMock()
34 mock.patch('neutron.services.service_base.load_drivers',
35 return_value=({'dummy_provider': self.driver},
36 'dummy_provider')).start()
37 mock.patch('neutron.db.servicetype_db.ServiceTypeManager.get_instance',
38 return_value=mock.MagicMock()).start()
39 self._plugin = tap_mirror_plugin.TapMirrorPlugin()
40 self._context = context.get_admin_context()
41
42 self._project_id = self._tenant_id = uuidutils.generate_uuid()
43 self._network_id = uuidutils.generate_uuid()
44 self._host_id = 'host-A'
45 self._port_id = uuidutils.generate_uuid()
46 self._port_details = {
47 'tenant_id': self._tenant_id,
48 'binding:host_id': self._host_id,
49 'mac_address': n_utils.get_random_mac(
50 'fa:16:3e:00:00:00'.split(':')),
51 }
52 self._tap_mirror = {
53 'project_id': self._project_id,
54 'tenant_id': self._tenant_id,
55 'name': 'MyMirror',
56 'description': 'This is my Tap Mirror',
57 'port_id': self._port_id,
58 'directions': {"IN": 101},
59 'remote_ip': '10.99.8.3',
60 'mirror_type': 'gre',
61 }
62
63 @contextlib.contextmanager
64 def tap_mirror(self, **kwargs):
65 self._tap_mirror.update(kwargs)
66 req = {
67 'tap_mirror': self._tap_mirror,
68 }
69 with mock.patch.object(self._plugin, 'get_port_details',
70 return_value=self._port_details):
71 mirror = self._plugin.create_tap_mirror(self._context, req)
72 self._tap_mirror['id'] = mock.ANY
73
74 self.driver.assert_has_calls([
75 mock.call.create_tap_mirror_precommit(mock.ANY),
76 mock.call.create_tap_mirror_postcommit(mock.ANY),
77 ])
78 pre_call_args = self.driver.create_tap_mirror_precommit.call_args[0][0]
79 self.assertEqual(self._context, pre_call_args._plugin_context)
80 self.assertEqual(self._tap_mirror, pre_call_args.tap_mirror)
81
82 post_call_args = self.driver.create_tap_mirror_postcommit.call_args
83 post_call_args = post_call_args[0][0]
84 self.assertEqual(self._context, post_call_args._plugin_context)
85 self.assertEqual(self._tap_mirror, post_call_args.tap_mirror)
86
87 yield self._plugin.get_tap_mirror(self._context,
88 mirror['id'])
89
90 def test_create_tap_mirror(self):
91 with self.tap_mirror():
92 pass
93
94 def test_create_tap_mirror_wrong_project_id(self):
95 self._port_details['project_id'] = 'other-tenant'
96 self._port_details['tenant_id'] = 'other-tenant'
97 with testtools.ExpectedException(taas_exc.PortDoesNotBelongToTenant), \
98 self.tap_mirror():
99 pass
100 self.assertEqual([], self.driver.mock_calls)
101
102 def test_create_duplicate_tunnel_id(self):
103 with self.tap_mirror() as tm1:
104 with mock.patch.object(self._plugin, 'get_tap_mirrors',
105 return_value=[tm1]):
106 with testtools.ExpectedException(
107 taas_exc.TapMirrorTunnelConflict), \
108 self.tap_mirror(directions={"IN": 101}):
109 pass
110
111 def test_create_different_tunnel_id(self):
112 with self.tap_mirror() as tm1:
113 with mock.patch.object(self._plugin, 'get_tap_mirrors',
114 return_value=[tm1]):
115 with self.tap_mirror(directions={"IN": 102}):
116 pass
117
118 def test_same_tunnel_id_different_direction(self):
119 with self.tap_mirror() as tm1:
120 with mock.patch.object(self._plugin, 'get_tap_mirrors',
121 return_value=[tm1]):
122 with testtools.ExpectedException(
123 taas_exc.TapMirrorTunnelConflict), \
124 self.tap_mirror(directions={"OUT": 101}):
125 pass
126
127 def test_two_direction_tunnel_id(self):
128 with self.tap_mirror(directions={'IN': 101, 'OUT': 102}) as tm1:
129 with mock.patch.object(self._plugin, 'get_tap_mirrors',
130 return_value=[tm1]):
131 with testtools.ExpectedException(
132 taas_exc.TapMirrorTunnelConflict), \
133 self.tap_mirror(directions={"OUT": 101}):
134 pass
135
136 def test_delete_tap_mrror(self):
137 with self.tap_mirror() as tm:
138 self._plugin.delete_tap_mirror(self._context, tm['id'])
139 self._tap_mirror['id'] = tm['id']
140
141 def test_delete_tap_mirror_non_existent(self):
142 with testtools.ExpectedException(taas_exc.TapMirrorNotFound):
143 self._plugin.delete_tap_mirror(self._context, 'non-existent')