Example #1
0
 def test_get_block_devices_in_filesystem_group(self):
     node = factory.make_Node()
     filesystem_group = factory.make_FilesystemGroup(
         group_type=FILESYSTEM_GROUP_TYPE.LVM_VG
     )
     block_devices = [
         filesystem.block_device
         for filesystem in filesystem_group.filesystems.all()
         if filesystem.block_device is not None
     ]
     block_device_with_partitions = factory.make_PhysicalBlockDevice(
         node=node
     )
     partition_table = factory.make_PartitionTable(
         block_device=block_device_with_partitions
     )
     partition = factory.make_Partition(partition_table=partition_table)
     factory.make_Filesystem(
         fstype=FILESYSTEM_TYPE.LVM_PV,
         partition=partition,
         filesystem_group=filesystem_group,
     )
     block_devices_in_filesystem_group = (
         BlockDevice.objects.get_block_devices_in_filesystem_group(
             filesystem_group
         )
     )
     self.assertItemsEqual(block_devices, block_devices_in_filesystem_group)
     self.assertNotIn(
         block_device_with_partitions, block_devices_in_filesystem_group
     )
Example #2
0
 def test_get_physical_block_devices_multiple(self):
     node = factory.make_Node()
     block_devices = [
         factory.make_PhysicalBlockDevice(node=node) for _ in range(3)
     ]
     filesystems = [
         factory.make_Filesystem(
             fstype=FILESYSTEM_TYPE.LVM_PV, block_device=block_device
         )
         for block_device in block_devices
     ]
     fsgroup = factory.make_FilesystemGroup(
         node=node,
         filesystems=filesystems,
         group_type=FILESYSTEM_GROUP_TYPE.LVM_VG,
     )
     virtual_block_device = factory.make_VirtualBlockDevice(
         filesystem_group=fsgroup
     )
     filesystem = factory.make_Filesystem(
         fstype=FILESYSTEM_TYPE.LVM_PV, block_device=virtual_block_device
     )
     self.assertEqual(
         filesystem.get_physical_block_devices(), block_devices
     )
Example #3
0
 def test_returns_none_when_allocated_state(self):
     node = factory.make_Node(status=NODE_STATUS.ALLOCATED)
     model = self.factory(node=node)
     factory.make_Filesystem(
         **{self.filesystem_property: model, "fstype": FILESYSTEM_TYPE.EXT4}
     )
     self.assertIsNone(get_effective_filesystem(model))
Example #4
0
    def test_read_returns_filesystems_on_partitions(self):
        node = factory.make_Node(with_boot_disk=False)
        block_size = 1024
        block_device = factory.make_PhysicalBlockDevice(node=node,
                                                        size=1000000 *
                                                        block_size)
        partition_table = factory.make_PartitionTable(
            block_device=block_device, table_type='MBR')
        # Use PartitionTable methods that auto-size and position partitions
        partition1 = partition_table.add_partition(size=50000 * block_size)
        partition2 = partition_table.add_partition()
        filesystem1 = factory.make_Filesystem(partition=partition1)
        filesystem2 = factory.make_Filesystem(partition=partition2)
        uri = get_blockdevices_uri(node)
        response = self.client.get(uri)

        self.assertEqual(http.client.OK, response.status_code,
                         response.content)
        parsed_devices = json_load_bytes(response.content)

        # We should have one device
        self.assertEqual(len(parsed_devices), 1)
        [parsed_device] = parsed_devices
        # Check whether the filesystems are what we expect them to be
        self.assertEqual(parsed_device['partitions'][0]['filesystem']['uuid'],
                         filesystem1.uuid)
        self.assertEqual(parsed_device['partitions'][1]['filesystem']['uuid'],
                         filesystem2.uuid)
Example #5
0
 def test_can_create_filesystem_on_partition_on_boot_disk(self):
     node = factory.make_Node(with_boot_disk=False)
     boot_disk = factory.make_PhysicalBlockDevice(node=node)
     partition_table = factory.make_PartitionTable(block_device=boot_disk)
     partition = factory.make_Partition(partition_table=partition_table)
     # Test is that an error is not raised.
     factory.make_Filesystem(partition=partition)
