1
# Copyright 2014 Alcatel-Lucent USA Inc.
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
# not use this file except in compliance with the License. You may obtain
5
# a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
# License for the specific language governing permissions and limitations
18
from neutron import context
19
from neutron.openstack.common import uuidutils
20
from neutron.plugins.nuage import nuage_models
21
from neutron.plugins.nuage import syncmanager as sync
22
from neutron.tests.unit.nuage import test_netpartition
23
from neutron.tests.unit.nuage import test_nuage_plugin
24
from neutron.tests.unit import test_extension_extraroute as extraroute_test
25
from neutron.tests.unit import test_extension_security_group as test_sg
26
from neutron.tests.unit import test_l3_plugin
28
_uuid = uuidutils.generate_uuid
31
class TestL3Sync(test_nuage_plugin.NuagePluginV2TestCase,
32
test_l3_plugin.L3NatDBIntTestCase):
35
self.session = context.get_admin_context().session
36
self.syncmanager = sync.SyncManager(
37
test_nuage_plugin.getNuageClient())
38
super(TestL3Sync, self).setUp()
40
def _make_floatingip_for_tenant_port(self, net_id, port_id, tenant_id):
41
data = {'floatingip': {'floating_network_id': net_id,
42
'tenant_id': tenant_id,
44
floatingip_req = self.new_create_request('floatingips', data, self.fmt)
45
res = floatingip_req.get_response(self.ext_api)
46
return self.deserialize(self.fmt, res)
48
def test_router_sync(self):
49
# If the router exists in neutron and not in VSD,
50
# sync will create it in VSD. But the nuage_router_id
51
# will now change and will be updated in neutron
53
rtr_res = self._create_router('json', 'foo', 'test-router', True)
54
router = self.deserialize('json', rtr_res)
56
self.syncmanager.synchronize('250')
58
# Check that the nuage_router_id is updated in entrtrmapping table
59
router_db = self.session.query(
60
nuage_models.NetPartitionRouter).filter_by(
61
router_id=router['router']['id']).first()
63
self.assertEqual('2d782c02-b88e-44ad-a79b-4bdf11f7df3d',
64
router_db['nuage_router_id'])
66
self._delete('routers', router['router']['id'])
68
def test_router_deleted_get(self):
69
data = self.syncmanager._get_router_data(_uuid())
70
self.assertIsNone(data[0])
71
self.assertIsNone(data[1])
73
def test_fip_sync(self):
74
with self.subnet(cidr='200.0.0.0/24') as public_sub:
75
self._set_net_external(public_sub['subnet']['network_id'])
76
with contextlib.nested(self.port(), self.port(), self.port()) as (
78
p1_id = p1['port']['id']
79
p2_id = p2['port']['id']
80
p3_id = p3['port']['id']
81
with contextlib.nested(self.floatingip_with_assoc(
82
port_id=p1_id), self.floatingip_with_assoc(
83
port_id=p2_id), self.floatingip_with_assoc(
84
port_id=p3_id)) as (fip1, fip2, fip3):
86
'add': [fip1['floatingip']['id']],
87
'associate': [fip2['floatingip']['id']],
88
'disassociate': [fip3['floatingip']['id']]
90
self.syncmanager._sync_fips(fip_dict)
92
def test_deleted_fip_sync(self):
95
'associate': [_uuid()],
96
'disassociate': [_uuid()]
98
self.syncmanager._sync_fips(fip_dict)
100
def test_fip_and_ipalloc_get(self):
101
with self.subnet(cidr='200.0.0.0/24') as public_sub:
102
self._set_net_external(public_sub['subnet']['network_id'])
103
with self.port() as port:
104
p_id = port['port']['id']
105
with self.floatingip_with_assoc(port_id=p_id) as fip:
107
data = self.syncmanager._get_fip_data(
108
fip['floatingip']['id'])
110
self.assertEqual(fip['floatingip']['id'], data['id'])
112
data = self.syncmanager._get_ipalloc_for_fip(
114
self.assertEqual(fip['floatingip']['floating_ip_address'],
117
def test_fip_and_ipalloc_deleted_get(self):
118
data = self.syncmanager._get_fip_data(_uuid())
119
self.assertIsNone(data)
123
'floating_network_id': _uuid(),
124
'floating_ip_address': '176.176.10.10'
126
data = self.syncmanager._get_ipalloc_for_fip(fip)
127
self.assertIsNone(data)
129
def test_domainsubnet_sync(self):
130
with self.subnet() as s1:
131
with contextlib.nested(
133
self.port()) as (r1, p1):
134
self._router_interface_action(
135
'add', r1['router']['id'],
136
s1['subnet']['id'], p1['port']['id'])
138
'domainsubnet': {'add': [s1['subnet']['id']]},
139
'port': {'sub_rtr_intf_port_dict': {s1['subnet']['id']:
141
self.syncmanager.sync_domainsubnets(domainsubn_dict)
142
self._router_interface_action('remove', r1['router']['id'],
143
s1['subnet']['id'], None)
145
def test_floatingip_update_different_router(self):
146
self._test_floatingip_update_different_router()
148
def test_floatingip_update_different_fixed_ip_same_port(self):
149
self._test_floatingip_update_different_fixed_ip_same_port()
151
def test_floatingip_create_different_fixed_ip_same_port(self):
152
self._test_floatingip_create_different_fixed_ip_same_port()
154
def test_network_update_external_failure(self):
155
self._test_network_update_external_failure()
158
class TestExtraRouteSync(extraroute_test.ExtraRouteDBIntTestCase):
161
self.session = context.get_admin_context().session
162
self.syncmanager = sync.SyncManager(
163
test_nuage_plugin.getNuageClient())
164
super(TestExtraRouteSync, self).setUp()
166
def test_route_sync(self):
167
route = {'destination': '135.207.0.0/16', 'nexthop': '10.0.1.3'}
168
with self.router() as r:
169
with self.subnet(cidr='10.0.1.0/24') as s:
170
net_id = s['subnet']['network_id']
171
res = self._create_port('json', net_id)
172
p = self.deserialize(self.fmt, res)
173
self._routes_update_prepare(r['router']['id'],
174
None, p['port']['id'], [route])
176
route_dict = {'route': {'add': [route]}}
177
self.syncmanager.sync_routes(route_dict)
179
self._routes_update_cleanup(p['port']['id'],
180
None, r['router']['id'], [])
182
def test_route_get(self):
183
routes = [{'destination': '135.207.0.0/16', 'nexthop': '10.0.1.3'}]
184
with self.router() as r:
185
with self.subnet(cidr='10.0.1.0/24') as s:
186
net_id = s['subnet']['network_id']
187
res = self._create_port('json', net_id)
188
p = self.deserialize(self.fmt, res)
189
self._routes_update_prepare(r['router']['id'],
190
None, p['port']['id'], routes)
192
data = self.syncmanager._get_route_data(routes[0])
193
self.assertEqual(routes[0]['destination'], data['destination'])
194
self.assertEqual(routes[0]['nexthop'], data['nexthop'])
195
self._routes_update_cleanup(p['port']['id'],
196
None, r['router']['id'], [])
198
def test_route_deleted_get(self):
199
route = {'destination': '135.207.0.0/16', 'nexthop': '10.0.1.3'}
200
data = self.syncmanager._get_route_data(route)
201
self.assertIsNone(data)
204
class TestNetPartSync(test_netpartition.NetPartitionTestCase):
207
self.session = context.get_admin_context().session
208
self.syncmanager = sync.SyncManager(
209
test_nuage_plugin.getNuageClient())
210
super(TestNetPartSync, self).setUp()
212
def test_net_partition_sync(self):
213
# If the net-partition exists in neutron and not in VSD,
214
# sync will create it in VSD. But the net-partition
215
# id will now change and has to be updated in neutron
217
netpart = self._make_netpartition('json', 'sync-new-netpartition')
219
self.syncmanager.synchronize('250')
221
# Check that the net-partition id is updated in db
222
netpart_db = self.session.query(
223
nuage_models.NetPartition).filter_by(name=netpart['net_partition'][
226
self.assertEqual('a917924f-3139-4bdb-a4c3-ea7c8011582f',
228
self._del_netpartition(netpart_db['id'])
230
def test_net_partition_deleted_get(self):
231
data = self.syncmanager._get_netpart_data(_uuid())
232
self.assertIsNone(data)
235
class TestL2Sync(test_nuage_plugin.NuagePluginV2TestCase):
238
self.session = context.get_admin_context().session
239
self.syncmanager = sync.SyncManager(
240
test_nuage_plugin.getNuageClient())
241
super(TestL2Sync, self).setUp()
243
def test_subnet_sync(self):
244
# If the subnet exists in neutron and not in VSD,
245
# sync will create it in VSD. But the nuage_subnet_id
246
# will now change and will be updated in neutron
248
net_res = self._create_network("json", "pub", True)
249
network = self.deserialize('json', net_res)
251
sub_res = self._create_subnet("json", network['network']['id'],
253
subnet = self.deserialize('json', sub_res)
255
self.syncmanager.synchronize('250')
257
# Check that the nuage_subnet_id is updated in db
258
subl2dom_db = self.session.query(
259
nuage_models.SubnetL2Domain).filter_by(subnet_id=subnet[
260
'subnet']['id']).first()
261
self.assertEqual('52daa465-cf33-4efd-91d3-f5bc2aebd',
262
subl2dom_db['nuage_subnet_id'])
264
self._delete('subnets', subnet['subnet']['id'])
265
self._delete('networks', network['network']['id'])
267
def test_subnet_deleted_get(self):
268
data = self.syncmanager._get_subnet_data(_uuid())
269
self.assertIsNone(data[0])
270
self.assertIsNone(data[1])
272
def test_sharednetwork_sync(self):
273
with self.subnet(cidr='200.0.0.0/24') as public_sub:
274
sharednet_dict = {'sharednetwork': {'add': [public_sub['subnet'][
276
self.syncmanager.sync_sharednetworks(sharednet_dict)
278
def test_vm_sync(self):
279
with self.port() as p:
280
port_dict = {'port': {'vm': [p['port']['id']]}}
281
self.syncmanager.sync_vms(port_dict)
284
class TestSecurityGroupSync(test_sg.TestSecurityGroups):
287
self.session = context.get_admin_context().session
288
self.syncmanager = sync.SyncManager(
289
test_nuage_plugin.getNuageClient())
290
super(TestSecurityGroupSync, self).setUp()
292
def test_sg_get(self):
293
with self.security_group() as sg:
294
data = self.syncmanager._get_sec_grp_data(
295
sg['security_group']['id'])
296
self.assertEqual(sg['security_group']['id'], data['id'])
298
def test_sg_deleted_get(self):
299
data = self.syncmanager._get_sec_grp_data(_uuid())
300
self.assertIsNone(data)
302
def test_sg_rule_get(self):
303
with self.security_group() as sg:
304
sg_rule_id = sg['security_group']['security_group_rules'][0]['id']
305
data = self.syncmanager._get_sec_grp_rule_data(sg_rule_id)
306
self.assertEqual(sg_rule_id, data['id'])
308
def test_sg_rule_deleted_get(self):
309
data = self.syncmanager._get_sec_grp_rule_data(_uuid())
310
self.assertIsNone(data)
312
def test_sg_grp_sync(self):
313
with contextlib.nested(self.security_group(),
314
self.security_group()) as (sg1, sg2):
315
sg1_id = sg1['security_group']['id']
316
sg2_id = sg2['security_group']['id']
317
sg_dict = {'security': {'secgroup': {'l2domain': {'add': {sg1_id: [
318
_uuid()]}}, 'domain': {'add': {sg2_id: [_uuid()]}}}}}
319
self.syncmanager.sync_secgrps(sg_dict)
321
def test_deleted_sg_grp_sync(self):
322
sg_dict = {'security': {'secgroup': {'l2domain': {'add': {_uuid(): [
323
_uuid()]}}, 'domain': {'add': {_uuid(): [_uuid()]}}}}}
324
self.syncmanager.sync_secgrps(sg_dict)
326
def test_sg_rule_sync(self):
327
with contextlib.nested(self.security_group(),
328
self.security_group()) as (sg1, sg2):
330
sg1['security_group']['security_group_rules'][0]['id'])
332
sg2['security_group']['security_group_rules'][0]['id'])
334
sg_dict = {'security': {'secgrouprule': {'l2domain': {
335
'add': [sg1_rule_id]}, 'domain': {'add': [sg2_rule_id]}}}}
336
self.syncmanager.sync_secgrp_rules(sg_dict)
338
def test_deleted_sg_grp_rule_sync(self):
339
sg_dict = {'security': {'secgrouprule':
340
{'l2domain': {'add': [_uuid()]},
341
'domain': {'add': [_uuid()]}}}}
342
self.syncmanager.sync_secgrp_rules(sg_dict)