Example #1
0
class TestEtcdAPI(BaseTestCase):
    def setUp(self):
        super(TestEtcdAPI, self).setUp()
        self.m_config = Mock(spec=Config)
        self.m_config.ETCD_ADDRS = [ETCD_ADDRESS]
        self.m_config.ETCD_SCHEME = "http"
        self.m_config.ETCD_KEY_FILE = None
        self.m_config.ETCD_CERT_FILE = None
        self.m_config.ETCD_CA_FILE = None
        self.m_config.USAGE_REPORT = False
        self.m_hosts_ipset = Mock(spec=IpsetActor)
        with patch("calico.felix.fetcd._FelixEtcdWatcher",
                   autospec=True) as m_etcd_watcher:
            with patch("gevent.spawn", autospec=True) as m_spawn:
                self.api = EtcdAPI(self.m_config, self.m_hosts_ipset)
        self.m_spawn = m_spawn
        self.m_etcd_watcher = m_etcd_watcher.return_value
        self.m_etcd_watcher.load_config = Mock(spec=Event)
        self.m_etcd_watcher.begin_polling = Mock(spec=Event)
        self.m_etcd_watcher.configured = Mock(spec=Event)

    def test_create(self):
        self.m_etcd_watcher.assert_has_calls([
            call.link(self.api._on_worker_died),
        ])
        self.assertFalse(self.m_spawn.called)

    def test_on_start(self):
        with patch.object(self.api._resync_greenlet, "start") as m_resync_st, \
                patch.object(self.api._status_reporting_greenlet, "start") as m_stat_start, \
                patch.object(self.api.status_reporter, "start") as m_sr_start:
            self.api._on_actor_started()
        m_resync_st.assert_called_once_with()
        m_stat_start.assert_called_once_with()
        m_sr_start.assert_called_once_with()
        self.m_etcd_watcher.start.assert_called_once_with()

    @patch("gevent.sleep", autospec=True)
    def test_periodic_resync_mainline(self, m_sleep):
        self.m_config.RESYNC_INTERVAL = 10
        m_configured = Mock(spec=Event)
        self.m_etcd_watcher.configured = m_configured
        with patch.object(self.api, "force_resync") as m_force_resync:
            m_force_resync.side_effect = ExpectedException()
            self.assertRaises(ExpectedException,
                              self.api._periodically_resync)
        m_configured.wait.assert_called_once_with()
        m_sleep.assert_called_once_with(ANY)
        sleep_time = m_sleep.call_args[0][0]
        self.assertTrue(sleep_time >= 10)
        self.assertTrue(sleep_time <= 12)

    @patch("gevent.sleep", autospec=True)
    def test_periodic_resync_disabled(self, m_sleep):
        self.m_config.RESYNC_INTERVAL = 0
        self.m_etcd_watcher.configured = Mock(spec=Event)
        with patch.object(self.api, "force_resync") as m_force_resync:
            m_force_resync.side_effect = Exception()
            self.api._periodically_resync()

    def test_force_resync(self):
        self.m_config.REPORT_ENDPOINT_STATUS = True
        with patch.object(self.api, "status_reporter") as m_status_rep:
            self.api.force_resync(async=True)
            self.step_actor(self.api)
        m_status_rep.resync.assert_called_once_with(async=True)
        self.assertTrue(self.m_etcd_watcher.resync_requested)

    def test_load_config(self):
        result = self.api.load_config(async=True)
        self.step_actor(self.api)
        conf = result.get()
        self.assertEqual(conf, self.m_etcd_watcher.configured)
        self.m_etcd_watcher.load_config.set.assert_called_once_with()

    def test_start_watch(self):
        m_splitter = Mock()
        self.api.load_config(async=True)
        result = self.api.start_watch(m_splitter, async=True)
        self.step_actor(self.api)
        self.m_etcd_watcher.load_config.set.assert_called_once_with()
        self.assertEqual(self.m_etcd_watcher.splitter, m_splitter)
        self.m_etcd_watcher.begin_polling.set.assert_called_once_with()

    @patch("sys.exit", autospec=True)
    def test_on_worker_died(self, m_exit):
        glet = gevent.spawn(lambda: None)
        glet.link(self.api._on_worker_died)
        glet.join(1)
        m_exit.assert_called_once_with(1)
