| 1 |
# (c) Copyright 2015 NEC Corporation, All Rights Reserved. |
| 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 |
|
| 18 |
from oslo_utils import uuidutils |
| 19 |
from webob import exc |
| 20 |
|
| 21 |
from neutron.tests.unit.api.v2 import test_base as test_api_v2 |
| 22 |
from neutron_lib.plugins import constants as nconstants |
| 23 |
|
| 24 |
from neutron_vpnaas.extensions import vpn_endpoint_groups |
| 25 |
from neutron_vpnaas.extensions import vpnaas |
| 26 |
from neutron_vpnaas.services.vpn.common import constants |
| 27 |
from neutron_vpnaas.tests import base |
| 28 |
|
| 29 |
_uuid = uuidutils.generate_uuid |
| 30 |
_get_path = test_api_v2._get_path |
| 31 |
|
| 32 |
|
| 33 |
class VpnEndpointGroupsTestPlugin( |
| 34 |
vpnaas.VPNPluginBase, |
| 35 |
vpn_endpoint_groups.VPNEndpointGroupsPluginBase): |
| 36 |
pass |
| 37 |
|
| 38 |
|
| 39 |
class VpnEndpointGroupsTestCase(base.ExtensionTestCase): |
| 40 |
|
| 41 |
fmt = 'json' |
| 42 |
|
| 43 |
def setUp(self): |
| 44 |
super().setUp() |
| 45 |
plural_mappings = {'endpoint_group': 'endpoint-groups'} |
| 46 |
self.setup_extension( |
| 47 |
'neutron_vpnaas.tests.unit.extensions.test_vpn_endpoint_groups.' |
| 48 |
'VpnEndpointGroupsTestPlugin', |
| 49 |
nconstants.VPN, |
| 50 |
vpn_endpoint_groups.Vpn_endpoint_groups, |
| 51 |
'vpn', plural_mappings=plural_mappings, |
| 52 |
use_quota=True) |
| 53 |
|
| 54 |
def helper_test_endpoint_group_create(self, data): |
| 55 |
"""Check that the endpoint_group_create works. |
| 56 |
|
| 57 |
Uses passed in endpoint group information, which specifies an |
| 58 |
endpoint type and values. |
| 59 |
""" |
| 60 |
data['endpoint_group'].update({'tenant_id': _uuid(), |
| 61 |
'name': 'my endpoint group', |
| 62 |
'description': 'my description'}) |
| 63 |
return_value = copy.copy(data['endpoint_group']) |
| 64 |
return_value.update({'id': _uuid()}) |
| 65 |
|
| 66 |
instance = self.plugin.return_value |
| 67 |
instance.create_endpoint_group.return_value = return_value |
| 68 |
res = self.api.post(_get_path('vpn/endpoint-groups', fmt=self.fmt), |
| 69 |
self.serialize(data), |
| 70 |
content_type='application/%s' % self.fmt) |
| 71 |
self.assertEqual(1, instance.create_endpoint_group.call_count) |
| 72 |
self.assertEqual(exc.HTTPCreated.code, res.status_int) |
| 73 |
res = self.deserialize(res) |
| 74 |
self.assertIn('endpoint_group', res) |
| 75 |
self.assertDictSupersetOf(return_value, res['endpoint_group']) |
| 76 |
|
| 77 |
def test_create_cidr_endpoint_group_create(self): |
| 78 |
"""Test creation of CIDR type endpoint group.""" |
| 79 |
data = {'endpoint_group': |
| 80 |
{'type': constants.CIDR_ENDPOINT, |
| 81 |
'endpoints': ['10.10.10.0/24', '20.20.20.0/24']}} |
| 82 |
self.helper_test_endpoint_group_create(data) |
| 83 |
|
| 84 |
def test_create_subnet_endpoint_group_create(self): |
| 85 |
"""Test creation of subnet type endpoint group.""" |
| 86 |
data = {'endpoint_group': |
| 87 |
{'type': constants.SUBNET_ENDPOINT, |
| 88 |
'endpoints': [_uuid(), _uuid()]}} |
| 89 |
self.helper_test_endpoint_group_create(data) |
| 90 |
|
| 91 |
def test_create_vlan_endpoint_group_create(self): |
| 92 |
"""Test creation of VLAN type endpoint group.""" |
| 93 |
data = {'endpoint_group': |
| 94 |
{'type': constants.VLAN_ENDPOINT, |
| 95 |
'endpoints': ['100', '200', '300', '400']}} |
| 96 |
self.helper_test_endpoint_group_create(data) |
| 97 |
|
| 98 |
def test_get_endpoint_group(self): |
| 99 |
"""Test show for endpoint group.""" |
| 100 |
endpoint_group_id = _uuid() |
| 101 |
return_value = {'id': endpoint_group_id, |
| 102 |
'tenant_id': _uuid(), |
| 103 |
'name': 'my-endpoint-group', |
| 104 |
'description': 'my endpoint group', |
| 105 |
'type': constants.CIDR_ENDPOINT, |
| 106 |
'endpoints': ['10.10.10.0/24']} |
| 107 |
|
| 108 |
instance = self.plugin.return_value |
| 109 |
instance.get_endpoint_group.return_value = return_value |
| 110 |
|
| 111 |
res = self.api.get(_get_path('vpn/endpoint-groups', |
| 112 |
id=endpoint_group_id, |
| 113 |
fmt=self.fmt)) |
| 114 |
|
| 115 |
instance.get_endpoint_group.assert_called_with(mock.ANY, |
| 116 |
endpoint_group_id, |
| 117 |
fields=mock.ANY) |
| 118 |
self.assertEqual(res.status_int, exc.HTTPOk.code) |
| 119 |
res = self.deserialize(res) |
| 120 |
self.assertIn('endpoint_group', res) |
| 121 |
self.assertEqual(res['endpoint_group'], return_value) |
| 122 |
|
| 123 |
def test_endpoint_group_list(self): |
| 124 |
"""Test listing all endpoint groups.""" |
| 125 |
return_value = [{'id': _uuid(), |
| 126 |
'tenant_id': _uuid(), |
| 127 |
'name': 'my-endpoint-group', |
| 128 |
'description': 'my endpoint group', |
| 129 |
'type': constants.CIDR_ENDPOINT, |
| 130 |
'endpoints': ['10.10.10.0/24']}, |
| 131 |
{'id': _uuid(), |
| 132 |
'tenant_id': _uuid(), |
| 133 |
'name': 'another-endpoint-group', |
| 134 |
'description': 'second endpoint group', |
| 135 |
'type': constants.VLAN_ENDPOINT, |
| 136 |
'endpoints': ['100', '200', '300']}] |
| 137 |
|
| 138 |
instance = self.plugin.return_value |
| 139 |
instance.get_endpoint_groups.return_value = return_value |
| 140 |
|
| 141 |
res = self.api.get(_get_path('vpn/endpoint-groups', fmt=self.fmt)) |
| 142 |
|
| 143 |
instance.get_endpoint_groups.assert_called_with(mock.ANY, |
| 144 |
fields=mock.ANY, |
| 145 |
filters=mock.ANY) |
| 146 |
self.assertEqual(res.status_int, exc.HTTPOk.code) |
| 147 |
|
| 148 |
def test_endpoint_group_delete(self): |
| 149 |
"""Test deleting an endpoint group.""" |
| 150 |
self._test_entity_delete('endpoint_group') |
| 151 |
|
| 152 |
def test_endpoint_group_update(self): |
| 153 |
"""Test updating endpoint_group.""" |
| 154 |
endpoint_group_id = _uuid() |
| 155 |
update_data = {'endpoint_group': {'description': 'new description'}} |
| 156 |
return_value = {'id': endpoint_group_id, |
| 157 |
'tenant_id': _uuid(), |
| 158 |
'name': 'my-endpoint-group', |
| 159 |
'description': 'new_description', |
| 160 |
'type': constants.CIDR_ENDPOINT, |
| 161 |
'endpoints': ['10.10.10.0/24']} |
| 162 |
|
| 163 |
instance = self.plugin.return_value |
| 164 |
instance.update_endpoint_group.return_value = return_value |
| 165 |
|
| 166 |
res = self.api.put(_get_path('vpn/endpoint-groups', |
| 167 |
id=endpoint_group_id, |
| 168 |
fmt=self.fmt), |
| 169 |
self.serialize(update_data)) |
| 170 |
|
| 171 |
instance.update_endpoint_group.assert_called_with( |
| 172 |
mock.ANY, endpoint_group_id, endpoint_group=update_data) |
| 173 |
self.assertEqual(res.status_int, exc.HTTPOk.code) |
| 174 |
res = self.deserialize(res) |
| 175 |
self.assertIn('endpoint_group', res) |
| 176 |
self.assertEqual(res['endpoint_group'], return_value) |
| 177 |
|
| 178 |
def test_fail_updating_endpoints_in_endpoint_group(self): |
| 179 |
"""Test fails to update the endpoints in an endpoint group. |
| 180 |
|
| 181 |
This documents that we are not allowing endpoints to be updated |
| 182 |
(currently), as doing so, implies that the connection using the |
| 183 |
enclosing endpoint group would also need to be updated. For now, |
| 184 |
a new endpoint group can be created, and the connection can be |
| 185 |
updated to point to the new endpoint group. |
| 186 |
""" |
| 187 |
endpoint_group_id = _uuid() |
| 188 |
update_data = {'endpoint_group': {'endpoints': ['10.10.10.0/24']}} |
| 189 |
res = self.api.put(_get_path('vpn/endpoint-groups', |
| 190 |
id=endpoint_group_id, |
| 191 |
fmt=self.fmt), |
| 192 |
params=self.serialize(update_data), |
| 193 |
expect_errors=True) |
| 194 |
self.assertEqual(exc.HTTPBadRequest.code, res.status_int) |