def mount(self, request, system_id, device_id, id): """Mount the filesystem on partition. :param mount_point: Path on the filesystem to mount. :param mount_options: Options to pass to mount(8). Returns 403 when the user doesn't have the ability to mount the \ partition. Returns 404 if the node, block device, or partition is not found. """ device = BlockDevice.objects.get_block_device_or_404( system_id, device_id, request.user, NODE_PERMISSION.EDIT) partition_table = get_object_or_404( PartitionTable, block_device=device) partition = get_partition_by_id_or_name__or_404( id, partition_table) raise_error_for_invalid_state_on_allocated_operations( device.get_node(), request.user, "mount") filesystem = partition.get_effective_filesystem() form = MountFilesystemForm(filesystem, data=request.data) if form.is_valid(): form.save() return partition else: raise MAASAPIValidationError(form.errors)
def test_ignores_mount_point_when_fs_does_not_use_mount_point(self): substrate = self.make_substrate() filesystem = factory.make_Filesystem(fstype=FILESYSTEM_TYPE.SWAP, **substrate) form = MountFilesystemForm(filesystem, data={}) self.assertFalse(filesystem.uses_mount_point) self.assertTrue(form.is_valid(), form.errors)
def test_requires_mount_point_when_fs_uses_mount_point(self): substrate = self.make_substrate() filesystem = factory.make_Filesystem(fstype=FILESYSTEM_TYPE.EXT4, **substrate) form = MountFilesystemForm(filesystem, data={}) self.assertTrue(filesystem.uses_mount_point) self.assertFalse(form.is_valid(), form.errors) self.assertItemsEqual(['mount_point'], form.errors.keys())
def test_is_not_valid_if_absolute_path_empty(self): substrate = self.make_substrate() filesystem = factory.make_Filesystem(fstype=FILESYSTEM_TYPE.EXT4, **substrate) data = {'mount_point': ""} form = MountFilesystemForm(filesystem, data=data) self.assertFalse( form.is_valid(), "Should be invalid because its not an absolute path.") self.assertEqual({'mount_point': ["This field is required."]}, form._errors)
def test_is_not_valid_if_invalid_absolute_path(self): substrate = self.make_substrate() filesystem = factory.make_Filesystem(fstype=FILESYSTEM_TYPE.EXT4, **substrate) data = {'mount_point': factory.make_absolute_path()[1:]} form = MountFilesystemForm(filesystem, data=data) self.assertFalse( form.is_valid(), "Should be invalid because it's not an absolute path.") self.assertEqual({'mount_point': ["Enter a valid value."]}, form._errors)
def test_is_not_valid_if_there_is_no_filesystem(self): data = {'mount_point': factory.make_absolute_path()} form = MountFilesystemForm(None, data=data) self.assertFalse( form.is_valid(), "Should be invalid because block device does " "not have a filesystem.") self.assertEqual( { '__all__': [ "Cannot mount an unformatted partition or block device.", ] }, form._errors)
def test_sets_mount_point_to_none_and_options_on_swap(self): substrate = self.make_substrate() filesystem = factory.make_Filesystem(fstype=FILESYSTEM_TYPE.SWAP, **substrate) self.assertThat(filesystem.is_mounted, Is(False)) mount_options = factory.make_name("options") data = {'mount_options': mount_options} form = MountFilesystemForm(filesystem, data=data) self.assertTrue(form.is_valid(), form._errors) form.save() self.assertEqual("none", filesystem.mount_point) self.assertEqual(mount_options, filesystem.mount_options) self.assertThat(filesystem.is_mounted, Is(True))
def test_is_not_valid_if_invalid_absolute_path_too_long(self): substrate = self.make_substrate() filesystem = factory.make_Filesystem(fstype=FILESYSTEM_TYPE.EXT4, **substrate) mount_point = factory.make_absolute_path(directory_length=4096) data = {'mount_point': mount_point} form = MountFilesystemForm(filesystem, data=data) self.assertFalse( form.is_valid(), "Should be invalid because its not an absolute path.") self.assertEqual( { 'mount_point': [ "Ensure this value has at most 4095 characters " "(it has %s)." % len(mount_point) ], }, form._errors)
def mount(self, request, system_id, device_id, id): """@description-title Mount a filesystem @description Mount a filesystem on machine system_id, device device_id and partition id. @param (string) "{system_id}" [required=true] The system_id. @param (int) "{device_id}" [required=true] The block device_id. @param (int) "{id}" [required=true] The partition id. @param (string) "mount_point" [required=true] Path on the filesystem to mount. @param (string) "mount_options" [required=false] Options to pass to mount(8). @success (http-status-code) "server-success" 200 @success (json) "success-json" A JSON object containing the updated partition object. @success-example "success-json" [exkey=partitions-mount] placeholder text @error (http-status-code) "403" 403 @error (content) "no-perms" The user does not have permissions to mount the filesystem. @error (http-status-code) "404" 404 @error (content) "not-found" The requested machine, device or partition is not found. @error-example "not-found" Not Found """ device = BlockDevice.objects.get_block_device_or_404( system_id, device_id, request.user, NodePermission.edit) partition_table = get_object_or_404(PartitionTable, block_device=device) partition = get_partition_by_id_or_name__or_404(id, partition_table) raise_error_for_invalid_state_on_allocated_operations( device.get_node(), request.user, "mount") filesystem = partition.get_effective_filesystem() form = MountFilesystemForm(filesystem, data=request.data) if form.is_valid(): form.save() return partition else: raise MAASAPIValidationError(form.errors)
def test_sets_mount_point_and_options_on_filesystem(self): substrate = self.make_substrate() filesystem = factory.make_Filesystem(fstype=FILESYSTEM_TYPE.EXT4, **substrate) self.assertThat(filesystem.is_mounted, Is(False)) mount_point = factory.make_absolute_path() mount_options = factory.make_name("options") data = { 'mount_point': mount_point, # Whitespace is stripped by form validation. 'mount_options': " " + mount_options + "\t\n", } form = MountFilesystemForm(filesystem, data=data) self.assertTrue(form.is_valid(), form._errors) form.save() self.assertEqual(mount_point, filesystem.mount_point) self.assertEqual(mount_options, filesystem.mount_options) self.assertThat(filesystem.is_mounted, Is(True))
def test_is_not_valid_if_substrate_in_filesystem_group(self): substrate = self.make_substrate() filesystem = factory.make_Filesystem(fstype=FILESYSTEM_TYPE.LVM_PV, **substrate) factory.make_FilesystemGroup(group_type=FILESYSTEM_GROUP_TYPE.LVM_VG, filesystems=[filesystem]) data = {'mount_point': factory.make_absolute_path()} form = MountFilesystemForm(filesystem, data=data) self.assertFalse( form.is_valid(), "Should be invalid because block device is in a filesystem group.") self.assertEqual( { '__all__': [ "Filesystem is part of a filesystem group, and cannot be " "mounted.", ] }, form._errors)
def mount(self, request, system_id, id): """Mount the filesystem on block device. :param mount_point: Path on the filesystem to mount. :param mount_options: Options to pass to mount(8). Returns 403 when the user doesn't have the ability to mount 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) raise_error_for_invalid_state_on_allocated_operations( device.get_node(), request.user, "mount") filesystem = device.get_effective_filesystem() form = MountFilesystemForm(filesystem, data=request.data) if form.is_valid(): form.save() return device else: raise MAASAPIValidationError(form.errors)