Exemplo n.º 1
0
    def test_teardown(self):
        self.device.exists = False
        with patch.object(self.device, "_teardown"):
            six.assertRaisesRegex(self, DeviceError, "has not been created",
                                  self.device.teardown)
            self.assertFalse(self.device._teardown.called)

        self.device.exists = True
        self.patches["status"].return_value = False
        with patch.object(self.device, "_teardown"):
            self.device.teardown()
            self.assertFalse(self.device._teardown.called)

        self.patches["udev"].reset_mock()
        self.device.update_sysfs_path.reset_mock()
        self.patches["status"].return_value = True
        with patch.object(self.device, "_teardown"):
            self.device.teardown()
            self.assertTrue(self.teardown_method_mock.called)

        self.assertEqual(self.patches["udev"].settle.called,
                         self.teardown_calls_udev_settle)
        self.assertEqual(self.device.update_sysfs_path.called,
                         self.teardown_updates_sysfs_path)
        self.patches["udev"].reset_mock()
        self.device.update_sysfs_path.reset_mock()
Exemplo n.º 2
0
    def test_create(self):
        # an existing device's create method should raise DeviceError
        self.device.exists = True
        self.patches["status"].return_value = True
        with patch.object(self.device, "_create"):
            six.assertRaisesRegex(self, DeviceError,
                                  "has already been created",
                                  self.device.create)
            self.assertFalse(self.device._create.called)
        self.device.exists = False

        # if _create raises an exception _post_create should not be called
        def _create():
            raise RuntimeError("problems")

        with patch.object(self.device, "_create"):
            with patch.object(self.device, "_post_create"):
                self.device._create.side_effect = _create
                six.assertRaisesRegex(self, RuntimeError, "problems",
                                      self.device.create)
                self.assertTrue(self.device._create.called)
                self.assertFalse(self.device._post_create.called)

        # successful create call
        with patch.object(self.device, "_create"):
            self.device.create()
            self.assertTrue(self.device._create.called)

        self.assertTrue(self.device.exists)
        self.assertEqual(self.device.update_sysfs_path.called,
                         self.create_updates_sysfs_path)
        self.assertEqual(self.patches["udev"].settle.called,
                         self.create_calls_udev_settle)
        self.patches["udev"].reset_mock()
        self.device.update_sysfs_path.reset_mock()
Exemplo n.º 3
0
    def test_destroy(self):
        # an non-existing device's destroy method should raise DeviceError
        self.device.exists = False
        self.patches["status"].return_value = True
        with patch.object(self.device, "_destroy"):
            six.assertRaisesRegex(self, DeviceError, "has not been created", self.device.destroy)
            self.assertFalse(self.device._destroy.called)
        self.device.exists = True

        # if _destroy raises an exception _post_destroy should not be called
        def _destroy():
            raise RuntimeError("problems")

        with patch.object(self.device, "_destroy"):
            with patch.object(self.device, "_post_destroy"):
                self.device._destroy.side_effect = _destroy
                six.assertRaisesRegex(self, RuntimeError, "problems", self.device.destroy)
                self.assertTrue(self.device._destroy.called)
                self.assertFalse(self.device._post_destroy.called)

        # successful destroy call
        self.assertTrue(self.device.exists)
        with patch.object(self.device, "_destroy"):
            self.device.destroy()
            self.assertTrue(self.device._destroy.called)

        self.assertFalse(self.device.exists)
        self.assertEqual(self.device.update_sysfs_path.called, self.destroy_updates_sysfs_path)
        self.assertEqual(self.patches["udev"].settle.called, self.destroy_calls_udev_settle)
        self.patches["udev"].reset_mock()
        self.device.update_sysfs_path.reset_mock()
Exemplo n.º 4
0
    def test_create(self):
        # an existing device's create method should raise DeviceError
        self.device.exists = True
        self.patches["status"].return_value = True
        with patch.object(self.device, "_create"):
            six.assertRaisesRegex(self, DeviceError, "has already been created", self.device.create)
            self.assertFalse(self.device._create.called)
        self.device.exists = False

        # if _create raises an exception _post_create should not be called
        def _create():
            raise RuntimeError("problems")

        with patch.object(self.device, "_create"):
            with patch.object(self.device, "_post_create"):
                self.device._create.side_effect = _create
                six.assertRaisesRegex(self, RuntimeError, "problems", self.device.create)
                self.assertTrue(self.device._create.called)
                self.assertFalse(self.device._post_create.called)

        # successful create call
        with patch.object(self.device, "_create"):
            self.device.create()
            self.assertTrue(self.device._create.called)

        self.assertTrue(self.device.exists)
        self.assertEqual(self.device.update_sysfs_path.called, self.create_updates_sysfs_path)
        self.assertEqual(self.patches["udev"].settle.called, self.create_calls_udev_settle)
        self.patches["udev"].reset_mock()
        self.device.update_sysfs_path.reset_mock()
Exemplo n.º 5
0
    def test_destroy(self):
        # an non-existing device's destroy method should raise DeviceError
        self.device.exists = False
        self.patches["status"].return_value = True
        with patch.object(self.device, "_destroy"):
            six.assertRaisesRegex(self, DeviceError, "has not been created",
                                  self.device.destroy)
            self.assertFalse(self.device._destroy.called)
        self.device.exists = True

        # if _destroy raises an exception _post_destroy should not be called
        def _destroy():
            raise RuntimeError("problems")

        with patch.object(self.device, "_destroy"):
            with patch.object(self.device, "_post_destroy"):
                self.device._destroy.side_effect = _destroy
                six.assertRaisesRegex(self, RuntimeError, "problems",
                                      self.device.destroy)
                self.assertTrue(self.device._destroy.called)
                self.assertFalse(self.device._post_destroy.called)

        # successful destroy call
        self.assertTrue(self.device.exists)
        with patch.object(self.device, "_destroy"):
            self.device.destroy()
            self.assertTrue(self.device._destroy.called)

        self.assertFalse(self.device.exists)
        self.assertEqual(self.device.update_sysfs_path.called,
                         self.destroy_updates_sysfs_path)
        self.assertEqual(self.patches["udev"].settle.called,
                         self.destroy_calls_udev_settle)
        self.patches["udev"].reset_mock()
        self.device.update_sysfs_path.reset_mock()
Exemplo n.º 6
0
 def set_patches(self):
     super(PartitionDeviceMethodsTestCase, self).set_patches()
     self.patchers["parted_partition"] = patch.object(self.device_class,
                                                      "parted_partition",
                                                      new=PropertyMock())
     self.patchers["disk"] = patch.object(self.device_class,
                                          "disk",
                                          new=PropertyMock())
Exemplo n.º 7
0
    def test_create(self, *args):  # pylint: disable=unused-argument,arguments-differ
        with patch.object(self.device, "_wipe"):
            super(PartitionDeviceMethodsTestCase, self).test_create()

        with patch.object(self.device, "_wipe"):
            self.device._create()
            self.assertTrue(self.device.disk.format.add_partition.called)
            self.assertTrue(self.device.disk.format.commit.called)
Exemplo n.º 8
0
    def test_create(self, *args):  # pylint: disable=unused-argument,arguments-differ
        with patch.object(self.device, "_wipe"):
            super(PartitionDeviceMethodsTestCase, self).test_create()

        with patch.object(self.device, "_wipe"):
            self.device._create()
            self.assertTrue(self.device.disk.format.add_partition.called)
            self.assertTrue(self.device.disk.format.commit.called)
