| 1 |
# Copyright 2017 FUJITSU LIMITED |
| 2 |
# All Rights Reserved |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
# |
| 16 |
|
| 17 |
from unittest import mock |
| 18 |
|
| 19 |
from osc_lib.cli import format_columns |
| 20 |
from osc_lib.tests import utils as tests_utils |
| 21 |
|
| 22 |
from neutronclient.osc import utils as osc_utils |
| 23 |
from neutronclient.osc.v2.vpnaas import ipsec_site_connection |
| 24 |
from neutronclient.tests.unit.osc.v2 import fakes as test_fakes |
| 25 |
from neutronclient.tests.unit.osc.v2.vpnaas import common |
| 26 |
from neutronclient.tests.unit.osc.v2.vpnaas import fakes |
| 27 |
|
| 28 |
|
| 29 |
_ipsec_site_conn = fakes.IPsecSiteConnection().create_conn() |
| 30 |
CONVERT_MAP = { |
| 31 |
'project': 'project_id', |
| 32 |
'ikepolicy': 'ikepolicy_id', |
| 33 |
'ipsecpolicy': 'ipsecpolicy_id', |
| 34 |
'vpnservice': 'vpnservice_id', |
| 35 |
'peer_endpoint_group': 'peer_ep_group_id', |
| 36 |
'local_endpoint_group': 'local_ep_group_id', |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
def _generate_data(ordered_dict=None, data=None): |
| 41 |
source = ordered_dict if ordered_dict else _ipsec_site_conn |
| 42 |
if data: |
| 43 |
source.update(data) |
| 44 |
return source |
| 45 |
|
| 46 |
|
| 47 |
def _generate_req_and_res(verifylist): |
| 48 |
request = dict(verifylist) |
| 49 |
response = _ipsec_site_conn |
| 50 |
for key, val in verifylist: |
| 51 |
converted = CONVERT_MAP.get(key, key) |
| 52 |
del request[key] |
| 53 |
new_value = val |
| 54 |
request[converted] = new_value |
| 55 |
response[converted] = new_value |
| 56 |
return request, response |
| 57 |
|
| 58 |
|
| 59 |
class TestIPsecSiteConn(test_fakes.TestNeutronClientOSCV2): |
| 60 |
|
| 61 |
def check_results(self, headers, data, exp_req, is_list=False): |
| 62 |
if is_list: |
| 63 |
req_body = {self.res_plural: list(exp_req)} |
| 64 |
else: |
| 65 |
req_body = exp_req |
| 66 |
self.mocked.assert_called_once_with(**req_body) |
| 67 |
self.assertEqual(self.ordered_headers, tuple(sorted(headers))) |
| 68 |
self.assertItemEqual(self.ordered_data, data) |
| 69 |
|
| 70 |
def setUp(self): |
| 71 |
super(TestIPsecSiteConn, self).setUp() |
| 72 |
|
| 73 |
def _mock_ipsec_site_conn(*args, **kwargs): |
| 74 |
return {'id': args[0]} |
| 75 |
|
| 76 |
self.networkclient.find_vpn_ipsec_site_connection.side_effect = \ |
| 77 |
mock.Mock(side_effect=_mock_ipsec_site_conn) |
| 78 |
osc_utils.find_project = mock.Mock() |
| 79 |
osc_utils.find_project.id = _ipsec_site_conn['project_id'] |
| 80 |
self.res = 'ipsec_site_connection' |
| 81 |
self.res_plural = 'ipsec_site_connections' |
| 82 |
self.resource = _ipsec_site_conn |
| 83 |
self.headers = ( |
| 84 |
'ID', |
| 85 |
'Name', |
| 86 |
'Peer Address', |
| 87 |
'Authentication Algorithm', |
| 88 |
'Status', |
| 89 |
'Project', |
| 90 |
'Peer CIDRs', |
| 91 |
'VPN Service', |
| 92 |
'IPSec Policy', |
| 93 |
'IKE Policy', |
| 94 |
'MTU', |
| 95 |
'Initiator', |
| 96 |
'State', |
| 97 |
'Description', |
| 98 |
'Pre-shared Key', |
| 99 |
'Route Mode', |
| 100 |
'Local ID', |
| 101 |
'Peer ID', |
| 102 |
'Local Endpoint Group ID', |
| 103 |
'Peer Endpoint Group ID', |
| 104 |
'DPD', |
| 105 |
) |
| 106 |
self.data = _generate_data() |
| 107 |
self.ordered_headers = ( |
| 108 |
'Authentication Algorithm', |
| 109 |
'DPD', |
| 110 |
'Description', |
| 111 |
'ID', |
| 112 |
'IKE Policy', |
| 113 |
'IPSec Policy', |
| 114 |
'Initiator', |
| 115 |
'Local Endpoint Group ID', |
| 116 |
'Local ID', |
| 117 |
'MTU', |
| 118 |
'Name', |
| 119 |
'Peer Address', |
| 120 |
'Peer CIDRs', |
| 121 |
'Peer Endpoint Group ID', |
| 122 |
'Peer ID', |
| 123 |
'Pre-shared Key', |
| 124 |
'Project', |
| 125 |
'Route Mode', |
| 126 |
'State', |
| 127 |
'Status', |
| 128 |
'VPN Service', |
| 129 |
) |
| 130 |
self.ordered_data = ( |
| 131 |
_ipsec_site_conn['auth_mode'], |
| 132 |
_ipsec_site_conn['dpd'], |
| 133 |
_ipsec_site_conn['description'], |
| 134 |
_ipsec_site_conn['id'], |
| 135 |
_ipsec_site_conn['ikepolicy_id'], |
| 136 |
_ipsec_site_conn['ipsecpolicy_id'], |
| 137 |
_ipsec_site_conn['initiator'], |
| 138 |
_ipsec_site_conn['local_ep_group_id'], |
| 139 |
_ipsec_site_conn['local_id'], |
| 140 |
_ipsec_site_conn['mtu'], |
| 141 |
_ipsec_site_conn['name'], |
| 142 |
_ipsec_site_conn['peer_address'], |
| 143 |
format_columns.ListColumn(_ipsec_site_conn['peer_cidrs']), |
| 144 |
_ipsec_site_conn['peer_ep_group_id'], |
| 145 |
_ipsec_site_conn['peer_id'], |
| 146 |
_ipsec_site_conn['psk'], |
| 147 |
_ipsec_site_conn['project_id'], |
| 148 |
_ipsec_site_conn['route_mode'], |
| 149 |
_ipsec_site_conn['admin_state_up'], |
| 150 |
_ipsec_site_conn['status'], |
| 151 |
_ipsec_site_conn['vpnservice_id'], |
| 152 |
) |
| 153 |
|
| 154 |
|
| 155 |
class TestCreateIPsecSiteConn(TestIPsecSiteConn, common.TestCreateVPNaaS): |
| 156 |
|
| 157 |
def setUp(self): |
| 158 |
super(TestCreateIPsecSiteConn, self).setUp() |
| 159 |
self.networkclient.create_vpn_ipsec_site_connection = mock.Mock( |
| 160 |
return_value=_ipsec_site_conn) |
| 161 |
self.mocked = self.networkclient.create_vpn_ipsec_site_connection |
| 162 |
self.cmd = ipsec_site_connection.CreateIPsecSiteConnection( |
| 163 |
self.app, self.namespace) |
| 164 |
|
| 165 |
def _update_expect_response(self, request, response): |
| 166 |
"""Set expected request and response |
| 167 |
|
| 168 |
:param request |
| 169 |
A dictionary of request body(dict of verifylist) |
| 170 |
:param response |
| 171 |
A OrderedDict of request body |
| 172 |
""" |
| 173 |
# Update response body |
| 174 |
self.networkclient.create_vpn_ipsec_site_connection.return_value = \ |
| 175 |
response |
| 176 |
osc_utils.find_project.return_value.id = response['project_id'] |
| 177 |
# Update response(finally returns 'data') |
| 178 |
self.data = _generate_data(ordered_dict=response) |
| 179 |
self.ordered_data = ( |
| 180 |
response['auth_mode'], |
| 181 |
response['dpd'], |
| 182 |
response['description'], |
| 183 |
response['id'], |
| 184 |
response['ikepolicy_id'], |
| 185 |
response['ipsecpolicy_id'], |
| 186 |
response['initiator'], |
| 187 |
response['local_ep_group_id'], |
| 188 |
response['local_id'], |
| 189 |
response['mtu'], |
| 190 |
response['name'], |
| 191 |
response['peer_address'], |
| 192 |
format_columns.ListColumn(response['peer_cidrs']), |
| 193 |
response['peer_ep_group_id'], |
| 194 |
response['peer_id'], |
| 195 |
response['psk'], |
| 196 |
response['project_id'], |
| 197 |
response['route_mode'], |
| 198 |
response['admin_state_up'], |
| 199 |
response['status'], |
| 200 |
response['vpnservice_id'], |
| 201 |
) |
| 202 |
|
| 203 |
def _set_all_params(self, args={}): |
| 204 |
tenant_id = args.get('tenant_id') or 'my-tenant' |
| 205 |
name = args.get('name') or 'connection1' |
| 206 |
peer_address = args.get('peer_address') or '192.168.2.10' |
| 207 |
peer_id = args.get('peer_id') or '192.168.2.10' |
| 208 |
psk = args.get('psk') or 'abcd' |
| 209 |
mtu = args.get('mtu') or '1500' |
| 210 |
initiator = args.get('initiator') or 'bi-directional' |
| 211 |
vpnservice_id = args.get('vpnservice') or 'vpnservice_id' |
| 212 |
ikepolicy_id = args.get('ikepolicy') or 'ikepolicy_id' |
| 213 |
ipsecpolicy_id = args.get('ipsecpolicy') or 'ipsecpolicy_id' |
| 214 |
local_ep_group = args.get('local_ep_group_id') or 'local-epg' |
| 215 |
peer_ep_group = args.get('peer_ep_group_id') or 'peer-epg' |
| 216 |
description = args.get('description') or 'my-vpn-connection' |
| 217 |
|
| 218 |
arglist = [ |
| 219 |
'--project', tenant_id, |
| 220 |
'--peer-address', peer_address, |
| 221 |
'--peer-id', peer_id, |
| 222 |
'--psk', psk, |
| 223 |
'--initiator', initiator, |
| 224 |
'--vpnservice', vpnservice_id, |
| 225 |
'--ikepolicy', ikepolicy_id, |
| 226 |
'--ipsecpolicy', ipsecpolicy_id, |
| 227 |
'--mtu', mtu, |
| 228 |
'--description', description, |
| 229 |
'--local-endpoint-group', local_ep_group, |
| 230 |
'--peer-endpoint-group', peer_ep_group, |
| 231 |
name, |
| 232 |
] |
| 233 |
verifylist = [ |
| 234 |
('project', tenant_id), |
| 235 |
('peer_address', peer_address), |
| 236 |
('peer_id', peer_id), |
| 237 |
('psk', psk), |
| 238 |
('initiator', initiator), |
| 239 |
('vpnservice', vpnservice_id), |
| 240 |
('ikepolicy', ikepolicy_id), |
| 241 |
('ipsecpolicy', ipsecpolicy_id), |
| 242 |
('mtu', mtu), |
| 243 |
('description', description), |
| 244 |
('local_endpoint_group', local_ep_group), |
| 245 |
('peer_endpoint_group', peer_ep_group), |
| 246 |
('name', name), |
| 247 |
] |
| 248 |
return arglist, verifylist |
| 249 |
|
| 250 |
def _test_create_with_all_params(self, args={}): |
| 251 |
arglist, verifylist = self._set_all_params(args) |
| 252 |
request, response = _generate_req_and_res(verifylist) |
| 253 |
|
| 254 |
def _mock_endpoint_group(*args, **kwargs): |
| 255 |
return {'id': args[0]} |
| 256 |
|
| 257 |
self.networkclient.find_vpn_endpoint_group.side_effect = mock.Mock( |
| 258 |
side_effect=_mock_endpoint_group) |
| 259 |
self.networkclient.find_vpn_service.side_effect = mock.Mock( |
| 260 |
side_effect=_mock_endpoint_group) |
| 261 |
self.networkclient.find_vpn_ike_policy.side_effect = mock.Mock( |
| 262 |
side_effect=_mock_endpoint_group) |
| 263 |
self.networkclient.find_vpn_ipsec_policy.side_effect = mock.Mock( |
| 264 |
side_effect=_mock_endpoint_group) |
| 265 |
self._update_expect_response(request, response) |
| 266 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 267 |
headers, data = self.cmd.take_action(parsed_args) |
| 268 |
|
| 269 |
self.check_results(headers, data, request) |
| 270 |
|
| 271 |
def test_create_with_no_options(self): |
| 272 |
arglist = [] |
| 273 |
verifylist = [] |
| 274 |
|
| 275 |
self.assertRaises(tests_utils.ParserException, self.check_parser, |
| 276 |
self.cmd, arglist, verifylist) |
| 277 |
|
| 278 |
def test_create_with_all_params(self): |
| 279 |
self._test_create_with_all_params() |
| 280 |
|
| 281 |
|
| 282 |
class TestDeleteIPsecSiteConn(TestIPsecSiteConn, common.TestDeleteVPNaaS): |
| 283 |
|
| 284 |
def setUp(self): |
| 285 |
super(TestDeleteIPsecSiteConn, self).setUp() |
| 286 |
self.networkclient.delete_vpn_ipsec_site_connection = mock.Mock() |
| 287 |
self.mocked = self.networkclient.delete_vpn_ipsec_site_connection |
| 288 |
self.cmd = ipsec_site_connection.DeleteIPsecSiteConnection( |
| 289 |
self.app, self.namespace) |
| 290 |
|
| 291 |
|
| 292 |
class TestListIPsecSiteConn(TestIPsecSiteConn): |
| 293 |
|
| 294 |
def setUp(self): |
| 295 |
super(TestListIPsecSiteConn, self).setUp() |
| 296 |
self.cmd = ipsec_site_connection.ListIPsecSiteConnection( |
| 297 |
self.app, self.namespace) |
| 298 |
|
| 299 |
self.short_header = ( |
| 300 |
'ID', |
| 301 |
'Name', |
| 302 |
'Peer Address', |
| 303 |
'Authentication Algorithm', |
| 304 |
'Status', |
| 305 |
) |
| 306 |
|
| 307 |
self.short_data = ( |
| 308 |
_ipsec_site_conn['id'], |
| 309 |
_ipsec_site_conn['name'], |
| 310 |
_ipsec_site_conn['peer_address'], |
| 311 |
_ipsec_site_conn['auth_mode'], |
| 312 |
_ipsec_site_conn['status'], |
| 313 |
) |
| 314 |
|
| 315 |
self.networkclient.vpn_ipsec_site_connections = mock.Mock( |
| 316 |
return_value=[_ipsec_site_conn]) |
| 317 |
self.mocked = self.networkclient.vpn_ipsec_site_connections |
| 318 |
|
| 319 |
def test_list_with_long_option(self): |
| 320 |
arglist = ['--long'] |
| 321 |
verifylist = [('long', True)] |
| 322 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 323 |
headers, data = self.cmd.take_action(parsed_args) |
| 324 |
|
| 325 |
self.mocked.assert_called_once_with() |
| 326 |
self.assertEqual(list(self.headers), headers) |
| 327 |
|
| 328 |
def test_list_with_no_option(self): |
| 329 |
arglist = [] |
| 330 |
verifylist = [] |
| 331 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 332 |
headers, data = self.cmd.take_action(parsed_args) |
| 333 |
|
| 334 |
self.mocked.assert_called_once_with() |
| 335 |
self.assertEqual(list(self.short_header), headers) |
| 336 |
self.assertEqual([self.short_data], list(data)) |
| 337 |
|
| 338 |
|
| 339 |
class TestSetIPsecSiteConn(TestIPsecSiteConn, common.TestSetVPNaaS): |
| 340 |
|
| 341 |
def setUp(self): |
| 342 |
super(TestSetIPsecSiteConn, self).setUp() |
| 343 |
self.networkclient.update_vpn_ipsec_site_connection = mock.Mock( |
| 344 |
return_value=_ipsec_site_conn) |
| 345 |
self.mocked = self.networkclient.update_vpn_ipsec_site_connection |
| 346 |
self.cmd = ipsec_site_connection.SetIPsecSiteConnection( |
| 347 |
self.app, self.namespace) |
| 348 |
|
| 349 |
def test_set_ipsec_site_conn_with_peer_id(self): |
| 350 |
target = self.resource['id'] |
| 351 |
peer_id = '192.168.3.10' |
| 352 |
arglist = [target, '--peer-id', peer_id] |
| 353 |
verifylist = [ |
| 354 |
(self.res, target), |
| 355 |
('peer_id', peer_id), |
| 356 |
] |
| 357 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 358 |
result = self.cmd.take_action(parsed_args) |
| 359 |
|
| 360 |
self.mocked.assert_called_once_with(target, **{'peer_id': peer_id}) |
| 361 |
self.assertIsNone(result) |
| 362 |
|
| 363 |
|
| 364 |
class TestShowIPsecSiteConn(TestIPsecSiteConn, common.TestShowVPNaaS): |
| 365 |
|
| 366 |
def setUp(self): |
| 367 |
super(TestShowIPsecSiteConn, self).setUp() |
| 368 |
self.networkclient.get_vpn_ipsec_site_connection = mock.Mock( |
| 369 |
return_value=_ipsec_site_conn) |
| 370 |
self.mocked = self.networkclient.get_vpn_ipsec_site_connection |
| 371 |
self.cmd = ipsec_site_connection.ShowIPsecSiteConnection( |
| 372 |
self.app, self.namespace) |