Пример #1
0
    def test_cache_set_creation_with_block_device(self):
        node = factory.make_Node()
        cache_device = factory.make_PhysicalBlockDevice(node=node)
        form = CreateCacheSetForm(node=node,
                                  data={"cache_device": cache_device.id})

        self.assertTrue(form.is_valid(), form.errors)
        cache_set = form.save()
        self.assertEqual(cache_device, cache_set.get_device())
Пример #2
0
    def test_required_fields(self):
        node = factory.make_Node()
        form = CreateCacheSetForm(node=node, data={})

        self.assertFalse(form.is_valid(), form.errors)
        self.assertEqual(
            form.errors["__all__"],
            ["Either cache_device or cache_partition must be specified."],
        )
Пример #3
0
    def test_cache_set_creation_with_boot_disk(self):
        node = factory.make_Node(with_boot_disk=False)
        boot_disk = factory.make_PhysicalBlockDevice(node=node)
        form = CreateCacheSetForm(node=node,
                                  data={"cache_device": boot_disk.id})

        self.assertTrue(form.is_valid(), form.errors)
        cache_set = form.save()
        boot_partition = boot_disk.get_partitiontable().partitions.first()
        self.assertEqual(boot_partition, cache_set.get_device())
Пример #4
0
    def test_required_fields(self):
        node = factory.make_Node()
        form = CreateCacheSetForm(node=node, data={})

        self.assertFalse(form.is_valid(), form.errors)
        self.assertDictContainsSubset(
            {
                '__all__':
                ['Either cache_device or cache_partition must be specified.']
            }, form.errors)
Пример #5
0
    def test_cache_set_creation_with_partition(self):
        node = factory.make_Node()
        block_device = factory.make_PhysicalBlockDevice(node=node)
        partition_table = factory.make_PartitionTable(
            block_device=block_device)
        partition = factory.make_Partition(partition_table=partition_table)
        form = CreateCacheSetForm(node=node,
                                  data={"cache_partition": partition.id})

        self.assertTrue(form.is_valid(), form.errors)
        cache_set = form.save()
        self.assertEqual(partition, cache_set.get_device())
Пример #6
0
 def test_choices_are_being_populated_correctly(self):
     node = factory.make_Node(with_boot_disk=False)
     # Make 10 block devices.
     bds = [
         factory.make_PhysicalBlockDevice(node=node, bootable=True)
         for _ in range(10)
     ]
     # Partition the last 5 devices with a single partition.
     partitions = [
         factory.make_PartitionTable(block_device=bd).add_partition()
         for bd in bds[5:]
     ]
     partition_choices = [partition.id for partition in partitions
                          ] + [partition.name for partition in partitions]
     # Get the IDs of the non-partitioned devices.
     block_devices = [
         bd.id for bd in bds if bd.get_partitiontable() is None
     ] + [bd.name for bd in bds if bd.get_partitiontable() is None]
     form = CreateCacheSetForm(node=node, data={})
     self.assertItemsEqual(
         block_devices,
         [k for (k, v) in form.fields["cache_device"].choices],
     )
     self.assertItemsEqual(
         partition_choices,
         [k for (k, v) in form.fields["cache_partition"].choices],
     )
Пример #7
0
    def create(self, request, system_id):
        """@description-title Creates a bcache cache set
        @description Creates a bcache cache set.

        Note: specifying both a cache_device and a cache_partition is not
        allowed.

        @param (string) "{system_id}" [required=true] A machine system_id.

        @param (string) "cache_device" [required=false] Cache block device.

        @param (string) "cache_partition" [required=false]  Cache partition.

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

        @error (http-status-code) "404" 404
        @error (content) "not-found" The requested machine 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.
        """
        machine = Machine.objects.get_node_or_404(system_id, request.user,
                                                  NodePermission.admin)
        if machine.status != NODE_STATUS.READY:
            raise NodeStateViolation(
                "Cannot create cache set because the node is not Ready.")
        form = CreateCacheSetForm(machine, data=request.data)
        if form.is_valid():
            create_audit_event(
                EVENT_TYPES.NODE,
                ENDPOINT.API,
                request,
                system_id,
                "Created bcache cache set.",
            )
            return form.save()
        else:
            raise MAASAPIValidationError(form.errors)
