| 1 |
# Copyright (c) 2018 Orange SA. |
| 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 |
import operator |
| 18 |
from unittest import mock |
| 19 |
|
| 20 |
from osc_lib.tests.utils import ParserException |
| 21 |
from osc_lib import utils as osc_utils |
| 22 |
from osc_lib.utils import columns as column_util |
| 23 |
|
| 24 |
from neutronclient.tests.unit.osc.v2.networking_bgpvpn import fakes |
| 25 |
|
| 26 |
|
| 27 |
columns_short = tuple(col for col, _, listing_mode |
| 28 |
in fakes.BgpvpnFakeRouterAssoc._attr_map |
| 29 |
if listing_mode in (column_util.LIST_BOTH, |
| 30 |
column_util.LIST_SHORT_ONLY)) |
| 31 |
columns_long = tuple(col for col, _, listing_mode |
| 32 |
in fakes.BgpvpnFakeRouterAssoc._attr_map |
| 33 |
if listing_mode in (column_util.LIST_BOTH, |
| 34 |
column_util.LIST_LONG_ONLY)) |
| 35 |
headers_short = tuple(head for _, head, listing_mode |
| 36 |
in fakes.BgpvpnFakeRouterAssoc._attr_map |
| 37 |
if listing_mode in (column_util.LIST_BOTH, |
| 38 |
column_util.LIST_SHORT_ONLY)) |
| 39 |
headers_long = tuple(head for _, head, listing_mode |
| 40 |
in fakes.BgpvpnFakeRouterAssoc._attr_map |
| 41 |
if listing_mode in (column_util.LIST_BOTH, |
| 42 |
column_util.LIST_LONG_ONLY)) |
| 43 |
sorted_attr_map = sorted(fakes.BgpvpnFakeRouterAssoc._attr_map, |
| 44 |
key=operator.itemgetter(1)) |
| 45 |
sorted_columns = tuple(col for col, _, _ in sorted_attr_map) |
| 46 |
sorted_headers = tuple(head for _, head, _ in sorted_attr_map) |
| 47 |
|
| 48 |
|
| 49 |
def _get_data(attrs, columns=sorted_columns): |
| 50 |
return osc_utils.get_dict_properties( |
| 51 |
attrs, columns, formatters=fakes.BgpvpnFakeAssoc._formatters) |
| 52 |
|
| 53 |
|
| 54 |
class TestCreateRouterAssoc(fakes.TestNeutronClientBgpvpn): |
| 55 |
def setUp(self): |
| 56 |
super(TestCreateRouterAssoc, self).setUp() |
| 57 |
self.cmd = fakes.CreateBgpvpnFakeRouterAssoc(self.app, self.namespace) |
| 58 |
self.fake_bgpvpn = fakes.create_one_bgpvpn() |
| 59 |
self.fake_router = fakes.create_one_resource() |
| 60 |
self.networkclient.find_bgpvpn = mock.Mock( |
| 61 |
side_effect=lambda name_or_id: {'id': name_or_id}) |
| 62 |
self.networkclient.find_fake_resource = mock.Mock( |
| 63 |
side_effect=lambda name_or_id: {'id': name_or_id}) |
| 64 |
|
| 65 |
def _build_args(self, param=None): |
| 66 |
arglist_base = [ |
| 67 |
self.fake_bgpvpn['id'], |
| 68 |
self.fake_router['id'], |
| 69 |
'--project', self.fake_bgpvpn['tenant_id'] |
| 70 |
] |
| 71 |
if param is not None: |
| 72 |
if isinstance(param, list): |
| 73 |
arglist_base.extend(param) |
| 74 |
else: |
| 75 |
arglist_base.append(param) |
| 76 |
return arglist_base |
| 77 |
|
| 78 |
def _build_verify_list(self, param=None): |
| 79 |
verifylist = [ |
| 80 |
('bgpvpn', self.fake_bgpvpn['id']), |
| 81 |
('resource', self.fake_router['id']), |
| 82 |
('project', self.fake_bgpvpn['tenant_id']) |
| 83 |
] |
| 84 |
if param is not None: |
| 85 |
verifylist.append(param) |
| 86 |
return verifylist |
| 87 |
|
| 88 |
def _exec_create_router_association( |
| 89 |
self, fake_res_assoc, arglist, verifylist): |
| 90 |
|
| 91 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 92 |
|
| 93 |
cols, data = self.cmd.take_action(parsed_args) |
| 94 |
|
| 95 |
fake_res_assoc_call = { |
| 96 |
'fake_resource_id': 'fake_resource_id', |
| 97 |
'tenant_id': 'fake_project_id' |
| 98 |
} |
| 99 |
for key, value in verifylist: |
| 100 |
if value not in fake_res_assoc_call.values(): |
| 101 |
fake_res_assoc_call[key] = value |
| 102 |
fake_res_assoc_call.pop('bgpvpn') |
| 103 |
|
| 104 |
self.networkclient.create_bgpvpn_router_association.\ |
| 105 |
assert_called_once_with( |
| 106 |
self.fake_bgpvpn['id'], |
| 107 |
**fake_res_assoc_call) |
| 108 |
return cols, data |
| 109 |
|
| 110 |
def test_create_router_association(self): |
| 111 |
fake_res_assoc = fakes.create_one_resource_association( |
| 112 |
self.fake_router) |
| 113 |
|
| 114 |
self.networkclient.create_bgpvpn_router_association = mock.Mock( |
| 115 |
return_value={ |
| 116 |
fakes.BgpvpnFakeRouterAssoc._resource: fake_res_assoc, |
| 117 |
'advertise_extra_routes': True}) |
| 118 |
|
| 119 |
arglist = self._build_args() |
| 120 |
# advertise_extra_routes will be False since none |
| 121 |
# of the mutually exclusive args present |
| 122 |
verifylist = self._build_verify_list(('advertise_extra_routes', False)) |
| 123 |
|
| 124 |
self._exec_create_router_association( |
| 125 |
fake_res_assoc, arglist, verifylist) |
| 126 |
|
| 127 |
def test_create_router_association_advertise(self): |
| 128 |
fake_res_assoc = fakes.create_one_resource_association( |
| 129 |
self.fake_router, |
| 130 |
{'advertise_extra_routes': True}) |
| 131 |
|
| 132 |
self.networkclient.create_bgpvpn_router_association = mock.Mock( |
| 133 |
return_value=fake_res_assoc) |
| 134 |
|
| 135 |
arglist = self._build_args('--advertise_extra_routes') |
| 136 |
verifylist = self._build_verify_list(('advertise_extra_routes', True)) |
| 137 |
|
| 138 |
cols, data = self._exec_create_router_association( |
| 139 |
fake_res_assoc, arglist, verifylist) |
| 140 |
self.assertEqual(sorted_columns, cols) |
| 141 |
self.assertEqual(_get_data(fake_res_assoc), data) |
| 142 |
|
| 143 |
def test_create_router_association_no_advertise(self): |
| 144 |
fake_res_assoc = fakes.create_one_resource_association( |
| 145 |
self.fake_router, |
| 146 |
{'advertise_extra_routes': False}) |
| 147 |
|
| 148 |
self.networkclient.create_bgpvpn_router_association = mock.Mock( |
| 149 |
return_value=fake_res_assoc) |
| 150 |
|
| 151 |
arglist = self._build_args('--no-advertise_extra_routes') |
| 152 |
verifylist = self._build_verify_list(('advertise_extra_routes', False)) |
| 153 |
|
| 154 |
cols, data = self._exec_create_router_association( |
| 155 |
fake_res_assoc, arglist, verifylist) |
| 156 |
self.assertEqual(sorted_columns, cols) |
| 157 |
self.assertEqual(_get_data(fake_res_assoc), data) |
| 158 |
|
| 159 |
def test_create_router_association_advertise_fault(self): |
| 160 |
arglist = self._build_args( |
| 161 |
['--advertise_extra_routes', '--no-advertise_extra_routes']) |
| 162 |
|
| 163 |
try: |
| 164 |
self._exec_create_router_association(None, arglist, None) |
| 165 |
except ParserException as e: |
| 166 |
self.assertEqual(format(e), 'Argument parse failed') |
| 167 |
|
| 168 |
def test_router_association_unknown_arg(self): |
| 169 |
arglist = self._build_args('--unknown arg') |
| 170 |
|
| 171 |
try: |
| 172 |
self._exec_create_router_association(None, arglist, None) |
| 173 |
except ParserException as e: |
| 174 |
self.assertEqual(format(e), 'Argument parse failed') |
| 175 |
|
| 176 |
|
| 177 |
class TestSetRouterAssoc(fakes.TestNeutronClientBgpvpn): |
| 178 |
|
| 179 |
def setUp(self): |
| 180 |
super(TestSetRouterAssoc, self).setUp() |
| 181 |
self.cmd = fakes.SetBgpvpnFakeRouterAssoc(self.app, self.namespace) |
| 182 |
self.fake_bgpvpn = fakes.create_one_bgpvpn() |
| 183 |
self.fake_router = fakes.create_one_resource() |
| 184 |
self.networkclient.find_bgpvpn = mock.Mock( |
| 185 |
side_effect=lambda name_or_id: {'id': name_or_id}) |
| 186 |
|
| 187 |
def _build_args(self, fake_res_assoc, param=None): |
| 188 |
arglist_base = [ |
| 189 |
fake_res_assoc['id'], |
| 190 |
self.fake_bgpvpn['id'] |
| 191 |
] |
| 192 |
if param is not None: |
| 193 |
if isinstance(param, list): |
| 194 |
arglist_base.extend(param) |
| 195 |
else: |
| 196 |
arglist_base.append(param) |
| 197 |
return arglist_base |
| 198 |
|
| 199 |
def _build_verify_list(self, fake_res_assoc, param=None): |
| 200 |
verifylist = [ |
| 201 |
('resource_association_id', fake_res_assoc['id']), |
| 202 |
('bgpvpn', self.fake_bgpvpn['id']) |
| 203 |
] |
| 204 |
if param is not None: |
| 205 |
verifylist.append(param) |
| 206 |
return verifylist |
| 207 |
|
| 208 |
def test_set_router_association_no_advertise(self): |
| 209 |
fake_res_assoc = fakes.create_one_resource_association( |
| 210 |
self.fake_router, |
| 211 |
{'advertise_extra_routes': True}) |
| 212 |
self.networkclient.update_bgpvpn_router_association = \ |
| 213 |
mock.Mock() |
| 214 |
|
| 215 |
arglist = self._build_args( |
| 216 |
fake_res_assoc, |
| 217 |
'--no-advertise_extra_routes') |
| 218 |
verifylist = [ |
| 219 |
('resource_association_id', fake_res_assoc['id']), |
| 220 |
('bgpvpn', self.fake_bgpvpn['id']), |
| 221 |
] |
| 222 |
|
| 223 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 224 |
result = self.cmd.take_action(parsed_args) |
| 225 |
|
| 226 |
self.networkclient.update_bgpvpn_router_association.\ |
| 227 |
assert_called_once_with( |
| 228 |
self.fake_bgpvpn['id'], |
| 229 |
fake_res_assoc['id'], |
| 230 |
**{'advertise_extra_routes': False} |
| 231 |
) |
| 232 |
self.assertIsNone(result) |
| 233 |
|
| 234 |
def test_set_router_association_advertise(self): |
| 235 |
fake_res_assoc = fakes.create_one_resource_association( |
| 236 |
self.fake_router, |
| 237 |
{'advertise_extra_routes': False}) |
| 238 |
self.networkclient.update_bgpvpn_router_association = \ |
| 239 |
mock.Mock() |
| 240 |
|
| 241 |
arglist = self._build_args( |
| 242 |
fake_res_assoc, |
| 243 |
'--advertise_extra_routes') |
| 244 |
verifylist = [ |
| 245 |
('resource_association_id', fake_res_assoc['id']), |
| 246 |
('bgpvpn', self.fake_bgpvpn['id']), |
| 247 |
] |
| 248 |
|
| 249 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 250 |
result = self.cmd.take_action(parsed_args) |
| 251 |
|
| 252 |
self.networkclient.update_bgpvpn_router_association.\ |
| 253 |
assert_called_once_with( |
| 254 |
self.fake_bgpvpn['id'], |
| 255 |
fake_res_assoc['id'], |
| 256 |
**{'advertise_extra_routes': True} |
| 257 |
) |
| 258 |
self.assertIsNone(result) |
| 259 |
|
| 260 |
|
| 261 |
class TestShowRouterAssoc(fakes.TestNeutronClientBgpvpn): |
| 262 |
def setUp(self): |
| 263 |
super(TestShowRouterAssoc, self).setUp() |
| 264 |
self.cmd = fakes.ShowBgpvpnFakeRouterAssoc(self.app, self.namespace) |
| 265 |
self.networkclient.find_bgpvpn = mock.Mock( |
| 266 |
side_effect=lambda name_or_id: {'id': name_or_id}) |
| 267 |
|
| 268 |
def test_show_router_association(self): |
| 269 |
fake_bgpvpn = fakes.create_one_bgpvpn() |
| 270 |
fake_res = fakes.create_one_resource() |
| 271 |
fake_res_assoc = fakes.create_one_resource_association( |
| 272 |
fake_res, |
| 273 |
{'advertise_extra_routes': True}) |
| 274 |
self.networkclient.get_bgpvpn_router_association = mock.Mock( |
| 275 |
return_value=fake_res_assoc) |
| 276 |
arglist = [ |
| 277 |
fake_res_assoc['id'], |
| 278 |
fake_bgpvpn['id'], |
| 279 |
] |
| 280 |
verifylist = [ |
| 281 |
('resource_association_id', fake_res_assoc['id']), |
| 282 |
('bgpvpn', fake_bgpvpn['id']), |
| 283 |
] |
| 284 |
|
| 285 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 286 |
|
| 287 |
cols, data = self.cmd.take_action(parsed_args) |
| 288 |
|
| 289 |
self.networkclient.get_bgpvpn_router_association.\ |
| 290 |
assert_called_once_with(fake_bgpvpn['id'], fake_res_assoc['id']) |
| 291 |
self.assertEqual(sorted_columns, cols) |
| 292 |
self.assertEqual(data, _get_data(fake_res_assoc)) |