Exemplo n.º 9
0
 def set_patches(self):
     super(LUKSMethodsTestCase, self).set_patches()
     self.patchers["configured"] = patch.object(
         self.format_class,
         "configured",
         new=PropertyMock(return_value=True))
     self.patchers["has_key"] = patch.object(
         self.format_class, "has_key", new=PropertyMock(return_value=True))
     self.patchers["blockdev"] = patch("blivet.formats.luks.blockdev")
Exemplo n.º 10
0
    def test_destroy(self):
        # fmt must exist
        self.format.exists = False
        with patch.object(self.format, "_destroy"):
            self.patches["os"].access.return_value = True
            six.assertRaisesRegex(self, DeviceFormatError, "has not been created", self.format.destroy)
            self.assertFalse(self.format._destroy.called)  # pylint: disable=no-member

        self.format.exists = True

        # format must be inactive
        with patch.object(self.format, "_destroy"):
            self.patches["status"].return_value = True
            six.assertRaisesRegex(self, DeviceFormatError, "is active", self.format.destroy)
            self.assertFalse(self.format._destroy.called)  # pylint: disable=no-member

        # device must be accessible
        with patch.object(self.format, "_destroy"):
            self.patches["os"].access.return_value = False
            self.patches["status"].return_value = False
            six.assertRaisesRegex(self, DeviceFormatError, "device path does not exist", self.format.destroy)
            self.assertFalse(self.format._destroy.called)  # pylint: disable=no-member

        self.patches["os"].access.return_value = True
        # _pre_destroy raises -> no _create

        # pylint: disable=unused-argument
        def _fail(*args, **kwargs):
            raise RuntimeError("problems")

        self.assertTrue(self.format.exists)
        with patch.object(self.format, "_destroy"):
            with patch.object(self.format, "_pre_destroy") as m:
                m.side_effect = _fail
                six.assertRaisesRegex(self, RuntimeError, "problems", self.format.destroy)
                self.assertFalse(self.format._destroy.called)  # pylint: disable=no-member
                self.assertTrue(self.format.exists)

        # _destroy raises -> no _post_destroy -> exists == True
        with patch.object(self.format, "_destroy") as m:
            m.side_effect = _fail
            six.assertRaisesRegex(self, RuntimeError, "problems", self.format.destroy)
            self.assertTrue(self.format._destroy.called)  # pylint: disable=no-member
            self.assertTrue(self.format.exists)

        # _destroy succeeds -> _post_destroy is what updates existence
        with patch.object(self.format, "_destroy"):
            with patch.object(self.format, "_post_destroy"):
                self.format.destroy()
                self.assertTrue(self.format._destroy.called)  # pylint: disable=no-member
                self.assertTrue(self.format.exists)

        # _post_destroy set exists to False
        with patch.object(self.format, "_destroy"):
            self.format.destroy()
            self.assertTrue(self.format._destroy.called)  # pylint: disable=no-member
            self.assertFalse(self.format.exists)

        self._test_destroy_backend()
Exemplo n.º 11
0
    def test_hide_ignored_disks(self):
        tree = DeviceTree()

        sda = DiskDevice("sda")
        sdb = DiskDevice("sdb")
        sdc = DiskDevice("sdc")

        tree._add_device(sda)
        tree._add_device(sdb)
        tree._add_device(sdc)

        self.assertTrue(sda in tree.devices)
        self.assertTrue(sdb in tree.devices)
        self.assertTrue(sdc in tree.devices)

        # test ignored_disks
        tree.ignored_disks = ["sdb"]

        # verify hide is called as expected
        with patch.object(tree, "hide") as hide:
            tree._hide_ignored_disks()
            hide.assert_called_with(sdb)

        # verify that hide works as expected
        tree._hide_ignored_disks()
        self.assertTrue(sda in tree.devices)
        self.assertFalse(sdb in tree.devices)
        self.assertTrue(sdc in tree.devices)

        # unhide sdb and make sure it works
        tree.unhide(sdb)
        self.assertTrue(sda in tree.devices)
        self.assertTrue(sdb in tree.devices)
        self.assertTrue(sdc in tree.devices)

        # test exclusive_disks
        tree.ignored_disks = []
        tree.exclusive_disks = ["sdc"]
        with patch.object(tree, "hide") as hide:
            tree._hide_ignored_disks()
            hide.assert_any_call(sda)
            hide.assert_any_call(sdb)

        tree._hide_ignored_disks()
        self.assertFalse(sda in tree.devices)
        self.assertFalse(sdb in tree.devices)
        self.assertTrue(sdc in tree.devices)

        tree.unhide(sda)
        tree.unhide(sdb)
        self.assertTrue(sda in tree.devices)
        self.assertTrue(sdb in tree.devices)
        self.assertTrue(sdc in tree.devices)
Exemplo n.º 12
0
 def set_patches(self):
     super(MDRaidArrayDeviceMethodsTestCase, self).set_patches()
     self.patchers["md"] = patch("blivet.devices.md.blockdev.md")
     self.patchers["is_disk"] = patch.object(self.device_class, "is_disk",
                                             new=PropertyMock(return_value=False))
     self.patchers["pvs_info"] = patch("blivet.devices.md.pvs_info")
     self.patchers["lvm"] = patch("blivet.devices.md.blockdev.lvm")
Exemplo n.º 13
0
    def test_destroy(self):
        with patch.object(self.device, "teardown"):
            super(LVMLogicalVolumeDeviceMethodsTestCase, self).test_destroy()

        with patch("blivet.devices.lvm.blockdev.lvm") as lvm:
            self.device._destroy()
            self.assertTrue(lvm.lvremove.called)
Exemplo n.º 14
0
    def test_device_name(self):
        # check that devicetree.names property contains all device's names

        # mock lvs_info to avoid blockdev call allowing run as non-root
        with patch.object(LVsInfo, 'cache',
                          new_callable=PropertyMock) as mock_lvs_cache:
            mock_lvs_cache.return_value = {"sdmock": "dummy"}

            tree = DeviceTree()
            dev_names = ["sda", "sdb", "sdc"]

            for dev_name in dev_names:
                dev = DiskDevice(dev_name)
                tree._add_device(dev)
                self.assertTrue(dev in tree.devices)

            # frobnicate a bit with the hidden status of the devices:
            # * hide sda
            # * hide and unhide again sdb
            # * leave sdc unchanged
            tree.hide(tree.get_device_by_name("sda"))
            tree.hide(tree.get_device_by_name("sdb"))
            tree.unhide(tree.get_device_by_name("sdb", hidden=True))

            # some lvs names may be already present in the system (mocked)
            lv_info = list(lvs_info.cache.keys())

            # all devices should still be present in the tree.names
            self.assertEqual(sorted(tree.names), sorted(lv_info + dev_names))
Exemplo n.º 15
0
    def test_ctor_parted_partition_error_handling(self):
        disk = StorageDevice("testdisk", exists=True)
        disk._partitionable = True
        with patch.object(disk, "_format") as fmt:
            fmt.type = "disklabel"
            self.assertTrue(disk.partitioned)

            fmt.supported = True

            # Normal case, no exn.
            device = PartitionDevice("testpart1", exists=True, parents=[disk])
            self.assertIn(device, disk.children)
            device.parents.remove(disk)
            self.assertEqual(len(disk.children),
                             0,
                             msg="disk has children when it should not")

            # Parted doesn't find a partition, exn is raised.
            fmt.parted_disk.getPartitionByPath.return_value = None
            self.assertRaises(DeviceError,
                              PartitionDevice,
                              "testpart1",
                              exists=True,
                              parents=[disk])
            self.assertEqual(
                len(disk.children),
                0,
                msg="device is still attached to disk in spite of ctor error")
