Add --output arg

This will allow us to print out the subuni stream on stdout, but write
the json itself on a seperate file. This way you can save the delta
result before processing it.
This commit is contained in:
Clint Byrum 2015-11-11 14:57:47 -08:00
parent 1d0cf8a515
commit 359e6b8b5f
2 changed files with 34 additions and 1 deletions

View File

@ -49,6 +49,8 @@ def main(argv=None, stdout=None):
help="Wrap the json output in a subunit stream. If an "
"argument is passed used that as the filename, "
"otherwise 'counters.json' will be used")
parser.add_argument('--output', help="Write JSON here. Does not disable "
"stdout.")
args = parser.parse_args(argv[1:])
logging.basicConfig(
format='%(asctime)-15s %(levelname)s %(threadName)s: %(message)s')
@ -70,7 +72,6 @@ def main(argv=None, stdout=None):
}
if args.delta:
collected = _delta.delta_with_file(args.delta, collected)
content = json.dumps(collected, indent=1, sort_keys=True).encode('utf-8')
if args.subunit is not None:
file_name = args.subunit or 'counters.json'
@ -82,6 +83,10 @@ def main(argv=None, stdout=None):
else:
stdout.write(content)
stdout.write(b"\n")
if args.output:
with open(args.output, 'w') as output:
output.write(content)
output.write(b"\n")
if __name__ == '__main__':
main()

View File

@ -21,6 +21,7 @@ Tests for `os_performance_tools.collect`
import json
import mock
import tempfile
from os_performance_tools import collect
from os_performance_tools.tests import base
@ -85,3 +86,30 @@ class TestCollect(base.TestCase):
self.assertTrue(isinstance(content, dict))
self.assertIn('mysql', content)
self.assertIn('queues', content)
@mock.patch('os_performance_tools.collectors.mysql.collect')
@mock.patch('os_performance_tools.collectors.queues.collect')
def test_collect_main_subunit_and_json(self, queues_mock, mysql_mock):
mysql_mock.return_value = {}
queues_mock.return_value = {}
with tempfile.NamedTemporaryFile() as tfile:
collect.main(
['os-collect-counters', '--subunit', '--output', tfile.name],
self.stdout)
content = json.loads(tfile.read())
self.assertTrue(isinstance(content, dict))
self.assertIn('mysql', content)
self.assertIn('queues', content)
self.stdout.seek(0)
stream = subunit.ByteStreamToStreamResult(self.stdout)
result = StreamResult()
result.startTestRun()
try:
stream.run(result)
finally:
result.stopTestRun()
self.assertIsNotNone(result.counters_content)
content = json.loads(result.counters_content.decode('utf-8'))
self.assertTrue(isinstance(content, dict))
self.assertIn('mysql', content)
self.assertIn('queues', content)