| 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 |
from unittest import mock |
| 14 |
|
| 15 |
from neutron.conf.plugins.ml2.drivers.ovn import ovn_conf |
| 16 |
from neutron.tests import base |
| 17 |
from neutron_lib.callbacks import events |
| 18 |
from neutron_lib.callbacks import resources |
| 19 |
|
| 20 |
from neutron_taas.services.taas.service_drivers.ovn import helper |
| 21 |
|
| 22 |
|
| 23 |
class TestTaasOvnProviderHelper(base.BaseTestCase): |
| 24 |
|
| 25 |
def setUp(self): |
| 26 |
super().setUp() |
| 27 |
ovn_conf.register_opts() |
| 28 |
|
| 29 |
ovn_nb_idl = mock.patch( |
| 30 |
'neutron_taas.services.taas.service_drivers.ovn.ovsdb.' |
| 31 |
'impl_idl_taas.OvnNbIdlForTaas') |
| 32 |
self.mock_ovn_nb_idl = ovn_nb_idl.start() |
| 33 |
self.addCleanup(ovn_nb_idl.stop) |
| 34 |
|
| 35 |
mock_thread = mock.patch( |
| 36 |
'neutron_taas.services.taas.service_drivers.ovn.helper.' |
| 37 |
'threading.Thread') |
| 38 |
self.mock_thread_class = mock_thread.start() |
| 39 |
self.addCleanup(mock_thread.stop) |
| 40 |
|
| 41 |
mock.patch( |
| 42 |
'ovsdbapp.backend.ovs_idl.idlutils.get_schema_helper').start() |
| 43 |
self.helper = helper.TaasOvnProviderHelper() |
| 44 |
self.helper._post_fork_initialize( |
| 45 |
resources.PROCESS, events.AFTER_INIT, None) |
| 46 |
|
| 47 |
self.ovn_nbdb_api = mock.patch.object(self.helper, 'ovn_nbdb_api') |
| 48 |
self.ovn_nbdb_api.start() |
| 49 |
self.addCleanup(self.ovn_nbdb_api.stop) |
| 50 |
|
| 51 |
add_req_thread = mock.patch.object(helper.TaasOvnProviderHelper, |
| 52 |
'add_request') |
| 53 |
self.mock_add_request = add_req_thread.start() |
| 54 |
self.addCleanup(add_req_thread.stop) |
| 55 |
|
| 56 |
def test_mirror_add(self): |
| 57 |
port_id = '1234' |
| 58 |
name = 'foo_mirror' |
| 59 |
dest_ip = '10.92.10.5' |
| 60 |
type = 'gre' |
| 61 |
tunnel_id = 101 |
| 62 |
direction = 'to-lport' |
| 63 |
|
| 64 |
self.helper.mirror_add({ |
| 65 |
'name': name, |
| 66 |
'direction_filter': direction, |
| 67 |
'dest': dest_ip, |
| 68 |
'mirror_type': type, |
| 69 |
'index': tunnel_id, |
| 70 |
'port_id': port_id |
| 71 |
}) |
| 72 |
|
| 73 |
self.helper.ovn_nbdb_api.lookup.assert_called_once_with( |
| 74 |
'Logical_Switch_Port', port_id) |
| 75 |
self.helper.ovn_nbdb_api.mirror_add.assert_called_once_with( |
| 76 |
name=name, |
| 77 |
direction_filter=direction, |
| 78 |
dest=dest_ip, |
| 79 |
mirror_type=type, |
| 80 |
index=tunnel_id |
| 81 |
) |
| 82 |
self.helper.ovn_nbdb_api.lsp_attach_mirror.assert_called_once() |
| 83 |
|
| 84 |
def test_mirror_del(self): |
| 85 |
port_id = '1234' |
| 86 |
name = 'foo_mirror' |
| 87 |
dest_ip = '10.92.10.5' |
| 88 |
type = 'gre' |
| 89 |
tunnel_id = 101 |
| 90 |
direction = 'to-lport' |
| 91 |
|
| 92 |
self.helper.mirror_del({ |
| 93 |
'port_id': port_id, |
| 94 |
'name': name, |
| 95 |
'direction_filter': direction, |
| 96 |
'dest': dest_ip, |
| 97 |
'mirror_type': type, |
| 98 |
'index': tunnel_id, |
| 99 |
'port_id': port_id |
| 100 |
}) |
| 101 |
|
| 102 |
self.helper.ovn_nbdb_api.lookup.assert_called_once_with( |
| 103 |
'Logical_Switch_Port', port_id) |
| 104 |
self.helper.ovn_nbdb_api.mirror_get.assert_called_once_with(name) |
| 105 |
self.helper.ovn_nbdb_api.lsp_detach_mirror.assert_called_once() |
| 106 |
self.helper.ovn_nbdb_api.mirror_del.assert_called_once() |