~niedbalski/ubuntu/vivid/neutron/fixes-1447803

« back to all changes in this revision

Viewing changes to neutron/tests/unit/nuage/test_syncmanager.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-10-03 18:45:23 UTC
  • mfrom: (1.1.15)
  • Revision ID: package-import@ubuntu.com-20141003184523-4mt6dy1q3j8n30c9
Tags: 1:2014.2~rc1-0ubuntu1
* New upstream release candidate:
  - d/p/*: Refreshed.
  - d/control: Add python-requests-mock to BD's.
  - d/control: Align versioned requirements with upstream.
* Transition linuxbridge and openvswitch plugin users to modular
  layer 2 plugin (LP: #1323729):
  - d/control: Mark removed plugin packages as transitional, depend
    on neutron-plugin-ml2, mark oldlibs/extra.
  - d/neutron-plugin-{linuxbridge,openvswitch}.install: Drop.
  - d/control: Depend on neutron-plugin-ml2 for linuxbridge
    agent package.
  - d/neutron-plugin-linuxbridge-agent.upstart: Use ml2 plugin
    configuration files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014 Alcatel-Lucent USA Inc.
 
2
#
 
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
 
6
#
 
7
#         http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
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
 
13
#    under the License.
 
14
#
 
15
 
 
16
import contextlib
 
17
 
 
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
 
27
 
 
28
_uuid = uuidutils.generate_uuid
 
29
 
 
30
 
 
31
class TestL3Sync(test_nuage_plugin.NuagePluginV2TestCase,
 
32
                 test_l3_plugin.L3NatDBIntTestCase):
 
33
 
 
34
    def setUp(self):
 
35
        self.session = context.get_admin_context().session
 
36
        self.syncmanager = sync.SyncManager(
 
37
            test_nuage_plugin.getNuageClient())
 
38
        super(TestL3Sync, self).setUp()
 
39
 
 
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,
 
43
                               'port_id': port_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)
 
47
 
 
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
 
52
        # accordingly
 
53
        rtr_res = self._create_router('json', 'foo', 'test-router', True)
 
54
        router = self.deserialize('json', rtr_res)
 
55
 
 
56
        self.syncmanager.synchronize('250')
 
57
 
 
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()
 
62
 
 
63
        self.assertEqual('2d782c02-b88e-44ad-a79b-4bdf11f7df3d',
 
64
                         router_db['nuage_router_id'])
 
65
 
 
66
        self._delete('routers', router['router']['id'])
 
67
 
 
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])
 
72
 
 
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 (
 
77
                p1, p2, p3):
 
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):
 
85
                    fip_dict = {'fip': {
 
86
                        'add': [fip1['floatingip']['id']],
 
87
                        'associate': [fip2['floatingip']['id']],
 
88
                        'disassociate': [fip3['floatingip']['id']]
 
89
                    }}
 
90
                    self.syncmanager._sync_fips(fip_dict)
 
91
 
 
92
    def test_deleted_fip_sync(self):
 
93
        fip_dict = {'fip': {
 
94
            'add': [_uuid()],
 
95
            'associate': [_uuid()],
 
96
            'disassociate': [_uuid()]
 
97
        }}
 
98
        self.syncmanager._sync_fips(fip_dict)
 
99
 
 
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:
 
106
 
 
107
                    data = self.syncmanager._get_fip_data(
 
108
                        fip['floatingip']['id'])
 
109
 
 
110
                    self.assertEqual(fip['floatingip']['id'], data['id'])
 
111
 
 
112
                    data = self.syncmanager._get_ipalloc_for_fip(
 
113
                        fip['floatingip'])
 
114
                    self.assertEqual(fip['floatingip']['floating_ip_address'],
 
115
                                     data['ip_address'])
 
116
 
 
117
    def test_fip_and_ipalloc_deleted_get(self):
 
118
        data = self.syncmanager._get_fip_data(_uuid())
 
119
        self.assertIsNone(data)
 
120
 
 
121
        fip = {
 
122
            'id': _uuid(),
 
123
            'floating_network_id': _uuid(),
 
124
            'floating_ip_address': '176.176.10.10'
 
125
        }
 
126
        data = self.syncmanager._get_ipalloc_for_fip(fip)
 
127
        self.assertIsNone(data)
 
128
 
 
129
    def test_domainsubnet_sync(self):
 
130
        with self.subnet() as s1:
 
131
            with contextlib.nested(
 
132
                    self.router(),
 
133
                    self.port()) as (r1, p1):
 
134
                self._router_interface_action(
 
135
                    'add', r1['router']['id'],
 
136
                    s1['subnet']['id'], p1['port']['id'])
 
137
                domainsubn_dict = {
 
138
                    'domainsubnet': {'add': [s1['subnet']['id']]},
 
139
                    'port': {'sub_rtr_intf_port_dict': {s1['subnet']['id']:
 
140
                                                        p1['port']['id']}}}
 
141
                self.syncmanager.sync_domainsubnets(domainsubn_dict)
 
142
                self._router_interface_action('remove', r1['router']['id'],
 
143
                                              s1['subnet']['id'], None)
 
144
 
 
145
    def test_floatingip_update_different_router(self):
 
146
        self._test_floatingip_update_different_router()
 
147
 
 
148
    def test_floatingip_update_different_fixed_ip_same_port(self):
 
149
        self._test_floatingip_update_different_fixed_ip_same_port()
 
150
 
 
151
    def test_floatingip_create_different_fixed_ip_same_port(self):
 
152
        self._test_floatingip_create_different_fixed_ip_same_port()
 
153
 
 
154
    def test_network_update_external_failure(self):
 
155
        self._test_network_update_external_failure()
 
156
 
 
157
 
 
158
class TestExtraRouteSync(extraroute_test.ExtraRouteDBIntTestCase):
 
159
 
 
160
    def setUp(self):
 
161
        self.session = context.get_admin_context().session
 
162
        self.syncmanager = sync.SyncManager(
 
163
            test_nuage_plugin.getNuageClient())
 
164
        super(TestExtraRouteSync, self).setUp()
 
165
 
 
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])
 
175
 
 
176
                route_dict = {'route': {'add': [route]}}
 
177
                self.syncmanager.sync_routes(route_dict)
 
178
 
 
179
                self._routes_update_cleanup(p['port']['id'],
 
180
                                            None, r['router']['id'], [])
 
181
 
 
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)
 
191
 
 
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'], [])
 
197
 
 
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)
 
202
 
 
203
 
 
204
class TestNetPartSync(test_netpartition.NetPartitionTestCase):
 
205
 
 
206
    def setUp(self):
 
207
        self.session = context.get_admin_context().session
 
208
        self.syncmanager = sync.SyncManager(
 
209
            test_nuage_plugin.getNuageClient())
 
210
        super(TestNetPartSync, self).setUp()
 
211
 
 
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
 
216
        # accordingly
 
217
        netpart = self._make_netpartition('json', 'sync-new-netpartition')
 
218
 
 
219
        self.syncmanager.synchronize('250')
 
220
 
 
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'][
 
224
                'name']).first()
 
225
 
 
226
        self.assertEqual('a917924f-3139-4bdb-a4c3-ea7c8011582f',
 
227
                         netpart_db['id'])
 
228
        self._del_netpartition(netpart_db['id'])
 
229
 
 
230
    def test_net_partition_deleted_get(self):
 
231
        data = self.syncmanager._get_netpart_data(_uuid())
 
232
        self.assertIsNone(data)
 
233
 
 
234
 
 
235
class TestL2Sync(test_nuage_plugin.NuagePluginV2TestCase):
 
236
 
 
237
    def setUp(self):
 
238
        self.session = context.get_admin_context().session
 
239
        self.syncmanager = sync.SyncManager(
 
240
            test_nuage_plugin.getNuageClient())
 
241
        super(TestL2Sync, self).setUp()
 
242
 
 
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
 
247
        # accordingly
 
248
        net_res = self._create_network("json", "pub", True)
 
249
        network = self.deserialize('json', net_res)
 
250
 
 
251
        sub_res = self._create_subnet("json", network['network']['id'],
 
252
                                      '10.0.0.0/24')
 
253
        subnet = self.deserialize('json', sub_res)
 
254
 
 
255
        self.syncmanager.synchronize('250')
 
256
 
 
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'])
 
263
 
 
264
        self._delete('subnets', subnet['subnet']['id'])
 
265
        self._delete('networks', network['network']['id'])
 
266
 
 
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])
 
271
 
 
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'][
 
275
                                                        'id']]}}
 
276
            self.syncmanager.sync_sharednetworks(sharednet_dict)
 
277
 
 
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)
 
282
 
 
283
 
 
284
class TestSecurityGroupSync(test_sg.TestSecurityGroups):
 
285
 
 
286
    def setUp(self):
 
287
        self.session = context.get_admin_context().session
 
288
        self.syncmanager = sync.SyncManager(
 
289
            test_nuage_plugin.getNuageClient())
 
290
        super(TestSecurityGroupSync, self).setUp()
 
291
 
 
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'])
 
297
 
 
298
    def test_sg_deleted_get(self):
 
299
        data = self.syncmanager._get_sec_grp_data(_uuid())
 
300
        self.assertIsNone(data)
 
301
 
 
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'])
 
307
 
 
308
    def test_sg_rule_deleted_get(self):
 
309
        data = self.syncmanager._get_sec_grp_rule_data(_uuid())
 
310
        self.assertIsNone(data)
 
311
 
 
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)
 
320
 
 
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)
 
325
 
 
326
    def test_sg_rule_sync(self):
 
327
        with contextlib.nested(self.security_group(),
 
328
                               self.security_group()) as (sg1, sg2):
 
329
            sg1_rule_id = (
 
330
                sg1['security_group']['security_group_rules'][0]['id'])
 
331
            sg2_rule_id = (
 
332
                sg2['security_group']['security_group_rules'][0]['id'])
 
333
 
 
334
            sg_dict = {'security': {'secgrouprule': {'l2domain': {
 
335
                'add': [sg1_rule_id]}, 'domain': {'add': [sg2_rule_id]}}}}
 
336
            self.syncmanager.sync_secgrp_rules(sg_dict)
 
337
 
 
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)