def test_show(self): b1, b1_file = write_stub_builder(self.tmpdir, 1) b2, b2_file = write_stub_builder(self.tmpdir, 2) args = ('', self.composite_builder_file, 'compose', b1_file, b2_file, '--output', self.composite_ring_file) exit_code, stdout, stderr = self._run_composer(args) self.assertEqual(0, exit_code) args = ('', self.composite_builder_file, 'show') exit_code, stdout, stderr = self._run_composer(args) self.assertEqual(0, exit_code) expected = { 'component_builder_files': { b1.id: b1_file, b2.id: b2_file }, 'components': [ { 'id': b1.id, 'replicas': b1.replicas, # added replicas devices plus rebalance 'version': b1.replicas + 1 }, { 'id': b2.id, 'replicas': b2.replicas, # added replicas devices plus rebalance 'version': b2.replicas + 1 } ], 'version': 1 } self.assertEqual(expected, json.loads(stdout))
def test_compose(self): b1, b1_file = write_stub_builder(self.tmpdir, 1) b2, b2_file = write_stub_builder(self.tmpdir, 2) args = ('', self.composite_builder_file, 'compose', b1_file, b2_file, '--output', self.composite_ring_file) exit_code, stdout, stderr = self._run_composer(args) self.assertEqual(0, exit_code) self.assertTrue(os.path.exists(self.composite_builder_file)) self.assertTrue(os.path.exists(self.composite_ring_file))
def test_compose_fails_to_write_composite_ring_file(self): b1, b1_file = write_stub_builder(self.tmpdir, 1) b2, b2_file = write_stub_builder(self.tmpdir, 2) args = ('', self.composite_builder_file, 'compose', b1_file, b2_file, '--output', self.composite_ring_file) with mock.patch('swift.common.ring.RingData.save', side_effect=IOError('io error')): exit_code, stdout, stderr = self._run_composer(args) self.assertEqual(2, exit_code) self.assertIn( 'An error occurred while writing the composite ring file', stderr) self.assertIn('io error', stderr) self.assertFalse(os.path.exists(self.composite_builder_file)) self.assertFalse(os.path.exists(self.composite_ring_file))
def test_compose_insufficient_component_builder_files(self): b1, b1_file = write_stub_builder(self.tmpdir, 1) args = ('', self.composite_builder_file, 'compose', b1_file, '--output', self.composite_ring_file) exit_code, stdout, stderr = self._run_composer(args) self.assertEqual(2, exit_code) self.assertIn('An error occurred while composing the ring', stderr) self.assertIn('Two or more component builders are required', stderr) self.assertFalse(os.path.exists(self.composite_builder_file)) self.assertFalse(os.path.exists(self.composite_ring_file))
def test_compose_existing(self): b1, b1_file = write_stub_builder(self.tmpdir, 1) b2, b2_file = write_stub_builder(self.tmpdir, 2) args = ('', self.composite_builder_file, 'compose', b1_file, b2_file, '--output', self.composite_ring_file) exit_code, stdout, stderr = self._run_composer(args) self.assertEqual(0, exit_code) os.unlink(self.composite_ring_file) # no changes - expect failure args = ('', self.composite_builder_file, 'compose', '--output', self.composite_ring_file) exit_code, stdout, stderr = self._run_composer(args) self.assertEqual(2, exit_code) self.assertFalse(os.path.exists(self.composite_ring_file)) # --force should force output args = ('', self.composite_builder_file, 'compose', '--output', self.composite_ring_file, '--force') exit_code, stdout, stderr = self._run_composer(args) self.assertEqual(0, exit_code) self.assertTrue(os.path.exists(self.composite_ring_file))
def test_compose_nonexistent_component_builder_file(self): b1, b1_file = write_stub_builder(self.tmpdir, 1) bad_file = os.path.join(self.tmpdir, 'non-existent-file') args = ('', self.composite_builder_file, 'compose', b1_file, bad_file, '--output', self.composite_ring_file) exit_code, stdout, stderr = self._run_composer(args) self.assertIn('An error occurred while composing the ring', stderr) self.assertIn('Ring Builder file does not exist', stderr) self.assertEqual(2, exit_code) self.assertFalse(os.path.exists(self.composite_builder_file)) self.assertFalse(os.path.exists(self.composite_ring_file))
def test_show(self): b1, b1_file = write_stub_builder(self.tmpdir, 1) b2, b2_file = write_stub_builder(self.tmpdir, 2) args = ('', self.composite_builder_file, 'compose', b1_file, b2_file, '--output', self.composite_ring_file) exit_code, stdout, stderr = self._run_composer(args) self.assertEqual(0, exit_code) args = ('', self.composite_builder_file, 'show') exit_code, stdout, stderr = self._run_composer(args) self.assertEqual(0, exit_code) expected = {'component_builder_files': {b1.id: b1_file, b2.id: b2_file}, 'components': [ {'id': b1.id, 'replicas': b1.replicas, # added replicas devices plus rebalance 'version': b1.replicas + 1}, {'id': b2.id, 'replicas': b2.replicas, # added replicas devices plus rebalance 'version': b2.replicas + 1}], 'version': 1 } self.assertEqual(expected, json.loads(stdout))