Example #6
0
 def test_is_not_valid_if_non_user_format_fstype(self):
     block_device = factory.make_PhysicalBlockDevice()
     factory.make_Filesystem(fstype="bcache-backing",
                             block_device=block_device)
     data = {"fstype": FILESYSTEM_TYPE.EXT4}
     form = FormatBlockDeviceForm(block_device, data=data)
     self.assertRaises(ValidationError, form.save)
Example #7
0
 def test_is_not_valid_if_non_user_format_fstype(self):
     partition = factory.make_Partition()
     factory.make_Filesystem(fstype='bcache-backing', partition=partition)
     data = {
         'fstype': FILESYSTEM_TYPE.EXT4,
     }
     form = FormatPartitionForm(partition, data=data)
     self.assertRaises(ValidationError, form.save)
Example #8
0
 def test_returns_acquired_filesystem(self):
     node = factory.make_Node(status=NODE_STATUS.ALLOCATED)
     model = self.factory(node=node)
     factory.make_Filesystem(**{self.filesystem_property: model})
     filesystem = factory.make_Filesystem(
         **{self.filesystem_property: model, "acquired": True}
     )
     self.assertEqual(filesystem, get_effective_filesystem(model))
Example #9
0
 def test_unmount_returns_403_if_ready_and_not_admin(self):
     node = factory.make_Node(status=NODE_STATUS.READY)
     block_device = factory.make_VirtualBlockDevice(node=node)
     factory.make_Filesystem(block_device=block_device, mount_point="/mnt")
     uri = get_blockdevice_uri(block_device)
     response = self.client.post(uri, {"op": "unmount"})
     self.assertEqual(http.client.FORBIDDEN, response.status_code,
                      response.content)
Example #10
0
 def test_unmount_returns_403_if_ready_and_not_admin(self):
     node = factory.make_Node(status=NODE_STATUS.READY)
     partition = self.make_partition(node)
     factory.make_Filesystem(partition=partition, mount_point="/mnt")
     uri = get_partition_uri(partition)
     response = self.client.post(uri, {"op": "unmount"})
     self.assertEqual(http.client.FORBIDDEN, response.status_code,
                      response.content)
Example #11
0
 def test_update_updates_volume_group(self):
     self.become_admin()
     node = factory.make_Node(status=NODE_STATUS.READY)
     volume_group = factory.make_VolumeGroup(node=node)
     delete_block_device = factory.make_PhysicalBlockDevice(node=node)
     factory.make_Filesystem(
         fstype=FILESYSTEM_TYPE.LVM_PV,
         block_device=delete_block_device,
         filesystem_group=volume_group,
     )
     delete_bd_for_partition = factory.make_PhysicalBlockDevice(node=node)
     delete_table = factory.make_PartitionTable(
         block_device=delete_bd_for_partition
     )
     delete_partition = factory.make_Partition(partition_table=delete_table)
     factory.make_Filesystem(
         fstype=FILESYSTEM_TYPE.LVM_PV,
         partition=delete_partition,
         filesystem_group=volume_group,
     )
     new_name = factory.make_name("vg")
     new_uuid = "%s" % uuid.uuid4()
     new_block_device = factory.make_PhysicalBlockDevice(node=node)
     new_bd_for_partition = factory.make_PhysicalBlockDevice(node=node)
     new_table = factory.make_PartitionTable(
         block_device=new_bd_for_partition
     )
     new_partition = factory.make_Partition(partition_table=new_table)
     uri = get_volume_group_uri(volume_group)
     response = self.client.put(
         uri,
         {
             "name": new_name,
             "uuid": new_uuid,
             "add_block_devices": [new_block_device.id],
             "remove_block_devices": [delete_block_device.id],
             "add_partitions": [new_partition.id],
             "remove_partitions": [delete_partition.id],
         },
     )
     self.assertEqual(
         http.client.OK, response.status_code, response.content
     )
     volume_group = reload_object(volume_group)
     self.assertEqual(new_name, volume_group.name)
     self.assertEqual(new_uuid, volume_group.uuid)
     self.assertEqual(
         volume_group.id,
         new_block_device.get_effective_filesystem().filesystem_group.id,
     )
     self.assertEqual(
         volume_group.id,
         new_partition.get_effective_filesystem().filesystem_group.id,
     )
     self.assertIsNone(delete_block_device.get_effective_filesystem())
     self.assertIsNone(delete_partition.get_effective_filesystem())
