def test_validate_ring(self): self._setup_obj_ring() #ok ring and ok md5 obj_ring = os.path.join(self.testdir, 'object.ring.gz') obj_ring_md5 = get_md5sum(obj_ring) minion = RingMinion(conf={'swiftdir': self.testdir}) minion._validate_ring(obj_ring, obj_ring_md5) # m5 miss match try: minion._validate_ring(obj_ring, 'badmd5') except Exception as err: self.assertEqual(err.message, "md5 missmatch") else: self.fail('Should have thrown md5 missmatch exception') # bad ring file bfile = os.path.join(self.testdir, 'test.ring.gz') with open(bfile, 'w') as f: f.write('whatisthis.') test_md5 = get_md5sum(bfile) try: minion._validate_ring(bfile, test_md5) except Exception as err: self.assertEqual(err.message, "Invalid ring") else: self.fail('Should have thrown Invalid ring exception')
def test_fetch_ring(self, fmoveinplace, fvalidatering, fwritering): self._setup_obj_ring() #test non 200 obj_ring = os.path.join(self.testdir, 'object.ring.gz') obj_ring_md5 = get_md5sum(obj_ring) minion = RingMinion(conf={'swiftdir': self.testdir}) minion.logger = MagicMock() self.urlopen_mock.return_value = MockResponse(code=203) result = minion.fetch_ring('object') self.assertEquals(self.urlopen_mock.call_count, 1) self.assertFalse(result) urllib2.urlopen.assert_called_once minion.logger.warning.assert_called_once_with('Received non 200 status code') minion.logger.warning.reset_mock() self.urlopen_mock.reset_mock() #test 304 self.urlopen_mock.side_effect = urllib2.HTTPError('http://a.com', 304, 'Nope', {}, None) minion.logger.debug.reset_mock() minion.logger.warning.reset_mock() minion.logger.exception.reset_mock() result = minion.fetch_ring('object') self.assertEquals(result, None) minion.logger.debug.assert_called_with('Ring-master reports ring unchanged.') minion.logger.debug.reset_mock() #test HTTPError non 304 self.urlopen_mock.side_effect = urllib2.HTTPError('http://a.com', 401, 'GTFO', {}, None) minion.logger.debug.reset_mock() minion.logger.warning.reset_mock() minion.logger.exception.reset_mock() result = minion.fetch_ring('object') self.assertFalse(result) minion.logger.exception.assert_called_with('Error communicating with ring-master') minion.logger.exception.reset_mock() #test urllib2.URLError self.urlopen_mock.side_effect = urllib2.URLError('oops') minion.logger.debug.reset_mock() minion.logger.warning.reset_mock() minion.logger.exception.reset_mock() result = minion.fetch_ring('object') self.assertFalse(result) minion.logger.exception.assert_called_with('Error communicating with ring-master') minion.logger.exception.reset_mock() #test general exception or timeout self.urlopen_mock.side_effect = Exception('oopsie') minion.logger.debug.reset_mock() minion.logger.warning.reset_mock() minion.logger.exception.reset_mock() result = minion.fetch_ring('object') self.assertFalse(result) minion.logger.exception.assert_called_with('Error retrieving or checking on ring') minion.logger.exception.reset_mock()