diff --git a/swift/cli/ringbuilder.py b/swift/cli/ringbuilder.py index 1b9cb05577..06ac83e3b4 100644 --- a/swift/cli/ringbuilder.py +++ b/swift/cli/ringbuilder.py @@ -1085,14 +1085,16 @@ swift-ring-builder write_ring 'set_info' calls when no rebalance is needed but you want to send out the new device information. """ + if not builder.devs: + print('Unable to write empty ring.') + exit(EXIT_ERROR) + ring_data = builder.get_ring() if not ring_data._replica2part2dev_id: if ring_data.devs: print('Warning: Writing a ring with no partition ' 'assignments but with devices; did you forget to run ' '"rebalance"?') - else: - print('Warning: Writing an empty ring') ring_data.save( pathjoin(backup_dir, '%d.' % time() + basename(ring_file))) ring_data.save(ring_file) diff --git a/test/unit/cli/test_ringbuilder.py b/test/unit/cli/test_ringbuilder.py index 7eec30114c..65c096f181 100644 --- a/test/unit/cli/test_ringbuilder.py +++ b/test/unit/cli/test_ringbuilder.py @@ -46,7 +46,6 @@ class RunSwiftRingBuilderMixin(object): if 'exp_results' in kwargs: exp_results = kwargs['exp_results'] - argv = argv[:-1] else: exp_results = None @@ -2078,6 +2077,13 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): argv = ["", self.tmpfile, "write_ring"] self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) + def test_write_empty_ring(self): + ring = RingBuilder(6, 3, 1) + ring.save(self.tmpfile) + exp_results = {'valid_exit_codes': [2]} + out, err = self.run_srb("write_ring", exp_results=exp_results) + self.assertEqual('Unable to write empty ring.\n', out) + def test_write_builder(self): # Test builder file already exists self.create_sample_ring()