~ubuntu-branches/ubuntu/vivid/ironic/vivid-updates

« back to all changes in this revision

Viewing changes to ironic/tests/dhcp/test_neutron.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2015-03-30 11:14:57 UTC
  • mfrom: (1.2.6)
  • Revision ID: package-import@ubuntu.com-20150330111457-kr4ju3guf22m4vbz
Tags: 2015.1~b3-0ubuntu1
* New upstream release.
  + d/control: 
    - Align with upstream dependencies.
    - Add dh-python to build-dependencies.
    - Add psmisc as a dependency. (LP: #1358820)
  + d/p/fix-requirements.patch: Rediffed.
  + d/ironic-conductor.init.in: Fixed typos in LSB headers,
    thanks to JJ Asghar. (LP: #1429962)

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
import mock
18
18
from neutronclient.common import exceptions as neutron_client_exc
19
19
from neutronclient.v2_0 import client
 
20
from oslo_utils import uuidutils
20
21
 
21
22
from ironic.common import dhcp_factory
22
23
from ironic.common import exception
23
24
from ironic.common import pxe_utils
24
 
from ironic.common import utils
25
25
from ironic.conductor import task_manager
26
26
from ironic.dhcp import neutron
27
27
from ironic.tests.conductor import utils as mgr_utils
34
34
    def setUp(self):
35
35
        super(TestNeutron, self).setUp()
36
36
        mgr_utils.mock_the_extension_manager(driver='fake')
 
37
        self.config(
 
38
            cleaning_network_uuid='00000000-0000-0000-0000-000000000000',
 
39
            group='neutron')
37
40
        self.config(enabled_drivers=['fake'])
38
41
        self.config(dhcp_provider='neutron',
39
42
                    group='dhcp')
49
52
                    auth_uri='test-auth-uri',
50
53
                    group='keystone_authtoken')
51
54
        self.node = object_utils.create_test_node(self.context)
 
55
        self.ports = [
 
56
            object_utils.create_test_port(
 
57
                self.context, node_id=self.node.id, id=2,
 
58
                uuid='1be26c0b-03f2-4d2e-ae87-c02d7f33c782',
 
59
                address='52:54:00:cf:2d:32')]
 
60
        # Very simple neutron port representation
 
61
        self.neutron_port = {'id': '132f871f-eaec-4fed-9475-0d54465e0f00',
 
62
                             'mac_address': '52:54:00:cf:2d:32'}
 
63
 
52
64
        dhcp_factory.DHCPFactory._dhcp_provider = None
53
65
 
54
66
    def test__build_client_invalid_auth_strategy(self):
301
313
        port = object_utils.create_test_port(self.context,
302
314
                                             node_id=self.node.id,
303
315
                                             address='aa:bb:cc',
304
 
                                             uuid=utils.generate_uuid(),
 
316
                                             uuid=uuidutils.generate_uuid(),
305
317
                                             extra={'vif_port_id':
306
318
                                                    'test-vif-A'},
307
319
                                             driver='fake')
323
335
        port = object_utils.create_test_port(self.context,
324
336
                                             node_id=self.node.id,
325
337
                                             address='aa:bb:cc',
326
 
                                             uuid=utils.generate_uuid(),
 
338
                                             uuid=uuidutils.generate_uuid(),
327
339
                                             extra={'vif_port_id':
328
340
                                                    'test-vif-A'},
329
341
                                             driver='fake')
340
352
    @mock.patch('ironic.dhcp.neutron.NeutronDHCPApi._get_port_ip_address')
341
353
    def test_get_ip_addresses(self, get_ip_mock):
342
354
        ip_address = '10.10.0.1'
343
 
        address = "aa:aa:aa:aa:aa:aa"
344
355
        expected = [ip_address]
345
 
        port = object_utils.create_test_port(self.context,
346
 
                                             node_id=self.node.id,
347
 
                                             address=address)
348
356
 
349
357
        get_ip_mock.return_value = ip_address
350
358
 
351
359
        with task_manager.acquire(self.context, self.node.uuid) as task:
352
360
            api = dhcp_factory.DHCPFactory().provider
353
361
            result = api.get_ip_addresses(task)
354
 
            get_ip_mock.assert_called_once_with(task, port.uuid, mock.ANY)
 
362
            get_ip_mock.assert_called_once_with(task, self.ports[0].uuid,
 
363
                                                mock.ANY)
355
364
        self.assertEqual(expected, result)
 
365
 
 
366
    @mock.patch.object(client.Client, 'create_port')
 
367
    def test_create_cleaning_ports(self, create_mock):
 
368
        # Ensure we can create cleaning ports for in band cleaning
 
369
        create_mock.return_value = {'port': self.neutron_port}
 
370
        expected = {self.ports[0].uuid: self.neutron_port['id']}
 
371
        api = dhcp_factory.DHCPFactory().provider
 
372
 
 
373
        with task_manager.acquire(self.context, self.node.uuid) as task:
 
374
            ports = api.create_cleaning_ports(task)
 
375
            self.assertEqual(expected, ports)
 
376
            create_mock.assert_called_once_with({'port': {
 
377
                'network_id': '00000000-0000-0000-0000-000000000000',
 
378
                'admin_state_up': True, 'mac_address': self.ports[0].address}})
 
379
 
 
380
    @mock.patch('ironic.conductor.manager.cleaning_error_handler')
 
381
    @mock.patch.object(client.Client, 'create_port')
 
382
    def test_create_cleaning_ports_fail(self, create_mock, error_mock):
 
383
        # Check that if creating a port fails, the node goes to cleanfail
 
384
        create_mock.side_effect = neutron_client_exc.ConnectionFailed
 
385
        api = dhcp_factory.DHCPFactory().provider
 
386
 
 
387
        with task_manager.acquire(self.context, self.node.uuid) as task:
 
388
            api.create_cleaning_ports(task)
 
389
            error_mock.assert_called_once_with(task, mock.ANY)
 
390
            create_mock.assert_called_once_with({'port': {
 
391
                'network_id': '00000000-0000-0000-0000-000000000000',
 
392
                'admin_state_up': True, 'mac_address': self.ports[0].address}})
 
393
 
 
394
    @mock.patch('ironic.conductor.manager.cleaning_error_handler')
 
395
    @mock.patch.object(client.Client, 'create_port')
 
396
    def test_create_cleaning_ports_bad_config(self, create_mock, error_mock):
 
397
        self.config(cleaning_network_uuid=None, group='neutron')
 
398
        api = dhcp_factory.DHCPFactory().provider
 
399
 
 
400
        with task_manager.acquire(self.context, self.node.uuid) as task:
 
401
            self.assertRaises(exception.InvalidParameterValue,
 
402
                              api.create_cleaning_ports, task)
 
403
 
 
404
    @mock.patch.object(client.Client, 'delete_port')
 
405
    @mock.patch.object(client.Client, 'list_ports')
 
406
    def test_delete_cleaning_ports(self, list_mock, delete_mock):
 
407
        # Ensure that we can delete cleaning ports, and that ports with
 
408
        # different macs don't get deleted
 
409
        other_port = {'id': '132f871f-eaec-4fed-9475-0d54465e0f01',
 
410
                      'mac_address': 'aa:bb:cc:dd:ee:ff'}
 
411
        list_mock.return_value = {'ports': [self.neutron_port, other_port]}
 
412
        api = dhcp_factory.DHCPFactory().provider
 
413
 
 
414
        with task_manager.acquire(self.context, self.node.uuid) as task:
 
415
            api.delete_cleaning_ports(task)
 
416
            list_mock.assert_called_once_with(
 
417
                network_id='00000000-0000-0000-0000-000000000000')
 
418
            delete_mock.assert_called_once_with(self.neutron_port['id'])
 
419
 
 
420
    @mock.patch('ironic.conductor.manager.cleaning_error_handler')
 
421
    @mock.patch.object(client.Client, 'list_ports')
 
422
    def test_delete_cleaning_ports_list_fail(self, list_mock, error_mock):
 
423
        # Check that if listing ports fails, the node goes to cleanfail
 
424
        list_mock.side_effect = neutron_client_exc.ConnectionFailed
 
425
        api = dhcp_factory.DHCPFactory().provider
 
426
 
 
427
        with task_manager.acquire(self.context, self.node.uuid) as task:
 
428
            api.delete_cleaning_ports(task)
 
429
            list_mock.assert_called_once_with(
 
430
                network_id='00000000-0000-0000-0000-000000000000')
 
431
            error_mock.assert_called_once_with(task, mock.ANY)
 
432
 
 
433
    @mock.patch('ironic.conductor.manager.cleaning_error_handler')
 
434
    @mock.patch.object(client.Client, 'delete_port')
 
435
    @mock.patch.object(client.Client, 'list_ports')
 
436
    def test_delete_cleaning_ports_delete_fail(self, list_mock, delete_mock,
 
437
                                               error_mock):
 
438
        # Check that if deleting ports fails, the node goes to cleanfail
 
439
        list_mock.return_value = {'ports': [self.neutron_port]}
 
440
        delete_mock.side_effect = neutron_client_exc.ConnectionFailed
 
441
        api = dhcp_factory.DHCPFactory().provider
 
442
 
 
443
        with task_manager.acquire(self.context, self.node.uuid) as task:
 
444
            api.delete_cleaning_ports(task)
 
445
            list_mock.assert_called_once_with(
 
446
                network_id='00000000-0000-0000-0000-000000000000')
 
447
            delete_mock.assert_called_once_with(self.neutron_port['id'])
 
448
            error_mock.assert_called_once_with(task, mock.ANY)