Example #12
0
 def test_update_400_when_invalid_id(self):
     self.become_admin()
     node = factory.make_Node(status=NODE_STATUS.READY)
     cache_set = factory.make_CacheSet(node=node)
     new_device = factory.make_PhysicalBlockDevice(node=node)
     factory.make_Filesystem(block_device=new_device)
     uri = get_bcache_cache_set_uri(cache_set)
     response = self.client.put(uri, {"cache_device": new_device})
     self.assertEqual(http.client.BAD_REQUEST, response.status_code,
                      response.content)
Example #13
0
 def test_cannot_create_filesystem_directly_on_boot_disk(self):
     node = factory.make_Node(with_boot_disk=False)
     boot_disk = factory.make_PhysicalBlockDevice(node=node)
     with ExpectedException(
             ValidationError,
             re.escape(
                 "{'__all__': ['Cannot place filesystem directly on the "
                 "boot disk. Create a partition on the boot disk first "
                 "and then format the partition.']}")):
         factory.make_Filesystem(block_device=boot_disk)
Example #14
0
 def test_unmount_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)
     factory.make_Filesystem(block_device=block_device, mount_point="/mnt")
     uri = get_blockdevice_uri(block_device)
     response = self.client.post(uri, {"op": "unmount"})
     self.assertEqual(http.client.CONFLICT, response.status_code,
                      response.content)
Example #15
0
 def test_unmount_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)
     factory.make_Filesystem(partition=partition, mount_point="/mnt")
     uri = get_partition_uri(partition)
     response = self.client.post(uri, {"op": "unmount"})
     self.assertEqual(http.client.CONFLICT, response.status_code,
                      response.content)
Example #16
0
    def test_unmount_returns_400_if_already_unmounted(self):
        self.become_admin()
        node = factory.make_Node(status=NODE_STATUS.READY)
        block_device = factory.make_VirtualBlockDevice(node=node)
        factory.make_Filesystem(block_device=block_device)
        uri = get_blockdevice_uri(block_device)
        response = self.client.post(uri, {"op": "unmount"})

        self.assertEqual(http.client.BAD_REQUEST, response.status_code,
                         response.content)
        self.assertEqual(b"Filesystem is already unmounted.", response.content)
Example #17
0
 def test__raid_spare(self):
     filesystem_group = factory.make_FilesystemGroup(
         group_type=factory.pick_choice(FILESYSTEM_GROUP_RAID_TYPE_CHOICES))
     slave_block_device = factory.make_PhysicalBlockDevice()
     factory.make_Filesystem(block_device=slave_block_device,
                             fstype=FILESYSTEM_TYPE.RAID_SPARE,
                             filesystem_group=filesystem_group)
     self.assertEqual(
         ("Spare %s device for %s" %
          (filesystem_group.group_type, filesystem_group.name)),
         used_for(slave_block_device))
Example #18
0
 def test_unmount_returns_400_if_already_unmounted(self):
     self.become_admin()
     node = factory.make_Node(status=NODE_STATUS.READY)
     partition = self.make_partition(node)
     factory.make_Filesystem(partition=partition)
     uri = get_partition_uri(partition)
     response = self.client.post(uri, {'op': 'unmount'})
     self.assertEqual(
         http.client.BAD_REQUEST, response.status_code, response.content)
     self.assertEqual(
         "Filesystem is already unmounted.",
         response.content.decode(settings.DEFAULT_CHARSET))
