Ejemplo n.º 1
0
    def _test_sync_state_helper(self, known_networks, active_networks):
        with mock.patch('quantum.agent.dhcp_agent.DhcpPluginApi') as plug:
            mock_plugin = mock.Mock()
            mock_plugin.get_active_networks.return_value = active_networks
            plug.return_value = mock_plugin

            dhcp = dhcp_agent.DhcpAgent(cfg.CONF)

            attrs_to_mock = dict([
                (a, mock.DEFAULT) for a in
                ['refresh_dhcp_helper', 'disable_dhcp_helper', 'cache']
            ])

            with mock.patch.multiple(dhcp, **attrs_to_mock) as mocks:
                mocks['cache'].get_network_ids.return_value = known_networks
                dhcp.sync_state()

                exp_refresh = [mock.call(net_id) for net_id in active_networks]

                diff = set(known_networks) - set(active_networks)
                exp_disable = [mock.call(net_id) for net_id in diff]

                mocks['cache'].assert_has_calls([mock.call.get_network_ids()])
                mocks['refresh_dhcp_helper'].assert_has_called(exp_refresh)
                mocks['disable_dhcp_helper'].assert_has_called(exp_disable)
Ejemplo n.º 2
0
 def test_update_lease(self):
     with mock.patch('quantum.agent.dhcp_agent.DhcpPluginApi') as plug:
         dhcp = dhcp_agent.DhcpAgent(cfg.CONF)
         dhcp.update_lease('net_id', '192.168.1.1', 120)
         plug.assert_has_calls([
             mock.call().update_lease_expiration('net_id', '192.168.1.1',
                                                 120)
         ])
Ejemplo n.º 3
0
 def test_call_driver(self):
     network = mock.Mock()
     network.id = '1'
     with mock.patch('quantum.agent.dhcp_agent.DeviceManager') as dev_mgr:
         dhcp = dhcp_agent.DhcpAgent(cfg.CONF)
         dhcp.call_driver('foo', network)
         dev_mgr.assert_called()
         self.driver.assert_called_once_with(cfg.CONF, mock.ANY, 'sudo',
                                             mock.ANY, 'qdhcp-1')
Ejemplo n.º 4
0
 def test_periodoc_resync_helper(self):
     with mock.patch.object(dhcp_agent.eventlet, 'sleep') as sleep:
         dhcp = dhcp_agent.DhcpAgent(cfg.CONF)
         dhcp.needs_resync = True
         with mock.patch.object(dhcp, 'sync_state') as sync_state:
             sync_state.side_effect = RuntimeError
             with self.assertRaises(RuntimeError):
                 dhcp._periodic_resync_helper()
             sync_state.assert_called_once_with()
             sleep.assert_called_once_with(dhcp.conf.resync_interval)
             self.assertFalse(dhcp.needs_resync)
Ejemplo n.º 5
0
    def test_sync_state_plugin_error(self):
        with mock.patch('quantum.agent.dhcp_agent.DhcpPluginApi') as plug:
            mock_plugin = mock.Mock()
            mock_plugin.get_active_networks.side_effect = Exception
            plug.return_value = mock_plugin

            with mock.patch.object(dhcp_agent.LOG, 'exception') as log:
                dhcp = dhcp_agent.DhcpAgent(cfg.CONF)
                dhcp.sync_state()

                self.assertTrue(log.called)
                self.assertTrue(dhcp.needs_resync)
Ejemplo n.º 6
0
 def test_call_driver_failure(self):
     network = mock.Mock()
     network.id = '1'
     self.driver.return_value.foo.side_effect = Exception
     with mock.patch('quantum.agent.dhcp_agent.DeviceManager') as dev_mgr:
         with mock.patch.object(dhcp_agent.LOG, 'exception') as log:
             dhcp = dhcp_agent.DhcpAgent(cfg.CONF)
             self.assertIsNone(dhcp.call_driver('foo', network))
             self.assertTrue(dev_mgr.called)
             self.driver.assert_called_once_with(cfg.CONF, mock.ANY, 'sudo',
                                                 mock.ANY, 'qdhcp-1')
             self.assertEqual(log.call_count, 1)
             self.assertTrue(dhcp.needs_resync)
