def test_format_partition_as_admin(self): self.become_admin() node = factory.make_Node(status=NODE_STATUS.READY) partition = self.make_partition(node) uri = get_partition_uri(partition) fs_uuid = str(uuid4()) fstype = factory.pick_filesystem_type() response = self.client.post( uri, { "op": "format", "uuid": fs_uuid, "fstype": fstype, "label": "mylabel", }, ) self.assertEqual( http.client.OK, response.status_code, response.content ) filesystem = json.loads( response.content.decode(settings.DEFAULT_CHARSET) )["filesystem"] self.assertEqual(fstype, filesystem["fstype"]) self.assertEqual("mylabel", filesystem["label"]) self.assertEqual(fs_uuid, filesystem["uuid"])
def test_format_formats_block_device_as_user(self): node = factory.make_Node(status=NODE_STATUS.ALLOCATED, owner=self.user) block_device = factory.make_VirtualBlockDevice(node=node) fstype = factory.pick_filesystem_type() fsuuid = "%s" % uuid.uuid4() uri = get_blockdevice_uri(block_device) response = self.client.post(uri, { "op": "format", "fstype": fstype, "uuid": fsuuid }) self.assertEqual(http.client.OK, response.status_code, response.content) parsed_device = json_load_bytes(response.content) self.assertDictContainsSubset( { "fstype": fstype, "uuid": fsuuid, "mount_point": None }, parsed_device["filesystem"], ) block_device = reload_object(block_device) self.assertIsNotNone(block_device.get_effective_filesystem())
def test_format_missing_partition(self): self.become_admin() node = factory.make_Node(status=NODE_STATUS.READY) device = factory.make_PhysicalBlockDevice( node=node, size=(MIN_PARTITION_SIZE * 4) + PARTITION_TABLE_EXTRA_SPACE, ) factory.make_PartitionTable(block_device=device) partition_id = random.randint(1, 1000) # Most likely a bogus one uri = reverse("partition_handler", args=[node.system_id, device.id, partition_id]) fs_uuid = str(uuid4()) fstype = factory.pick_filesystem_type() response = self.client.post( uri, { "op": "format", "uuid": fs_uuid, "fstype": fstype, "label": "mylabel", }, ) # Fails with a NOT_FOUND status. self.assertEqual(http.client.NOT_FOUND, response.status_code, response.content)
def test_is_not_valid_if_invalid_uuid(self): fstype = factory.pick_filesystem_type() block_device = factory.make_PhysicalBlockDevice() data = {"fstype": fstype, "uuid": factory.make_string(size=32)} form = FormatBlockDeviceForm(block_device, 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_if_invalid_uuid(self): fstype = factory.pick_filesystem_type() partition = factory.make_Partition() data = {"fstype": fstype, "uuid": factory.make_string(size=32)} form = FormatPartitionForm(partition, 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_creates_filesystem(self): fsuuid = "%s" % uuid.uuid4() fstype = factory.pick_filesystem_type() partition = factory.make_Partition() data = {"uuid": fsuuid, "fstype": fstype} form = FormatPartitionForm(partition, data=data) self.assertTrue(form.is_valid(), form._errors) form.save() filesystem = get_one(Filesystem.objects.filter(partition=partition)) self.assertIsNotNone(filesystem) self.assertEqual(fstype, filesystem.fstype) self.assertEqual(fsuuid, filesystem.uuid)
def test_is_not_valid_if_invalid_uuid_prepend_XYZ(self): fstype = factory.pick_filesystem_type() block_device = factory.make_PhysicalBlockDevice() data = { 'fstype': fstype, 'uuid': "XYZaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", } form = FormatBlockDeviceForm(block_device, 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_format_returns_403_if_ready_and_not_admin(self): node = factory.make_Node(status=NODE_STATUS.READY) block_device = factory.make_VirtualBlockDevice(node=node) fstype = factory.pick_filesystem_type() fsuuid = "%s" % uuid.uuid4() uri = get_blockdevice_uri(block_device) response = self.client.post(uri, { "op": "format", "fstype": fstype, "uuid": fsuuid }) self.assertEqual(http.client.FORBIDDEN, response.status_code, response.content)
def test_creates_filesystem(self): fsuuid = "%s" % uuid.uuid4() fstype = factory.pick_filesystem_type() block_device = factory.make_PhysicalBlockDevice() data = {"uuid": fsuuid, "fstype": fstype} form = FormatBlockDeviceForm(block_device, data=data) self.assertTrue(form.is_valid(), form._errors) form.save() filesystem = get_one( Filesystem.objects.filter(block_device=block_device)) self.assertIsNotNone(filesystem) self.assertEqual(fstype, filesystem.fstype) self.assertEqual(fsuuid, filesystem.uuid)
def test_format_returns_403_if_ready_and_not_admin(self): node = factory.make_Node(status=NODE_STATUS.READY) partition = self.make_partition(node) uri = get_partition_uri(partition) fs_uuid = str(uuid4()) fstype = factory.pick_filesystem_type() response = self.client.post(uri, { 'op': 'format', 'uuid': fs_uuid, 'fstype': fstype, 'label': 'mylabel', }) self.assertEqual( http.client.FORBIDDEN, response.status_code, response.content)
def test_format_returns_409_if_not_allocated_or_ready(self): status = factory.pick_enum( NODE_STATUS, but_not=[NODE_STATUS.READY, NODE_STATUS.ALLOCATED]) node = factory.make_Node(status=status, owner=self.user) block_device = factory.make_VirtualBlockDevice(node=node) fstype = factory.pick_filesystem_type() fsuuid = "%s" % uuid.uuid4() uri = get_blockdevice_uri(block_device) response = self.client.post(uri, { "op": "format", "fstype": fstype, "uuid": fsuuid }) self.assertEqual(http.client.CONFLICT, response.status_code, response.content)
def test_is_not_valid_if_block_device_has_partition_table(self): fstype = factory.pick_filesystem_type() block_device = factory.make_PhysicalBlockDevice() factory.make_PartitionTable(block_device=block_device) data = { 'fstype': fstype, } form = FormatBlockDeviceForm(block_device, data=data) self.assertFalse( form.is_valid(), "Should be invalid because block device has a partition table.") self.assertEqual({ '__all__': [ "Cannot format block device with a partition table.", ]}, form._errors)
def test_format_returns_409_if_not_allocated_or_ready(self): self.become_admin() status = factory.pick_enum( NODE_STATUS, but_not=[NODE_STATUS.READY, NODE_STATUS.ALLOCATED]) node = factory.make_Node(status=status, owner=self.user) partition = self.make_partition(node) uri = get_partition_uri(partition) fs_uuid = str(uuid4()) fstype = factory.pick_filesystem_type() response = self.client.post(uri, { 'op': 'format', 'uuid': fs_uuid, 'fstype': fstype, 'label': 'mylabel', }) self.assertEqual( http.client.CONFLICT, response.status_code, response.content)
def test_deletes_old_filesystem_and_creates_new_one(self): fstype = factory.pick_filesystem_type() partition = factory.make_Partition() prev_filesystem = factory.make_Filesystem(partition=partition) data = {"fstype": fstype} form = FormatPartitionForm(partition, data=data) self.assertTrue(form.is_valid(), form._errors) form.save() self.assertEqual( 1, Filesystem.objects.filter(partition=partition).count(), "Should only be one filesystem that exists for partition.", ) self.assertIsNone(reload_object(prev_filesystem)) filesystem = get_one(Filesystem.objects.filter(partition=partition)) self.assertIsNotNone(filesystem) self.assertEqual(fstype, filesystem.fstype)
def test_deletes_old_filesystem_and_creates_new_one(self): fstype = factory.pick_filesystem_type() block_device = factory.make_PhysicalBlockDevice() prev_filesystem = factory.make_Filesystem(block_device=block_device) data = {"fstype": fstype} form = FormatBlockDeviceForm(block_device, data=data) self.assertTrue(form.is_valid(), form._errors) form.save() self.assertEqual( 1, Filesystem.objects.filter(block_device=block_device).count(), "Should only be one filesystem that exists for block device.", ) self.assertIsNone(reload_object(prev_filesystem)) filesystem = get_one( Filesystem.objects.filter(block_device=block_device)) self.assertIsNotNone(filesystem) self.assertEqual(fstype, filesystem.fstype)
def test_format_partition_as_user(self): node = factory.make_Node( status=NODE_STATUS.ALLOCATED, owner=self.user) partition = self.make_partition(node) uri = get_partition_uri(partition) fs_uuid = str(uuid4()) fstype = factory.pick_filesystem_type() response = self.client.post(uri, { 'op': 'format', 'uuid': fs_uuid, 'fstype': fstype, 'label': 'mylabel', }) self.assertEqual( http.client.OK, response.status_code, response.content) filesystem = json.loads( response.content.decode(settings.DEFAULT_CHARSET))['filesystem'] self.assertEqual(fstype, filesystem['fstype']) self.assertEqual('mylabel', filesystem['label']) self.assertEqual(fs_uuid, filesystem['uuid'])
def test_format_formats_block_device_as_admin(self): self.become_admin() node = factory.make_Node(status=NODE_STATUS.READY) block_device = factory.make_VirtualBlockDevice(node=node) fstype = factory.pick_filesystem_type() fsuuid = "%s" % uuid.uuid4() uri = get_blockdevice_uri(block_device) response = self.client.post(uri, { "op": "format", "fstype": fstype, "uuid": fsuuid }) self.assertEqual(http.client.OK, response.status_code, response.content) parsed_device = json_load_bytes(response.content) self.assertEqual(parsed_device["filesystem"]["fstype"], fstype) self.assertEqual(parsed_device["filesystem"]["uuid"], fsuuid) self.assertIsNone(parsed_device["filesystem"]["mount_point"]) block_device = reload_object(block_device) self.assertIsNotNone(block_device.get_effective_filesystem())
def test_format_formats_block_device_as_user(self): node = factory.make_Node(status=NODE_STATUS.ALLOCATED, owner=self.user) block_device = factory.make_VirtualBlockDevice(node=node) fstype = factory.pick_filesystem_type() fsuuid = '%s' % uuid.uuid4() uri = get_blockdevice_uri(block_device) response = self.client.post(uri, { 'op': 'format', 'fstype': fstype, 'uuid': fsuuid }) self.assertEqual(http.client.OK, response.status_code, response.content) parsed_device = json_load_bytes(response.content) self.assertDictContainsSubset( { 'fstype': fstype, 'uuid': fsuuid, 'mount_point': None, }, parsed_device['filesystem']) block_device = reload_object(block_device) self.assertIsNotNone(block_device.get_effective_filesystem())