From b0bcf9858923d1078e436af965890c8dc3e01fd0 Mon Sep 17 00:00:00 2001 From: Jan Fajerski Date: Sat, 3 Oct 2020 09:40:33 +0200 Subject: [PATCH] ceph-volume batch: return valid empty json reports Fixes: https://tracker.ceph.com/issues/47729 Signed-off-by: Jan Fajerski (cherry picked from commit ab59269a6ca5bb80c28e94beef0338f23fc10fff) --- .../ceph_volume/devices/lvm/batch.py | 23 ++++++++++------ .../tests/devices/lvm/test_batch.py | 26 ++++++++++++++++++- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/ceph-volume/ceph_volume/devices/lvm/batch.py b/src/ceph-volume/ceph_volume/devices/lvm/batch.py index 21fcdd09cd8e1..a6ced6a108504 100644 --- a/src/ceph-volume/ceph_volume/devices/lvm/batch.py +++ b/src/ceph-volume/ceph_volume/devices/lvm/batch.py @@ -325,12 +325,11 @@ class Batch(object): setattr(self, '{}usable'.format(dev_list), []) def report(self, plan): - if self.args.format == 'json': - print(json.dumps([osd.report_json() for osd in plan])) - elif self.args.format == 'json-pretty': - print(json.dumps([osd.report_json() for osd in plan], indent=4, - sort_keys=True)) - else: + report = self._create_report(plan) + print(report) + + def _create_report(self, plan): + if self.args.format == 'pretty': report = '' report += templates.total_osds.format(total_osds=len(plan)) @@ -338,8 +337,16 @@ class Batch(object): for osd in plan: report += templates.osd_header report += osd.report() - - print(report) + return report + else: + json_report = [] + for osd in plan: + json_report.append(osd.report_json()) + if self.args.format == 'json': + return json.dumps(json_report) + elif self.args.format == 'json-pretty': + return json.dumps(json_report, indent=4, + sort_keys=True) def _check_slot_args(self): ''' diff --git a/src/ceph-volume/ceph_volume/tests/devices/lvm/test_batch.py b/src/ceph-volume/ceph_volume/tests/devices/lvm/test_batch.py index 692bce7456e0f..29d23fd72b64e 100644 --- a/src/ceph-volume/ceph_volume/tests/devices/lvm/test_batch.py +++ b/src/ceph-volume/ceph_volume/tests/devices/lvm/test_batch.py @@ -1,4 +1,5 @@ import pytest +import json import random from ceph_volume.devices.lvm import batch @@ -19,7 +20,7 @@ class TestBatch(object): assert 'Device lists are not disjoint' in str(disjoint_ex.value) @pytest.mark.parametrize('format_', ['pretty', 'json', 'json-pretty']) - def test_json_report(self, format_, factory, conf_ceph_stub, mock_device_generator): + def test_report(self, format_, factory, conf_ceph_stub, mock_device_generator): # just ensure reporting works conf_ceph_stub('[global]\nfsid=asdf-lkjh') devs = [mock_device_generator() for _ in range(5)] @@ -40,6 +41,29 @@ class TestBatch(object): b.args = args b.report(plan) + @pytest.mark.parametrize('format_', ['json', 'json-pretty']) + def test_json_report_valid_empty(self, format_, factory, conf_ceph_stub, mock_device_generator): + # ensure json reports are valid when empty + conf_ceph_stub('[global]\nfsid=asdf-lkjh') + devs = [mock_device_generator() for _ in range(5)] + args = factory(data_slots=1, + osds_per_device=1, + osd_ids=[], + report=True, + format=format_, + devices=devs, + db_devices=[], + wal_devices=[], + bluestore=True, + block_db_size="1G", + dmcrypt=True, + ) + b = batch.Batch([]) + plan = b.get_plan(args) + b.args = args + report = b._create_report(plan) + json.loads(report) + def test_get_physical_osds_return_len(self, factory, mock_devices_available, conf_ceph_stub, -- 2.39.5