Example #19
0
 def test_mount_returns_400_on_missing_mount_point(self):
     self.become_admin()
     node = factory.make_Node(status=NODE_STATUS.READY)
     partition = self.make_partition(node)
     factory.make_Filesystem(partition=partition)
     uri = get_partition_uri(partition)
     response = self.client.post(uri, {"op": "mount"})
     self.assertEqual(http.client.BAD_REQUEST, response.status_code,
                      response.content)
     parsed_error = json.loads(
         response.content.decode(settings.DEFAULT_CHARSET))
     self.assertEqual({"mount_point": ["This field is required."]},
                      parsed_error)
Example #20
0
 def test_unformat_partition_as_user(self):
     node = factory.make_Node(status=NODE_STATUS.ALLOCATED, owner=self.user)
     partition = self.make_partition(node)
     factory.make_Filesystem(partition=partition, acquired=True)
     uri = get_partition_uri(partition)
     response = self.client.post(uri, {"op": "unformat"})
     # Returns the partition without the filesystem.
     self.assertEqual(http.client.OK, response.status_code,
                      response.content)
     partition = json.loads(
         response.content.decode(settings.DEFAULT_CHARSET))
     self.assertIsNone(partition.get("filesystem"),
                       "Partition still has a filesystem.")
Example #21
0
    def test_mount_returns_400_on_missing_mount_point(self):
        self.become_admin()
        node = factory.make_Node(status=NODE_STATUS.READY)
        block_device = factory.make_VirtualBlockDevice(node=node)
        factory.make_Filesystem(block_device=block_device)
        uri = get_blockdevice_uri(block_device)
        response = self.client.post(uri, {"op": "mount"})

        self.assertEqual(http.client.BAD_REQUEST, response.status_code,
                         response.content)
        parsed_error = json_load_bytes(response.content)
        self.assertEqual({"mount_point": ["This field is required."]},
                         parsed_error)
Example #22
0
    def test_unformat_deletes_filesystem_as_user(self):
        node = factory.make_Node(status=NODE_STATUS.ALLOCATED, owner=self.user)
        block_device = factory.make_PhysicalBlockDevice(node=node)
        factory.make_Filesystem(block_device=block_device, acquired=True)
        uri = get_blockdevice_uri(block_device)
        response = self.client.post(uri, {"op": "unformat"})

        self.assertEqual(http.client.OK, response.status_code,
                         response.content)
        parsed_device = json_load_bytes(response.content)
        self.assertIsNone(parsed_device["filesystem"])
        block_device = reload_object(block_device)
        self.assertIsNone(block_device.get_effective_filesystem())
Example #23
0
    def test_unformat_returns_400_if_mounted(self):
        self.become_admin()
        node = factory.make_Node(status=NODE_STATUS.READY)
        block_device = factory.make_VirtualBlockDevice(node=node)
        factory.make_Filesystem(block_device=block_device, mount_point="/mnt")
        uri = get_blockdevice_uri(block_device)
        response = self.client.post(uri, {'op': 'unformat'})

        self.assertEqual(http.client.BAD_REQUEST, response.status_code,
                         response.content)
        self.assertEqual(
            b"Filesystem is mounted and cannot be unformatted. Unmount the "
            b"filesystem before unformatting the block device.",
            response.content)
Example #24
0
 def test_mount_returns_403_if_ready_and_not_admin(self):
     node = factory.make_Node(status=NODE_STATUS.READY)
     block_device = factory.make_PhysicalBlockDevice(node=node)
     partition_table = factory.make_PartitionTable(
         block_device=block_device)
     partition = partition_table.add_partition()
     factory.make_Filesystem(
         partition=partition)
     uri = get_partition_uri(partition)
     mount_point = '/mnt'
     response = self.client.post(
         uri, {'op': 'mount', 'mount_point': mount_point})
     self.assertEqual(
         http.client.FORBIDDEN, response.status_code, response.content)
