예제 #1
0
    def create_bcache(self, params):
        """Create a bcache."""
        node = self._get_node_or_permission_error(params)
        block_id = params.get('block_id')
        partition_id = params.get('partition_id')

        data = {
            "name": params["name"],
            "cache_set": params["cache_set"],
            "cache_mode": params["cache_mode"],
        }

        if partition_id is not None:
            data["backing_partition"] = partition_id
        elif block_id is not None:
            data["backing_device"] = block_id
        else:
            raise HandlerError("Either block_id or partition_id is required.")

        form = CreateBcacheForm(node=node, data=data)
        if not form.is_valid():
            raise HandlerError(form.errors)
        else:
            bcache = form.save()

        self._update_obj_tags(bcache.virtual_device, params)
        if 'fstype' in params:
            self.update_blockdevice_filesystem(bcache.virtual_device,
                                               params.get("fstype"),
                                               params.get("mount_point"),
                                               params.get("mount_options"))
예제 #2
0
    def test_bcache_creation_with_invalid_names_fails(self):
        node = factory.make_Node()
        uuid = str(uuid4())
        form = CreateBcacheForm(
            node=node,
            data={
                "name": "bcache0",
                "uuid": uuid,
                "cache_set": "sdapart1",
                "backing_partition": "sda-partXD",
                "cache_mode": CACHE_MODE_TYPE.WRITEBACK,
            },
        )

        self.assertFalse(form.is_valid(), form.errors)
        self.assertEqual(
            {
                "cache_set": [
                    "Select a valid choice. sdapart1 is not one of the "
                    "available choices."
                ],
                "backing_partition": [
                    "Select a valid choice. sda-partXD is not one of the "
                    "available choices."
                ],
                "__all__": ["Bcache requires a cache_set."],
            },
            form.errors,
        )
예제 #3
0
    def test_bcache_creation_with_names(self):
        node = factory.make_Node()
        backing_size = 10 * 1000**4
        cache_set = factory.make_CacheSet(node=node)
        backing_device = factory.make_PhysicalBlockDevice(node=node,
                                                          size=backing_size)
        backing_partition_table = factory.make_PartitionTable(
            block_device=backing_device)
        backing_partition = backing_partition_table.add_partition()
        uuid = str(uuid4())
        form = CreateBcacheForm(
            node=node,
            data={
                "name": "bcache0",
                "uuid": uuid,
                "cache_set": cache_set.name,
                "backing_partition": backing_partition.name,
                "cache_mode": CACHE_MODE_TYPE.WRITEBACK,
            },
        )

        self.assertTrue(form.is_valid(), form.errors)
        bcache = form.save()
        self.assertEqual("bcache0", bcache.name)
        self.assertEqual(uuid, bcache.uuid)
        self.assertEqual(cache_set, bcache.cache_set)
        self.assertEqual(
            backing_partition.get_effective_filesystem(),
            bcache.filesystems.get(fstype=FILESYSTEM_TYPE.BCACHE_BACKING),
        )
        self.assertEqual(FILESYSTEM_GROUP_TYPE.BCACHE, bcache.group_type)
예제 #4
0
    def test_required_fields(self):
        node = factory.make_Node()
        form = CreateBcacheForm(node=node, data={})

        self.assertFalse(form.is_valid(), form.errors)
        self.assertDictContainsSubset(
            {"cache_mode": ["This field is required."]}, form.errors)
예제 #5
0
파일: test_bcache.py 프로젝트: zhangrb/maas
    def test_bcache_creation_on_save(self):
        node = factory.make_Node()
        backing_size = 10 * 1000**4
        cache_set = factory.make_CacheSet(node=node)
        backing_device = factory.make_PhysicalBlockDevice(node=node,
                                                          size=backing_size)
        uuid = str(uuid4())
        form = CreateBcacheForm(node=node,
                                data={
                                    'name': 'bcache0',
                                    'uuid': uuid,
                                    'cache_set': cache_set.id,
                                    'backing_device': backing_device.id,
                                    'cache_mode': CACHE_MODE_TYPE.WRITEBACK,
                                })

        self.assertTrue(form.is_valid(), form.errors)
        bcache = form.save()
        self.assertEqual('bcache0', bcache.name)
        self.assertEqual(uuid, bcache.uuid)
        self.assertEqual(cache_set, bcache.cache_set)
        self.assertEqual(
            backing_device.get_effective_filesystem(),
            bcache.filesystems.get(fstype=FILESYSTEM_TYPE.BCACHE_BACKING))
        self.assertEqual(backing_size, bcache.get_size())
        self.assertEqual(FILESYSTEM_GROUP_TYPE.BCACHE, bcache.group_type)