Exemplo n.º 16
0
 def set_patches(self):
     super(MDRaidArrayDeviceMethodsTestCase, self).set_patches()
     self.patchers["md"] = patch("blivet.devices.md.blockdev.md")
     self.patchers["is_disk"] = patch.object(
         self.device_class, "is_disk", new=PropertyMock(return_value=False))
     self.patchers["pvs_info"] = patch("blivet.devices.md.pvs_info")
     self.patchers["lvm"] = patch("blivet.devices.md.blockdev.lvm")
Exemplo n.º 17
0
 def _test_setup_backend(self):
     with patch.object(self.format, "_mount"):
         self.patches[
             "fs_os"].path.normpath.return_value = sentinel.mountpoint
         self.format.setup()
         self.format._mount.do_task.assert_called_with(sentinel.mountpoint,
                                                       options="")  # pylint: disable=no-member
Exemplo n.º 18
0
    def test_new_lv_from_lvs(self):
        b = blivet.Blivet()
        pv = StorageDevice("pv1",
                           fmt=blivet.formats.get_format("lvmpv"),
                           size=Size("1 GiB"),
                           exists=True)
        vg = LVMVolumeGroupDevice("testvg", parents=[pv], exists=True)
        lv1 = LVMLogicalVolumeDevice("data_lv",
                                     parents=[vg],
                                     size=Size("500 MiB"),
                                     exists=True)
        lv2 = LVMLogicalVolumeDevice("metadata_lv",
                                     parents=[vg],
                                     size=Size("50 MiB"),
                                     exists=True)

        for dev in (pv, vg, lv1, lv2):
            b.devicetree._add_device(dev)

        # check that all the above devices are in the expected places
        self.assertEqual(set(b.devices), {pv, vg, lv1, lv2})
        self.assertEqual(set(b.vgs), {vg})
        self.assertEqual(set(b.lvs), {lv1, lv2})
        self.assertEqual(set(b.vgs[0].lvs), {lv1, lv2})

        self.assertEqual(vg.size, Size("1020 MiB"))
        self.assertEqual(lv1.size, Size("500 MiB"))
        self.assertEqual(lv2.size, Size("50 MiB"))

        # combine the two LVs into a thin pool (the LVs should become its internal LVs)
        pool = b.new_lv_from_lvs(vg,
                                 name="pool",
                                 seg_type="thin-pool",
                                 from_lvs=(lv1, lv2))

        # add the pool LV into the devicetree
        b.devicetree._add_device(pool)

        self.assertEqual(set(b.devices), {pv, vg, pool})
        self.assertEqual(set(b.vgs), {vg})
        self.assertEqual(set(b.lvs), {pool})
        self.assertEqual(set(b.vgs[0].lvs), {pool})
        self.assertEqual(set(b.vgs[0].lvs[0]._internal_lvs), {lv1, lv2})

        self.assertTrue(lv1.is_internal_lv)
        self.assertEqual(lv1.int_lv_type, LVMInternalLVtype.data)
        self.assertEqual(lv1.size, Size("500 MiB"))
        self.assertTrue(lv2.is_internal_lv)
        self.assertEqual(lv2.int_lv_type, LVMInternalLVtype.meta)
        self.assertEqual(lv2.size, Size("50 MiB"))

        self.assertEqual(pool.name, "testvg-pool")
        self.assertEqual(pool.size, Size("500 MiB"))
        self.assertEqual(pool.metadata_size, Size("50 MiB"))
        self.assertIs(pool.vg, vg)

        with patch("blivet.devices.lvm.blockdev.lvm") as lvm:
            with patch.object(pool, "_pre_create"):
                pool.create()
                self.assertTrue(lvm.thpool_convert.called)
Exemplo n.º 19
0
    def test_destroy(self):
        with patch.object(self.device, "teardown"):
            super(LVMLogicalVolumeDeviceMethodsTestCase, self).test_destroy()

        with patch("blivet.devices.lvm.blockdev.lvm") as lvm:
            self.device._destroy()
            self.assertTrue(lvm.lvremove.called)
Exemplo n.º 20
0
    def test_create(self):
        # fmt cannot exist
        self.format.exists = True
        with patch.object(self.format, "_create"):
            self.set_os_path_exists(True)
            six.assertRaisesRegex(self, DeviceFormatError,
                                  "format already exists", self.format.create)
            self.assertFalse(self.format._create.called)  # pylint: disable=no-member
        self.format.exists = False

        # device must be accessible
        with patch.object(self.format, "_create"):
            # device must be accessible
            self.set_os_path_exists(False)
            six.assertRaisesRegex(self, DeviceFormatError,
                                  "invalid device specification",
                                  self.format.create)
            self.assertFalse(self.format._create.called)  # pylint: disable=no-member
        self.set_os_path_exists(True)

        # _pre_create raises -> no _create
        self.assertFalse(self.format.exists)

        # pylint: disable=unused-argument
        def _fail(*args, **kwargs):
            raise RuntimeError("problems")

        with patch.object(self.format, "_create"):
            with patch.object(self.format, "_pre_create") as m:
                m.side_effect = _fail
                six.assertRaisesRegex(self, RuntimeError, "problems",
                                      self.format.create)
                self.assertFalse(self.format._create.called)  # pylint: disable=no-member
                self.assertFalse(self.format.exists)

        # _create raises -> no _post_create -> exists == False
        with patch.object(self.format, "_create") as m:
            m.side_effect = _fail
            six.assertRaisesRegex(self, RuntimeError, "problems",
                                  self.format.create)
            self.assertTrue(self.format._create.called)  # pylint: disable=no-member
            self.assertFalse(self.format.exists)

        # _create succeeds -> make sure _post_create sets existence
        with patch.object(self.format, "_create"):
            with patch.object(self.format, "_post_create"):
                self.format.create()
                self.assertTrue(self.format._create.called)  # pylint: disable=no-member
                self.assertFalse(self.format.exists)

        # _post_create sets exists to True
        with patch.object(self.format, "_create"):
            self.format.create()
            self.assertTrue(self.format._create.called)  # pylint: disable=no-member
            self.assertTrue(self.format.exists)

        self._test_create_backend()
Exemplo n.º 21
0
 def set_patches(self):
     super(FSMethodsTestCase, self).set_patches()
     self.patchers["udev"] = patch("blivet.formats.fs.udev")
     self.patchers["util"] = patch("blivet.formats.fs.util")
     self.patchers["system_mountpoint"] = patch.object(self.format_class,
                                                       "system_mountpoint",
                                                       new=PropertyMock(return_value='/fake/mountpoint'))
     self.patchers["fs_os"] = patch("blivet.formats.fs.os")
Exemplo n.º 22
0
    def test_setup(self):
        self.device.exists = False
        self.patches["status"].return_value = False
        with patch.object(self.device, "_setup"):
            six.assertRaisesRegex(self, DeviceError, "has not been created",
                                  self.device.setup)
            self.assertFalse(self.device._setup.called)

        self.device.exists = True
        self.patches["status"].return_value = True
        with patch.object(self.device, "_setup"):
            self.device.setup()
            self.assertFalse(self.device._setup.called)

        self.patches["udev"].reset_mock()
        self.device.update_sysfs_path.reset_mock()
        self.patches["status"].return_value = False
        with patch.object(self.device, "_setup"):
            self.device.setup()
            self.assertTrue(self.device._setup.called)

        # called from _pre_setup
        self.assertTrue(self.device.setup_parents.called)

        # called from _post_setup
        self.assertEqual(self.patches["udev"].settle.called,
                         self.setup_calls_udev_settle)
        self.assertEqual(self.device.update_sysfs_path.called,
                         self.setup_updates_sysfs_path)
        self.assertFalse(self.device.update_size.called)

        #
        # a device whose size is 0 will call self.update_size from _post_setup
        #
        self.patches["udev"].reset_mock()
        self.device.update_sysfs_path.reset_mock()
        self.patches["status"].return_value = False
        self.device._size = Size(0)
        with patch.object(self.device, "_setup"):
            self.device.setup()

        # called from _post_setup
        self.assertTrue(self.device.update_size.called)

        self.patches["udev"].reset_mock()
        self.device.update_sysfs_path.reset_mock()
