Skip to content

Commit

Permalink
Merge pull request #692 from dwlehman/missing-deps-test-fixups
Browse files Browse the repository at this point in the history
Fixups for missing dependency tests.
  • Loading branch information
dwlehman authored Jul 11, 2018
2 parents dbbc3d8 + e6bdb9f commit ed4098f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
20 changes: 17 additions & 3 deletions blivet/tasks/availability.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,23 @@ def availability_errors(self):
:returns: [] if the resource is available
:rtype: list of str
"""
if self._availability_errors is None or not CACHE_AVAILABILITY:
self._availability_errors = self._method.availability_errors(self)
return self._availability_errors[:]
_errors = list()

# Prepare error cache and return value based on current caching setting.
if CACHE_AVAILABILITY:
_errors = self._availability_errors
else:
self._availability_errors = None

# Check for errors if necessary.
if self._availability_errors is None:
_errors = self._method.availability_errors(self)

# Update error cache if necessary.
if CACHE_AVAILABILITY and self._availability_errors is None:
self._availability_errors = _errors[:]

return _errors

@property
def available(self):
Expand Down
35 changes: 17 additions & 18 deletions tests/devices_test/dependencies_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from blivet.deviceaction import ActionDestroyFormat

from blivet.devices import DiskDevice
from blivet.devices import DiskFile
from blivet.devices import LUKSDevice
from blivet.devices import MDRaidArrayDevice
from blivet.devices import PartitionDevice
Expand Down Expand Up @@ -146,21 +147,19 @@ class MissingWeakDependenciesTestCase(unittest.TestCase):
def setUp(self):
self.addCleanup(self._clean_up)
self.disk1_file = create_sparse_tempfile("disk1", Size("2GiB"))
self.plugins = blockdev.plugin_specs_from_names(blockdev.get_available_plugin_names())

def _clean_up(self):
# reload all libblockdev plugins
blockdev.try_reinit(require_plugins=None, reload=False)
self.load_all_plugins()

for disk in self.bvt.disks:
self.bvt.recursive_remove(disk)
if os.path.exists(self.disk1_file):
os.unlink(self.disk1_file)

self.bvt.devicetree.teardown_disk_images()
for fn in self.bvt.disk_images.values():
if os.path.exists(fn):
os.unlink(fn)
availability.CACHE_AVAILABILITY = True

def load_all_plugins(self):
result, plugins = blockdev.try_reinit(require_plugins=None, reload=False)
result, plugins = blockdev.try_reinit(require_plugins=self.plugins, reload=True)
if not result:
self.fail("Could not reload libblockdev plugins")
return plugins
Expand All @@ -177,19 +176,19 @@ def test_weak_dependencies(self):
# reinitialize blockdev without the plugins
# TODO: uncomment (workaround (1/2) for blivet.reset fail)
# self.unload_all_plugins()
self.bvt.disk_images["disk1"] = self.disk1_file
self.bvt.exclusive_disks = self.bvt.disk_images["disk1"]
try:
self.bvt.reset()
except blockdev.BlockDevNotImplementedError: # pylint: disable=catching-non-exception
self.fail("Improper handling of missing libblockdev plugin")
disk1 = DiskFile(self.disk1_file)

self.bvt.exclusive_disks = [disk1.name]
if os.geteuid() == 0:
try:
self.bvt.reset()
except blockdev.BlockDevNotImplementedError: # pylint: disable=catching-non-exception
self.fail("Improper handling of missing libblockdev plugin")
# TODO: remove line (workaround (2/2) for blivet.reset fail)
self.unload_all_plugins()

disk1 = self.bvt.devicetree.get_device_by_name("disk1")

with six.assertRaisesRegex(self, DependencyError, "requires unavailable_dependencies"):
self.bvt.initialize_disk(disk1)
self.bvt.devicetree._add_device(disk1)
self.bvt.initialize_disk(disk1)

pv = self.bvt.new_partition(size=Size("8GiB"), fmt_type="lvmpv")
pv_fail = self.bvt.new_partition(size=Size("8GiB"), fmt_type="lvmpv")
Expand Down

0 comments on commit ed4098f

Please sign in to comment.