def test_compose_missing_builder_id(self): def check_missing_id(cb, builders): # not ok to compose with builder_files that have no id assigned orig_version = cb.version no_id = random.randint(0, len(builders) - 1) # rewrite the builder files so that one has missing id self.save_builders(builders, missing_ids=[no_id]) with self.assertRaises(ValueError) as cm: cb.compose() error_lines = cm.exception.message.split('\n') self.assertIn("Problem with builder at index %s" % no_id, error_lines[0]) self.assertIn("id attribute has not been initialised", error_lines[0]) self.assertFalse(error_lines[1:]) self.assertEqual(orig_version, cb.version) # check with compose not previously called, cb has no existing metadata builders = self.create_sample_ringbuilders(3) builder_files = self.save_builders(builders) cb = CompositeRingBuilder(builder_files) check_missing_id(cb, builders) # now save good copies of builders and compose so this cb has # existing component metadata builder_files = self.save_builders(builders) cb = CompositeRingBuilder(builder_files) cb.compose() # cb now has component metadata check_missing_id(cb, builders)
def test_save_errors(self): cb_file = os.path.join(self.tmpdir, 'test-composite-ring.json') def do_test(cb): with self.assertRaises(ValueError) as cm: cb.save(cb_file) self.assertIn("No composed ring to save", cm.exception.message) do_test(CompositeRingBuilder()) do_test(CompositeRingBuilder([])) do_test(CompositeRingBuilder(['file1', 'file2']))
def do_test(builder_files): cb = CompositeRingBuilder(builder_files) with self.assertRaises(ValueError) as cm: cb.compose() self.assertIn('Two or more component builders are required', cm.exception.message) cb = CompositeRingBuilder() with self.assertRaises(ValueError) as cm: cb.compose(builder_files) self.assertIn('Two or more component builders are required', cm.exception.message)
def _make_composite_builder(self, builders): # helper to compose a ring, save it and sanity check it builder_files = self.save_builders(builders) cb = CompositeRingBuilder(builder_files) cb.compose().save(self.output_ring) self.check_composite_ring(self.output_ring, builders) return cb, builder_files
def test_compose_duplicate_builder_ids(self): builders = self.create_sample_ringbuilders(3) builders[2]._id = builders[0]._id cb = CompositeRingBuilder(self.save_builders(builders)) with self.assertRaises(ValueError) as cm: cb.compose() error_lines = cm.exception.message.split('\n') self.assertIn("Builder id %r used at indexes 0, 2" % builders[0].id, error_lines[0]) self.assertFalse(error_lines[1:]) self.assertEqual(0, cb.version)
def test_compose_with_builder_files(self): cb_file = os.path.join(self.tmpdir, 'test-composite-ring.json') builders = self.create_sample_ringbuilders(2) cb = CompositeRingBuilder(self.save_builders(builders)) cb.compose().save(self.output_ring) self.check_composite_ring(self.output_ring, builders) cb.save(cb_file) for i, b in enumerate(builders): self.add_dev_and_rebalance(b) self.save_builders(builders) cb = CompositeRingBuilder.load(cb_file) cb.compose().save(self.output_ring) self.check_composite_ring(self.output_ring, builders)
def test_abs_paths_persisted(self): cwd = os.getcwd() try: os.chdir(self.tmpdir) builders = self.create_sample_ringbuilders(2) builder_files = self.save_builders(builders) rel_builder_files = [os.path.basename(bf) for bf in builder_files] cb = CompositeRingBuilder(rel_builder_files) cb.compose().save(self.output_ring) self.check_composite_ring(self.output_ring, builders) cb_file = os.path.join(self.tmpdir, 'test-composite-ring.json') rel_cb_file = os.path.basename(cb_file) cb.save(rel_cb_file) self.check_composite_meta(rel_cb_file, rel_builder_files) finally: os.chdir(cwd)
def compose(composite_builder, args): composite_builder = composite_builder or CompositeRingBuilder() try: ring_data = composite_builder.compose( args.builder_files, force=args.force, require_modified=True) except Exception as err: _print_err( 'An error occurred while composing the ring.', err) return EXIT_ERROR try: ring_data.save(args.output) except Exception as err: _print_err( 'An error occurred while writing the composite ring file.', err) return EXIT_ERROR try: composite_builder.save(args.composite_builder_file) except Exception as err: _print_err( 'An error occurred while writing the composite builder file.', err) return EXIT_ERROR return EXIT_SUCCESS