Exemplo n.º 23
0
    def test_ListDevices(self):
        """ Verify that ListDevices returns what it should.

            It should return a dbus.Array w/ signature 'o' containing the
            dbus object path of each device in the DBusBlivet.
        """
        device_ids = [1, 11, 20, 101]
        dbus_devices = [mock_dbus_device(i) for i in device_ids]
        object_paths = dbus.Array((d.object_path for d in sorted(dbus_devices, key=lambda d: d.id)),
                                  signature='o')
        with patch.object(self.dbus_object, "_list_dbus_devices", return_value=dbus_devices):
            self.assertEqual(self.dbus_object.ListDevices(), object_paths)

        # now test the devices property for good measure. it should have the
        # same value.
        with patch.object(self.dbus_object, "_list_dbus_devices", return_value=dbus_devices):
            self.assertEqual(self.dbus_object.Get(BLIVET_INTERFACE, 'Devices'), object_paths)
Exemplo n.º 24
0
    def test_skip_activate(self):
        pv = StorageDevice("pv1",
                           fmt=blivet.formats.get_format("lvmpv"),
                           size=Size("1 GiB"),
                           exists=True)
        vg = LVMVolumeGroupDevice("testvg", parents=[pv], exists=True)
        lv = LVMLogicalVolumeDevice("data_lv",
                                    parents=[vg],
                                    size=Size("500 MiB"),
                                    exists=True)

        with patch("blivet.devices.lvm.blockdev.lvm") as lvm:
            with patch.object(lv, "_pre_setup"):
                lv.setup()
                self.assertTrue(
                    lvm.lvactivate.called_with(vg.name,
                                               lv.lvname,
                                               ignore_skip=False))

        lv.ignore_skip_activation += 1
        with patch("blivet.devices.lvm.blockdev.lvm") as lvm:
            with patch.object(lv, "_pre_setup"):
                lv.setup()
                self.assertTrue(
                    lvm.lvactivate.called_with(vg.name,
                                               lv.lvname,
                                               ignore_skip=True))

        lv.ignore_skip_activation += 1
        with patch("blivet.devices.lvm.blockdev.lvm") as lvm:
            with patch.object(lv, "_pre_setup"):
                lv.setup()
                self.assertTrue(
                    lvm.lvactivate.called_with(vg.name,
                                               lv.lvname,
                                               ignore_skip=True))

        lv.ignore_skip_activation -= 2
        with patch("blivet.devices.lvm.blockdev.lvm") as lvm:
            with patch.object(lv, "_pre_setup"):
                lv.setup()
                self.assertTrue(
                    lvm.lvactivate.called_with(vg.name,
                                               lv.lvname,
                                               ignore_skip=False))
Exemplo n.º 25
0
    def test_disk_is_empty(self):
        disk = StorageDevice("testdisk", exists=True)
        disk._partitionable = True
        with patch.object(disk, "_format") as fmt:
            fmt.type = "disklabel"
            self.assertTrue(disk.is_empty)

            PartitionDevice("testpart1", exists=True, parents=[disk])
            self.assertFalse(disk.is_empty)
Exemplo n.º 26
0
    def test_RemoveDevice(self):
        self.dbus_object._blivet.reset_mock()
        dbus_device = mock_dbus_device(23)
        with patch.object(self.dbus_object._manager, "get_object_by_path", return_value=dbus_device):
            with patch("blivet.dbus.blivet.isinstance", return_value=True):
                self.dbus_object.RemoveDevice(dbus_device.object_path)

        self.dbus_object._blivet.devicetree.recursive_remove.assert_called_once_with(dbus_device._device)
        self.dbus_object._blivet.reset_mock()
Exemplo n.º 27
0
 def _test_create_backend(self):
     with patch.object(self.format, "_mkfs"):
         self.format.exists = False
         self.format.create()
         # pylint: disable=no-member
         self.format._mkfs.do_task.assert_called_with(
             options=None,
             label=not self.format.relabels(),
             set_uuid=self.format.can_set_uuid())
Exemplo n.º 28
0
 def set_patches(self):
     super(FSMethodsTestCase, self).set_patches()
     self.patchers["udev"] = patch("blivet.formats.fs.udev")
     self.patchers["util"] = patch("blivet.formats.fs.util")
     self.patchers["system_mountpoint"] = patch.object(
         self.format_class,
         "system_mountpoint",
         new=PropertyMock(return_value='/fake/mountpoint'))
     self.patchers["fs_os"] = patch("blivet.formats.fs.os")
Exemplo n.º 29
0
    def test_setup(self):
        self.device.exists = False
        self.patches["status"].return_value = False
        with patch.object(self.device, "_setup"):
            six.assertRaisesRegex(self, DeviceError, "has not been created", self.device.setup)
            self.assertFalse(self.device._setup.called)

        self.device.exists = True
        self.patches["status"].return_value = True
        with patch.object(self.device, "_setup"):
            self.device.setup()
            self.assertFalse(self.device._setup.called)

        self.patches["udev"].reset_mock()
        self.device.update_sysfs_path.reset_mock()
        self.patches["status"].return_value = False
        with patch.object(self.device, "_setup"):
            self.device.setup()
            self.assertTrue(self.device._setup.called)

        # called from _pre_setup
        self.assertTrue(self.device.setup_parents.called)

        # called from _post_setup
        self.assertEqual(self.patches["udev"].settle.called, self.setup_calls_udev_settle)
        self.assertEqual(self.device.update_sysfs_path.called, self.setup_updates_sysfs_path)
        self.assertFalse(self.device.update_size.called)

        #
        # a device whose size is 0 will call self.update_size from _post_setup
        #
        self.patches["udev"].reset_mock()
        self.device.update_sysfs_path.reset_mock()
        self.patches["status"].return_value = False
        self.device._size = Size(0)
        with patch.object(self.device, "_setup"):
            self.device.setup()

        # called from _post_setup
        self.assertTrue(self.device.update_size.called)

        self.patches["udev"].reset_mock()
        self.device.update_sysfs_path.reset_mock()
Exemplo n.º 30
0
 def _test_create_backend(self):
     with patch.object(self.format, "_mkfs"):
         self.format.exists = False
         self.format.create()
         # pylint: disable=no-member
         self.format._mkfs.do_task.assert_called_with(
             options=None,
             label=not self.format.relabels(),
             set_uuid=self.format.can_set_uuid()
         )