Пример #8
0
    def create_cache_set(self, params):
        """Create a cache set."""
        node = self._get_node_or_permission_error(params)
        block_id = params.get('block_id')
        partition_id = params.get('partition_id')

        data = {}
        if partition_id is not None:
            data["cache_partition"] = partition_id
        elif block_id is not None:
            data["cache_device"] = block_id
        else:
            raise HandlerError("Either block_id or partition_id is required.")

        form = CreateCacheSetForm(node=node, data=data)
        if not form.is_valid():
            raise HandlerError(form.errors)
        else:
            form.save()
Пример #9
0
    def test_bcache_creation_fails_with_both_set(self):
        node = factory.make_Node()
        cache_device = factory.make_PhysicalBlockDevice(node=node)
        block_device = factory.make_PhysicalBlockDevice(node=node)
        partition_table = factory.make_PartitionTable(
            block_device=block_device)
        partition = factory.make_Partition(partition_table=partition_table)
        form = CreateCacheSetForm(node=node,
                                  data={
                                      'cache_device': cache_device.id,
                                      'cache_partition': partition.id,
                                  })

        self.assertFalse(form.is_valid(), form.errors)
        self.assertDictContainsSubset(
            {
                '__all__':
                ['Cannot set both cache_device and cache_partition.'],
            }, form.errors)
Пример #10
0
    def test_bcache_creation_fails_with_both_set(self):
        node = factory.make_Node()
        cache_device = factory.make_PhysicalBlockDevice(node=node)
        block_device = factory.make_PhysicalBlockDevice(node=node)
        partition_table = factory.make_PartitionTable(
            block_device=block_device)
        partition = factory.make_Partition(partition_table=partition_table)
        form = CreateCacheSetForm(
            node=node,
            data={
                "cache_device": cache_device.id,
                "cache_partition": partition.id,
            },
        )

        self.assertFalse(form.is_valid(), form.errors)
        self.assertEqual(
            form.errors["__all__"],
            ["Cannot set both cache_device and cache_partition."],
        )
Пример #11
0
    def create(self, request, system_id):
        """Creates a Bcache Cache Set.

        :param cache_device: Cache block device.
        :param cache_partition: Cache partition.

        Specifying both a cache_device and a cache_partition is not allowed.

        Returns 404 if the machine is not found.
        Returns 409 if the machine is not Ready.
        """
        machine = Machine.objects.get_node_or_404(system_id, request.user,
                                                  NODE_PERMISSION.ADMIN)
        if machine.status != NODE_STATUS.READY:
            raise NodeStateViolation(
                "Cannot create cache set because the node is not Ready.")
        form = CreateCacheSetForm(machine, data=request.data)
        if form.is_valid():
            return form.save()
        else:
            raise MAASAPIValidationError(form.errors)
Пример #12
0
    def create_cache_set(self, params):
        """Create a cache set."""
        # Only admin users can perform delete.
        if not reload_object(self.user).is_superuser:
            raise HandlerPermissionError()

        node = self.get_object(params)
        block_id = params.get('block_id')
        partition_id = params.get('partition_id')

        data = {}
        if partition_id is not None:
            data["cache_partition"] = partition_id
        elif block_id is not None:
            data["cache_device"] = block_id
        else:
            raise HandlerError("Either block_id or partition_id is required.")

        form = CreateCacheSetForm(node=node, data=data)
        if not form.is_valid():
            raise HandlerError(form.errors)
        else:
            form.save()
Пример #13
0
    def create(self, request, system_id):
        """Creates a bcache Cache Set.

        :param cache_device: Cache block device.
        :param cache_partition: Cache partition.

        Specifying both a cache_device and a cache_partition is not allowed.

        Returns 404 if the machine is not found.
        Returns 409 if the machine is not Ready.
        """
        machine = Machine.objects.get_node_or_404(system_id, request.user,
                                                  NodePermission.admin)
        if machine.status != NODE_STATUS.READY:
            raise NodeStateViolation(
                "Cannot create cache set because the node is not Ready.")
        form = CreateCacheSetForm(machine, data=request.data)
        if form.is_valid():
            create_audit_event(EVENT_TYPES.NODE, ENDPOINT.API, request,
                               system_id, "Created bcache cache set.")
            return form.save()
        else:
            raise MAASAPIValidationError(form.errors)