def test_source_destination_missing_nic(self): user = factory.make_admin() source = factory.make_Machine(with_boot_disk=False) factory.make_Interface(node=source, name="eth0") destination = factory.make_Machine( status=random.choice( [NODE_STATUS.READY, NODE_STATUS.FAILED_TESTING]), with_boot_disk=False, ) factory.make_Interface(node=destination, name="eth1") form = CloneForm( user, data={ "source": source.system_id, "destinations": [destination.system_id], "interfaces": True, }, ) self.assertFalse(form.is_valid()) self.assertEqual( { "destinations": [ "Machine 1 in the array did not validate: " "destination node physical interfaces do not match " "the source nodes physical interfaces: eth0" ] }, form.errors, )
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_source_destination_smaller_storage(self): user = factory.make_admin() source = factory.make_Machine(with_boot_disk=False) factory.make_PhysicalBlockDevice(node=source, size=8 * 1024**3, name="sda") destination = factory.make_Machine( status=random.choice( [NODE_STATUS.READY, NODE_STATUS.FAILED_TESTING]), with_boot_disk=False, ) factory.make_PhysicalBlockDevice(node=destination, size=4 * 1024**3, name="sda") form = CloneForm( user, data={ "source": source.system_id, "destinations": [destination.system_id], "storage": True, }, ) self.assertFalse(form.is_valid()) self.assertEqual( { "destinations": [ "Machine 1 in the array did not validate: " "destination boot disk(sda) is smaller than " "source boot disk(sda)" ] }, form.errors, )
def test__source_destination_match_error(self): user = factory.make_admin() source = factory.make_Machine( status=random.choice( [NODE_STATUS.READY, NODE_STATUS.FAILED_TESTING] ) ) form = CloneForm( user, data={ "source": source.system_id, "destinations": [source.system_id], "storage": True, }, ) self.assertFalse(form.is_valid()) self.assertEquals( { "destinations": [ "Machine 0 in the array did not validate: " "Source machine cannot be a destination machine." ] }, form.errors, )
def test__permission_errors(self): user = factory.make_User() 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') destination = factory.make_Machine(status=NODE_STATUS.READY, with_boot_disk=False) factory.make_PhysicalBlockDevice(node=destination, size=8 * 1024**3, name="sda") factory.make_Interface(node=destination, name='eth0') form = CloneForm(user, data={ 'source': source.system_id, 'destinations': [destination.system_id], 'storage': True, 'interfaces': True, }) self.assertFalse(form.is_valid()) self.assertEquals( { 'destinations': [ 'Machine 0 in the array did not validate: ' 'Select a valid choice. %s is not one of the available ' 'choices.' % destination.system_id ], }, form.errors)
def test__source_destination_smaller_storage(self): user = factory.make_admin() source = factory.make_Machine(with_boot_disk=False) factory.make_PhysicalBlockDevice(node=source, size=8 * 1024**3, name="sda") destination = factory.make_Machine(status=NODE_STATUS.READY, with_boot_disk=False) factory.make_PhysicalBlockDevice(node=destination, size=4 * 1024**3, name="sda") form = CloneForm(user, data={ 'source': source.system_id, 'destinations': [destination.system_id], 'storage': True, }) self.assertFalse(form.is_valid()) self.assertEquals( { 'destinations': [ 'Machine 0 in the array did not validate: ' 'destination boot disk(sda) is smaller than ' 'source boot disk(sda)' ], }, form.errors)
def test_multiple_errors(self): user = factory.make_admin() source = factory.make_Machine(status=NODE_STATUS.READY, with_boot_disk=False) factory.make_PhysicalBlockDevice(node=source, size=8 * 1024**3, name="sda") destination = factory.make_Machine(status=NODE_STATUS.READY, with_boot_disk=False) factory.make_PhysicalBlockDevice(node=destination, size=4 * 1024**3, name="sda") form = CloneForm( user, data={ "source": source.system_id, "destinations": [source.system_id, destination.system_id], "storage": True, }, ) self.assertFalse(form.is_valid()) self.assertEqual( { "destinations": [ f"Source machine {source} cannot be a destination machine.", f"{destination} is invalid: " "destination boot disk(sda) is smaller than " "source boot disk(sda)", ] }, form.errors, )
def test_permission_errors(self): user = factory.make_User() 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") destination = factory.make_Machine( status=random.choice( [NODE_STATUS.READY, NODE_STATUS.FAILED_TESTING]), with_boot_disk=False, ) factory.make_PhysicalBlockDevice(node=destination, size=8 * 1024**3, name="sda") factory.make_Interface(node=destination, name="eth0") form = CloneForm( user, data={ "source": source.system_id, "destinations": [destination.system_id], "storage": True, "interfaces": True, }, ) self.assertFalse(form.is_valid()) self.assertEqual( { "destinations": [ f"Machine 1 is invalid: Select a valid choice. {destination.system_id} is not one of the available choices." ] }, form.errors, )
def test__empty_errors(self): user = factory.make_admin() form = CloneForm(user, data={}) self.assertFalse(form.is_valid()) self.assertEquals( { 'source': ['This field is required.'], 'destinations': ['This field is required.'], '__all__': ['Either storage or interfaces must be true.'] }, form.errors)
def test_empty_errors(self): user = factory.make_admin() form = CloneForm(user, data={}) self.assertFalse(form.is_valid()) self.assertEqual( { "source": ["This field is required."], "destinations": ["This field is required."], "__all__": ["Either storage or interfaces must be true."], }, form.errors, )
def test__source_destination_match_error(self): user = factory.make_admin() source = factory.make_Machine(status=NODE_STATUS.READY) form = CloneForm(user, data={ 'source': source.system_id, 'destinations': [source.system_id], 'storage': True, }) self.assertFalse(form.is_valid()) self.assertEquals( { 'destinations': [ 'Machine 0 in the array did not validate: ' 'Source machine cannot be a destination machine.' ], }, form.errors)
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 test__source_destination_missing_nic(self): user = factory.make_admin() source = factory.make_Machine(with_boot_disk=False) factory.make_Interface(node=source, name='eth0') destination = factory.make_Machine(status=NODE_STATUS.READY, with_boot_disk=False) factory.make_Interface(node=destination, name='eth1') form = CloneForm(user, data={ 'source': source.system_id, 'destinations': [destination.system_id], 'interfaces': True, }) self.assertFalse(form.is_valid()) self.assertEquals( { 'destinations': [ 'Machine 0 in the array did not validate: ' 'destination node physical interfaces do not match ' 'the source nodes physical interfaces: eth0' ], }, form.errors)
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))