| 1 |
# Copyright 2017 FUJITSU LABORATORIES LTD. |
| 2 |
|
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# 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, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import copy |
| 16 |
from unittest import mock |
| 17 |
from webob import exc |
| 18 |
|
| 19 |
from oslo_utils import uuidutils |
| 20 |
|
| 21 |
from neutron.api import extensions |
| 22 |
from neutron.conf import common as conf_common |
| 23 |
from neutron.tests.unit.api.v2 import test_base as test_api_v2 |
| 24 |
from neutron.tests.unit.extensions import base as test_extensions_base |
| 25 |
from neutron_lib.api.definitions import taas as taas_api |
| 26 |
|
| 27 |
from neutron_taas import extensions as taas_extensions |
| 28 |
|
| 29 |
_uuid = uuidutils.generate_uuid |
| 30 |
_get_path = test_api_v2._get_path |
| 31 |
|
| 32 |
TAP_SERVICE_PATH = 'taas/tap_services' |
| 33 |
TAP_FLOW_PATH = 'taas/tap_flows' |
| 34 |
|
| 35 |
|
| 36 |
class TaasExtensionTestCase(test_extensions_base.ExtensionTestCase): |
| 37 |
|
| 38 |
def setUp(self): |
| 39 |
conf_common.register_core_common_config_opts() |
| 40 |
extensions.append_api_extensions_path(taas_extensions.__path__) |
| 41 |
super().setUp() |
| 42 |
plural_mappings = {'tap_service': 'tap_services', |
| 43 |
'tap_flow': 'tap_flows'} |
| 44 |
self.setup_extension( |
| 45 |
'{}.{}'.format(taas_extensions.taas.TaasPluginBase.__module__, |
| 46 |
taas_extensions.taas.TaasPluginBase.__name__), |
| 47 |
taas_api.ALIAS, |
| 48 |
taas_extensions.taas.Taas, |
| 49 |
'taas', |
| 50 |
plural_mappings=plural_mappings, |
| 51 |
translate_resource_name=False) |
| 52 |
self.instance = self.plugin.return_value |
| 53 |
|
| 54 |
def test_create_tap_service(self): |
| 55 |
tenant_id = _uuid() |
| 56 |
tap_service_data = { |
| 57 |
'tenant_id': tenant_id, |
| 58 |
'name': 'MyTap', |
| 59 |
'description': 'This is my tap service', |
| 60 |
'port_id': _uuid(), |
| 61 |
'project_id': tenant_id, |
| 62 |
} |
| 63 |
data = {'tap_service': tap_service_data} |
| 64 |
expected_ret_val = copy.copy(data['tap_service']) |
| 65 |
expected_ret_val.update({'id': _uuid()}) |
| 66 |
instance = self.plugin.return_value |
| 67 |
self.instance.create_tap_service.return_value = expected_ret_val |
| 68 |
|
| 69 |
res = self.api.post(_get_path(TAP_SERVICE_PATH, fmt=self.fmt), |
| 70 |
self.serialize(data), |
| 71 |
content_type='application/%s' % self.fmt) |
| 72 |
instance.create_tap_service.assert_called_with( |
| 73 |
mock.ANY, |
| 74 |
tap_service=data) |
| 75 |
self.assertEqual(exc.HTTPCreated.code, res.status_int) |
| 76 |
res = self.deserialize(res) |
| 77 |
self.assertIn('tap_service', res) |
| 78 |
self.assertEqual(expected_ret_val, res['tap_service']) |
| 79 |
|
| 80 |
def test_delete_tap_service(self): |
| 81 |
self._test_entity_delete('tap_service') |
| 82 |
|
| 83 |
def _get_expected_tap_flow(self, data): |
| 84 |
return data |
| 85 |
|
| 86 |
def test_create_tap_flow(self): |
| 87 |
tenant_id = _uuid() |
| 88 |
tap_flow_data = { |
| 89 |
'tenant_id': tenant_id, |
| 90 |
'name': 'MyTapFlow', |
| 91 |
'description': 'This is my tap flow', |
| 92 |
'direction': 'BOTH', |
| 93 |
'tap_service_id': _uuid(), |
| 94 |
'source_port': _uuid(), |
| 95 |
'project_id': tenant_id, |
| 96 |
} |
| 97 |
data = {'tap_flow': tap_flow_data} |
| 98 |
expected_data = self._get_expected_tap_flow(data) |
| 99 |
expected_ret_val = copy.copy(expected_data['tap_flow']) |
| 100 |
expected_ret_val.update({'id': _uuid()}) |
| 101 |
instance = self.plugin.return_value |
| 102 |
instance.create_tap_flow.return_value = expected_ret_val |
| 103 |
|
| 104 |
res = self.api.post(_get_path(TAP_FLOW_PATH, fmt=self.fmt), |
| 105 |
self.serialize(data), |
| 106 |
content_type='application/%s' % self.fmt) |
| 107 |
instance.create_tap_flow.assert_called_with( |
| 108 |
mock.ANY, |
| 109 |
tap_flow=expected_data) |
| 110 |
self.assertEqual(exc.HTTPCreated.code, res.status_int) |
| 111 |
res = self.deserialize(res) |
| 112 |
self.assertIn('tap_flow', res) |
| 113 |
self.assertEqual(expected_ret_val, res['tap_flow']) |
| 114 |
|
| 115 |
def test_delete_tap_flow(self): |
| 116 |
self._test_entity_delete('tap_flow') |