From f8036386bd44c94fc170b6d59c2d026702413417 Mon Sep 17 00:00:00 2001 From: Lianhao Lu Date: Thu, 12 Sep 2013 14:36:54 +0800 Subject: [PATCH] Correctly output the sample content in the file publisher This fixes bug #1224190. Change-Id: I4faf06febac0eb7e1663ef2c68763930c19ca6a4 --- ceilometer/publisher/file.py | 3 ++- tests/publisher/test_file.py | 31 +++++++++++++++++++------------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/ceilometer/publisher/file.py b/ceilometer/publisher/file.py index e529ff0e9..7c10a6da2 100644 --- a/ceilometer/publisher/file.py +++ b/ceilometer/publisher/file.py @@ -93,4 +93,5 @@ class FilePublisher(publisher.PublisherBase): :param samples: Samples from pipeline after transformation """ if self.publisher_logger: - self.publisher_logger.info(samples) + for sample in samples: + self.publisher_logger.info(sample.as_dict()) diff --git a/tests/publisher/test_file.py b/tests/publisher/test_file.py index f9b7ee4df..91879cfd3 100644 --- a/tests/publisher/test_file.py +++ b/tests/publisher/test_file.py @@ -66,10 +66,10 @@ class TestFilePublisher(base.TestCase): ), ] - def test_file_publisher(self): + def test_file_publisher_maxbytes(self): # Test valid configurations - parsed_url = urlsplit( - 'file:///tmp/log_file?max_bytes=50&backup_count=3') + name = '%s/log_file' % self.tempdir.path + parsed_url = urlsplit('file://%s?max_bytes=50&backup_count=3' % name) publisher = file.FilePublisher(parsed_url) publisher.publish_samples(None, self.test_data) @@ -79,13 +79,14 @@ class TestFilePublisher(base.TestCase): logging.handlers.RotatingFileHandler)) self.assertEqual([handler.maxBytes, handler.baseFilename, handler.backupCount], - [50, '/tmp/log_file', 3]) + [50, name, 3]) # The rotating file gets created since only allow 50 bytes. - self.assertTrue(os.path.exists('/tmp/log_file.1')) + self.assertTrue(os.path.exists('%s.1' % name)) + def test_file_publisher(self): # Test missing max bytes, backup count configurations - parsed_url = urlsplit( - 'file:///tmp/log_file_plain') + name = '%s/log_file_plain' % self.tempdir.path + parsed_url = urlsplit('file://%s' % name) publisher = file.FilePublisher(parsed_url) publisher.publish_samples(None, self.test_data) @@ -95,14 +96,20 @@ class TestFilePublisher(base.TestCase): logging.handlers.RotatingFileHandler)) self.assertEqual([handler.maxBytes, handler.baseFilename, handler.backupCount], - [0, '/tmp/log_file_plain', 0]) - - # The rotating file gets created since only allow 50 bytes. - self.assertTrue(os.path.exists('/tmp/log_file_plain')) + [0, name, 0]) + # Test the content is corrected saved in the file + self.assertTrue(os.path.exists(name)) + with open(name, 'r') as f: + content = f.read() + for sample in self.test_data: + self.assertTrue(sample.id in content) + self.assertTrue(sample.timestamp in content) + def test_file_publisher_invalid(self): # Test invalid max bytes, backup count configurations parsed_url = urlsplit( - 'file:///tmp/log_file_bad?max_bytes=yus&backup_count=5y') + 'file://%s/log_file_bad' + '?max_bytes=yus&backup_count=5y' % self.tempdir.path) publisher = file.FilePublisher(parsed_url) publisher.publish_samples(None, self.test_data)