Exemplo n.º 31
0
    def test_create(self):
        # fmt cannot exist
        self.format.exists = True
        with patch.object(self.format, "_create"):
            self.set_os_path_exists(True)
            six.assertRaisesRegex(self, DeviceFormatError, "format already exists", self.format.create)
            self.assertFalse(self.format._create.called)  # pylint: disable=no-member
        self.format.exists = False

        # device must be accessible
        with patch.object(self.format, "_create"):
            # device must be accessible
            self.set_os_path_exists(False)
            six.assertRaisesRegex(self, DeviceFormatError, "invalid device specification", self.format.create)
            self.assertFalse(self.format._create.called)  # pylint: disable=no-member
        self.set_os_path_exists(True)

        # _pre_create raises -> no _create
        self.assertFalse(self.format.exists)

        # pylint: disable=unused-argument
        def _fail(*args, **kwargs):
            raise RuntimeError("problems")

        with patch.object(self.format, "_create"):
            with patch.object(self.format, "_pre_create") as m:
                m.side_effect = _fail
                six.assertRaisesRegex(self, RuntimeError, "problems", self.format.create)
                self.assertFalse(self.format._create.called)  # pylint: disable=no-member
                self.assertFalse(self.format.exists)

        # _create raises -> no _post_create -> exists == False
        with patch.object(self.format, "_create") as m:
            m.side_effect = _fail
            six.assertRaisesRegex(self, RuntimeError, "problems", self.format.create)
            self.assertTrue(self.format._create.called)  # pylint: disable=no-member
            self.assertFalse(self.format.exists)

        # _create succeeds -> make sure _post_create sets existence
        with patch.object(self.format, "_create"):
            with patch.object(self.format, "_post_create"):
                self.format.create()
                self.assertTrue(self.format._create.called)  # pylint: disable=no-member
                self.assertFalse(self.format.exists)

        # _post_create sets exists to True
        with patch.object(self.format, "_create"):
            self.format.create()
            self.assertTrue(self.format._create.called)  # pylint: disable=no-member
            self.assertTrue(self.format.exists)

        self._test_create_backend()
Exemplo n.º 32
0
    def test_resizable(self):
        """ Test resizable property of unformatted devices. """
        # Devices with no (or unrecognized) formatting should not be resizable.
        device = StorageDevice("testdev1", exists=True, size=Size("100 G"), fmt=get_format("ext4", exists=True))
        device._resizable = True
        with patch.object(device, "_format", exists=True, resizable=True):
            self.assertTrue(device.resizable)

        device = StorageDevice("testdev1", exists=True, size=Size("100 G"))
        device._resizable = True
        self.assertFalse(device.resizable)
Exemplo n.º 33
0
    def test_resizable(self):
        """ Test resizable property of unformatted devices. """
        # Devices with no (or unrecognized) formatting should not be resizable.
        device = StorageDevice("testdev1", exists=True, size=Size("100 G"), fmt=get_format("ext4", exists=True))
        device._resizable = True
        with patch.object(device, "_format", exists=True, resizable=True):
            self.assertTrue(device.resizable)

        device = StorageDevice("testdev1", exists=True, size=Size("100 G"))
        device._resizable = True
        self.assertFalse(device.resizable)
Exemplo n.º 34
0
    def test_exclusive_disks_multipath_2(self):
        # all disks exclusive -> mpath should also be exclusive
        self.tree.exclusive_disks = ["sda", "sdb", "sdc"]
        with patch.object(self.tree, "hide") as hide:
            self.tree._hide_ignored_disks()
            self.assertFalse(hide.called)

        self.tree._hide_ignored_disks()
        self.assertTrue(self.sda in self.tree.devices)
        self.assertTrue(self.sdb in self.tree.devices)
        self.assertTrue(self.sdc in self.tree.devices)
        self.assertTrue(self.mpatha in self.tree.devices)
Exemplo n.º 35
0
    def test_recursive_remove(self):
        dt = DeviceTree()
        dev1 = StorageDevice("dev1", exists=False, parents=[])
        dev2 = StorageDevice("dev2", exists=False, parents=[dev1])
        dt._add_device(dev1)
        dt._add_device(dev2)

        # normal
        self.assertTrue(dev1 in dt.devices)
        self.assertTrue(dev2 in dt.devices)
        self.assertEqual(dt.actions._actions, list())
        dt.recursive_remove(dev1)
        self.assertFalse(dev1 in dt.devices)
        self.assertFalse(dev2 in dt.devices)
        self.assertNotEqual(dt.actions._actions, list())

        dt.reset()
        dt._add_device(dev1)
        dt._add_device(dev2, new=False)  # restore parent/child relationships

        # remove_device clears descendants and formatting but preserves the device
        dev1.format = get_format("swap")
        self.assertEqual(dev1.format.type, "swap")
        self.assertEqual(dt.actions._actions, list())
        dt.recursive_remove(dev1, remove_device=False)
        self.assertTrue(dev1 in dt.devices)
        self.assertFalse(dev2 in dt.devices)
        self.assertEqual(dev1.format.type, None)
        self.assertNotEqual(dt.actions._actions, list())

        dt.reset()
        dt._add_device(dev1)
        dt._add_device(dev2, new=False)  # restore parent/child relationships

        # actions=False performs the removals without scheduling actions
        self.assertEqual(dt.actions._actions, list())
        dt.recursive_remove(dev1, actions=False)
        self.assertFalse(dev1 in dt.devices)
        self.assertFalse(dev2 in dt.devices)
        self.assertEqual(dt.actions._actions, list())

        dt.reset()
        dt._add_device(dev1)
        dt._add_device(dev2, new=False)  # restore parent/child relationships

        # modparent only works when actions=False is passed
        with patch.object(dt, "_remove_device") as remove_device:
            dt.recursive_remove(dev1, actions=False)
            remove_device.assert_called_with(dev1, modparent=True)

            dt.recursive_remove(dev1, actions=False, modparent=False)
            remove_device.assert_called_with(dev1, modparent=False)
Exemplo n.º 36
0
    def test_recursive_remove(self):
        dt = DeviceTree()
        dev1 = StorageDevice("dev1", exists=False, parents=[])
        dev2 = StorageDevice("dev2", exists=False, parents=[dev1])
        dt._add_device(dev1)
        dt._add_device(dev2)

        # normal
        self.assertTrue(dev1 in dt.devices)
        self.assertTrue(dev2 in dt.devices)
        self.assertEqual(dt.actions._actions, list())
        dt.recursive_remove(dev1)
        self.assertFalse(dev1 in dt.devices)
        self.assertFalse(dev2 in dt.devices)
        self.assertNotEqual(dt.actions._actions, list())

        dt.reset()
        dt._add_device(dev1)
        dt._add_device(dev2, new=False)  # restore parent/child relationships

        # remove_device clears descendants and formatting but preserves the device
        dev1.format = get_format("swap")
        self.assertEqual(dev1.format.type, "swap")
        self.assertEqual(dt.actions._actions, list())
        dt.recursive_remove(dev1, remove_device=False)
        self.assertTrue(dev1 in dt.devices)
        self.assertFalse(dev2 in dt.devices)
        self.assertEqual(dev1.format.type, None)
        self.assertNotEqual(dt.actions._actions, list())

        dt.reset()
        dt._add_device(dev1)
        dt._add_device(dev2, new=False)  # restore parent/child relationships

        # actions=False performs the removals without scheduling actions
        self.assertEqual(dt.actions._actions, list())
        dt.recursive_remove(dev1, actions=False)
        self.assertFalse(dev1 in dt.devices)
        self.assertFalse(dev2 in dt.devices)
        self.assertEqual(dt.actions._actions, list())

        dt.reset()
        dt._add_device(dev1)
        dt._add_device(dev2, new=False)  # restore parent/child relationships

        # modparent only works when actions=False is passed
        with patch.object(dt, "_remove_device") as remove_device:
            dt.recursive_remove(dev1, actions=False)
            remove_device.assert_called_with(dev1, modparent=True)

            dt.recursive_remove(dev1, actions=False, modparent=False)
            remove_device.assert_called_with(dev1, modparent=False)
