def test_destroy_namespace_exception(self):
     ns = 'qrouter-6e322ac7-ab50-4f53-9cdc-d1d3c1164b6d'
     conf = mock.Mock()
     conf.AGENT.root_helper = 'sudo'
     with mock.patch('quantum.agent.linux.ip_lib.IPWrapper') as ip_wrap:
         ip_wrap.side_effect = Exception()
         util.destroy_namespace(conf, ns)
    def _test_destroy_namespace_helper(self, force, num_devices):
        ns = "qrouter-6e322ac7-ab50-4f53-9cdc-d1d3c1164b6d"
        conf = mock.Mock()
        #        conf.AGENT.root_helper = 'sudo'

        lo_device = mock.Mock()
        lo_device.name = "lo"

        devices = [lo_device]

        while num_devices:
            dev = mock.Mock()
            dev.name = "tap%d" % num_devices
            devices.append(dev)
            num_devices -= 1

        with mock.patch("quantum.agent.linux.ip_lib.IPWrapper") as ip_wrap:
            ip_wrap.return_value.get_devices.return_value = devices
            ip_wrap.return_value.netns.exists.return_value = True

            with mock.patch.object(util, "unplug_device") as unplug:

                with mock.patch.object(util, "kill_dhcp") as kill_dhcp:
                    util.destroy_namespace(conf, ns, force)
                    expected = [mock.call(conf.AGENT.root_helper, ns)]

                    if force:
                        expected.extend([mock.call().netns.exists(ns), mock.call().get_devices(exclude_loopback=True)])
                        self.assertTrue(kill_dhcp.called)
                        unplug.assert_has_calls([mock.call(conf, d) for d in devices[1:]])

                    expected.append(mock.call().garbage_collect_namespace())
                    ip_wrap.assert_has_calls(expected)
 def test_destroy_namespace_exception(self):
     ns = 'qrouter-6e322ac7-ab50-4f53-9cdc-d1d3c1164b6d'
     conf = mock.Mock()
     conf.root_helper = 'sudo'
     with mock.patch('quantum.agent.linux.ip_lib.IPWrapper') as ip_wrap:
         ip_wrap.side_effect = Exception()
         util.destroy_namespace(conf, ns)
    def _test_destroy_namespace_helper(self, force, num_devices):
        ns = 'qrouter-6e322ac7-ab50-4f53-9cdc-d1d3c1164b6d'
        conf = mock.Mock()
        conf.root_helper = 'sudo'

        lo_device = mock.Mock()
        lo_device.name = 'lo'

        devices = [lo_device]

        while num_devices:
            dev = mock.Mock()
            dev.name = 'tap%d' % num_devices
            devices.append(dev)
            num_devices -= 1

        with mock.patch('quantum.agent.linux.ip_lib.IPWrapper') as ip_wrap:
            ip_wrap.return_value.get_devices.return_value = devices
            ip_wrap.return_value.netns.exists.return_value = True

            with mock.patch.object(util, 'unplug_device') as unplug:

                with mock.patch.object(util, 'kill_dhcp') as kill_dhcp:
                    util.destroy_namespace(conf, ns, force)
                    expected = [mock.call('sudo', ns)]

                    if force:
                        expected.extend([
                            mock.call().netns.exists(ns),
                            mock.call().get_devices(exclude_loopback=True)
                        ])
                        self.assertTrue(kill_dhcp.called)
                        unplug.assert_has_calls(
                            [mock.call(conf, d) for d in devices[1:]])

                    expected.append(mock.call().garbage_collect_namespace())
                    ip_wrap.assert_has_calls(expected)