| 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 copy |
| 18 |
from unittest import mock |
| 19 |
|
| 20 |
from openstack.network.v2 import bgpvpn as _bgpvpn |
| 21 |
from openstack import resource as sdk_resource |
| 22 |
from osc_lib.utils import columns as column_util |
| 23 |
|
| 24 |
from neutronclient.osc import utils as nc_osc_utils |
| 25 |
from neutronclient.osc.v2.networking_bgpvpn.resource_association import\ |
| 26 |
CreateBgpvpnResAssoc |
| 27 |
from neutronclient.osc.v2.networking_bgpvpn.resource_association import\ |
| 28 |
DeleteBgpvpnResAssoc |
| 29 |
from neutronclient.osc.v2.networking_bgpvpn.resource_association import\ |
| 30 |
ListBgpvpnResAssoc |
| 31 |
from neutronclient.osc.v2.networking_bgpvpn.resource_association import\ |
| 32 |
SetBgpvpnResAssoc |
| 33 |
from neutronclient.osc.v2.networking_bgpvpn.resource_association import\ |
| 34 |
ShowBgpvpnResAssoc |
| 35 |
from neutronclient.osc.v2.networking_bgpvpn.resource_association import\ |
| 36 |
UnsetBgpvpnResAssoc |
| 37 |
from neutronclient.osc.v2.networking_bgpvpn.router_association import\ |
| 38 |
CreateBgpvpnRouterAssoc |
| 39 |
from neutronclient.osc.v2.networking_bgpvpn.router_association import\ |
| 40 |
SetBgpvpnRouterAssoc |
| 41 |
from neutronclient.osc.v2.networking_bgpvpn.router_association import\ |
| 42 |
ShowBgpvpnRouterAssoc |
| 43 |
from neutronclient.tests.unit.osc.v2 import fakes as test_fakes |
| 44 |
|
| 45 |
|
| 46 |
_FAKE_PROJECT_ID = 'fake_project_id' |
| 47 |
|
| 48 |
|
| 49 |
class TestNeutronClientBgpvpn(test_fakes.TestNeutronClientOSCV2): |
| 50 |
|
| 51 |
def setUp(self): |
| 52 |
super(TestNeutronClientBgpvpn, self).setUp() |
| 53 |
self.neutronclient.find_resource = mock.Mock( |
| 54 |
side_effect=lambda resource, name_or_id, project_id=None, |
| 55 |
cmd_resource=None, parent_id=None, fields=None: |
| 56 |
{'id': name_or_id, 'tenant_id': _FAKE_PROJECT_ID}) |
| 57 |
self.neutronclient.find_resource_by_id = mock.Mock( |
| 58 |
side_effect=lambda resource, resource_id, cmd_resource=None, |
| 59 |
parent_id=None, fields=None: |
| 60 |
{'id': resource_id, 'tenant_id': _FAKE_PROJECT_ID}) |
| 61 |
nc_osc_utils.find_project = mock.Mock( |
| 62 |
side_effect=lambda _, name_or_id, __: mock.Mock(id=name_or_id)) |
| 63 |
|
| 64 |
|
| 65 |
def create_one_bgpvpn(attrs=None): |
| 66 |
"""Create a fake BGP VPN.""" |
| 67 |
|
| 68 |
attrs = attrs or {} |
| 69 |
|
| 70 |
# Set default attributes. |
| 71 |
bgpvpn_attrs = { |
| 72 |
'id': 'fake_bgpvpn_id', |
| 73 |
'tenant_id': _FAKE_PROJECT_ID, |
| 74 |
'name': '', |
| 75 |
'type': 'l3', |
| 76 |
'route_targets': [], |
| 77 |
'import_targets': [], |
| 78 |
'export_targets': [], |
| 79 |
'route_distinguishers': [], |
| 80 |
'networks': [], |
| 81 |
'routers': [], |
| 82 |
'ports': [], |
| 83 |
'vni': 100, |
| 84 |
'local_pref': 777, |
| 85 |
} |
| 86 |
|
| 87 |
# Overwrite default attributes. |
| 88 |
bgpvpn_attrs.update(attrs) |
| 89 |
return _bgpvpn.BgpVpn(**bgpvpn_attrs) |
| 90 |
|
| 91 |
|
| 92 |
def create_bgpvpns(attrs=None, count=1): |
| 93 |
"""Create multiple fake BGP VPN.""" |
| 94 |
|
| 95 |
bgpvpns = [] |
| 96 |
for i in range(0, count): |
| 97 |
if attrs is None: |
| 98 |
attrs = {'id': 'fake_id%d' % i} |
| 99 |
elif getattr(attrs, 'id', None) is None: |
| 100 |
attrs['id'] = 'fake_id%d' % i |
| 101 |
bgpvpns.append(create_one_bgpvpn(attrs)) |
| 102 |
|
| 103 |
return bgpvpns |
| 104 |
|
| 105 |
|
| 106 |
class BgpvpnFakeAssoc(object): |
| 107 |
_assoc_res_name = 'fake_resource' |
| 108 |
_resource = '%s_association' % _assoc_res_name |
| 109 |
_resource_plural = '%ss' % _resource |
| 110 |
|
| 111 |
_attr_map = ( |
| 112 |
('id', 'ID', column_util.LIST_BOTH), |
| 113 |
('%s_id' % _assoc_res_name, '%s ID' % _assoc_res_name.capitalize(), |
| 114 |
column_util.LIST_BOTH), |
| 115 |
('name', 'Name', column_util.LIST_BOTH), |
| 116 |
('project_id', 'Project ID', column_util.LIST_BOTH), |
| 117 |
) |
| 118 |
_formatters = {} |
| 119 |
|
| 120 |
|
| 121 |
class CreateBgpvpnFakeResAssoc(BgpvpnFakeAssoc, CreateBgpvpnResAssoc): |
| 122 |
pass |
| 123 |
|
| 124 |
|
| 125 |
class SetBgpvpnFakeResAssoc(BgpvpnFakeAssoc, SetBgpvpnResAssoc): |
| 126 |
pass |
| 127 |
|
| 128 |
|
| 129 |
class UnsetBgpvpnFakeResAssoc(BgpvpnFakeAssoc, UnsetBgpvpnResAssoc): |
| 130 |
pass |
| 131 |
|
| 132 |
|
| 133 |
class DeleteBgpvpnFakeResAssoc(BgpvpnFakeAssoc, DeleteBgpvpnResAssoc): |
| 134 |
pass |
| 135 |
|
| 136 |
|
| 137 |
class ListBgpvpnFakeResAssoc(BgpvpnFakeAssoc, ListBgpvpnResAssoc): |
| 138 |
pass |
| 139 |
|
| 140 |
|
| 141 |
class ShowBgpvpnFakeResAssoc(BgpvpnFakeAssoc, ShowBgpvpnResAssoc): |
| 142 |
pass |
| 143 |
|
| 144 |
|
| 145 |
class BgpvpnFakeRouterAssoc(object): |
| 146 |
_assoc_res_name = 'fake_resource' |
| 147 |
_resource = '%s_association' % _assoc_res_name |
| 148 |
_resource_plural = '%ss' % _resource |
| 149 |
|
| 150 |
_attr_map = ( |
| 151 |
('id', 'ID', column_util.LIST_BOTH), |
| 152 |
('%s_id' % _assoc_res_name, '%s ID' % _assoc_res_name.capitalize(), |
| 153 |
column_util.LIST_BOTH), |
| 154 |
('advertise_extra_routes', 'Advertise extra routes', |
| 155 |
column_util.LIST_LONG_ONLY), |
| 156 |
('name', 'Name', column_util.LIST_BOTH), |
| 157 |
('project_id', 'Project ID', column_util.LIST_BOTH), |
| 158 |
) |
| 159 |
_formatters = {} |
| 160 |
|
| 161 |
|
| 162 |
class CreateBgpvpnFakeRouterAssoc(BgpvpnFakeRouterAssoc, |
| 163 |
CreateBgpvpnRouterAssoc): |
| 164 |
pass |
| 165 |
|
| 166 |
|
| 167 |
class SetBgpvpnFakeRouterAssoc(BgpvpnFakeRouterAssoc, SetBgpvpnRouterAssoc): |
| 168 |
pass |
| 169 |
|
| 170 |
|
| 171 |
class ShowBgpvpnFakeRouterAssoc(BgpvpnFakeRouterAssoc, ShowBgpvpnRouterAssoc): |
| 172 |
pass |
| 173 |
|
| 174 |
|
| 175 |
class FakeResource(sdk_resource.Resource): |
| 176 |
resource_key = 'fakeresource' |
| 177 |
resources_key = 'fakeresources' |
| 178 |
base_path = '/bgpvpn/fakeresources' |
| 179 |
|
| 180 |
_allow_unknown_attrs_in_body = True |
| 181 |
|
| 182 |
# capabilities |
| 183 |
allow_create = True |
| 184 |
allow_fetch = True |
| 185 |
allow_commit = True |
| 186 |
allow_delete = True |
| 187 |
allow_list = True |
| 188 |
|
| 189 |
id = sdk_resource.Body('id') |
| 190 |
tenant_id = sdk_resource.Body('tenant_id', deprecated=True) |
| 191 |
project_id = sdk_resource.Body('project_id', alias='tenant_id') |
| 192 |
|
| 193 |
|
| 194 |
class FakeResoureAssociation(sdk_resource.Resource): |
| 195 |
resource_key = 'fakeresourceassociation' |
| 196 |
resources_key = 'fakeresourceassociations' |
| 197 |
base_path = '/bgpvpn/fakeresourceassociations' |
| 198 |
|
| 199 |
_allow_unknown_attrs_in_body = True |
| 200 |
|
| 201 |
# capabilities |
| 202 |
allow_create = True |
| 203 |
allow_fetch = True |
| 204 |
allow_commit = True |
| 205 |
allow_delete = True |
| 206 |
allow_list = True |
| 207 |
|
| 208 |
id = sdk_resource.Body('id') |
| 209 |
tenant_id = sdk_resource.Body('tenant_id', deprecated=True) |
| 210 |
project_id = sdk_resource.Body('project_id', alias='tenant_id') |
| 211 |
|
| 212 |
|
| 213 |
def create_one_resource(attrs=None): |
| 214 |
"""Create a fake resource.""" |
| 215 |
attrs = attrs or {} |
| 216 |
|
| 217 |
# Set default attributes. |
| 218 |
res_attrs = { |
| 219 |
'id': 'fake_resource_id', |
| 220 |
'tenant_id': _FAKE_PROJECT_ID, |
| 221 |
} |
| 222 |
|
| 223 |
# Overwrite default attributes. |
| 224 |
res_attrs.update(attrs) |
| 225 |
return FakeResource(**res_attrs) |
| 226 |
|
| 227 |
|
| 228 |
def create_resources(attrs=None, count=1): |
| 229 |
"""Create multiple fake resources.""" |
| 230 |
|
| 231 |
resources = [] |
| 232 |
for i in range(0, count): |
| 233 |
if attrs is None: |
| 234 |
attrs = {'id': 'fake_id%d' % i} |
| 235 |
elif getattr(attrs, 'id', None) is None: |
| 236 |
attrs['id'] = 'fake_id%d' % i |
| 237 |
resources.append(create_one_resource(attrs)) |
| 238 |
|
| 239 |
return resources |
| 240 |
|
| 241 |
|
| 242 |
def create_one_resource_association(resource, attrs=None): |
| 243 |
"""Create a fake resource association.""" |
| 244 |
|
| 245 |
attrs = attrs or {} |
| 246 |
|
| 247 |
res_assoc_attrs = { |
| 248 |
'id': 'fake_association_id', |
| 249 |
'tenant_id': resource['tenant_id'], |
| 250 |
'fake_resource_id': resource['id'], |
| 251 |
} |
| 252 |
|
| 253 |
# Overwrite default attributes. |
| 254 |
res_assoc_attrs.update(attrs) |
| 255 |
return FakeResoureAssociation(**res_assoc_attrs) |
| 256 |
|
| 257 |
|
| 258 |
def create_resource_associations(resources): |
| 259 |
"""Create multiple fake resource associations.""" |
| 260 |
|
| 261 |
res_assocs = [] |
| 262 |
for idx, resource in enumerate(resources): |
| 263 |
res_assoc_attrs = { |
| 264 |
'id': 'fake_association_id%d' % idx, |
| 265 |
'tenant_id': resource['tenant_id'], |
| 266 |
'fake_resource_id': resource['id'], |
| 267 |
} |
| 268 |
res_assocs.append(copy.deepcopy(res_assoc_attrs)) |
| 269 |
|
| 270 |
return res_assocs |