~ubuntu-branches/ubuntu/vivid/neutron/vivid-updates

« back to all changes in this revision

Viewing changes to neutron/tests/unit/test_metadata_agent.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2015-03-30 11:17:19 UTC
  • mfrom: (1.1.21)
  • Revision ID: package-import@ubuntu.com-20150330111719-h0gx7233p4jkkgfh
Tags: 1:2015.1~b3-0ubuntu1
* New upstream milestone release:
  - d/control: Align version requirements with upstream.
  - d/control: Add new dependency on oslo-log.
  - d/p/*: Rebase.
  - d/control,d/neutron-plugin-hyperv*: Dropped, decomposed into
    separate project upstream.
  - d/control,d/neutron-plugin-openflow*: Dropped, decomposed into
    separate project upstream.
  - d/neutron-common.install: Add neutron-rootwrap-daemon and 
    neutron-keepalived-state-change binaries.
  - d/rules: Ignore neutron-hyperv-agent when installing; only for Windows.
  - d/neutron-plugin-cisco.install: Drop neutron-cisco-cfg-agent as
    decomposed into separate project upstream.
  - d/neutron-plugin-vmware.install: Drop neutron-check-nsx-config and
    neutron-nsx-manage as decomposed into separate project upstream.
  - d/control: Add dependency on python-neutron-fwaas to neutron-l3-agent.
* d/pydist-overrides: Add overrides for oslo packages.
* d/control: Fixup type in package description (LP: #1263539).
* d/p/fixup-driver-test-execution.patch: Cherry pick fix from upstream VCS
  to support unit test exection in out-of-tree vendor drivers.
* d/neutron-common.postinst: Allow general access to /etc/neutron but limit
  access to root/neutron to /etc/neutron/neutron.conf to support execution
  of unit tests in decomposed vendor drivers.
* d/control: Add dependency on python-neutron-fwaas to neutron-l3-agent
  package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
#    under the License.
14
14
 
15
15
import contextlib
16
 
import socket
17
16
 
18
17
import mock
19
18
import testtools
20
19
import webob
21
20
 
 
21
from neutron.agent.linux import utils as agent_utils
22
22
from neutron.agent.metadata import agent
23
23
from neutron.agent import metadata_agent
24
24
from neutron.common import constants
163
163
                 'not_used': [1, 2, 3]}
164
164
        expected_networks = ('network_id1',)
165
165
        with mock.patch(
166
 
            'oslo.utils.timeutils.utcnow_ts', return_value=0):
 
166
            'oslo_utils.timeutils.utcnow_ts', return_value=0):
167
167
            mock_list_ports = self.qclient.return_value.list_ports
168
168
            mock_list_ports.return_value = ports
169
169
            networks = self.handler._get_router_networks(router_id)
510
510
            2, self.qclient.return_value.list_ports.call_count)
511
511
 
512
512
 
513
 
class TestUnixDomainHttpProtocol(base.BaseTestCase):
514
 
    def test_init_empty_client(self):
515
 
        u = agent.UnixDomainHttpProtocol(mock.Mock(), '', mock.Mock())
516
 
        self.assertEqual(u.client_address, ('<local>', 0))
517
 
 
518
 
    def test_init_with_client(self):
519
 
        u = agent.UnixDomainHttpProtocol(mock.Mock(), 'foo', mock.Mock())
520
 
        self.assertEqual(u.client_address, 'foo')
521
 
 
522
 
 
523
 
class TestUnixDomainWSGIServer(base.BaseTestCase):
524
 
    def setUp(self):
525
 
        super(TestUnixDomainWSGIServer, self).setUp()
526
 
        self.eventlet_p = mock.patch.object(agent, 'eventlet')
527
 
        self.eventlet = self.eventlet_p.start()
528
 
        self.server = agent.UnixDomainWSGIServer('test')
529
 
 
530
 
    def test_start(self):
531
 
        mock_app = mock.Mock()
532
 
        with mock.patch.object(self.server, '_launch') as launcher:
533
 
            self.server.start(mock_app, '/the/path', workers=5, backlog=128)
534
 
            self.eventlet.assert_has_calls([
535
 
                mock.call.listen(
536
 
                    '/the/path',
537
 
                    family=socket.AF_UNIX,
538
 
                    backlog=128
539
 
                )]
540
 
            )
541
 
            launcher.assert_called_once_with(mock_app, workers=5)
542
 
 
543
 
    def test_run(self):
544
 
        with mock.patch.object(agent, 'logging') as logging:
545
 
            self.server._run('app', 'sock')
546
 
 
547
 
            self.eventlet.wsgi.server.assert_called_once_with(
548
 
                'sock',
549
 
                'app',
550
 
                protocol=agent.UnixDomainHttpProtocol,
551
 
                log=mock.ANY,
552
 
                custom_pool=self.server.pool
553
 
            )
554
 
            self.assertTrue(len(logging.mock_calls))
555
 
 
556
 
 
557
513
class TestUnixDomainMetadataProxy(base.BaseTestCase):
558
514
    def setUp(self):
559
515
        super(TestUnixDomainMetadataProxy, self).setUp()
566
522
        self.cfg.CONF.metadata_workers = 0
567
523
        self.cfg.CONF.metadata_backlog = 128
568
524
 
569
 
    def test_init_doesnot_exists(self):
570
 
        with mock.patch('os.path.isdir') as isdir:
571
 
            with mock.patch('os.makedirs') as makedirs:
572
 
                isdir.return_value = False
573
 
                agent.UnixDomainMetadataProxy(mock.Mock())
574
 
 
575
 
                isdir.assert_called_once_with('/the')
576
 
                makedirs.assert_called_once_with('/the', 0o755)
 
525
    @mock.patch.object(agent_utils, 'ensure_dir')
 
526
    def test_init_doesnot_exists(self, ensure_dir):
 
527
        agent.UnixDomainMetadataProxy(mock.Mock())
 
528
        ensure_dir.assert_called_once_with('/the')
577
529
 
578
530
    def test_init_exists(self):
579
531
        with mock.patch('os.path.isdir') as isdir:
580
532
            with mock.patch('os.unlink') as unlink:
581
533
                isdir.return_value = True
582
534
                agent.UnixDomainMetadataProxy(mock.Mock())
583
 
 
584
 
                isdir.assert_called_once_with('/the')
585
535
                unlink.assert_called_once_with('/the/path')
586
536
 
587
537
    def test_init_exists_unlink_no_file(self):
593
543
                    unlink.side_effect = OSError
594
544
 
595
545
                    agent.UnixDomainMetadataProxy(mock.Mock())
596
 
 
597
 
                    isdir.assert_called_once_with('/the')
598
546
                    unlink.assert_called_once_with('/the/path')
599
 
                    exists.assert_called_once_with('/the/path')
600
547
 
601
548
    def test_init_exists_unlink_fails_file_still_exists(self):
602
549
        with mock.patch('os.path.isdir') as isdir:
608
555
 
609
556
                    with testtools.ExpectedException(OSError):
610
557
                        agent.UnixDomainMetadataProxy(mock.Mock())
611
 
 
612
 
                    isdir.assert_called_once_with('/the')
613
558
                    unlink.assert_called_once_with('/the/path')
614
 
                    exists.assert_called_once_with('/the/path')
615
 
 
616
 
    def test_run(self):
617
 
        with mock.patch.object(agent, 'MetadataProxyHandler') as handler:
618
 
            with mock.patch.object(agent, 'UnixDomainWSGIServer') as server:
619
 
                with mock.patch('os.path.isdir') as isdir:
620
 
                    with mock.patch('os.makedirs') as makedirs:
621
 
                        isdir.return_value = False
622
 
 
623
 
                        p = agent.UnixDomainMetadataProxy(self.cfg.CONF)
624
 
                        p.run()
625
 
 
626
 
                        isdir.assert_called_once_with('/the')
627
 
                        makedirs.assert_called_once_with('/the', 0o755)
628
 
                        server.assert_has_calls([
629
 
                            mock.call('neutron-metadata-agent'),
630
 
                            mock.call().start(handler.return_value,
631
 
                                              '/the/path', workers=0,
632
 
                                              backlog=128),
633
 
                            mock.call().wait()]
634
 
                        )
 
559
 
 
560
    @mock.patch.object(agent, 'MetadataProxyHandler')
 
561
    @mock.patch.object(agent_utils, 'UnixDomainWSGIServer')
 
562
    @mock.patch.object(agent_utils, 'ensure_dir')
 
563
    def test_run(self, ensure_dir, server, handler):
 
564
        p = agent.UnixDomainMetadataProxy(self.cfg.CONF)
 
565
        p.run()
 
566
 
 
567
        ensure_dir.assert_called_once_with('/the')
 
568
        server.assert_has_calls([
 
569
            mock.call('neutron-metadata-agent'),
 
570
            mock.call().start(handler.return_value,
 
571
                              '/the/path', workers=0,
 
572
                              backlog=128),
 
573
            mock.call().wait()]
 
574
        )
635
575
 
636
576
    def test_main(self):
637
577
        with mock.patch.object(agent, 'UnixDomainMetadataProxy') as proxy: