def test__performs_clone(self): user = factory.make_admin() source = factory.make_Machine(with_boot_disk=False) factory.make_PhysicalBlockDevice(node=source, size=8 * 1024**3, name="sda") factory.make_Interface(node=source, name='eth0') destination1 = factory.make_Machine(status=NODE_STATUS.READY, with_boot_disk=False) factory.make_PhysicalBlockDevice(node=destination1, size=8 * 1024**3, name="sda") factory.make_Interface(node=destination1, name='eth0') destination2 = factory.make_Machine(status=NODE_STATUS.READY, with_boot_disk=False) factory.make_PhysicalBlockDevice(node=destination2, size=8 * 1024**3, name="sda") factory.make_Interface(node=destination2, name='eth0') form = CloneForm(user, data={ 'source': source.system_id, 'destinations': [destination1.system_id, destination2.system_id], 'storage': True, 'interfaces': True, }) self.assertTrue(form.is_valid()) # An exception here will cause the test to fail. form.save()
def test__performs_clone(self): user = factory.make_admin() source = factory.make_Machine(with_boot_disk=False) factory.make_PhysicalBlockDevice( node=source, size=8 * 1024 ** 3, name="sda" ) factory.make_Interface(node=source, name="eth0") destination1 = factory.make_Machine( status=random.choice( [NODE_STATUS.READY, NODE_STATUS.FAILED_TESTING] ), with_boot_disk=False, ) factory.make_PhysicalBlockDevice( node=destination1, size=8 * 1024 ** 3, name="sda" ) factory.make_Interface(node=destination1, name="eth0") destination2 = factory.make_Machine( status=random.choice( [NODE_STATUS.READY, NODE_STATUS.FAILED_TESTING] ), with_boot_disk=False, ) factory.make_PhysicalBlockDevice( node=destination2, size=8 * 1024 ** 3, name="sda" ) factory.make_Interface(node=destination2, name="eth0") form = CloneForm( user, data={ "source": source.system_id, "destinations": [ destination1.system_id, destination2.system_id, ], "storage": True, "interfaces": True, }, ) self.assertTrue(form.is_valid()) # An exception here will cause the test to fail. form.save()
def _execute( self, destinations=None, storage=False, interfaces=False, _error_data=None, ): data = { "source": self.node, "destinations": destinations, "storage": storage, "interfaces": interfaces, } form = CloneForm(self.user, data=data) if form.has_error("destinations", "storage") or form.has_error( "destinations", "network"): # Try again with all the bad destinations removed new_destinations = form.strip_failed_destinations() if new_destinations: return self._execute( destinations=new_destinations, storage=storage, interfaces=interfaces, _error_data=form.errors, ) if not form.is_valid(): raise NodeActionError(self._format_errors_as_json(form.errors)) try: form.save() except ValidationError as exc: raise NodeActionError(self._format_errors_as_json(exc.errors)) if _error_data: for name, error_list in _error_data.items(): if name in form.errors: form.errors[name].extend(error_list) else: form.errors[name] = error_list raise NodeActionError(self._format_errors_as_json(form.errors))