def test_trigger_present(self): LOG.info("Trigger present; nothing happens") self.client.get.side_effect = iter([ # Read trigger: present with good lease. [(self.pid.encode(), {'mod_revision': '2000', 'lease': '1'})], ]) mech_calico.check_request_etcd_compaction() self.client.status.assert_not_called() self.client.put.assert_not_called() self.client.post.assert_not_called()
def test_iter_1(self): LOG.info("1. no compaction keys present") self.client.get.side_effect = iter([ # Read trigger: not found. [], # Read last: not found. [], ]) mech_calico.check_request_etcd_compaction() self.assertEqual([ mock.call('/calico/compaction/v1/last', '0', lease=None), mock.call( '/calico/compaction/v1/trigger', self.pid, lease=mock.ANY), ], self.client.put.mock_calls) self.client.post.assert_not_called()
def test_trigger_present_missing_lease(self): LOG.info("Trigger present but with missing lease") self.client.get.side_effect = iter([ # Read trigger: present but lease missing. [(self.pid, { 'mod_revision': '2000' })], ]) mech_calico.check_request_etcd_compaction() self.assertEqual([ mock.call('/calico/compaction/v1/last', '0', lease=None), mock.call( '/calico/compaction/v1/trigger', self.pid, lease=mock.ANY), ], self.client.put.mock_calls) self.client.post.assert_not_called()
def test_trigger_present_bad_lease(self): LOG.info("Trigger present but with bad lease") self.client.get.side_effect = iter([ # Read trigger: present but lease has unreasonably large TTL. [(self.pid.encode(), {'mod_revision': '2000', 'lease': '2'})], ]) mech_calico.check_request_etcd_compaction() self.assertEqual([ mock.call('/calico/compaction/v1/last', '0', lease=None), mock.call('/calico/compaction/v1/trigger', self.pid, lease=mock.ANY), ], self.client.put.mock_calls ) self.client.post.assert_not_called()
def test_iter_2(self): LOG.info("2. compacted@0 checked@10 now=100 => no compaction") self.client.get.side_effect = iter([ # Read trigger: not found. [], # Read last. [('0'.encode(), {'mod_revision': '10'})], ]) self.client.status.return_value = {'header': { 'cluster_id': '12345', 'revision': '100', }} mech_calico.check_request_etcd_compaction() self.assertEqual([ mock.call('/calico/compaction/v1/last', '0', lease=None), mock.call('/calico/compaction/v1/trigger', self.pid, lease=mock.ANY), ], self.client.put.mock_calls ) self.client.post.assert_not_called()
def test_bogus_last_compaction_rev(self): LOG.info("Bogus last compaction revision > current revision") self.client.get.side_effect = iter([ # Read trigger: not found. [], # Read last. [('3000', {'mod_revision': '1100'})], ]) self.client.status.return_value = {'header': { 'cluster_id': '12345', 'revision': '1200', }} mech_calico.check_request_etcd_compaction() self.assertEqual([ mock.call('/calico/compaction/v1/last', '0', lease=None), mock.call('/calico/compaction/v1/trigger', self.pid, lease=mock.ANY), ], self.client.put.mock_calls ) self.client.post.assert_not_called()
def test_exception_detail_logging(self): LOG.info("Logging detail for etcd3 failure exception") # This is like test_iter_5, except we arrange for the # etcdv3.request_compaction call to raise an exception, and # check the resulting logging. e3e = Etcd3Exception('revision has been compacted') self.client.post.side_effect = e3e self.client.get.side_effect = iter([ # Read trigger: not found. [], # Read last. [('100'.encode(), {'mod_revision': '1100'})], ]) self.client.status.return_value = {'header': { 'cluster_id': '12345', 'revision': '1200', }} with mock.patch.object( mech_calico.LOG, 'info') as mock_li: mech_calico.check_request_etcd_compaction() self.assertEqual([ mock.call('/calico/compaction/v1/last', '1200', lease=None), mock.call('/calico/compaction/v1/trigger', self.pid, lease=mock.ANY), ], self.client.put.mock_calls ) self.client.post.assert_called() mock_li.assert_called_with( 'Someone else has requested etcd compaction:\n%s', 'revision has been compacted', )