Ejemplo n.º 7
0
 def test_run_completes_single_pass(self):
     with mock.patch('quantum.agent.dhcp_agent.DeviceManager') as dev_mgr:
         dhcp = dhcp_agent.DhcpAgent(cfg.CONF)
         attrs_to_mock = dict([
             (a, mock.DEFAULT)
             for a in ['sync_state', 'lease_relay', 'periodic_resync']
         ])
         with mock.patch.multiple(dhcp, **attrs_to_mock) as mocks:
             dhcp.run()
             mocks['sync_state'].assert_called_once_with()
             mocks['periodic_resync'].assert_called_once_with()
             mocks['lease_relay'].assert_has_mock_calls([mock.call.start()])
             self.notification.assert_has_calls([mock.call.run_dispatch()])
Ejemplo n.º 8
0
    def test_update_lease_failure(self):
        with mock.patch('quantum.agent.dhcp_agent.DhcpPluginApi') as plug:
            plug.return_value.update_lease_expiration.side_effect = Exception

            with mock.patch.object(dhcp_agent.LOG, 'exception') as log:
                dhcp = dhcp_agent.DhcpAgent(cfg.CONF)
                dhcp.update_lease('net_id', '192.168.1.1', 120)
                plug.assert_has_calls([
                    mock.call().update_lease_expiration(
                        'net_id', '192.168.1.1', 120)
                ])

                self.assertTrue(log.called)
                self.assertTrue(dhcp.needs_resync)
Ejemplo n.º 9
0
    def test_run_completes_single_pass(self):
        with mock.patch('quantum.agent.dhcp_agent.DeviceManager') as dev_mgr:
            with mock.patch('quantum.agent.dhcp_agent.DhcpPluginApi') as plug:
                mock_plugin = mock.Mock()
                mock_plugin.get_active_networks.return_value = ['a']
                plug.return_value = mock_plugin

                dhcp = dhcp_agent.DhcpAgent(cfg.CONF)
                with mock.patch.object(dhcp, 'enable_dhcp_helper') as enable:
                    with mock.patch.object(dhcp, 'lease_relay') as relay:
                        dhcp.run()
                        enable.assert_called_once_with('a')
                        plug.assert_called_once_with('q-plugin', mock.ANY)
                        mock_plugin.assert_has_calls(
                            [mock.call.get_active_networks()])
                        relay.assert_has_mock_calls([mock.call.run()])

        self.notification.assert_has_calls([mock.call.run_dispatch()])
Ejemplo n.º 10
0
    def setUp(self):
        cfg.CONF.register_opts(dhcp_agent.DeviceManager.OPTS)
        cfg.CONF.set_override('interface_driver',
                              'quantum.agent.linux.interface.NullDriver')
        cfg.CONF.root_helper = 'sudo'
        cfg.CONF.register_opts(dhcp_agent.DhcpAgent.OPTS)
        self.notification_p = mock.patch(
            'quantum.agent.rpc.NotificationDispatcher')
        self.notification = self.notification_p.start()

        self.plugin_p = mock.patch('quantum.agent.dhcp_agent.DhcpPluginApi')
        plugin_cls = self.plugin_p.start()
        self.plugin = mock.Mock()
        plugin_cls.return_value = self.plugin

        self.cache_p = mock.patch('quantum.agent.dhcp_agent.NetworkCache')
        cache_cls = self.cache_p.start()
        self.cache = mock.Mock()
        cache_cls.return_value = self.cache

        self.dhcp = dhcp_agent.DhcpAgent(cfg.CONF)
        self.call_driver_p = mock.patch.object(self.dhcp, 'call_driver')

        self.call_driver = self.call_driver_p.start()
Ejemplo n.º 11
0
 def test_periodic_resync(self):
     dhcp = dhcp_agent.DhcpAgent(cfg.CONF)
     with mock.patch.object(dhcp_agent.eventlet, 'spawn') as spawn:
         dhcp.periodic_resync()
         spawn.assert_called_once_with(dhcp._periodic_resync_helper)