Exemplo n.º 37
0
    def test_teardown(self):
        # device must be accessible

        # fmt must exist
        self.format.exists = False
        with patch.object(self.format, "_teardown"):
            self.set_os_path_exists(True)
            six.assertRaisesRegex(self, DeviceFormatError,
                                  "has not been created", self.format.teardown)
            self.assertFalse(self.format._teardown.called)  # pylint: disable=no-member
        self.format.exists = True

        # FIXME -- _pre_teardown should be checking for an accessible device
        # device must be accessible
        # with patch.object(self.format, "_teardown"):
        #    self.set_os_path_exists(False)
        #    six.assertRaisesRegex(self, DeviceFormatError, "invalid device specification", self.format.teardown)
        #    self.assertFalse(self.format._teardown.called)  # pylint: disable=no-member

        # _teardown fails -> no _post_teardown
        self.patches["status"].return_value = True

        # pylint: disable=unused-argument
        def _fail(*args, **kwargs):
            raise RuntimeError("problems")

        with patch.object(self.format, "_teardown", side_effect=_fail):
            with patch.object(self.format, "_post_teardown"):
                six.assertRaisesRegex(self, RuntimeError, "problems",
                                      self.format.teardown)
                self.assertFalse(self.format._post_teardown.called)  # pylint: disable=no-member

        # _teardown succeeds -> _post_teardown
        with patch.object(self.format, "_teardown"):
            with patch.object(self.format, "_post_teardown"):
                self.format.teardown()
                self.assertTrue(self.format._post_teardown.called)  # pylint: disable=no-member

        self._test_teardown_backend()
Exemplo n.º 38
0
    def test_exclusive_disks_multipath_1(self):
        # multipath is exclusive -> all disks should be exclusive
        self.tree.ignored_disks = []
        self.tree.exclusive_disks = ["mpatha"]

        with patch.object(self.tree, "hide") as hide:
            self.tree._hide_ignored_disks()
            self.assertFalse(hide.called)

        self.tree._hide_ignored_disks()
        self.assertTrue(self.sda in self.tree.devices)
        self.assertTrue(self.sdb in self.tree.devices)
        self.assertTrue(self.sdc in self.tree.devices)
        self.assertTrue(self.mpatha in self.tree.devices)
Exemplo n.º 39
0
    def test_exclusive_disks_multipath_3(self):
        # some disks exclusive -> mpath should be hidden
        self.tree.exclusive_disks = ["sda", "sdb"]
        with patch.object(self.tree, "hide") as hide:
            self.tree._hide_ignored_disks()
            hide.assert_any_call(self.mpatha)
            hide.assert_any_call(self.sdc)

        # verify that hide works as expected
        self.tree._hide_ignored_disks()
        self.assertTrue(self.sda in self.tree.devices)
        self.assertTrue(self.sdb in self.tree.devices)
        self.assertFalse(self.sdc in self.tree.devices)
        self.assertFalse(self.mpatha in self.tree.devices)
Exemplo n.º 40
0
    def test_teardown(self):
        self.device.exists = False
        with patch.object(self.device, "_teardown"):
            six.assertRaisesRegex(self, DeviceError, "has not been created", self.device.teardown)
            self.assertFalse(self.device._teardown.called)

        self.device.exists = True
        self.patches["status"].return_value = False
        with patch.object(self.device, "_teardown"):
            self.device.teardown()
            self.assertFalse(self.device._teardown.called)

        self.patches["udev"].reset_mock()
        self.device.update_sysfs_path.reset_mock()
        self.patches["status"].return_value = True
        with patch.object(self.device, "_teardown"):
            self.device.teardown()
            self.assertTrue(self.teardown_method_mock.called)

        self.assertEqual(self.patches["udev"].settle.called, self.teardown_calls_udev_settle)
        self.assertEqual(self.device.update_sysfs_path.called, self.teardown_updates_sysfs_path)
        self.patches["udev"].reset_mock()
        self.device.update_sysfs_path.reset_mock()
Exemplo n.º 41
0
    def test_device_name(self):
        # check that devicetree.names property contains all device's names

        # mock lvs_info to avoid blockdev call allowing run as non-root
        with patch.object(LVsInfo, 'cache',
                          new_callable=PropertyMock) as mock_lvs_cache:
            mock_lvs_cache.return_value = {
                "sdmock": "dummy",
                "testvg-testlv": "dummy"
            }

            tree = DeviceTree()
            dev_names = ["sda", "sdb", "sdc"]

            for dev_name in dev_names:
                dev = DiskDevice(dev_name, size=Size("1 GiB"))
                tree._add_device(dev)
                self.assertTrue(dev in tree.devices)
                self.assertTrue(dev.name in tree.names)

            dev.format = get_format("lvmpv", device=dev.path)
            vg = LVMVolumeGroupDevice("testvg", parents=[dev])
            tree._add_device(vg)
            dev_names.append(vg.name)

            lv = LVMLogicalVolumeDevice("testlv", parents=[vg])
            tree._add_device(lv)
            dev_names.append(lv.name)

            # frobnicate a bit with the hidden status of the devices:
            # * hide sda
            # * hide and unhide again sdb
            # * leave sdc unchanged
            tree.hide(tree.get_device_by_name("sda"))
            tree.hide(tree.get_device_by_name("sdb"))
            tree.unhide(tree.get_device_by_name("sdb", hidden=True))

            # some lvs names may be already present in the system (mocked)
            lv_info = list(lvs_info.cache.keys())

            # all devices should still be present in the tree.names
            self.assertEqual(set(tree.names), set(lv_info + dev_names))

            # "remove" the LV, it should no longer be in the list
            tree.actions._actions.append(
                Mock(device=lv,
                     type=ACTION_TYPE_DESTROY,
                     obj=ACTION_OBJECT_DEVICE))
            tree._remove_device(lv)
            self.assertFalse(lv.name in tree.names)
Exemplo n.º 42
0
 def set_patches(self):
     self.patchers["update_sysfs_path"] = patch.object(self.device, "update_sysfs_path")
     self.patchers["udev"] = patch("blivet.devices.storage.udev")
     self.patchers["update_size"] = patch.object(self.device, "update_size")
     self.patchers["setup_parents"] = patch.object(self.device, "setup_parents")
     self.patchers["teardown_parents"] = patch.object(self.device, "teardown_parents")
     self.patchers["media_present"] = patch.object(self.device_class, "media_present",
                                                   new=PropertyMock(return_value=True))
     self.patchers["status"] = patch.object(self.device_class, "status", new=PropertyMock())
Exemplo n.º 43
0
 def set_patches(self):
     self.patchers["update_sysfs_path"] = patch.object(self.device, "update_sysfs_path")
     self.patchers["udev"] = patch("blivet.devices.storage.udev")
     self.patchers["update_size"] = patch.object(self.device, "update_size")
     self.patchers["setup_parents"] = patch.object(self.device, "setup_parents")
     self.patchers["teardown_parents"] = patch.object(self.device, "teardown_parents")
     self.patchers["media_present"] = patch.object(self.device_class, "media_present",
                                                   new=PropertyMock(return_value=True))
     self.patchers["status"] = patch.object(self.device_class, "status", new=PropertyMock())
