def test_clean_up_endpoint_status_etcd_error(self): self.m_config.REPORT_ENDPOINT_STATUS = True with patch.object(self.rep, "_attempt_cleanup") as m_clean: m_clean.side_effect = EtcdException() self.rep.clean_up_endpoint_statuses(async=True) self.step_actor(self.rep) self.assertTrue(self.rep._cleanup_pending)
def test_mainline(self, m_sleep): m_snap_response = Mock() m_snap_response.etcd_index = 1 m_poll_response = Mock() m_poll_response.modifiedIndex = 2 responses = [ m_snap_response, m_poll_response, ResyncRequired(), # Loop 1 EtcdException(), # Loop 2 ExpectedException(), # Loop 3, Break out of loop. ] self.m_client.read.side_effect = iter(responses) with patch.object(self.watcher, "_on_pre_resync", autospec=True) as m_pre_r: with patch.object(self.watcher, "_on_snapshot_loaded", autospec=True) as m_snap_load: self.assertRaises(ExpectedException, self.watcher.loop) # _on_pre_resync() called once per loop. self.assertEqual(m_pre_r.mock_calls, [call(), call(), call()]) # The snapshot only loads successfully the first time. self.assertEqual(m_snap_load.mock_calls, [call(m_snap_response)]) self.assertEqual(self.m_dispatcher.handle_event.mock_calls, [call(m_poll_response)]) # Should sleep after exception. m_sleep.assert_called_once_with(1)
def write(self, k, v): try: self._client.write(k, v) except EtcdException, e: errorMessage = str(e) + "\n" errorMessage = "%s %s" % (errorMessage, "[Registry] write %s = %s" % (k, v)) raise EtcdException(errorMessage)
def create_directory(self, k, v): try: self._client.write(k, v, dir=True) except EtcdException, e: errorMessage = str(e) + "\n" errorMessage = "%s [Registry] write %s = %s " % (errorMessage, k, v) module_logger.fatal("[registry] write %s=%s" % (errorMessage, k, v)) raise EtcdException(errorMessage)
def get_system_summary(cluster_type): try: summary = SystemSummary(sds_type=cluster_type) if not summary.exists(): raise EtcdException("No clusters of type %s found" % cluster_type) summary = summary.load().to_json() for key, value in summary.items(): if (key.startswith("_") or key in ['hash', 'updated_at', 'value', 'list']): del summary[key] return summary except (EtcdKeyNotFound, AttributeError, TypeError, ValueError) as ex: raise ex
def get_cluster_summary(cluster_id): try: summary = ClusterSummary(cluster_id=cluster_id) if not summary.exists(): raise EtcdException("No summary found for cluster %s" % cluster_id) summary = summary.load().to_json() for key, value in summary.items(): if (key.startswith("_") or key in ['hash', 'updated_at', 'value', 'list']): del summary[key] return summary except (EtcdKeyNotFound, AttributeError, TypeError, ValueError) as ex: raise ex
def test_on_endpoint_status_failure(self): # Send in an endpoint status update. endpoint_id = EndpointId("foo", "bar", "baz", "biff") self.m_client.set.side_effect = EtcdException() with patch("gevent.spawn_later", autospec=True) as m_spawn: self.rep.on_endpoint_status_changed(endpoint_id, IPV4, {"status": "up"}, async=True) self.step_actor(self.rep) # Should do the write. self.assertEqual(self.m_client.set.mock_calls, [ call("/calico/felix/v1/host/foo/workload/bar/baz/endpoint/biff", JSONString({"status": "up"})) ]) # But endpoint should be re-queued in the newer set. self.assertEqual(self.rep._newer_dirty_endpoints, set([endpoint_id])) self.assertEqual(self.rep._older_dirty_endpoints, set())
def read(self, key, wait=False, waitIndex=None, timeout=None, recursive=None, **kwargs): with global_condvar: if wait: if global_index == 0: raise etcd.EtcdKeyError() if waitIndex > global_index: global_condvar.wait(0.1) if waitIndex > global_index: raise EtcdException("Read timed out") if global_data == "": raise EtcdKeyError("") ret = self.fake_result() return ret
def test_monitors_continue_on_etcd_exception(self): env = 'test' d = {} max_events = 2 self.mgr._client.watch = MagicMock( side_effect=EtcdException('timed out')) lambdas = [ lambda: self.mgr.monitor_env_defaults( env, conf=d, max_events=max_events), lambda: self.mgr.monitor_config_sets( conf=d, max_events=max_events), ] for l in lambdas: t0 = time.time() t = l() self.assertEqual(max_events, t.result) self.assertEqual( True, (time.time() - t0) < (max_events * self.mgr.long_polling_safety_delay) ) self.assertEqual(False, t.is_alive())