def test_info_bogus_file(self): self.assertIsNone(whisper.info('bogus-file')) # Validate "corrupt" whisper metadata whisper.create(self.filename, self.retention) with SimulatedCorruptWhisperFile(): with AssertRaisesException(whisper.CorruptWhisperFile('Unable to read header', self.filename)): whisper.info(self.filename) # Validate "corrupt" whisper archive data with SimulatedCorruptWhisperFile(corrupt_archive=True): with AssertRaisesException(whisper.CorruptWhisperFile('Unable to read archive0 metadata', self.filename)): whisper.info(self.filename)
def test_str(self): try: raise whisper.CorruptWhisperFile(self.error, self.path) except whisper.CorruptWhisperFile as exc: self.assertEqual( str(exc), "{0} ({1})".format(self.error, self.path) )
def test_repr(self): try: raise whisper.CorruptWhisperFile(self.error, self.path) except whisper.CorruptWhisperFile as exc: self.assertEqual( repr(exc), '<CorruptWhisperFile[%s] %s>' % (self.path, self.error), )
def test_setAggregation(self): """ Create a db, change aggregation, xFilesFactor, then use info() to validate """ original_lock = whisper.LOCK original_caching = whisper.CACHE_HEADERS original_autoflush = whisper.AUTOFLUSH whisper.LOCK = True whisper.AUTOFLUSH = True whisper.CACHE_HEADERS = True # create a new db with a valid configuration whisper.create(self.filename, self.retention) with AssertRaisesException( whisper.InvalidAggregationMethod( 'Unrecognized aggregation method: yummy beer')): whisper.setAggregationMethod(self.filename, 'yummy beer') #set setting every AggregationMethod available for ag in whisper.aggregationMethods: for xff in 0.0, 0.2, 0.4, 0.7, 0.75, 1.0: # original xFilesFactor info0 = whisper.info(self.filename) # optional xFilesFactor not passed whisper.setAggregationMethod(self.filename, ag) # original value should not change info1 = whisper.info(self.filename) self.assertEqual(info0['xFilesFactor'], info1['xFilesFactor']) # the selected aggregation method should have applied self.assertEqual(ag, info1['aggregationMethod']) # optional xFilesFactor used whisper.setAggregationMethod(self.filename, ag, xff) # new info should match what we just set it to info2 = whisper.info(self.filename) # packing and unpacking because # AssertionError: 0.20000000298023224 != 0.2 target_xff = struct.unpack("!f", struct.pack("!f", xff))[0] self.assertEqual(info2['xFilesFactor'], target_xff) # same aggregationMethod asssertion again, but double-checking since # we are playing with packed values and seek() self.assertEqual(ag, info2['aggregationMethod']) with SimulatedCorruptWhisperFile(): with AssertRaisesException( whisper.CorruptWhisperFile('Unable to read header', self.filename)): whisper.setAggregationMethod(self.filename, ag) whisper.LOCK = original_lock whisper.AUTOFLUSH = original_autoflush whisper.CACHE_HEADERS = original_caching
def read_header(map): try: (aggregationType, maxRetention, xFilesFactor, archiveCount) = struct.unpack(whisper.metadataFormat, map[:whisper.metadataSize]) except: raise whisper.CorruptWhisperFile("Unable to unpack header") archives = [] archiveOffset = whisper.metadataSize for i in xrange(archiveCount): try: (offset, secondsPerPoint, points) = struct.unpack( whisper.archiveInfoFormat, map[archiveOffset:archiveOffset + whisper.archiveInfoSize]) except: raise whisper.CorruptWhisperFile( "Unable to read archive %d metadata" % i) archiveInfo = { 'offset': offset, 'secondsPerPoint': secondsPerPoint, 'points': points, 'retention': secondsPerPoint * points, 'size': points * whisper.pointSize, } archives.append(archiveInfo) archiveOffset += whisper.archiveInfoSize header = { 'aggregationMethod': whisper.aggregationTypeToMethod.get(aggregationType, 'average'), 'maxRetention': maxRetention, 'xFilesFactor': xFilesFactor, 'archives': archives, } return header
def test_path(self): try: raise whisper.CorruptWhisperFile(self.error, self.path) except whisper.CorruptWhisperFile as exc: self.assertEqual(exc.path, self.path)