Skip to content

Commit

Permalink
test: Library checking script for tests
Browse files Browse the repository at this point in the history
Storage role development often relies on blivet library and changes in
it. Whether or not is specific feature supported by blivet is usually determined
by its version. That is a cumbersome process, especially when the feature
has not yet been added into blivet and the version has to be guessed.

Added script verifies existence of the feature by using python
introspection and asking for existence of specific item in the library
(e.g. 'blivet.formats.lvmpv.LVMPhysicalVolume.grow_to_fill').
The script is supposed to be used for the tests only.
  • Loading branch information
japokorn committed Apr 30, 2024
1 parent acd20be commit b845afb
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions tests/scripts/does_library_support.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python

# This script checks for blivet compatibility by trying to access
# blivet parts specified by given parameter
# Returns True if part is found, False otherwise

# The script is meant to be a supporting tool for the storage role tests
import sys
import importlib


def is_supported(var):

parts = var.split('.')
imports = ''
obj = sys.modules[__name__]

try:
# create a variable named parts[0] so the subsequent imports work
globals()[parts[0]] = importlib.import_module(parts[0])
except ImportError:
return False

# try to import each part
while parts:
part = parts.pop(0)
imports += part + '.'

try:
importlib.import_module(imports.rstrip('.'))
except ImportError:
break

# generate access to the object for later use
obj = getattr(obj, part)

else:
# break did not happen in the cycle
# it means the whole string was importable
return True

# part of the string was not importable, the rest can be attributes

# get part back to parts to simplify the following loop
parts = [part] + parts

while parts:
part = parts.pop(0)
obj = getattr(obj, part, None)

if obj is None:
return False

return True


if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python %s <parameter>" % sys.argv[0])
sys.exit(-1)

print(is_supported(sys.argv[1]))

0 comments on commit b845afb

Please sign in to comment.