Exemplo n.º 44
0
    def test_teardown(self):
        # device must be accessible

        # fmt must exist
        self.format.exists = False
        with patch.object(self.format, "_teardown"):
            self.set_os_path_exists(True)
            six.assertRaisesRegex(self, DeviceFormatError, "has not been created", self.format.teardown)
            self.assertFalse(self.format._teardown.called)  # pylint: disable=no-member
        self.format.exists = True

        # FIXME -- _pre_teardown should be checking for an accessible device
        # device must be accessible
        # with patch.object(self.format, "_teardown"):
        #    self.set_os_path_exists(False)
        #    six.assertRaisesRegex(self, DeviceFormatError, "invalid device specification", self.format.teardown)
        #    self.assertFalse(self.format._teardown.called)  # pylint: disable=no-member

        # _teardown fails -> no _post_teardown
        self.patches["status"].return_value = True

        # pylint: disable=unused-argument
        def _fail(*args, **kwargs):
            raise RuntimeError("problems")

        with patch.object(self.format, "_teardown", side_effect=_fail):
            with patch.object(self.format, "_post_teardown"):
                six.assertRaisesRegex(self, RuntimeError, "problems", self.format.teardown)
                self.assertFalse(self.format._post_teardown.called)  # pylint: disable=no-member

        # _teardown succeeds -> _post_teardown
        with patch.object(self.format, "_teardown"):
            with patch.object(self.format, "_post_teardown"):
                self.format.teardown()
                self.assertTrue(self.format._post_teardown.called)  # pylint: disable=no-member

        self._test_teardown_backend()
Exemplo n.º 45
0
    def test_new_lv_from_lvs(self):
        b = blivet.Blivet()
        pv = StorageDevice("pv1", fmt=blivet.formats.get_format("lvmpv"),
                           size=Size("1 GiB"), exists=True)
        vg = LVMVolumeGroupDevice("testvg", parents=[pv], exists=True)
        lv1 = LVMLogicalVolumeDevice("data_lv", parents=[vg], size=Size("500 MiB"), exists=True)
        lv2 = LVMLogicalVolumeDevice("metadata_lv", parents=[vg], size=Size("50 MiB"), exists=True)

        for dev in (pv, vg, lv1, lv2):
            b.devicetree._add_device(dev)

        # check that all the above devices are in the expected places
        self.assertEqual(set(b.devices), {pv, vg, lv1, lv2})
        self.assertEqual(set(b.vgs), {vg})
        self.assertEqual(set(b.lvs), {lv1, lv2})
        self.assertEqual(set(b.vgs[0].lvs), {lv1, lv2})

        self.assertEqual(vg.size, Size("1020 MiB"))
        self.assertEqual(lv1.size, Size("500 MiB"))
        self.assertEqual(lv2.size, Size("50 MiB"))

        # combine the two LVs into a thin pool (the LVs should become its internal LVs)
        pool = b.new_lv_from_lvs(vg, name="pool", seg_type="thin-pool", from_lvs=(lv1, lv2))

        # add the pool LV into the devicetree
        b.devicetree._add_device(pool)

        self.assertEqual(set(b.devices), {pv, vg, pool})
        self.assertEqual(set(b.vgs), {vg})
        self.assertEqual(set(b.lvs), {pool})
        self.assertEqual(set(b.vgs[0].lvs), {pool})
        self.assertEqual(set(b.vgs[0].lvs[0]._internal_lvs), {lv1, lv2})

        self.assertTrue(lv1.is_internal_lv)
        self.assertEqual(lv1.int_lv_type, LVMInternalLVtype.data)
        self.assertEqual(lv1.size, Size("500 MiB"))
        self.assertTrue(lv2.is_internal_lv)
        self.assertEqual(lv2.int_lv_type, LVMInternalLVtype.meta)
        self.assertEqual(lv2.size, Size("50 MiB"))

        self.assertEqual(pool.name, "testvg-pool")
        self.assertEqual(pool.size, Size("500 MiB"))
        self.assertEqual(pool.metadata_size, Size("50 MiB"))
        self.assertIs(pool.vg, vg)

        with patch("blivet.devices.lvm.blockdev.lvm") as lvm:
            with patch.object(pool, "_pre_create"):
                pool.create()
                self.assertTrue(lvm.thpool_convert.called)
Exemplo n.º 46
0
    def test_ignored_disks_multipath_2(self):
        # all disks ignored -> mpath should be hidden
        self.tree.ignored_disks = ["sda", "sdb", "sdc"]
        self.tree.exclusive_disks = []

        with patch.object(self.tree, "hide") as hide:
            self.tree._hide_ignored_disks()
            hide.assert_any_call(self.mpatha)
            hide.assert_any_call(self.sda)
            hide.assert_any_call(self.sdb)
            hide.assert_any_call(self.sdc)

        self.tree._hide_ignored_disks()
        self.assertFalse(self.sda in self.tree.devices)
        self.assertFalse(self.sdb in self.tree.devices)
        self.assertFalse(self.sdc in self.tree.devices)
        self.assertFalse(self.mpatha in self.tree.devices)
Exemplo n.º 47
0
    def test_ctor_parted_partition_error_handling(self):
        disk = StorageDevice("testdisk", exists=True)
        disk._partitionable = True
        with patch.object(disk, "_format") as fmt:
            fmt.type = "disklabel"
            self.assertTrue(disk.partitioned)

            fmt.supported = True

            # Normal case, no exn.
            device = PartitionDevice("testpart1", exists=True, parents=[disk])
            self.assertIn(device, disk.children)
            device.parents.remove(disk)
            self.assertEqual(len(disk.children), 0, msg="disk has children when it should not")

            # Parted doesn't find a partition, exn is raised.
            fmt.parted_disk.getPartitionByPath.return_value = None
            self.assertRaises(DeviceError, PartitionDevice, "testpart1", exists=True, parents=[disk])
            self.assertEqual(len(disk.children), 0, msg="device is still attached to disk in spite of ctor error")
Exemplo n.º 48
0
    def test_setup(self):
        # fmt must exist
        self.format.exists = False
        with patch.object(self.format, "_setup"):
            self.set_os_path_exists(True)
            six.assertRaisesRegex(self, DeviceFormatError, "has not been created", self.format.setup)
            # _pre_setup raises exn -> no _setup
            self.assertFalse(self.format._setup.called)  # pylint: disable=no-member
        self.format.exists = True

        # device must be accessible
        with patch.object(self.format, "_setup"):
            self.set_os_path_exists(False)
            six.assertRaisesRegex(self, DeviceFormatError, "invalid|does not exist", self.format.setup)
            # _pre_setup raises exn -> no _setup
            self.assertFalse(self.format._setup.called)  # pylint: disable=no-member

        # _pre_setup returns False -> no _setup
        with patch.object(self.format, "_setup"):
            self.set_os_path_exists(True)
            self.patches["status"].return_value = True
            self.format.setup()
            self.assertEqual(self.format._setup.called, isinstance(self, FSMethodsTestCase))  # pylint: disable=no-member

        # _setup fails -> no _post_setup
        self.patches["status"].return_value = False

        # pylint: disable=unused-argument
        def _fail(*args, **kwargs):
            raise RuntimeError("problems")

        with patch.object(self.format, "_setup", side_effect=_fail):
            with patch.object(self.format, "_post_setup"):
                six.assertRaisesRegex(self, RuntimeError, "problems", self.format.setup)
                self.assertFalse(self.format._post_setup.called)  # pylint: disable=no-member

        # _setup succeeds -> _post_setup
        with patch.object(self.format, "_setup"):
            with patch.object(self.format, "_post_setup"):
                self.format.setup()
                self.assertTrue(self.format._post_setup.called)  # pylint: disable=no-member

        self._test_setup_backend()
