From 6d8be59fce17008369656628615efcd2677dfe36 Mon Sep 17 00:00:00 2001 From: Kota Tsuyuzaki Date: Mon, 22 Feb 2016 22:41:51 -0800 Subject: [PATCH] Fix ringbuilder tests Some of tests in test/unit/cli/test_ringbuilder doesn't assert the exit code and unfortunately some of these passed even if the statement fails for the assertion actually. This patch enables to assert the exit code from ringbuider and fixes some code/test bugs I noticed. Change-Id: I18fa675ba8a90678e2b5ccb5f90eafab01d22787 --- swift/cli/ringbuilder.py | 2 +- test/unit/cli/test_ringbuilder.py | 520 +++++++++++------------------- 2 files changed, 191 insertions(+), 331 deletions(-) diff --git a/swift/cli/ringbuilder.py b/swift/cli/ringbuilder.py index 076f1975f8..a7a005ef9a 100644 --- a/swift/cli/ringbuilder.py +++ b/swift/cli/ringbuilder.py @@ -1183,7 +1183,7 @@ swift-ring-builder set_overload [%] def main(arguments=None): global argv, backup_dir, builder, builder_file, ring_file - if arguments: + if arguments is not None: argv = arguments else: argv = sys_argv diff --git a/test/unit/cli/test_ringbuilder.py b/test/unit/cli/test_ringbuilder.py index 8856e039f1..fd80b1ab53 100644 --- a/test/unit/cli/test_ringbuilder.py +++ b/test/unit/cli/test_ringbuilder.py @@ -24,6 +24,7 @@ import uuid import shlex from swift.cli import ringbuilder +from swift.cli.ringbuilder import EXIT_SUCCESS, EXIT_WARNING, EXIT_ERROR from swift.common import exceptions from swift.common.ring import RingBuilder @@ -132,6 +133,11 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): }) ring.save(self.tmpfile) + def assertSystemExit(self, return_code, func, *argv): + with self.assertRaises(SystemExit) as cm: + func(*argv) + self.assertEqual(return_code, cm.exception.code) + def test_parse_search_values_old_format(self): # Test old format argv = ["d0r0z0-127.0.0.1:6000R127.0.0.1:6000/sda1_some meta data"] @@ -170,12 +176,8 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): def test_parse_search_values_number_of_arguments(self): # Test Number of arguments abnormal argv = ["--region", "2", "test"] - err = None - try: - ringbuilder._parse_search_values(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit( + EXIT_ERROR, ringbuilder._parse_search_values, argv) def test_find_parts(self): rb = RingBuilder(8, 3, 0) @@ -214,82 +216,52 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): def test_parse_list_parts_values_number_of_arguments(self): # Test Number of arguments abnormal argv = ["--region", "2", "test"] - err = None - try: - ringbuilder._parse_list_parts_values(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit( + EXIT_ERROR, ringbuilder._parse_list_parts_values, argv) def test_parse_add_values_number_of_arguments(self): # Test Number of arguments abnormal argv = ["--region", "2", "test"] - err = None - try: - ringbuilder._parse_add_values(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit( + EXIT_ERROR, ringbuilder._parse_add_values, argv) def test_set_weight_values_no_devices(self): # Test no devices - err = None - try: - ringbuilder._set_weight_values([], 100) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + # _set_weight_values doesn't take argv-like arguments + self.assertSystemExit( + EXIT_ERROR, ringbuilder._set_weight_values, [], 100) def test_parse_set_weight_values_number_of_arguments(self): # Test Number of arguments abnormal argv = ["r1", "100", "r2"] - err = None - try: - ringbuilder._parse_set_weight_values(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit( + EXIT_ERROR, ringbuilder._parse_set_weight_values, argv) argv = ["--region", "2"] - err = None - try: - ringbuilder._parse_set_weight_values(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit( + EXIT_ERROR, ringbuilder._parse_set_weight_values, argv) def test_set_info_values_no_devices(self): # Test no devices - err = None - try: - ringbuilder._set_info_values([], 100) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + # _set_info_values doesn't take argv-like arguments + self.assertSystemExit( + EXIT_ERROR, ringbuilder._set_info_values, [], 100) def test_parse_set_info_values_number_of_arguments(self): # Test Number of arguments abnormal argv = ["r1", "127.0.0.1", "r2"] - err = None - try: - ringbuilder._parse_set_info_values(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit( + EXIT_ERROR, ringbuilder._parse_set_info_values, argv) def test_parse_remove_values_number_of_arguments(self): # Test Number of arguments abnormal argv = ["--region", "2", "test"] - err = None - try: - ringbuilder._parse_remove_values(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit( + EXIT_ERROR, ringbuilder._parse_remove_values, argv) def test_create_ring(self): argv = ["", self.tmpfile, "create", "6", "3.14159265359", "1"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) self.assertEqual(ring.part_power, 6) self.assertEqual(ring.replicas, 3.14159265359) @@ -298,14 +270,14 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): def test_create_ring_number_of_arguments(self): # Test missing arguments argv = ["", self.tmpfile, "create"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_add_device_ipv4_old_format(self): self.create_sample_ring() # Test ipv4(old format) argv = ["", self.tmpfile, "add", "r2z3-127.0.0.1:6000/sda3_some meta data", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Check that device was created with given data ring = RingBuilder.load(self.tmpfile) @@ -326,7 +298,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): argv = ["", self.tmpfile, "add", "r1z1-127.0.0.1:6000/sda9", "3.14159265359", "r1z1-127.0.0.1:6000/sda9", "2"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_add_device_ipv6_old_format(self): self.create_sample_ring() @@ -336,7 +308,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "r2z3-2001:0000:1234:0000:0000:C1C0:ABCD:0876:6000" "R2::10:7000/sda3_some meta data", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Check that device was created with given data ring = RingBuilder.load(self.tmpfile) @@ -366,7 +338,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "6000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Check that device was created with given data ring = RingBuilder.load(self.tmpfile) @@ -396,7 +368,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Check that device was created with given data ring = RingBuilder.load(self.tmpfile) @@ -426,7 +398,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Check that device was created with given data ring = RingBuilder.load(self.tmpfile) @@ -447,23 +419,13 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): def test_add_device_number_of_arguments(self): # Test Number of arguments abnormal argv = ["", self.tmpfile, "add"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_add_device_already_exists(self): # Test Add a device that already exists argv = ["", self.tmpfile, "add", "r0z0-127.0.0.1:6000/sda1_some meta data", "100"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_add_device_old_missing_region(self): self.create_sample_ring() @@ -481,7 +443,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): for search_value in self.search_values: self.create_sample_ring() argv = ["", self.tmpfile, "remove", search_value] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) # Check that weight was set to 0 @@ -513,7 +475,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): # Test ipv4(old format) argv = ["", self.tmpfile, "remove", "d0r0z0-127.0.0.1:6000R127.0.0.1:6000/sda1_some meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) # Check that weight was set to 0 @@ -552,13 +514,13 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Test ipv6(old format) argv = ["", self.tmpfile, "remove", "d4r2z3-[2001:0000:1234:0000:0000:C1C0:ABCD:0876]:6000" "R[2::10]:7000/sda3_some meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) # Check that second device in ring is not affected @@ -601,7 +563,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-ip", "127.0.0.1", "--replication-port", "6000", "--device", "sda1", "--meta", "some meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) # Check that weight was set to 0 @@ -639,7 +601,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "9000", "--device", "sda30", "--meta", "other meta data", "--weight", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Test ipv6(new format) argv = \ @@ -650,7 +612,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-ip", "[3::10]", "--replication-port", "9000", "--device", "sda30", "--meta", "other meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) # Check that second device in ring is not affected @@ -694,7 +656,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Test domain name argv = \ @@ -705,7 +667,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-ip", "r.test.com", "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) # Check that second device in ring is not affected @@ -741,24 +703,14 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): self.create_sample_ring() # Test Number of arguments abnormal argv = ["", self.tmpfile, "remove"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_remove_device_no_matching(self): self.create_sample_ring() # Test No matching devices argv = ["", self.tmpfile, "remove", "--ip", "unknown"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_set_weight(self): for search_value in self.search_values: @@ -766,7 +718,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): argv = ["", self.tmpfile, "set_weight", search_value, "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) # Check that weight was changed @@ -787,7 +739,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): argv = ["", self.tmpfile, "set_weight", "d0r0z0-127.0.0.1:6000R127.0.0.1:6000/sda1_some meta data", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) # Check that weight was changed @@ -814,13 +766,13 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "100"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Test ipv6(old format) argv = ["", self.tmpfile, "set_weight", "d4r2z3-[2001:0000:1234:0000:0000:C1C0:ABCD:0876]:6000" "R[2::10]:7000/sda3_some meta data", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) # Check that second device in ring is not affected @@ -850,7 +802,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-ip", "127.0.0.1", "--replication-port", "6000", "--device", "sda1", "--meta", "some meta data", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) # Check that weight was changed @@ -877,7 +829,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "100"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Test ipv6(new format) argv = \ @@ -888,7 +840,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-ip", "[2::10]", "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) # Check that second device in ring is not affected @@ -919,7 +871,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "100"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Test domain name argv = \ @@ -930,7 +882,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-ip", "r.test.com", "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) # Check that second device in ring is not affected @@ -953,24 +905,14 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): self.create_sample_ring() # Test Number of arguments abnormal argv = ["", self.tmpfile, "set_weight"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_set_weight_no_matching(self): self.create_sample_ring() # Test No matching devices argv = ["", self.tmpfile, "set_weight", "--ip", "unknown"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_set_info(self): for search_value in self.search_values: @@ -978,7 +920,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): self.create_sample_ring() argv = ["", self.tmpfile, "set_info", search_value, "127.0.1.1:8000/sda1_other meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Check that device was created with given data ring = RingBuilder.load(self.tmpfile) @@ -1005,7 +947,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): argv = ["", self.tmpfile, "set_info", "d0r0z0-127.0.0.1:6000R127.0.0.1:6000/sda1_some meta data", "127.0.1.1:8000R127.0.1.1:8000/sda10_other meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Check that device was created with given data ring = RingBuilder.load(self.tmpfile) @@ -1040,7 +982,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Test ipv6(old format) argv = ["", self.tmpfile, "set_info", @@ -1048,7 +990,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "R[2::10]:7000/sda3_some meta data", "[3001:0000:1234:0000:0000:C1C0:ABCD:0876]:8000" "R[3::10]:8000/sda30_other meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) # Check that second device in ring is not affected @@ -1096,7 +1038,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--change-replication-ip", "127.0.2.1", "--change-replication-port", "9000", "--change-device", "sda100", "--change-meta", "other meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Check that device was created with given data ring = RingBuilder.load(self.tmpfile) @@ -1131,7 +1073,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Test ipv6(new format) argv = \ @@ -1147,7 +1089,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--change-replication-ip", "[4::10]", "--change-replication-port", "9000", "--change-device", "sda300", "--change-meta", "other meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) # Check that second device in ring is not affected @@ -1192,7 +1134,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Test domain name argv = \ @@ -1208,7 +1150,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--change-replication-ip", "r.test2.com", "--change-replication-port", "9000", "--change-device", "sda300", "--change-meta", "other meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) # Check that second device in ring is not affected @@ -1244,24 +1186,14 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): self.create_sample_ring() # Test Number of arguments abnormal argv = ["", self.tmpfile, "set_info"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_set_info_no_matching(self): self.create_sample_ring() # Test No matching devices argv = ["", self.tmpfile, "set_info", "--ip", "unknown"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_set_info_already_exists(self): self.create_sample_ring() @@ -1279,17 +1211,12 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--change-replication-ip", "127.0.0.2", "--change-replication-port", "6001", "--change-device", "sda2", "--change-meta", ""] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_set_min_part_hours(self): self.create_sample_ring() argv = ["", self.tmpfile, "set_min_part_hours", "24"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) self.assertEqual(ring.min_part_hours, 24) @@ -1297,38 +1224,33 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): self.create_sample_ring() # Test Number of arguments abnormal argv = ["", self.tmpfile, "set_min_part_hours"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_set_replicas(self): self.create_sample_ring() argv = ["", self.tmpfile, "set_replicas", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) self.assertEqual(ring.replicas, 3.14159265359) def test_set_overload(self): self.create_sample_ring() argv = ["", self.tmpfile, "set_overload", "0.19878"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) self.assertEqual(ring.overload, 0.19878) def test_set_overload_negative(self): self.create_sample_ring() argv = ["", self.tmpfile, "set_overload", "-0.19878"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) self.assertEqual(ring.overload, 0.0) def test_set_overload_non_numeric(self): self.create_sample_ring() argv = ["", self.tmpfile, "set_overload", "swedish fish"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) self.assertEqual(ring.overload, 0.0) @@ -1372,38 +1294,23 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): self.create_sample_ring() # Test missing arguments argv = ["", self.tmpfile, "set_overload"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_set_replicas_number_of_arguments(self): self.create_sample_ring() # Test Number of arguments abnormal argv = ["", self.tmpfile, "set_replicas"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_set_replicas_invalid_value(self): self.create_sample_ring() # Test not a valid number argv = ["", self.tmpfile, "set_replicas", "test"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) # Test new replicas is 0 argv = ["", self.tmpfile, "set_replicas", "0"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_validate(self): self.create_sample_ring() @@ -1411,17 +1318,12 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): ring.rebalance() ring.save(self.tmpfile) argv = ["", self.tmpfile, "validate"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_validate_empty_file(self): open(self.tmpfile, 'a').close argv = ["", self.tmpfile, "validate"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_validate_corrupted_file(self): self.create_sample_ring() @@ -1434,53 +1336,33 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): # corrupt the file with open(self.tmpfile, 'wb') as f: f.write(os.urandom(1024)) - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_validate_non_existent_file(self): rand_file = '%s/%s' % ('/tmp', str(uuid.uuid4())) argv = ["", rand_file, "validate"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_validate_non_accessible_file(self): with mock.patch.object( RingBuilder, 'load', mock.Mock(side_effect=exceptions.PermissionError)): argv = ["", self.tmpfile, "validate"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_validate_generic_error(self): with mock.patch.object( RingBuilder, 'load', mock.Mock( side_effect=IOError('Generic error occurred'))): argv = ["", self.tmpfile, "validate"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_search_device_ipv4_old_format(self): self.create_sample_ring() # Test ipv4(old format) argv = ["", self.tmpfile, "search", "d0r0z0-127.0.0.1:6000R127.0.0.1:6000/sda1_some meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_search_device_ipv6_old_format(self): self.create_sample_ring() @@ -1494,13 +1376,18 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) + + # write ring file + ring = RingBuilder.load(self.tmpfile) + ring.rebalance() + ring.save(self.tmpfile) # Test ipv6(old format) argv = ["", self.tmpfile, "search", - "d2r2z3-[2001:0000:1234:0000:0000:C1C0:ABCD:0876]:6000" + "d4r2z3-[2001:0000:1234:0000:0000:C1C0:ABCD:0876]:6000" "R[2::10]:7000/sda3_some meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_search_device_ipv4_new_format(self): self.create_sample_ring() @@ -1513,7 +1400,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-ip", "127.0.0.1", "--replication-port", "6000", "--device", "sda1", "--meta", "some meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_search_device_ipv6_new_format(self): self.create_sample_ring() @@ -1527,18 +1414,23 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) + + # write ring file + ring = RingBuilder.load(self.tmpfile) + ring.rebalance() + ring.save(self.tmpfile) # Test ipv6(new format) argv = \ ["", self.tmpfile, "search", - "--id", "2", "--region", "2", "--zone", "3", + "--id", "4", "--region", "2", "--zone", "3", "--ip", "[2001:0000:1234:0000:0000:C1C0:ABCD:0876]", "--port", "6000", "--replication-ip", "[2::10]", "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_search_device_domain_new_format(self): self.create_sample_ring() @@ -1552,48 +1444,45 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) + # write ring file + ring = RingBuilder.load(self.tmpfile) + ring.rebalance() + ring.save(self.tmpfile) # Test domain name argv = \ ["", self.tmpfile, "search", - "--id", "2", "--region", "2", "--zone", "3", + "--id", "4", "--region", "2", "--zone", "3", "--ip", "test.test.com", "--port", "6000", "--replication-ip", "r.test.com", "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_search_device_number_of_arguments(self): self.create_sample_ring() # Test Number of arguments abnormal argv = ["", self.tmpfile, "search"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_search_device_no_matching(self): self.create_sample_ring() # Test No matching devices argv = ["", self.tmpfile, "search", "--ip", "unknown"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_list_parts_ipv4_old_format(self): self.create_sample_ring() + ring = RingBuilder.load(self.tmpfile) + ring.rebalance() + ring.save(self.tmpfile) # Test ipv4(old format) argv = ["", self.tmpfile, "list_parts", "d0r0z0-127.0.0.1:6000R127.0.0.1:6000/sda1_some meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_list_parts_ipv6_old_format(self): self.create_sample_ring() @@ -1607,16 +1496,24 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) + + # write ring file + ring = RingBuilder.load(self.tmpfile) + ring.rebalance() + ring.save(self.tmpfile) # Test ipv6(old format) argv = ["", self.tmpfile, "list_parts", - "d2r2z3-[2001:0000:1234:0000:0000:C1C0:ABCD:0876]:6000" + "d4r2z3-[2001:0000:1234:0000:0000:C1C0:ABCD:0876]:6000" "R[2::10]:7000/sda3_some meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_list_parts_ipv4_new_format(self): self.create_sample_ring() + ring = RingBuilder.load(self.tmpfile) + ring.rebalance() + ring.save(self.tmpfile) # Test ipv4(new format) argv = \ ["", self.tmpfile, "list_parts", @@ -1626,7 +1523,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-ip", "127.0.0.1", "--replication-port", "6000", "--device", "sda1", "--meta", "some meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_list_parts_ipv6_new_format(self): self.create_sample_ring() @@ -1640,18 +1537,23 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) + + # write ring file + ring = RingBuilder.load(self.tmpfile) + ring.rebalance() + ring.save(self.tmpfile) # Test ipv6(new format) argv = \ ["", self.tmpfile, "list_parts", - "--id", "2", "--region", "2", "--zone", "3", + "--id", "4", "--region", "2", "--zone", "3", "--ip", "[2001:0000:1234:0000:0000:C1C0:ABCD:0876]", "--port", "6000", "--replication-ip", "[2::10]", "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_list_parts_domain_new_format(self): self.create_sample_ring() @@ -1665,56 +1567,46 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data", "--weight", "3.14159265359"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) + + # write ring file + ring = RingBuilder.load(self.tmpfile) + ring.rebalance() + ring.save(self.tmpfile) # Test domain name argv = \ ["", self.tmpfile, "list_parts", - "--id", "2", "--region", "2", "--zone", "3", + "--id", "4", "--region", "2", "--zone", "3", "--ip", "test.test.com", "--port", "6000", "--replication-ip", "r.test.com", "--replication-port", "7000", "--device", "sda3", "--meta", "some meta data"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_list_parts_number_of_arguments(self): self.create_sample_ring() # Test Number of arguments abnormal argv = ["", self.tmpfile, "list_parts"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_list_parts_no_matching(self): self.create_sample_ring() # Test No matching devices argv = ["", self.tmpfile, "list_parts", "--ip", "unknown"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_unknown(self): self.create_sample_ring() argv = ["", self.tmpfile, "unknown"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_default(self): self.create_sample_ring() argv = ["", self.tmpfile] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_default_show_removed(self): mock_stdout = six.StringIO() @@ -1724,17 +1616,17 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): # Note: it also sets device's weight to zero. argv = ["", self.tmpfile, "remove", "--id", "1"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # Setting another device's weight to zero to be sure we distinguish # real removed device and device with zero weight. argv = ["", self.tmpfile, "set_weight", "0", "--id", "3"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) argv = ["", self.tmpfile] with mock.patch("sys.stdout", mock_stdout): with mock.patch("sys.stderr", mock_stderr): - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) expected = "%s, build version 6\n" \ "64 partitions, 3.000000 replicas, 4 regions, 4 zones, " \ @@ -1770,43 +1662,43 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): argv = ["", self.tmpfile] with mock.patch("sys.stdout", mock_stdout): with mock.patch("sys.stderr", mock_stderr): - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring_not_found_re = re.compile("Ring file .*\.ring\.gz not found") self.assertTrue(ring_not_found_re.findall(mock_stdout.getvalue())) # write ring file argv = ["", self.tmpfile, "rebalance"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # ring file is up-to-date mock_stdout = six.StringIO() argv = ["", self.tmpfile] with mock.patch("sys.stdout", mock_stdout): with mock.patch("sys.stderr", mock_stderr): - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring_up_to_date_re = re.compile("Ring file .*\.ring\.gz is up-to-date") self.assertTrue(ring_up_to_date_re.findall(mock_stdout.getvalue())) # change builder (set weight) argv = ["", self.tmpfile, "set_weight", "0", "--id", "3"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # ring file is obsolete after set_weight mock_stdout = six.StringIO() argv = ["", self.tmpfile] with mock.patch("sys.stdout", mock_stdout): with mock.patch("sys.stderr", mock_stderr): - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring_obsolete_re = re.compile("Ring file .*\.ring\.gz is obsolete") self.assertTrue(ring_obsolete_re.findall(mock_stdout.getvalue())) # write ring file argv = ["", self.tmpfile, "write_ring"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) # ring file up-to-date again mock_stdout = six.StringIO() argv = ["", self.tmpfile] with mock.patch("sys.stdout", mock_stdout): with mock.patch("sys.stderr", mock_stderr): - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) self.assertTrue(ring_up_to_date_re.findall(mock_stdout.getvalue())) # Break ring file e.g. just make it empty @@ -1816,14 +1708,14 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): argv = ["", self.tmpfile] with mock.patch("sys.stdout", mock_stdout): with mock.patch("sys.stderr", mock_stderr): - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring_invalid_re = re.compile("Ring file .*\.ring\.gz is invalid") self.assertTrue(ring_invalid_re.findall(mock_stdout.getvalue())) def test_rebalance(self): self.create_sample_ring() argv = ["", self.tmpfile, "rebalance", "3"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) self.assertTrue(ring.validate()) @@ -1834,24 +1726,14 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): ring.save(self.tmpfile) # Test No change to the device argv = ["", self.tmpfile, "rebalance", "3"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 1) + self.assertSystemExit(EXIT_WARNING, ringbuilder.main, argv) def test_rebalance_no_devices(self): # Test no devices argv = ["", self.tmpfile, "create", "6", "3.14159265359", "1"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) argv = ["", self.tmpfile, "rebalance"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_rebalance_remove_zero_weighted_device(self): self.create_sample_ring() @@ -1864,7 +1746,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): # Test rebalance after remove 0 weighted device argv = ["", self.tmpfile, "rebalance", "3"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) self.assertTrue(ring.validate()) self.assertEqual(ring.devs[3], None) @@ -1881,7 +1763,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): time += 3600 with mock.patch(time_path, return_value=time): self.assertEqual(ring.min_part_seconds_left, 0) - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) self.assertEqual(ring.min_part_seconds_left, 3600) @@ -1893,7 +1775,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): time += 3600 with mock.patch(time_path, return_value=time): self.assertEqual(ring.min_part_seconds_left, 0) - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_WARNING, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) self.assertTrue(ring.min_part_seconds_left, 3600) @@ -1915,7 +1797,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): # min part hours not passed with mock.patch(time_path, return_value=(3600 * 0.6)): - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_WARNING, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) self.assertEqual(ring.min_part_seconds_left, 3600 * 0.4) @@ -1923,7 +1805,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): # min part hours passed, no partitions need to be moved with mock.patch(time_path, return_value=(3600 * 1.5)): - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_WARNING, ringbuilder.main, argv) ring = RingBuilder.load(self.tmpfile) self.assertEqual(ring.min_part_seconds_left, 0) @@ -1931,21 +1813,21 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): self.create_sample_ring() # Test rebalance using explicit seed parameter argv = ["", self.tmpfile, "rebalance", "--seed", "2"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_write_ring(self): self.create_sample_ring() argv = ["", self.tmpfile, "rebalance"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) argv = ["", self.tmpfile, "write_ring"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_write_builder(self): # Test builder file already exists self.create_sample_ring() argv = ["", self.tmpfile, "rebalance"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) argv = ["", self.tmpfile, "write_builder"] exp_results = {'valid_exit_codes': [2]} self.run_srb(*argv, exp_results=exp_results) @@ -1955,17 +1837,17 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): # and lost the builder file self.create_sample_ring() - argv = ["", self.tmpfile, "add", "r1z1-127.0.0.1:6000/sdb" "1.0"] - self.assertRaises(SystemExit, ringbuilder.main, argv) - argv = ["", self.tmpfile, "add", "r1z1-127.0.0.1:6000/sdc" "1.0"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + argv = ["", self.tmpfile, "add", "r1z1-127.0.0.1:6000/sdb", "1.0"] + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) + argv = ["", self.tmpfile, "add", "r1z1-127.0.0.1:6000/sdc", "1.0"] + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) argv = ["", self.tmpfile, "rebalance"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_WARNING, ringbuilder.main, argv) argv = ["", self.tmpfile, "remove", "--id", "0"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) argv = ["", self.tmpfile, "rebalance"] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_WARNING, ringbuilder.main, argv) backup_file = os.path.join(os.path.dirname(self.tmpfile), os.path.basename(self.tmpfile) + ".ring.gz") @@ -1988,12 +1870,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): ring.devs[0]['weight'] = 10 ring.save(self.tmpfile) argv = ["", self.tmpfile, "rebalance"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 1) + self.assertSystemExit(EXIT_WARNING, ringbuilder.main, argv) def test_no_warn_when_balanced(self): # when the number of total part replicas (3 * 2 ** 10 = 3072 in @@ -2009,28 +1886,18 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): ring.devs[0]['weight'] = 10 ring.save(self.tmpfile) argv = ["", self.tmpfile, "rebalance"] - err = None - try: - ringbuilder.main(argv) - except SystemExit as e: - err = e - self.assertEqual(err.code, 0) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_invalid_device_name(self): self.create_sample_ring() for device_name in ["", " ", " sda1", "sda1 ", " meta "]: - err = 0 argv = ["", self.tmpfile, "add", "r1z1-127.0.0.1:6000/%s" % device_name, "1"] - try: - ringbuilder.main(argv) - except SystemExit as exc: - err = exc - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) argv = ["", self.tmpfile, @@ -2041,11 +1908,7 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): "--port", "6000", "--device", device_name, "--weight", "100"] - try: - ringbuilder.main(argv) - except SystemExit as exc: - err = exc - self.assertEqual(err.code, 2) + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) def test_dispersion_command(self): self.create_sample_ring() @@ -2060,12 +1923,9 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): argv = ["", "object.ring.gz"] - try: - with mock.patch("sys.stdout", mock_stdout): - with mock.patch("sys.stderr", mock_stderr): - ringbuilder.main(argv) - except SystemExit: - pass + with mock.patch("sys.stdout", mock_stdout): + with mock.patch("sys.stderr", mock_stderr): + self.assertSystemExit(EXIT_ERROR, ringbuilder.main, argv) expected = "Note: using object.builder instead of object.ring.gz " \ "as builder file\n" \ "Ring Builder file does not exist: object.builder\n" @@ -2074,18 +1934,18 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): def test_main_no_arguments(self): # Test calling main with no arguments argv = [] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_main_single_argument(self): # Test calling main with single argument argv = [""] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) def test_main_with_safe(self): # Test calling main with '-safe' argument self.create_sample_ring() argv = ["-safe", self.tmpfile] - self.assertRaises(SystemExit, ringbuilder.main, argv) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv) class TestRebalanceCommand(unittest.TestCase, RunSwiftRingBuilderMixin):