示例#1
0
 def test_bootable_is_set_on_partition(self):
     block_device = factory.make_PhysicalBlockDevice()
     data = {"size": MIN_BLOCK_DEVICE_SIZE, "bootable": True}
     form = AddPartitionForm(block_device, data=data)
     self.assertTrue(form.is_valid(), form.errors)
     partition = form.save()
     self.assertTrue(partition.bootable, "Partition should be bootable.")
示例#2
0
 def test_uuid_is_set_on_partition(self):
     block_device = factory.make_PhysicalBlockDevice()
     part_uuid = "%s" % uuid.uuid4()
     data = {"size": MIN_BLOCK_DEVICE_SIZE, "uuid": part_uuid}
     form = AddPartitionForm(block_device, data=data)
     self.assertTrue(form.is_valid(), form.errors)
     partition = form.save()
     self.assertEqual(part_uuid, partition.uuid)
示例#3
0
 def test_is_valid_if_size_a_string(self):
     block_device = factory.make_PhysicalBlockDevice()
     k_size = (MIN_BLOCK_DEVICE_SIZE // 1000) + 1
     size = "%sk" % k_size
     data = {"size": size}
     form = AddPartitionForm(block_device, data=data)
     self.assertTrue(
         form.is_valid(),
         "Should be valid because size is large enough and a string.",
     )
示例#4
0
 def test_max_possible_size_if_not_specified(self):
     block_device = factory.make_PhysicalBlockDevice()
     partition_table = factory.make_PartitionTable(
         block_device=block_device)
     first_partition = factory.make_Partition(
         partition_table=partition_table, size=10 * 1024 * 1024)
     data = {"uuid": str(uuid.uuid4())}
     form = AddPartitionForm(block_device, data=data)
     self.assertTrue(form.is_valid(), form.errors)
     partition = form.save()
     self.assertEqual(partition.size,
                      partition_table.get_size() - first_partition.size)
示例#5
0
 def test_size_rounded_down_and_placed_on_alignment_boundry(self):
     block_size = 4096
     block_device = factory.make_PhysicalBlockDevice(block_size=block_size)
     k_size = (MIN_BLOCK_DEVICE_SIZE // 1000) + 1
     size = "%sk" % k_size
     rounded_size = round_size_to_nearest_block(k_size * 1000,
                                                PARTITION_ALIGNMENT_SIZE,
                                                False)
     data = {"size": size}
     form = AddPartitionForm(block_device, data=data)
     self.assertTrue(form.is_valid(), form.errors)
     partition = form.save()
     self.assertEqual(rounded_size, partition.size)
示例#6
0
 def test_is_not_valid_if_size_greater_than_block_size(self):
     block_device = factory.make_PhysicalBlockDevice()
     data = {"size": block_device.size + 1}
     form = AddPartitionForm(block_device, data=data)
     self.assertFalse(form.is_valid(),
                      "Should be invalid because size is to large.")
     self.assertEqual(
         {
             "size": [
                 "Ensure this value is less than or equal to %s." %
                 (block_device.size)
             ]
         },
         form._errors,
     )
示例#7
0
 def test_is_not_valid_if_size_less_than_min_size(self):
     block_device = factory.make_PhysicalBlockDevice()
     data = {"size": MIN_BLOCK_DEVICE_SIZE - 1}
     form = AddPartitionForm(block_device, data=data)
     self.assertFalse(form.is_valid(),
                      "Should be invalid because size below zero.")
     self.assertEqual(
         {
             "size": [
                 "Ensure this value is greater than or equal to %s." %
                 MIN_BLOCK_DEVICE_SIZE
             ]
         },
         form._errors,
     )
示例#8
0
    def create_partition(self, params):
        """Create a partition."""
        node = self._get_node_or_permission_error(params)
        disk_obj = BlockDevice.objects.get(id=params['block_id'], node=node)
        form = AddPartitionForm(disk_obj, {
            'size': params['partition_size'],
        })
        if not form.is_valid():
            raise HandlerError(form.errors)
        else:
            partition = form.save()

        if 'fstype' in params:
            self.update_partition_filesystem(node, partition.id,
                                             params.get("fstype"),
                                             params.get("mount_point"),
                                             params.get("mount_options"))
示例#9
0
    def create_partition(self, params):
        """Create a partition."""
        node = self._get_node_or_permission_error(
            params, permission=self._meta.edit_permission)
        disk_obj = BlockDevice.objects.get(id=params["block_id"], node=node)
        form = AddPartitionForm(disk_obj, {"size": params["partition_size"]})
        if not form.is_valid():
            raise HandlerError(form.errors)
        else:
            partition = form.save()

        self._update_obj_tags(partition, params)
        if "fstype" in params:
            self.update_partition_filesystem(
                node,
                partition.id,
                params.get("fstype"),
                params.get("mount_point"),
                params.get("mount_options"),
            )
示例#10
0
    def create_partition(self, params):
        """Create a partition."""
        # Only admin users can perform delete.
        if not reload_object(self.user).is_superuser:
            raise HandlerPermissionError()

        node = self.get_object(params)
        disk_obj = BlockDevice.objects.get(id=params['block_id'], node=node)
        form = AddPartitionForm(disk_obj, {
            'size': params['partition_size'],
        })
        if not form.is_valid():
            raise HandlerError(form.errors)
        else:
            partition = form.save()

        if 'fstype' in params:
            self.update_partition_filesystem(node, partition.id,
                                             params.get("fstype"),
                                             params.get("mount_point"),
                                             params.get("mount_options"))
示例#11
0
    def create(self, request, system_id, device_id):
        """Create a partition on the block device.

        :param size: The size of the partition.
        :param uuid: UUID for the partition. Only used if the partition table
            type for the block device is GPT.
        :param bootable: If the partition should be marked bootable.

        Returns 404 if the node or the block device are not found.
        """
        device = BlockDevice.objects.get_block_device_or_404(
            system_id, device_id, request.user, NODE_PERMISSION.ADMIN)
        node = device.get_node()
        if node.status != NODE_STATUS.READY:
            raise NodeStateViolation(
                "Cannot create partition because the node is not Ready.")
        form = AddPartitionForm(device, data=request.data)
        if not form.is_valid():
            raise MAASAPIValidationError(form.errors)
        else:
            return form.save()
示例#12
0
    def create(self, request, system_id, device_id):
        """@description-title Create a partition
        @description Create a partition on a block device.

        @param (string) "{system_id}" [required=true] The system_id.
        @param (int) "{device_id}" [required=true] The block device_id.

        @param (int) "size" [required=false] The size of the partition. If not
        specified, all available space will be used.

        @param (string) "uuid" [required=false] UUID for the partition. Only
        used if the partition table type for the block device is GPT.

        @param (boolean) "bootable" [required=false] If the partition should be
        marked bootable.

        @success (http-status-code) "server-success" 200
        @success (json) "success-json" A JSON object containing the new
        partition object.
        @success-example "success-json" [exkey=partitions-create]
        placeholder text

        @error (http-status-code) "404" 404
        @error (content) "not-found" The requested machine or device is not
        found.
        @error-example "not-found"
            Not Found
        """
        device = BlockDevice.objects.get_block_device_or_404(
            system_id, device_id, request.user, NodePermission.admin)
        node = device.get_node()
        if node.status != NODE_STATUS.READY:
            raise NodeStateViolation(
                "Cannot create partition because the node is not Ready.")
        form = AddPartitionForm(device, data=request.data)
        if not form.is_valid():
            raise MAASAPIValidationError(form.errors)
        else:
            return form.save()
示例#13
0
 def test_requires_fields(self):
     form = AddPartitionForm(block_device=factory.make_BlockDevice(),
                             data={})
     self.assertFalse(form.is_valid(), form.errors)
     self.assertItemsEqual(['size'], form.errors.keys())