| 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.tests import utils as tests_utils |
| 20 |
|
| 21 |
from neutronclient.osc import utils as osc_utils |
| 22 |
from neutronclient.osc.v2.vpnaas import endpoint_group |
| 23 |
from neutronclient.tests.unit.osc.v2 import fakes as test_fakes |
| 24 |
from neutronclient.tests.unit.osc.v2.vpnaas import common |
| 25 |
from neutronclient.tests.unit.osc.v2.vpnaas import fakes |
| 26 |
|
| 27 |
|
| 28 |
_endpoint_group = fakes.EndpointGroup().create() |
| 29 |
CONVERT_MAP = { |
| 30 |
'project': 'tenant_id', |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
def _generate_data(ordered_dict=None, data=None): |
| 35 |
source = ordered_dict if ordered_dict else _endpoint_group |
| 36 |
if data: |
| 37 |
source.update(data) |
| 38 |
return source |
| 39 |
|
| 40 |
|
| 41 |
def _generate_req_and_res(verifylist): |
| 42 |
request = dict(verifylist) |
| 43 |
response = _endpoint_group |
| 44 |
for key, val in verifylist: |
| 45 |
converted = CONVERT_MAP.get(key, key) |
| 46 |
del request[key] |
| 47 |
new_value = val |
| 48 |
request[converted] = new_value |
| 49 |
response[converted] = new_value |
| 50 |
return request, response |
| 51 |
|
| 52 |
|
| 53 |
class TestEndpointGroup(test_fakes.TestNeutronClientOSCV2): |
| 54 |
|
| 55 |
def check_results(self, headers, data, exp_req, is_list=False): |
| 56 |
if is_list: |
| 57 |
req_body = {self.res_plural: list(exp_req)} |
| 58 |
else: |
| 59 |
req_body = exp_req |
| 60 |
self.mocked.assert_called_once_with(**req_body) |
| 61 |
self.assertEqual(self.ordered_headers, tuple(sorted(headers))) |
| 62 |
self.assertEqual(self.ordered_data, data) |
| 63 |
|
| 64 |
def setUp(self): |
| 65 |
super(TestEndpointGroup, self).setUp() |
| 66 |
|
| 67 |
def _mock_endpoint_group(*args, **kwargs): |
| 68 |
self.networkclient.find_vpn_endpoint_group.assert_called_once_with( |
| 69 |
self.resource['id'], ignore_missing=False) |
| 70 |
return {'id': args[0]} |
| 71 |
|
| 72 |
self.networkclient.find_vpn_endpoint_group.side_effect = mock.Mock( |
| 73 |
side_effect=_mock_endpoint_group) |
| 74 |
osc_utils.find_project = mock.Mock() |
| 75 |
osc_utils.find_project.id = _endpoint_group['project_id'] |
| 76 |
self.res = 'endpoint_group' |
| 77 |
self.res_plural = 'endpoint_groups' |
| 78 |
self.resource = _endpoint_group |
| 79 |
self.headers = ( |
| 80 |
'ID', |
| 81 |
'Name', |
| 82 |
'Type', |
| 83 |
'Endpoints', |
| 84 |
'Description', |
| 85 |
'Project', |
| 86 |
) |
| 87 |
self.data = _generate_data() |
| 88 |
self.ordered_headers = ( |
| 89 |
'Description', |
| 90 |
'Endpoints', |
| 91 |
'ID', |
| 92 |
'Name', |
| 93 |
'Project', |
| 94 |
'Type', |
| 95 |
) |
| 96 |
self.ordered_data = ( |
| 97 |
_endpoint_group['description'], |
| 98 |
_endpoint_group['endpoints'], |
| 99 |
_endpoint_group['id'], |
| 100 |
_endpoint_group['name'], |
| 101 |
_endpoint_group['project_id'], |
| 102 |
_endpoint_group['type'], |
| 103 |
) |
| 104 |
self.ordered_columns = ( |
| 105 |
'description', |
| 106 |
'endpoints', |
| 107 |
'id', |
| 108 |
'name', |
| 109 |
'project_id', |
| 110 |
'type', |
| 111 |
) |
| 112 |
|
| 113 |
|
| 114 |
class TestCreateEndpointGroup(TestEndpointGroup, common.TestCreateVPNaaS): |
| 115 |
|
| 116 |
def setUp(self): |
| 117 |
super(TestCreateEndpointGroup, self).setUp() |
| 118 |
self.networkclient.create_vpn_endpoint_group = mock.Mock( |
| 119 |
return_value=_endpoint_group) |
| 120 |
self.mocked = self.networkclient.create_vpn_endpoint_group |
| 121 |
self.cmd = endpoint_group.CreateEndpointGroup(self.app, self.namespace) |
| 122 |
|
| 123 |
def _update_expect_response(self, request, response): |
| 124 |
"""Set expected request and response |
| 125 |
|
| 126 |
:param request |
| 127 |
A dictionary of request body(dict of verifylist) |
| 128 |
:param response |
| 129 |
A OrderedDict of request body |
| 130 |
""" |
| 131 |
# Update response body |
| 132 |
self.neutronclient.create_endpoint_group.return_value = \ |
| 133 |
{self.res: dict(response)} |
| 134 |
osc_utils.find_project.return_value.id = response['tenant_id'] |
| 135 |
# Update response(finally returns 'data') |
| 136 |
self.data = _generate_data(ordered_dict=response) |
| 137 |
self.ordered_data = tuple( |
| 138 |
response[column] for column in self.ordered_columns |
| 139 |
) |
| 140 |
|
| 141 |
def _set_all_params_cidr(self, args={}): |
| 142 |
name = args.get('name') or 'my-name' |
| 143 |
description = args.get('description') or 'my-desc' |
| 144 |
endpoint_type = args.get('type') or 'cidr' |
| 145 |
endpoints = args.get('endpoints') or ['10.0.0.0/24', '20.0.0.0/24'] |
| 146 |
tenant_id = args.get('project_id') or 'my-tenant' |
| 147 |
arglist = [ |
| 148 |
'--description', description, |
| 149 |
'--type', endpoint_type, |
| 150 |
'--value', '10.0.0.0/24', |
| 151 |
'--value', '20.0.0.0/24', |
| 152 |
'--project', tenant_id, |
| 153 |
name, |
| 154 |
] |
| 155 |
verifylist = [ |
| 156 |
('description', description), |
| 157 |
('type', endpoint_type), |
| 158 |
('endpoints', endpoints), |
| 159 |
('project', tenant_id), |
| 160 |
('name', name), |
| 161 |
] |
| 162 |
return arglist, verifylist |
| 163 |
|
| 164 |
def _test_create_with_all_params_cidr(self, args={}): |
| 165 |
arglist, verifylist = self._set_all_params_cidr(args) |
| 166 |
request, response = _generate_req_and_res(verifylist) |
| 167 |
self._update_expect_response(request, response) |
| 168 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 169 |
headers, data = self.cmd.take_action(parsed_args) |
| 170 |
|
| 171 |
self.check_results(headers, data, request) |
| 172 |
|
| 173 |
def test_create_with_no_options(self): |
| 174 |
arglist = [] |
| 175 |
verifylist = [] |
| 176 |
|
| 177 |
self.assertRaises(tests_utils.ParserException, self.check_parser, |
| 178 |
self.cmd, arglist, verifylist) |
| 179 |
|
| 180 |
def test_create_with_all_params_cidr(self): |
| 181 |
self._test_create_with_all_params_cidr() |
| 182 |
|
| 183 |
|
| 184 |
class TestDeleteEndpointGroup(TestEndpointGroup, common.TestDeleteVPNaaS): |
| 185 |
|
| 186 |
def setUp(self): |
| 187 |
super(TestDeleteEndpointGroup, self).setUp() |
| 188 |
self.networkclient.delete_vpn_endpoint_group = mock.Mock() |
| 189 |
self.mocked = self.networkclient.delete_vpn_endpoint_group |
| 190 |
self.cmd = endpoint_group.DeleteEndpointGroup(self.app, self.namespace) |
| 191 |
|
| 192 |
|
| 193 |
class TestListEndpointGroup(TestEndpointGroup): |
| 194 |
|
| 195 |
def setUp(self): |
| 196 |
super(TestListEndpointGroup, self).setUp() |
| 197 |
self.cmd = endpoint_group.ListEndpointGroup(self.app, self.namespace) |
| 198 |
|
| 199 |
self.short_header = ( |
| 200 |
'ID', |
| 201 |
'Name', |
| 202 |
'Type', |
| 203 |
'Endpoints', |
| 204 |
) |
| 205 |
|
| 206 |
self.short_data = ( |
| 207 |
_endpoint_group['id'], |
| 208 |
_endpoint_group['name'], |
| 209 |
_endpoint_group['type'], |
| 210 |
_endpoint_group['endpoints'], |
| 211 |
) |
| 212 |
|
| 213 |
self.networkclient.vpn_endpoint_groups = mock.Mock( |
| 214 |
return_value=[_endpoint_group]) |
| 215 |
self.mocked = self.networkclient.vpn_endpoint_groups |
| 216 |
|
| 217 |
def test_list_with_long_option(self): |
| 218 |
arglist = ['--long'] |
| 219 |
verifylist = [('long', True)] |
| 220 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 221 |
headers, data = self.cmd.take_action(parsed_args) |
| 222 |
|
| 223 |
self.mocked.assert_called_once_with() |
| 224 |
self.assertEqual(list(self.headers), headers) |
| 225 |
|
| 226 |
def test_list_with_no_option(self): |
| 227 |
arglist = [] |
| 228 |
verifylist = [] |
| 229 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 230 |
headers, data = self.cmd.take_action(parsed_args) |
| 231 |
|
| 232 |
self.mocked.assert_called_once_with() |
| 233 |
self.assertEqual(list(self.short_header), headers) |
| 234 |
self.assertEqual([self.short_data], list(data)) |
| 235 |
|
| 236 |
|
| 237 |
class TestSetEndpointGroup(TestEndpointGroup, common.TestSetVPNaaS): |
| 238 |
|
| 239 |
def setUp(self): |
| 240 |
super(TestSetEndpointGroup, self).setUp() |
| 241 |
self.networkclient.update_vpn_endpoint_group = mock.Mock( |
| 242 |
return_value=_endpoint_group) |
| 243 |
self.mocked = self.networkclient.update_vpn_endpoint_group |
| 244 |
self.cmd = endpoint_group.SetEndpointGroup(self.app, self.namespace) |
| 245 |
|
| 246 |
|
| 247 |
class TestShowEndpointGroup(TestEndpointGroup, common.TestShowVPNaaS): |
| 248 |
|
| 249 |
def setUp(self): |
| 250 |
super(TestShowEndpointGroup, self).setUp() |
| 251 |
self.networkclient.get_vpn_endpoint_group = mock.Mock( |
| 252 |
return_value=_endpoint_group) |
| 253 |
self.mocked = self.networkclient.get_vpn_endpoint_group |
| 254 |
self.cmd = endpoint_group.ShowEndpointGroup(self.app, self.namespace) |