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:
parent
1d0cf8a515
commit
359e6b8b5f
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue