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)
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"))
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, )
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)
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)
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)
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."])
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."])
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)
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)
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)
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."])
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)
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." ], )
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))