| 1 |
# Copyright (c) 2016 Juniper Networks Inc. |
| 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 import exceptions |
| 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.BgpvpnFakeAssoc._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.BgpvpnFakeAssoc._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.BgpvpnFakeAssoc._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.BgpvpnFakeAssoc._attr_map |
| 41 |
if listing_mode in (column_util.LIST_BOTH, |
| 42 |
column_util.LIST_LONG_ONLY)) |
| 43 |
sorted_attr_map = sorted(fakes.BgpvpnFakeAssoc._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 TestCreateResAssoc(fakes.TestNeutronClientBgpvpn): |
| 55 |
def setUp(self): |
| 56 |
super(TestCreateResAssoc, self).setUp() |
| 57 |
self.networkclient.find_bgpvpn = mock.Mock( |
| 58 |
side_effect=lambda name_or_id: {'id': name_or_id}) |
| 59 |
self.networkclient.find_fake_resource = mock.Mock( |
| 60 |
side_effect=lambda name_or_id: {'id': name_or_id}) |
| 61 |
self.cmd = fakes.CreateBgpvpnFakeResAssoc(self.app, self.namespace) |
| 62 |
|
| 63 |
def test_create_resource_association(self): |
| 64 |
fake_bgpvpn = fakes.create_one_bgpvpn() |
| 65 |
fake_res = fakes.create_one_resource() |
| 66 |
fake_res_assoc = fakes.create_one_resource_association( |
| 67 |
fake_res) |
| 68 |
self.networkclient.create_bgpvpn_router_association = mock.Mock( |
| 69 |
return_value=fake_res_assoc) |
| 70 |
self.networkclient.find_bgpvpn_fake_resource_association = mock.Mock( |
| 71 |
side_effect=lambda name_or_id: {'id': name_or_id}) |
| 72 |
arglist = [ |
| 73 |
fake_bgpvpn['id'], |
| 74 |
fake_res['id'], |
| 75 |
'--project', fake_bgpvpn['tenant_id'], |
| 76 |
] |
| 77 |
verifylist = [ |
| 78 |
('bgpvpn', fake_bgpvpn['id']), |
| 79 |
('resource', fake_res['id']), |
| 80 |
('project', fake_bgpvpn['tenant_id']) |
| 81 |
] |
| 82 |
|
| 83 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 84 |
|
| 85 |
cols, data = self.cmd.take_action(parsed_args) |
| 86 |
|
| 87 |
fake_res_assoc_call = { |
| 88 |
'fake_resource_id': 'fake_resource_id', |
| 89 |
'tenant_id': 'fake_project_id' |
| 90 |
} |
| 91 |
|
| 92 |
self.networkclient.create_bgpvpn_router_association.\ |
| 93 |
assert_called_once_with( |
| 94 |
fake_bgpvpn['id'], |
| 95 |
**fake_res_assoc_call) |
| 96 |
self.assertEqual(sorted_columns, cols) |
| 97 |
self.assertEqual(_get_data(fake_res_assoc), data) |
| 98 |
|
| 99 |
|
| 100 |
class TestSetResAssoc(fakes.TestNeutronClientBgpvpn): |
| 101 |
def setUp(self): |
| 102 |
super(TestSetResAssoc, self).setUp() |
| 103 |
self.cmd = fakes.SetBgpvpnFakeResAssoc(self.app, self.namespace) |
| 104 |
|
| 105 |
def test_set_resource_association(self): |
| 106 |
fake_bgpvpn = fakes.create_one_bgpvpn() |
| 107 |
fake_res = fakes.create_one_resource() |
| 108 |
fake_res_assoc = fakes.create_one_resource_association( |
| 109 |
fake_res) |
| 110 |
self.networkclient.update_bgpvpn_router_association = mock.Mock( |
| 111 |
return_value={fakes.BgpvpnFakeAssoc._resource: fake_res_assoc}) |
| 112 |
arglist = [ |
| 113 |
fake_res_assoc['id'], |
| 114 |
fake_bgpvpn['id'], |
| 115 |
] |
| 116 |
verifylist = [ |
| 117 |
('resource_association_id', fake_res_assoc['id']), |
| 118 |
('bgpvpn', fake_bgpvpn['id']), |
| 119 |
] |
| 120 |
|
| 121 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 122 |
|
| 123 |
result = self.cmd.take_action(parsed_args) |
| 124 |
|
| 125 |
self.networkclient.update_bgpvpn_router_association.\ |
| 126 |
assert_not_called() |
| 127 |
self.assertIsNone(result) |
| 128 |
|
| 129 |
|
| 130 |
class TestDeleteResAssoc(fakes.TestNeutronClientBgpvpn): |
| 131 |
def setUp(self): |
| 132 |
super(TestDeleteResAssoc, self).setUp() |
| 133 |
self.networkclient.find_bgpvpn = mock.Mock( |
| 134 |
side_effect=lambda name_or_id: {'id': name_or_id}) |
| 135 |
self.cmd = fakes.DeleteBgpvpnFakeResAssoc(self.app, self.namespace) |
| 136 |
|
| 137 |
def test_delete_one_association(self): |
| 138 |
fake_bgpvpn = fakes.create_one_bgpvpn() |
| 139 |
fake_res = fakes.create_one_resource() |
| 140 |
fake_res_assoc = fakes.create_one_resource_association( |
| 141 |
fake_res) |
| 142 |
self.networkclient.delete_bgpvpn_router_association = \ |
| 143 |
mock.Mock() |
| 144 |
arglist = [ |
| 145 |
fake_res_assoc['id'], |
| 146 |
fake_bgpvpn['id'], |
| 147 |
] |
| 148 |
verifylist = [ |
| 149 |
('resource_association_ids', [fake_res_assoc['id']]), |
| 150 |
('bgpvpn', fake_bgpvpn['id']), |
| 151 |
] |
| 152 |
|
| 153 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 154 |
|
| 155 |
result = self.cmd.take_action(parsed_args) |
| 156 |
|
| 157 |
self.networkclient.delete_bgpvpn_router_association.\ |
| 158 |
assert_called_once_with(fake_bgpvpn['id'], fake_res_assoc['id']) |
| 159 |
self.assertIsNone(result) |
| 160 |
|
| 161 |
def test_delete_multi_bpgvpn(self): |
| 162 |
count = 3 |
| 163 |
fake_bgpvpn = fakes.create_one_bgpvpn() |
| 164 |
fake_res = fakes.create_resources(count=count) |
| 165 |
fake_res_assocs = fakes.create_resource_associations( |
| 166 |
fake_res) |
| 167 |
fake_res_assoc_ids = [ |
| 168 |
fake_res_assoc['id'] for fake_res_assoc in fake_res_assocs |
| 169 |
] |
| 170 |
self.networkclient.delete_bgpvpn_router_association = \ |
| 171 |
mock.Mock() |
| 172 |
arglist = \ |
| 173 |
fake_res_assoc_ids + [ |
| 174 |
fake_bgpvpn['id'] |
| 175 |
] |
| 176 |
verifylist = [ |
| 177 |
('resource_association_ids', fake_res_assoc_ids), |
| 178 |
('bgpvpn', fake_bgpvpn['id']), |
| 179 |
] |
| 180 |
|
| 181 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 182 |
|
| 183 |
result = self.cmd.take_action(parsed_args) |
| 184 |
|
| 185 |
self.networkclient.delete_bgpvpn_router_association.\ |
| 186 |
assert_has_calls([ |
| 187 |
mock.call( |
| 188 |
fake_bgpvpn['id'], id) for id in fake_res_assoc_ids]) |
| 189 |
self.assertIsNone(result) |
| 190 |
|
| 191 |
def test_delete_multi_bpgvpn_with_unknown(self): |
| 192 |
count = 3 |
| 193 |
fake_bgpvpn = fakes.create_one_bgpvpn() |
| 194 |
fake_res = fakes.create_resources(count=count) |
| 195 |
fake_res_assocs = fakes.create_resource_associations( |
| 196 |
fake_res) |
| 197 |
fake_res_assoc_ids = [ |
| 198 |
fake_res_assoc['id'] for fake_res_assoc in fake_res_assocs |
| 199 |
] |
| 200 |
|
| 201 |
def raise_unknonw_resource(resource_path, name_or_id): |
| 202 |
if str(count - 2) in name_or_id: |
| 203 |
raise Exception() |
| 204 |
self.networkclient.delete_bgpvpn_router_association = mock.Mock( |
| 205 |
side_effect=raise_unknonw_resource) |
| 206 |
arglist = \ |
| 207 |
fake_res_assoc_ids + [ |
| 208 |
fake_bgpvpn['id'] |
| 209 |
] |
| 210 |
verifylist = [ |
| 211 |
('resource_association_ids', fake_res_assoc_ids), |
| 212 |
('bgpvpn', fake_bgpvpn['id']), |
| 213 |
] |
| 214 |
|
| 215 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 216 |
|
| 217 |
self.assertRaises(exceptions.CommandError, self.cmd.take_action, |
| 218 |
parsed_args) |
| 219 |
|
| 220 |
self.networkclient.delete_bgpvpn_router_association.\ |
| 221 |
assert_has_calls([ |
| 222 |
mock.call(fake_bgpvpn['id'], id) for id in fake_res_assoc_ids] |
| 223 |
) |
| 224 |
|
| 225 |
|
| 226 |
class TestListResAssoc(fakes.TestNeutronClientBgpvpn): |
| 227 |
def setUp(self): |
| 228 |
super(TestListResAssoc, self).setUp() |
| 229 |
self.networkclient.find_bgpvpn = mock.Mock( |
| 230 |
side_effect=lambda name_or_id: {'id': name_or_id}) |
| 231 |
self.cmd = fakes.ListBgpvpnFakeResAssoc(self.app, self.namespace) |
| 232 |
|
| 233 |
def test_list_bgpvpn_associations(self): |
| 234 |
count = 3 |
| 235 |
fake_bgpvpn = fakes.create_one_bgpvpn() |
| 236 |
fake_res = fakes.create_resources(count=count) |
| 237 |
fake_res_assocs = fakes.create_resource_associations( |
| 238 |
fake_res) |
| 239 |
self.networkclient.bgpvpn_router_associations = mock.Mock( |
| 240 |
return_value=fake_res_assocs) |
| 241 |
arglist = [ |
| 242 |
fake_bgpvpn['id'], |
| 243 |
] |
| 244 |
verifylist = [ |
| 245 |
('bgpvpn', fake_bgpvpn['id']), |
| 246 |
] |
| 247 |
|
| 248 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 249 |
|
| 250 |
headers, data = self.cmd.take_action(parsed_args) |
| 251 |
|
| 252 |
self.networkclient.bgpvpn_router_associations.\ |
| 253 |
assert_called_once_with(fake_bgpvpn['id'], retrieve_all=True) |
| 254 |
self.assertEqual(headers, list(headers_short)) |
| 255 |
self.assertEqual( |
| 256 |
list(data), |
| 257 |
[_get_data(fake_res_assoc, columns_short) for fake_res_assoc |
| 258 |
in fake_res_assocs]) |
| 259 |
|
| 260 |
def test_list_bgpvpn_associations_long_mode(self): |
| 261 |
count = 3 |
| 262 |
fake_bgpvpn = fakes.create_one_bgpvpn() |
| 263 |
fake_res = fakes.create_resources(count=count) |
| 264 |
fake_res_assocs = fakes.create_resource_associations( |
| 265 |
fake_res) |
| 266 |
self.networkclient.bgpvpn_router_associations = mock.Mock( |
| 267 |
return_value=fake_res_assocs) |
| 268 |
arglist = [ |
| 269 |
'--long', |
| 270 |
fake_bgpvpn['id'], |
| 271 |
] |
| 272 |
verifylist = [ |
| 273 |
('long', True), |
| 274 |
('bgpvpn', fake_bgpvpn['id']), |
| 275 |
] |
| 276 |
|
| 277 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 278 |
|
| 279 |
headers, data = self.cmd.take_action(parsed_args) |
| 280 |
|
| 281 |
self.networkclient.bgpvpn_router_associations.\ |
| 282 |
assert_called_once_with(fake_bgpvpn['id'], retrieve_all=True) |
| 283 |
self.assertEqual(headers, list(headers_long)) |
| 284 |
self.assertEqual( |
| 285 |
list(data), |
| 286 |
[_get_data(fake_res_assoc, columns_long) for fake_res_assoc |
| 287 |
in fake_res_assocs]) |
| 288 |
|
| 289 |
|
| 290 |
class TestShowResAssoc(fakes.TestNeutronClientBgpvpn): |
| 291 |
def setUp(self): |
| 292 |
super(TestShowResAssoc, self).setUp() |
| 293 |
self.networkclient.find_bgpvpn = mock.Mock( |
| 294 |
side_effect=lambda name_or_id: {'id': name_or_id}) |
| 295 |
self.cmd = fakes.ShowBgpvpnFakeResAssoc(self.app, self.namespace) |
| 296 |
|
| 297 |
def test_show_resource_association(self): |
| 298 |
fake_bgpvpn = fakes.create_one_bgpvpn() |
| 299 |
fake_res = fakes.create_one_resource() |
| 300 |
fake_res_assoc = fakes.create_one_resource_association( |
| 301 |
fake_res) |
| 302 |
self.networkclient.get_bgpvpn_router_association = mock.Mock( |
| 303 |
return_value=fake_res_assoc) |
| 304 |
arglist = [ |
| 305 |
fake_res_assoc['id'], |
| 306 |
fake_bgpvpn['id'], |
| 307 |
] |
| 308 |
verifylist = [ |
| 309 |
('resource_association_id', fake_res_assoc['id']), |
| 310 |
('bgpvpn', fake_bgpvpn['id']), |
| 311 |
] |
| 312 |
|
| 313 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 314 |
|
| 315 |
columns, data = self.cmd.take_action(parsed_args) |
| 316 |
|
| 317 |
self.networkclient.get_bgpvpn_router_association.\ |
| 318 |
assert_called_once_with(fake_bgpvpn['id'], fake_res_assoc['id']) |
| 319 |
self.assertEqual(sorted_columns, columns) |
| 320 |
self.assertEqual(data, _get_data(fake_res_assoc)) |