Пример #1
0
class TestCleanupManager(BaseTestCase):
    def setUp(self):
        super(TestCleanupManager, self).setUp()

        self.config = load_config("felix_default.cfg",
                                  host_dict={"StartupCleanupDelay": 12})

        # We need to check the order between the iptables and ipsets cleanup
        # calls so make sure they have a common root mock.
        self.m_root_mock = mock.Mock()
        self.m_ipt_updr = self.m_root_mock.m_ipt_updr
        self.m_ips_mgr = self.m_root_mock.m_ips_mgr

        self.mgr = CleanupManager(self.config,
                                  [self.m_ipt_updr],
                                  [self.m_ips_mgr])

    def test_on_datamodel_in_sync(self):
        with mock.patch("gevent.spawn_later", autospec=True) as m_spawn_later:
            self.mgr.on_datamodel_in_sync(async=True)
            self.step_actor(self.mgr)
        self.assertTrue(self.mgr._cleanup_done)
        # Check we got only the expected call to spawn.
        self.assertEqual(m_spawn_later.mock_calls, [mock.call(12, mock.ANY)])
        # Grab the callable.
        do_cleanup = m_spawn_later.call_args[0][1]
        self.assertTrue(callable(do_cleanup))
        # Check it really invokes the cleanup.
        do_cleanup()
        self.step_actor(self.mgr)
        self.assertEqual(
            self.m_root_mock.mock_calls,
            [
                # iptables call should come first.
                mock.call.m_ipt_updr.cleanup(async=False),
                mock.call.m_ips_mgr.cleanup(async=False),
            ]
        )
        # Finally, check that subsequent in-sync calls are ignored.
        with mock.patch("gevent.spawn_later", autospec=True) as m_spawn_later:
            self.mgr.on_datamodel_in_sync(async=True)
            self.step_actor(self.mgr)
        self.assertEqual(m_spawn_later.mock_calls, [])

    def test_cleanup_failure(self):
        self.m_ips_mgr.cleanup.side_effect = RuntimeError
        with mock.patch("os._exit") as m_exit:
            result = self.mgr._do_cleanup(async=True)
            self.step_actor(self.mgr)
        self.assertEqual(m_exit.mock_calls, [mock.call(1)])
        self.assertRaises(RuntimeError, result.get)