def test_is_not_valid_if_invalid_uuid(self): fstype = factory.pick_filesystem_type() block_device = factory.make_PhysicalBlockDevice() data = {"fstype": fstype, "uuid": factory.make_string(size=32)} form = FormatBlockDeviceForm(block_device, data=data) self.assertFalse(form.is_valid(), "Should be invalid because of an invalid uuid.") self.assertEqual({"uuid": ["Enter a valid value."]}, form._errors)
def update_blockdevice_filesystem(self, blockdevice, fstype, mount_point, mount_options): fs = blockdevice.get_effective_filesystem() if not fstype: if fs: fs.delete() return if fs is None or fstype != fs.fstype: form = FormatBlockDeviceForm(blockdevice, {'fstype': fstype}) if not form.is_valid(): raise HandlerError(form.errors) form.save() fs = blockdevice.get_effective_filesystem() if mount_point != fs.mount_point: # XXX: Elsewhere, a mount_point of "" would somtimes mean that the # filesystem is mounted, sometimes that it is *not* mounted. Which # is correct was not clear from the code history, so the existing # behaviour is maintained here. if mount_point is None or mount_point == "": fs.mount_point = None fs.mount_options = None fs.save() else: form = MountFilesystemForm( blockdevice.get_effective_filesystem(), { 'mount_point': mount_point, 'mount_options': mount_options }) if not form.is_valid(): raise HandlerError(form.errors) else: form.save()
def test_is_not_valid_if_invalid_uuid_prepend_XYZ(self): fstype = factory.pick_filesystem_type() block_device = factory.make_PhysicalBlockDevice() data = { 'fstype': fstype, 'uuid': "XYZaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", } form = FormatBlockDeviceForm(block_device, data=data) self.assertFalse( form.is_valid(), "Should be invalid because of an invalid uuid.") self.assertEqual({'uuid': ["Enter a valid value."]}, form._errors)
def test_creates_filesystem(self): fsuuid = "%s" % uuid.uuid4() fstype = factory.pick_filesystem_type() block_device = factory.make_PhysicalBlockDevice() data = {"uuid": fsuuid, "fstype": fstype} form = FormatBlockDeviceForm(block_device, data=data) self.assertTrue(form.is_valid(), form._errors) form.save() filesystem = get_one( Filesystem.objects.filter(block_device=block_device)) self.assertIsNotNone(filesystem) self.assertEqual(fstype, filesystem.fstype) self.assertEqual(fsuuid, filesystem.uuid)
def test_is_not_valid_if_invalid_format_fstype(self): block_device = factory.make_PhysicalBlockDevice() data = {"fstype": FILESYSTEM_TYPE.LVM_PV} form = FormatBlockDeviceForm(block_device, data=data) self.assertFalse(form.is_valid(), "Should be invalid because of an invalid fstype.") self.assertEqual( { "fstype": [ "Select a valid choice. lvm-pv is not one of the " "available choices." ] }, form._errors, )
def test_is_not_valid_if_non_user_format_fstype(self): block_device = factory.make_PhysicalBlockDevice() factory.make_Filesystem(fstype="bcache-backing", block_device=block_device) data = {"fstype": FILESYSTEM_TYPE.EXT4} form = FormatBlockDeviceForm(block_device, data=data) self.assertRaises(ValidationError, form.save)
def test_is_not_valid_if_block_device_has_partition_table(self): fstype = factory.pick_filesystem_type() block_device = factory.make_PhysicalBlockDevice() factory.make_PartitionTable(block_device=block_device) data = { 'fstype': fstype, } form = FormatBlockDeviceForm(block_device, data=data) self.assertFalse( form.is_valid(), "Should be invalid because block device has a partition table.") self.assertEqual({ '__all__': [ "Cannot format block device with a partition table.", ]}, form._errors)
def test_deletes_old_filesystem_and_creates_new_one(self): fstype = factory.pick_filesystem_type() block_device = factory.make_PhysicalBlockDevice() prev_filesystem = factory.make_Filesystem(block_device=block_device) data = {"fstype": fstype} form = FormatBlockDeviceForm(block_device, data=data) self.assertTrue(form.is_valid(), form._errors) form.save() self.assertEqual( 1, Filesystem.objects.filter(block_device=block_device).count(), "Should only be one filesystem that exists for block device.", ) self.assertIsNone(reload_object(prev_filesystem)) filesystem = get_one( Filesystem.objects.filter(block_device=block_device)) self.assertIsNotNone(filesystem) self.assertEqual(fstype, filesystem.fstype)
def format(self, request, system_id, id): """Format block device with filesystem. :param fstype: Type of filesystem. :param uuid: UUID of the filesystem. Returns 403 when the user doesn't have the ability to format the \ block device. Returns 404 if the machine or block device is not found. Returns 409 if the machine is not Ready or Allocated. """ device = BlockDevice.objects.get_block_device_or_404( system_id, id, request.user, NODE_PERMISSION.EDIT) node = device.get_node() raise_error_for_invalid_state_on_allocated_operations( node, request.user, "format") form = FormatBlockDeviceForm(device, data=request.data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def format(self, request, system_id, id): """@description-title Format block device @description Format block device with filesystem. @param (string) "{system_id}" [required=true] The machine system_id. @param (string) "{id}" [required=true] The block device's id. @param (string) "fstype" [required=true] Type of filesystem. @param (string) "uuid" [required=false] UUID of the filesystem. @success (http-status-code) "server-success" 200 @success (json) "success-json" A JSON object containing the updated block device. @success-example "success-json" [exkey=block-devs-format] placeholder text @error (http-status-code) "403" 403 @error (content) "no-perms" The user does not have permissions to format the block device. @error (http-status-code) "404" 404 @error (content) "not-found" The requested machine or block device is not found. @error-example "not-found" Not Found @error (http-status-code) "409" 409 @error (content) "not-ready" The requested machine is not ready. """ device = BlockDevice.objects.get_block_device_or_404( system_id, id, request.user, NodePermission.edit) node = device.get_node() raise_error_for_invalid_state_on_allocated_operations( node, request.user, "format") form = FormatBlockDeviceForm(device, data=request.data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def test_requires_fields(self): form = FormatBlockDeviceForm(block_device=factory.make_BlockDevice(), data={}) self.assertFalse(form.is_valid(), form.errors) self.assertItemsEqual(["fstype"], form.errors.keys())