Exemplo n.º 49
0
    def test_weight_1(self, *patches):
        arch = patches[0]

        dev = PartitionDevice('req1', exists=False)

        arch.is_x86.return_value = False
        arch.is_efi.return_value = False
        arch.is_arm.return_value = False
        arch.is_ppc.return_value = False

        dev.req_base_weight = -7
        self.assertEqual(dev.weight, -7)
        dev.req_base_weight = None

        with patch.object(dev, "_format") as fmt:
            fmt.mountable = True

            # weights for / and /boot are not platform-specific (except for arm)
            fmt.mountpoint = "/"
            self.assertEqual(dev.weight, 0)

            fmt.mountpoint = "/boot"
            self.assertEqual(dev.weight, 2000)

            #
            # x86 (BIOS)
            #
            arch.is_x86.return_value = True
            arch.is_efi.return_value = False

            # user-specified weight should override other logic
            dev.req_base_weight = -7
            self.assertEqual(dev.weight, -7)
            dev.req_base_weight = None

            fmt.mountpoint = ""
            self.assertEqual(dev.weight, 0)

            fmt.type = "biosboot"
            self.assertEqual(dev.weight, 5000)

            fmt.mountpoint = "/boot/efi"
            fmt.type = "efi"
            self.assertEqual(dev.weight, 0)

            #
            # UEFI
            #
            arch.is_x86.return_value = False
            arch.is_efi.return_value = True
            self.assertEqual(dev.weight, 5000)

            fmt.type = "biosboot"
            self.assertEqual(dev.weight, 0)

            fmt.mountpoint = "/"
            self.assertEqual(dev.weight, 0)

            #
            # arm
            #
            arch.is_x86.return_value = False
            arch.is_efi.return_value = False
            arch.is_arm.return_value = True

            fmt.mountpoint = "/"
            self.assertEqual(dev.weight, -100)

            #
            # ppc
            #
            arch.is_arm.return_value = False
            arch.is_ppc.return_value = True
            arch.is_pmac.return_value = False
            arch.is_ipseries.return_value = False

            fmt.mountpoint = "/"
            self.assertEqual(dev.weight, 0)

            fmt.type = "prepboot"
            self.assertEqual(dev.weight, 0)

            fmt.type = "appleboot"
            self.assertEqual(dev.weight, 0)

            arch.is_pmac.return_value = True
            self.assertEqual(dev.weight, 5000)

            fmt.type = "prepboot"
            self.assertEqual(dev.weight, 0)

            arch.is_pmac.return_value = False
            arch.is_ipseries.return_value = True
            self.assertEqual(dev.weight, 5000)

            fmt.type = "appleboot"
            self.assertEqual(dev.weight, 0)

            fmt.mountpoint = "/boot/efi"
            fmt.type = "efi"
            self.assertEqual(dev.weight, 0)

            fmt.type = "biosboot"
            self.assertEqual(dev.weight, 0)

            fmt.mountpoint = "/"
            self.assertEqual(dev.weight, 0)

            fmt.mountpoint = "/boot"
            self.assertEqual(dev.weight, 2000)
Exemplo n.º 50
0
 def set_patches(self):
     super(LUKSMethodsTestCase, self).set_patches()
     self.patchers["configured"] = patch.object(self.format_class, "configured", new=PropertyMock(return_value=True))
     self.patchers["has_key"] = patch.object(self.format_class, "has_key", new=PropertyMock(return_value=True))
     self.patchers["blockdev"] = patch("blivet.formats.luks.blockdev")
Exemplo n.º 51
0
 def set_patches(self):
     super(PartitionDeviceMethodsTestCase, self).set_patches()
     self.patchers["parted_partition"] = patch.object(self.device_class, "parted_partition",
                                                      new=PropertyMock())
     self.patchers["disk"] = patch.object(self.device_class, "disk", new=PropertyMock())
Exemplo n.º 52
0
 def set_patches(self):
     super(LVMVolumeGroupDeviceMethodsTestCase, self).set_patches()
     self.patchers["complete"] = patch.object(self.device_class, "complete",
                                              new=PropertyMock(return_value=True))
Exemplo n.º 53
0
 def _test_setup_backend(self):
     with patch.object(self.format, "_mount"):
         self.patches["fs_os"].path.normpath.return_value = sentinel.mountpoint
         self.format.setup()
         self.format._mount.do_task.assert_called_with(sentinel.mountpoint, options="")  # pylint: disable=no-member
Exemplo n.º 54
0
    def test_hide_ignored_disks(self):
        tree = DeviceTree()

        sda = DiskDevice("sda")
        sdb = DiskDevice("sdb")
        sdc = DiskDevice("sdc")

        tree._add_device(sda)
        tree._add_device(sdb)
        tree._add_device(sdc)

        self.assertTrue(sda in tree.devices)
        self.assertTrue(sdb in tree.devices)
        self.assertTrue(sdc in tree.devices)

        # test ignored_disks
        tree.ignored_disks = ["sdb"]

        # verify hide is called as expected
        with patch.object(tree, "hide") as hide:
            tree._hide_ignored_disks()
            hide.assert_called_with(sdb)

        # verify that hide works as expected
        tree._hide_ignored_disks()
        self.assertTrue(sda in tree.devices)
        self.assertFalse(sdb in tree.devices)
        self.assertTrue(sdc in tree.devices)

        # unhide sdb and make sure it works
        tree.unhide(sdb)
        self.assertTrue(sda in tree.devices)
        self.assertTrue(sdb in tree.devices)
        self.assertTrue(sdc in tree.devices)

        # test exclusive_disks
        tree.ignored_disks = []
        tree.exclusive_disks = ["sdc"]
        with patch.object(tree, "hide") as hide:
            tree._hide_ignored_disks()
            hide.assert_any_call(sda)
            hide.assert_any_call(sdb)

        tree._hide_ignored_disks()
        self.assertFalse(sda in tree.devices)
        self.assertFalse(sdb in tree.devices)
        self.assertTrue(sdc in tree.devices)

        tree.unhide(sda)
        tree.unhide(sdb)
        self.assertTrue(sda in tree.devices)
        self.assertTrue(sdb in tree.devices)
        self.assertTrue(sdc in tree.devices)

        # now test exclusive_disks special cases for multipath
        sda.format = get_format("multipath_member", exists=True)
        sdb.format = get_format("multipath_member", exists=True)
        sdc.format = get_format("multipath_member", exists=True)
        mpatha = MultipathDevice("mpatha", parents=[sda, sdb, sdc])
        tree._add_device(mpatha)

        tree.ignored_disks = []
        tree.exclusive_disks = ["mpatha"]

        with patch.object(tree, "hide") as hide:
            tree._hide_ignored_disks()
            self.assertFalse(hide.called)

        tree._hide_ignored_disks()
        self.assertTrue(sda in tree.devices)
        self.assertTrue(sdb in tree.devices)
        self.assertTrue(sdc in tree.devices)
        self.assertTrue(mpatha in tree.devices)

        # all members in exclusive_disks implies the mpath in exclusive_disks
        tree.exclusive_disks = ["sda", "sdb", "sdc"]
        with patch.object(tree, "hide") as hide:
            tree._hide_ignored_disks()
            self.assertFalse(hide.called)

        tree._hide_ignored_disks()
        self.assertTrue(sda in tree.devices)
        self.assertTrue(sdb in tree.devices)
        self.assertTrue(sdc in tree.devices)
        self.assertTrue(mpatha in tree.devices)

        tree.exclusive_disks = ["sda", "sdb"]
        with patch.object(tree, "hide") as hide:
            tree._hide_ignored_disks()
            hide.assert_any_call(mpatha)
            hide.assert_any_call(sdc)

        # verify that hide works as expected
        tree._hide_ignored_disks()
        self.assertTrue(sda in tree.devices)
        self.assertTrue(sdb in tree.devices)
        self.assertFalse(sdc in tree.devices)
        self.assertFalse(mpatha in tree.devices)
Exemplo n.º 55
0
 def set_patches(self):
     # self.patchers["update_sysfs_path"] = patch.object(self.device, "update_sysfs_path")
     self.patchers["status"] = patch.object(self.format_class, "status", new=PropertyMock(return_value=False))
     self.patchers["os"] = patch("blivet.formats.os")