Example #25
0
    def test_unformat_deletes_filesystem_as_admin(self):
        self.become_admin()
        node = factory.make_Node(status=NODE_STATUS.READY)
        block_device = factory.make_PhysicalBlockDevice(node=node)
        factory.make_Filesystem(block_device=block_device)
        uri = get_blockdevice_uri(block_device)
        response = self.client.post(uri, {'op': 'unformat'})

        self.assertEqual(http.client.OK, response.status_code,
                         response.content)
        parsed_device = json_load_bytes(response.content)
        self.assertIsNone(parsed_device["filesystem"])
        block_device = reload_object(block_device)
        self.assertIsNone(block_device.get_effective_filesystem())
Example #26
0
 def test_get_free_block_devices_for_node(self):
     node = factory.make_Node(with_boot_disk=False)
     free_devices = [factory.make_BlockDevice(node=node) for _ in range(3)]
     # Block devices with partition tables.
     for _ in range(3):
         factory.make_PartitionTable(block_device=factory.make_BlockDevice(
             node=node))
     # Block devices with filesystems.
     for _ in range(3):
         factory.make_Filesystem(block_device=factory.make_BlockDevice(
             node=node))
     self.assertItemsEqual(
         free_devices,
         BlockDevice.objects.get_free_block_devices_for_node(node))
Example #27
0
 def test_unformat_partition_as_admin(self):
     self.become_admin()
     node = factory.make_Node(status=NODE_STATUS.READY)
     partition = self.make_partition(node)
     factory.make_Filesystem(partition=partition)
     uri = get_partition_uri(partition)
     response = self.client.post(uri, {'op': 'unformat'})
     # Returns the partition without the filesystem.
     self.assertEqual(
         http.client.OK, response.status_code, response.content)
     partition = json.loads(
         response.content.decode(settings.DEFAULT_CHARSET))
     self.assertIsNone(
         partition.get('filesystem'), 'Partition still has a filesystem.')
Example #28
0
 def test_removes_block_device_by_name(self):
     node = factory.make_Node()
     volume_group = factory.make_VolumeGroup(node=node)
     block_device = factory.make_PhysicalBlockDevice(node=node)
     factory.make_Filesystem(fstype=FILESYSTEM_TYPE.LVM_PV,
                             block_device=block_device,
                             filesystem_group=volume_group)
     data = {
         'remove_block_devices': [block_device.name],
     }
     form = UpdateVolumeGroupForm(volume_group, data=data)
     self.assertTrue(form.is_valid(), form._errors)
     volume_group = form.save()
     self.assertIsNone(block_device.get_effective_filesystem())
Example #29
0
 def test_can_create_mountable_filesystem(self):
     substrate = self.make_substrate()
     filesystem = factory.make_Filesystem(fstype=self.fstype, **substrate)
     self.assertThat(filesystem, IsInstance(Filesystem))
     self.assertThat(filesystem.fstype, Equals(self.fstype))
     self.assertThat(filesystem.is_mountable, Is(True))
     self.assertThat(filesystem, MatchesStructure.byEquality(**substrate))
Example #30
0
    def test_unique_on_block_device_and_acquired(self):
        # For any given block device, at most one unacquired and one acquired
        # filesystem record may exist.
        block_device = factory.make_BlockDevice()
        filesystem = factory.make_Filesystem(block_device=block_device)
        self.assertIsNone(filesystem.partition)

        # XXX: Why no Filesystem.acquire() method?
        filesystem_acquired = copy(filesystem)
        filesystem_acquired.id = None  # Force INSERT.
        filesystem_acquired.acquired = True
        filesystem_acquired.save()

        # Create a duplicate.
        filesystem_dupe = copy(filesystem)
        filesystem_dupe.id = None  # Force INSERT.

        error_messages_expected = Equals(
            ['Filesystem with this Block device and Acquired already exists.'])

        # Saving an unacquired duplicate fails.
        filesystem_dupe.acquired = False
        error = self.assertRaises(ValidationError, filesystem_dupe.save)
        self.assertThat(error.messages, error_messages_expected)

        # Saving an acquired duplicate fails.
        filesystem_dupe.acquired = True
        error = self.assertRaises(ValidationError, filesystem_dupe.save)
        self.assertThat(error.messages, error_messages_expected)