예제 #6
0
파일: bcache.py 프로젝트: jamal-fuma/maas
    def create(self, request, system_id):
        """Creates a Bcache.

        :param name: Name of the Bcache.
        :param uuid: UUID of the Bcache.
        :param cache_set: Cache set.
        :param backing_device: Backing block device.
        :param backing_partition: Backing partition.
        :param cache_mode: Cache mode (WRITEBACK, WRITETHROUGH, WRITEAROUND).

        Specifying both a device and a partition for a given role (cache or
        backing) 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 Bcache because the machine is not Ready.")
        form = CreateBcacheForm(machine, data=request.data)
        if form.is_valid():
            create_audit_event(
                EVENT_TYPES.NODE, ENDPOINT.API, request,
                system_id, "Created bcache.")
            return form.save()
        else:
            raise MAASAPIValidationError(form.errors)
예제 #7
0
    def test_bcache_creation_without_storage_fails(self):
        node = factory.make_Node()
        form = CreateBcacheForm(
            node=node, data={"cache_mode": CACHE_MODE_TYPE.WRITEAROUND})

        self.assertFalse(form.is_valid(), form.errors)
        self.assertEqual(form.errors["cache_set"], ["This field is required."])
예제 #8
0
    def test_required_fields(self):
        node = factory.make_Node()
        form = CreateBcacheForm(node=node, data={})

        self.assertFalse(form.is_valid(), form.errors)
        self.assertEqual(form.errors["cache_mode"],
                         ["This field is required."])
예제 #9
0
파일: test_bcache.py 프로젝트: zhangrb/maas
    def test_bcache_creation_without_storage_fails(self):
        node = factory.make_Node()
        form = CreateBcacheForm(
            node=node, data={'cache_mode': CACHE_MODE_TYPE.WRITEAROUND})

        self.assertFalse(form.is_valid(), form.errors)
        self.assertDictContainsSubset(
            {'cache_set': ['This field is required.']}, form.errors)
예제 #10
0
파일: bcache.py 프로젝트: th3architect/maas
    def create(self, request, system_id):
        """@description-title Creates a bcache
        @description Creates a bcache.

        Specifying both a device and a partition for a given role (cache or
        backing) is not allowed.

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

        @param (string) "name" [required=false] Name of the Bcache.

        @param (string) "uuid" [required=false] UUID of the Bcache.

        @param (string) "cache_set" [required=false] Cache set.

        @param (string) "backing_device" [required=false] Backing block device.

        @param (string) "backing_partition" [required=false] Backing partition.

        @param (string) "cache_mode" [required=false] Cache mode:
        ``WRITEBACK``, ``WRITETHROUGH``, ``WRITEAROUND``.

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

        @error (http-status-code) "404" 404
        @error (content) "not-found" The requested system_id 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 Bcache because the machine is not Ready.")
        form = CreateBcacheForm(machine, data=request.data)
        if form.is_valid():
            create_audit_event(
                EVENT_TYPES.NODE,
                ENDPOINT.API,
                request,
                system_id,
                "Created bcache.",
            )
            return form.save()
        else:
            raise MAASAPIValidationError(form.errors)
