def test_creates_with_partitions(self): node = make_Node_with_VMFS6_layout() block_device = factory.make_PhysicalBlockDevice( node=node, size=(MIN_BLOCK_DEVICE_SIZE * 3) + PARTITION_TABLE_EXTRA_SPACE) partition_table = factory.make_PartitionTable( block_device=block_device) partitions = [ partition_table.add_partition(size=MIN_BLOCK_DEVICE_SIZE) for _ in range(2) ] partition_ids = [ partition.id for partition in partitions ] data = { 'name': factory.make_name('name'), 'partitions': partition_ids, } form = CreateVMFSForm(node, data=data) self.assertTrue(form.is_valid(), form._errors) vmfs = form.save() self.assertItemsEqual( partition_ids, [fs.get_parent().id for fs in vmfs.filesystems.all()])
def test_creates_with_partitions_by_name(self): node = make_Node_with_VMFS6_layout() block_device = factory.make_PhysicalBlockDevice( node=node, size=(MIN_BLOCK_DEVICE_SIZE * 3) + PARTITION_TABLE_EXTRA_SPACE) partition_table = factory.make_PartitionTable( block_device=block_device) partitions = [ partition_table.add_partition(size=MIN_BLOCK_DEVICE_SIZE) for _ in range(2) ] partition_names = [ partition.name for partition in partitions ] data = { 'name': factory.make_name('name'), 'partitions': partition_names, } form = CreateVMFSForm(node, data=data) self.assertTrue(form.is_valid(), form._errors) volume_group = form.save() partitions_in_vg = [ filesystem.partition for filesystem in volume_group.filesystems.all() ] self.assertItemsEqual(partitions, partitions_in_vg)
def create_vmfs_datastore(self, params): """Create a VMFS datastore.""" node = self._get_node_or_permission_error( params, permission=self._meta.edit_permission) form = CreateVMFSForm(node, data=params) if not form.is_valid(): raise HandlerError(form.errors) else: form.save()
def test_is_not_valid_if_vmfs_layout_is_not_applied(self): node = factory.make_Node(with_boot_disk=False) block_device = factory.make_PhysicalBlockDevice(node=node) data = { 'name': factory.make_name('name'), 'block_devices': [block_device.id], } form = CreateVMFSForm(node, data=data) self.assertFalse(form.is_valid()) self.assertIn('VMFS6', form.errors)
def test_is_not_valid_if_invalid_uuid(self): node = make_Node_with_VMFS6_layout() block_device = factory.make_PhysicalBlockDevice(node=node) data = { "name": factory.make_name("name"), "uuid": factory.make_string(size=32), "block_devices": [block_device.id], } form = CreateVMFSForm(node, data=data) self.assertFalse(form.is_valid(), "Should be invalid because of an invalid uuid.") self.assertEqual({"uuid": ["Enter a valid value."]}, form._errors)
def test_is_not_valid_missing_block_devices_and_partitions(self): node = make_Node_with_VMFS6_layout() data = { 'name': factory.make_name('name'), 'uuid': uuid.uuid4(), } form = CreateVMFSForm(node, data=data) self.assertFalse( form.is_valid(), 'Should be invalid because of missing block_devices and ' 'partitions.') self.assertEqual({ '__all__': [ 'At least one valid block device or partition is required.', ]}, form._errors)
def test_is_not_valid_missing_block_devices_and_partitions(self): node = make_Node_with_VMFS6_layout() data = {"name": factory.make_name("name"), "uuid": uuid.uuid4()} form = CreateVMFSForm(node, data=data) self.assertFalse( form.is_valid(), "Should be invalid because of missing block_devices and " "partitions.", ) self.assertEqual( { "__all__": ["At least one valid block device or partition is required."] }, form._errors, )
def create(self, request, system_id): """@description-title Create a VMFS datastore. @description Create a VMFS datastore belonging to a machine with the given system_id. Note that at least one valid block device or partition is required. @param (string) "{system_id}" [required=true] The machine system_id on which to create the VMFS datastore. @param (string) "name" [required=true] Name of the VMFS datastore. @param (string) "uuid" [required=false] (optional) UUID of the VMFS group. @param (string) "block_devices" [required=false] Block devices to add to the VMFS datastore. @param (string) "partitions" [required=false] Partitions to add to the VMFS datastore. @success (http-status-code) "server-success" 200 @success (json) "success-json" A JSON object containing the new VMFS datastore. @success-example "success-json" [exkey=vmfs-datastores-create] 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 VMFS group because the machine is not Ready." ) form = CreateVMFSForm(machine, data=request.data) if not form.is_valid(): raise MAASAPIValidationError(form.errors) else: return form.save()
def test_is_not_valid_if_partition_does_not_belong_to_node(self): node = make_Node_with_VMFS6_layout() partition = factory.make_Partition() data = { 'name': factory.make_name('name'), 'partitions': [partition.id], } form = CreateVMFSForm(node, data=data) self.assertFalse( form.is_valid(), 'Should be invalid because of partition does not ' 'belonging to node.') self.assertEqual({ 'partitions': [ 'Select a valid choice. %s is not one of the available ' 'choices.' % partition.id, ]}, form._errors)
def test_is_not_valid_if_block_device_does_not_belong_to_node(self): node = make_Node_with_VMFS6_layout() block_device = factory.make_PhysicalBlockDevice() data = { 'name': factory.make_name('name'), 'block_devices': [block_device.id], } form = CreateVMFSForm(node, data=data) self.assertFalse( form.is_valid(), 'Should be invalid because of block device does not ' 'belonging to node.') self.assertEqual({ 'block_devices': [ 'Select a valid choice. %s is not one of the available ' 'choices.' % block_device.id, ]}, form._errors)
def test_creates_volume_group_with_block_devices(self): node = make_Node_with_VMFS6_layout() block_devices = [ factory.make_PhysicalBlockDevice(node=node) for _ in range(3) ] block_device_ids = [block_device.id for block_device in block_devices] data = { "name": factory.make_name("name"), "block_devices": block_device_ids, } form = CreateVMFSForm(node, data=data) self.assertTrue(form.is_valid(), form._errors) vmfs = form.save() self.assertItemsEqual( block_device_ids, [ fs.get_parent().partition_table.block_device.id for fs in vmfs.filesystems.all() ], )
def test_is_not_valid_if_partition_does_not_belong_to_node(self): node = make_Node_with_VMFS6_layout() partition = factory.make_Partition() data = { "name": factory.make_name("name"), "partitions": [partition.id], } form = CreateVMFSForm(node, data=data) self.assertFalse( form.is_valid(), "Should be invalid because of partition does not " "belonging to node.", ) self.assertEqual( { "partitions": [ "Select a valid choice. %s is not one of the available " "choices." % partition.id ] }, form._errors, )
def test_is_not_valid_if_block_device_does_not_belong_to_node(self): node = make_Node_with_VMFS6_layout() block_device = factory.make_PhysicalBlockDevice() data = { "name": factory.make_name("name"), "block_devices": [block_device.id], } form = CreateVMFSForm(node, data=data) self.assertFalse( form.is_valid(), "Should be invalid because of block device does not " "belonging to node.", ) self.assertEqual( { "block_devices": [ "Select a valid choice. %s is not one of the available " "choices." % block_device.id ] }, form._errors, )
def test_creates_with_block_devices_by_name(self): node = make_Node_with_VMFS6_layout() block_devices = [ factory.make_PhysicalBlockDevice(node=node) for _ in range(3) ] block_device_names = [ block_device.name for block_device in block_devices ] data = { 'name': factory.make_name('name'), 'block_devices': block_device_names, } form = CreateVMFSForm(node, data=data) self.assertTrue(form.is_valid(), form._errors) vmfs = form.save() self.assertItemsEqual( [ block_device.id for block_device in block_devices ], [ fs.get_parent().partition_table.block_device.id for fs in vmfs.filesystems.all() ])
def test_requires_fields(self): node = make_Node_with_VMFS6_layout() form = CreateVMFSForm(node, data={}) self.assertFalse(form.is_valid(), form.errors) self.assertItemsEqual(['name'], form.errors.keys())