Example #2
0
class TestEtcdAPI(BaseTestCase):
    def setUp(self):
        super(TestEtcdAPI, self).setUp()
        self.m_config = Mock(spec=Config)
        self.m_config.ETCD_ADDRS = [ETCD_ADDRESS]
        self.m_config.ETCD_SCHEME = "http"
        self.m_config.ETCD_KEY_FILE = None
        self.m_config.ETCD_CERT_FILE = None
        self.m_config.ETCD_CA_FILE = None
        self.m_hosts_ipset = Mock(spec=IpsetActor)
        with patch("calico.felix.fetcd._FelixEtcdWatcher",
                   autospec=True) as m_etcd_watcher:
            with patch("gevent.spawn", autospec=True) as m_spawn:
                self.api = EtcdAPI(self.m_config, self.m_hosts_ipset)
        self.m_spawn = m_spawn
        self.m_etcd_watcher = m_etcd_watcher.return_value
        self.m_etcd_watcher.load_config = Mock(spec=Event)
        self.m_etcd_watcher.begin_polling = Mock(spec=Event)
        self.m_etcd_watcher.configured = Mock(spec=Event)

    def test_create(self):
        self.m_etcd_watcher.assert_has_calls([
            call.link(self.api._on_worker_died),
        ])
        self.assertFalse(self.m_spawn.called)

    def test_on_start(self):
        with patch.object(self.api._resync_greenlet, "start") as m_resync_st, \
                patch.object(self.api._status_reporting_greenlet, "start") as m_stat_start, \
                patch.object(self.api.status_reporter, "start") as m_sr_start:
            self.api._on_actor_started()
        m_resync_st.assert_called_once_with()
        m_stat_start.assert_called_once_with()
        m_sr_start.assert_called_once_with()
        self.m_etcd_watcher.start.assert_called_once_with()

    @patch("gevent.sleep", autospec=True)
    def test_periodic_resync_mainline(self, m_sleep):
        self.m_config.RESYNC_INTERVAL = 10
        m_configured = Mock(spec=Event)
        self.m_etcd_watcher.configured = m_configured
        with patch.object(self.api, "force_resync") as m_force_resync:
            m_force_resync.side_effect = ExpectedException()
            self.assertRaises(ExpectedException,
                              self.api._periodically_resync)
        m_configured.wait.assert_called_once_with()
        m_sleep.assert_called_once_with(ANY)
        sleep_time = m_sleep.call_args[0][0]
        self.assertTrue(sleep_time >= 10)
        self.assertTrue(sleep_time <= 12)

    @patch("gevent.sleep", autospec=True)
    def test_periodic_resync_disabled(self, m_sleep):
        self.m_config.RESYNC_INTERVAL = 0
        self.m_etcd_watcher.configured = Mock(spec=Event)
        with patch.object(self.api, "force_resync") as m_force_resync:
            m_force_resync.side_effect = Exception()
            self.api._periodically_resync()

    def test_force_resync(self):
        self.m_config.REPORT_ENDPOINT_STATUS = True
        with patch.object(self.api, "status_reporter") as m_status_rep:
            self.api.force_resync(async=True)
            self.step_actor(self.api)
        m_status_rep.resync.assert_called_once_with(async=True)
        self.assertTrue(self.m_etcd_watcher.resync_requested)

    def test_load_config(self):
        result = self.api.load_config(async=True)
        self.step_actor(self.api)
        conf = result.get()
        self.assertEqual(conf, self.m_etcd_watcher.configured)
        self.m_etcd_watcher.load_config.set.assert_called_once_with()

    def test_start_watch(self):
        m_splitter = Mock()
        self.api.load_config(async=True)
        result = self.api.start_watch(m_splitter, async=True)
        self.step_actor(self.api)
        self.m_etcd_watcher.load_config.set.assert_called_once_with()
        self.assertEqual(self.m_etcd_watcher.splitter, m_splitter)
        self.m_etcd_watcher.begin_polling.set.assert_called_once_with()

    @patch("sys.exit", autospec=True)
    def test_on_worker_died(self, m_exit):
        glet = gevent.spawn(lambda: None)
        glet.link(self.api._on_worker_died)
        glet.join(1)
        m_exit.assert_called_once_with(1)