예제 #11
0
파일: test_bcache.py 프로젝트: zhangrb/maas
    def test_bcache_creation_without_cache_set_fails(self):
        node = factory.make_Node()
        backing_size = 10 * 1000**4
        backing_device = factory.make_PhysicalBlockDevice(node=node,
                                                          size=backing_size)
        form = CreateBcacheForm(node=node,
                                data={
                                    'cache_mode': CACHE_MODE_TYPE.WRITEAROUND,
                                    'backing_device': backing_device.id
                                })

        self.assertFalse(form.is_valid(), form.errors)
        self.assertDictContainsSubset(
            {'cache_set': ['This field is required.']}, form.errors)
예제 #12
0
    def test_bcache_creation_without_cache_set_fails(self):
        node = factory.make_Node()
        backing_size = 10 * 1000**4
        backing_device = factory.make_PhysicalBlockDevice(node=node,
                                                          size=backing_size)
        form = CreateBcacheForm(
            node=node,
            data={
                "cache_mode": CACHE_MODE_TYPE.WRITEAROUND,
                "backing_device": backing_device.id,
            },
        )

        self.assertFalse(form.is_valid(), form.errors)
        self.assertEqual(form.errors["cache_set"], ["This field is required."])
예제 #13
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, size=10 * 1000**4)
         for _ in range(10)
     ]
     # Make 3 cache sets.
     cache_sets = [factory.make_CacheSet(node=node) for _ in range(3)]
     cache_set_choices = [cache_set.id for cache_set in cache_sets
                          ] + [cache_set.name for cache_set in cache_sets]
     # 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 = CreateBcacheForm(node=node, data={})
     self.assertItemsEqual(
         cache_set_choices,
         [k for (k, v) in form.fields["cache_set"].choices],
     )
     self.assertItemsEqual(
         block_devices,
         [k for (k, v) in form.fields["backing_device"].choices],
     )
     self.assertItemsEqual(
         partition_choices,
         [k for (k, v) in form.fields["backing_partition"].choices],
     )
예제 #14
0
파일: test_bcache.py 프로젝트: zhangrb/maas
    def test_bcache_creation_without_backing_fails(self):
        node = factory.make_Node()
        cache_set = factory.make_CacheSet(node=node)
        form = CreateBcacheForm(node=node,
                                data={
                                    'cache_mode': CACHE_MODE_TYPE.WRITEAROUND,
                                    'cache_set': cache_set.id
                                })

        self.assertFalse(form.is_valid(), form.errors)
        self.assertDictContainsSubset(
            {
                '__all__': [
                    'Either backing_device or backing_partition must be '
                    'specified.'
                ]
            }, form.errors)
예제 #15
0
    def test_bcache_creation_without_backing_fails(self):
        node = factory.make_Node()
        cache_set = factory.make_CacheSet(node=node)
        form = CreateBcacheForm(
            node=node,
            data={
                "cache_mode": CACHE_MODE_TYPE.WRITEAROUND,
                "cache_set": cache_set.id,
            },
        )

        self.assertFalse(form.is_valid(), form.errors)
        self.assertEqual(
            form.errors["__all__"],
            [
                "Either backing_device or backing_partition must be "
                "specified."
            ],
        )
예제 #16
0
파일: test_bcache.py 프로젝트: zhangrb/maas
    def test_bcache_creation_on_boot_disk(self):
        node = factory.make_Node(with_boot_disk=False)
        boot_disk = factory.make_PhysicalBlockDevice(node=node)
        cache_set = factory.make_CacheSet(node=node)
        form = CreateBcacheForm(node=node,
                                data={
                                    'name': 'bcache0',
                                    'cache_set': cache_set.id,
                                    'backing_device': boot_disk.id,
                                    'cache_mode': CACHE_MODE_TYPE.WRITEBACK,
                                })

        self.assertTrue(form.is_valid(), form.errors)
        bcache = form.save()
        self.assertEqual('bcache0', bcache.name)
        self.assertEqual(cache_set, bcache.cache_set)
        self.assertEqual(FILESYSTEM_GROUP_TYPE.BCACHE, bcache.group_type)
        boot_partition = (boot_disk.get_partitiontable().partitions.first())
        self.assertEqual(
            boot_partition.get_effective_filesystem(),
            bcache.filesystems.get(fstype=FILESYSTEM_TYPE.BCACHE_BACKING))