| 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 copy |
| 14 |
from unittest import mock |
| 15 |
|
| 16 |
from neutron.tests import base |
| 17 |
from oslo_utils import uuidutils |
| 18 |
|
| 19 |
from neutron_taas.services.taas.service_drivers.ovn import helper |
| 20 |
from neutron_taas.services.taas.service_drivers.ovn import taas_ovn |
| 21 |
|
| 22 |
|
| 23 |
class FakeMirrorContext(): |
| 24 |
def __init__(self, tap_mirror): |
| 25 |
self._tap_mirror = tap_mirror |
| 26 |
|
| 27 |
@property |
| 28 |
def tap_mirror(self): |
| 29 |
return self._tap_mirror |
| 30 |
|
| 31 |
|
| 32 |
class TestTaasOvnDriver(base.BaseTestCase): |
| 33 |
|
| 34 |
def setUp(self): |
| 35 |
super().setUp() |
| 36 |
ovn_nb_idl = mock.patch( |
| 37 |
'neutron_taas.services.taas.service_drivers.ovn.ovsdb.' |
| 38 |
'impl_idl_taas.OvnNbIdlForTaas') |
| 39 |
self.mock_ovn_nb_idl = ovn_nb_idl.start() |
| 40 |
self.addCleanup(ovn_nb_idl.stop) |
| 41 |
|
| 42 |
mock_thread = mock.patch( |
| 43 |
'neutron_taas.services.taas.service_drivers.ovn.helper.' |
| 44 |
'threading.Thread') |
| 45 |
self.mock_thread_class = mock_thread.start() |
| 46 |
self.addCleanup(mock_thread.stop) |
| 47 |
|
| 48 |
self.driver = taas_ovn.TaasOvnDriver('tapmirror') |
| 49 |
|
| 50 |
add_req_thread = mock.patch.object(helper.TaasOvnProviderHelper, |
| 51 |
'add_request') |
| 52 |
self.mock_add_request = add_req_thread.start() |
| 53 |
self.addCleanup(add_req_thread.stop) |
| 54 |
|
| 55 |
self.tap_mirror_dict = { |
| 56 |
'mirror_type': 'gre', |
| 57 |
'directions': {'IN': 101}, |
| 58 |
'id': uuidutils.generate_uuid(), |
| 59 |
'remote_ip': '10.92.10.5', |
| 60 |
'port_id': uuidutils.generate_uuid() |
| 61 |
} |
| 62 |
self.multi_dir_t_mirror = copy.deepcopy(self.tap_mirror_dict) |
| 63 |
self.multi_dir_t_mirror['directions'] = {'IN': 101, |
| 64 |
'OUT': 102, |
| 65 |
'BOTH': 103} |
| 66 |
|
| 67 |
def test_create_tap_mirror_postcommit(self): |
| 68 |
ctx = FakeMirrorContext(self.tap_mirror_dict) |
| 69 |
self.driver.create_tap_mirror_postcommit(ctx) |
| 70 |
expected_dict = { |
| 71 |
'type': 'mirror_add', |
| 72 |
'info': { |
| 73 |
'name': mock.ANY, |
| 74 |
'direction_filter': 'to-lport', |
| 75 |
'dest': self.tap_mirror_dict['remote_ip'], |
| 76 |
'mirror_type': self.tap_mirror_dict['mirror_type'], |
| 77 |
'index': self.tap_mirror_dict['directions']['IN'], |
| 78 |
'port_id': self.tap_mirror_dict['port_id'], |
| 79 |
} |
| 80 |
} |
| 81 |
self.mock_add_request.assert_called_once_with(expected_dict) |
| 82 |
|
| 83 |
def test_create_tap_mirror_postcommit_multi_dir(self): |
| 84 |
ctx = FakeMirrorContext(self.multi_dir_t_mirror) |
| 85 |
self.driver.create_tap_mirror_postcommit(ctx) |
| 86 |
|
| 87 |
expected_in_call = { |
| 88 |
'type': 'mirror_add', |
| 89 |
'info': { |
| 90 |
'name': mock.ANY, |
| 91 |
'direction_filter': 'to-lport', |
| 92 |
'dest': self.tap_mirror_dict['remote_ip'], |
| 93 |
'mirror_type': self.tap_mirror_dict['mirror_type'], |
| 94 |
'index': self.tap_mirror_dict['directions']['IN'], |
| 95 |
'port_id': self.tap_mirror_dict['port_id'], |
| 96 |
} |
| 97 |
} |
| 98 |
expected_out_call = copy.deepcopy(expected_in_call) |
| 99 |
expected_out_call['info']['direction_filter'] = 'from-lport' |
| 100 |
out_dir_tun_id = self.multi_dir_t_mirror['directions']['OUT'] |
| 101 |
expected_out_call['info']['index'] = out_dir_tun_id |
| 102 |
|
| 103 |
expected_both_call = copy.deepcopy(expected_in_call) |
| 104 |
expected_both_call['info']['direction_filter'] = 'both' |
| 105 |
both_dir_tun_id = self.multi_dir_t_mirror['directions']['BOTH'] |
| 106 |
expected_both_call['info']['index'] = both_dir_tun_id |
| 107 |
expected_calls = [ |
| 108 |
mock.call(expected_in_call), |
| 109 |
mock.call(expected_out_call), |
| 110 |
mock.call(expected_both_call) |
| 111 |
] |
| 112 |
|
| 113 |
self.mock_add_request.assert_has_calls(expected_calls) |
| 114 |
|
| 115 |
def test_delete_tap_mirror_precommit(self): |
| 116 |
ctx = FakeMirrorContext(self.tap_mirror_dict) |
| 117 |
self.driver.delete_tap_mirror_precommit(ctx) |
| 118 |
|
| 119 |
expected_dict = { |
| 120 |
'type': 'mirror_del', |
| 121 |
'info': { |
| 122 |
'id': self.tap_mirror_dict['id'], |
| 123 |
'name': mock.ANY, |
| 124 |
'sink': self.tap_mirror_dict['remote_ip'], |
| 125 |
'port_id': self.tap_mirror_dict['port_id']} |
| 126 |
} |
| 127 |
self.mock_add_request.assert_called_once_with(expected_dict) |
| 128 |
|
| 129 |
def test_delete_tap_mirror_precommit_multi_dir(self): |
| 130 |
ctx = FakeMirrorContext(self.multi_dir_t_mirror) |
| 131 |
self.driver.delete_tap_mirror_precommit(ctx) |
| 132 |
|
| 133 |
expected_call = { |
| 134 |
'type': 'mirror_del', |
| 135 |
'info': { |
| 136 |
'id': self.tap_mirror_dict['id'], |
| 137 |
'name': mock.ANY, |
| 138 |
'sink': self.tap_mirror_dict['remote_ip'], |
| 139 |
'port_id': self.tap_mirror_dict['port_id'], |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
expected_calls = [ |
| 144 |
mock.call(expected_call), |
| 145 |
mock.call(expected_call) |
| 146 |
] |
| 147 |
|
| 148 |
self.mock_add_request.assert_has_calls(expected_calls) |