1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
import os
import unittest
import re
import overrides_hack
from utils import fake_path, TestTags, tag_test
from gi.repository import GLib, BlockDev
class LibraryOpsTestCase(unittest.TestCase):
log = ""
# all plugins except for 'btrfs', 'fs' and 'mpath' -- these don't have all
# the dependencies on CentOS/Debian and we don't need them for this test
requested_plugins = BlockDev.plugin_specs_from_names(("crypto", "dm",
"kbd", "loop", "lvm",
"mdraid", "part", "swap"))
orig_config_dir = ""
@classmethod
def setUpClass(cls):
if not BlockDev.is_initialized():
BlockDev.init(cls.requested_plugins, None)
else:
BlockDev.reinit(cls.requested_plugins, True, None)
def setUp(self):
self.orig_config_dir = os.environ.get("LIBBLOCKDEV_CONFIG_DIR", "")
self.addCleanup(self._clean_up)
def _clean_up(self):
# change the sources back and recompile
os.system("sed -ri 's?1024;//test-change?BD_LVM_MAX_LV_SIZE;?' src/plugins/lvm.c > /dev/null")
os.system("make -C src/plugins/ libbd_lvm.la >/dev/null 2>&1")
os.environ["LIBBLOCKDEV_CONFIG_DIR"] = self.orig_config_dir
# try to get everything back to normal by (re)loading all plugins
BlockDev.reinit(self.requested_plugins, True, None)
# recompiles the LVM plugin
@tag_test(TestTags.SLOW, TestTags.CORE, TestTags.SOURCEONLY)
def test_reload(self):
"""Verify that reloading plugins works as expected"""
# max LV size should be something sane (not 1024 bytes)
orig_max_size = BlockDev.lvm_get_max_lv_size()
self.assertNotEqual(orig_max_size, 1024)
# change the sources and recompile
os.system("sed -ri 's?BD_LVM_MAX_LV_SIZE;?1024;//test-change?' src/plugins/lvm.c > /dev/null")
os.system("make -C src/plugins/ libbd_lvm.la >/dev/null 2>&1")
# library should successfully reinitialize without reloading plugins
self.assertTrue(BlockDev.reinit(self.requested_plugins, False, None))
# LVM plugin not reloaded, max LV size should be the same
self.assertEqual(BlockDev.lvm_get_max_lv_size(), orig_max_size)
# library should successfully reinitialize reloading plugins
self.assertTrue(BlockDev.reinit(self.requested_plugins, True, None))
# LVM plugin reloaded, max LV size should be 1024 bytes
self.assertEqual(BlockDev.lvm_get_max_lv_size(), 1024)
# change the sources back and recompile
os.system("sed -ri 's?1024;//test-change?BD_LVM_MAX_LV_SIZE;?' src/plugins/lvm.c > /dev/null")
os.system("make -C src/plugins/ libbd_lvm.la >/dev/null 2>&1")
# library should successfully reinitialize reloading original plugins
self.assertTrue(BlockDev.reinit(self.requested_plugins, True, None))
# recompiles the LVM plugin
@tag_test(TestTags.SLOW, TestTags.SOURCEONLY)
def test_force_plugin(self):
"""Verify that forcing plugin to be used works as expected"""
# library should be successfully initialized
self.assertTrue(BlockDev.is_initialized())
# init() called twice, should give a warning and return False
self.assertFalse(BlockDev.init(self.requested_plugins, None))
# max LV size should be something sane (not 1024 bytes)
orig_max_size = BlockDev.lvm_get_max_lv_size()
self.assertNotEqual(orig_max_size, 1024)
# change the sources and recompile
os.system("sed -ri 's?BD_LVM_MAX_LV_SIZE;?1024;//test-change?' src/plugins/lvm.c > /dev/null")
os.system("make -C src/plugins/ libbd_lvm.la >/dev/null 2>&1")
# proclaim the new build a different plugin
os.system("cp src/plugins/.libs/libbd_lvm.so src/plugins/.libs/libbd_lvm2.so")
# change the sources back and recompile
os.system("sed -ri 's?1024;//test-change?BD_LVM_MAX_LV_SIZE;?' src/plugins/lvm.c > /dev/null")
os.system("make -C src/plugins/ libbd_lvm.la >/dev/null 2>&1")
# force the new plugin to be used
ps = BlockDev.PluginSpec()
ps.name = BlockDev.Plugin.LVM
ps.so_name = "libbd_lvm2.so"
self.assertTrue(BlockDev.reinit([ps], True, None))
# new LVM plugin loaded, max LV size should be 1024 bytes
self.assertEqual(BlockDev.lvm_get_max_lv_size(), 1024)
# clean after ourselves
os.system ("rm -f src/plugins/.libs/libbd_lvm2.so")
# force the old plugin to be used
ps = BlockDev.PluginSpec()
ps.name = BlockDev.Plugin.LVM
ps.so_name = "libbd_lvm.so"
self.assertTrue(BlockDev.reinit([ps], True, None))
self.assertEqual(BlockDev.lvm_get_max_lv_size(), orig_max_size)
# recompiles the LVM plugin
@tag_test(TestTags.SLOW, TestTags.SOURCEONLY)
def test_plugin_priority(self):
"""Verify that preferring plugin to be used works as expected"""
# library should be successfully initialized
self.assertTrue(BlockDev.is_initialized())
# max LV size should be something sane (not 1024 bytes)
orig_max_size = BlockDev.lvm_get_max_lv_size()
self.assertNotEqual(orig_max_size, 1024)
# change the sources and recompile
os.system("sed -ri 's?BD_LVM_MAX_LV_SIZE;?1024;//test-change?' src/plugins/lvm.c > /dev/null")
os.system("make -C src/plugins/ libbd_lvm.la >/dev/null 2>&1")
# proclaim the new build a different plugin
os.system("cp src/plugins/.libs/libbd_lvm.so src/plugins/.libs/libbd_lvm2.so.2")
# change the sources back and recompile
os.system("sed -ri 's?1024;//test-change?BD_LVM_MAX_LV_SIZE;?' src/plugins/lvm.c > /dev/null")
os.system("make -C src/plugins/ libbd_lvm.la >/dev/null 2>&1")
# now reinit the library with the config preferring the new build
orig_conf_dir = os.environ.get("LIBBLOCKDEV_CONFIG_DIR")
os.environ["LIBBLOCKDEV_CONFIG_DIR"] = "tests/plugin_prio_conf.d"
self.assertTrue(BlockDev.reinit(self.requested_plugins, True, None))
# new LVM plugin loaded, max LV size should be 1024 bytes
self.assertEqual(BlockDev.get_plugin_soname(BlockDev.Plugin.LVM), "libbd_lvm2.so.2")
self.assertEqual(BlockDev.lvm_get_max_lv_size(), 1024)
# reinit with the original config
if orig_conf_dir:
os.environ["LIBBLOCKDEV_CONFIG_DIR"] = orig_conf_dir
else:
del os.environ["LIBBLOCKDEV_CONFIG_DIR"]
self.assertTrue(BlockDev.reinit(self.requested_plugins, True, None))
self.assertEqual(BlockDev.lvm_get_max_lv_size(), orig_max_size)
# now reinit the library with the another config preferring the new
# build
orig_conf_dir = os.environ.get("LIBBLOCKDEV_CONFIG_DIR")
os.environ["LIBBLOCKDEV_CONFIG_DIR"] = "tests/plugin_multi_conf.d"
self.assertTrue(BlockDev.reinit(self.requested_plugins, True, None))
# new LVM plugin loaded, max LV size should be 1024 bytes
self.assertEqual(BlockDev.get_plugin_soname(BlockDev.Plugin.LVM), "libbd_lvm2.so.2")
self.assertEqual(BlockDev.lvm_get_max_lv_size(), 1024)
# reinit with the original config
if orig_conf_dir:
os.environ["LIBBLOCKDEV_CONFIG_DIR"] = orig_conf_dir
else:
del os.environ["LIBBLOCKDEV_CONFIG_DIR"]
self.assertTrue(BlockDev.reinit(self.requested_plugins, True, None))
self.assertEqual(BlockDev.lvm_get_max_lv_size(), orig_max_size)
# clean after ourselves
os.system ("rm -f src/plugins/.libs/libbd_lvm2.so")
# recompiles the LVM plugin
@tag_test(TestTags.SLOW, TestTags.SOURCEONLY)
def test_plugin_fallback(self):
"""Verify that fallback when loading plugins works as expected"""
# library should be successfully initialized
self.assertTrue(BlockDev.is_initialized())
# max LV size should be something sane (not 1024 bytes)
orig_max_size = BlockDev.lvm_get_max_lv_size()
self.assertNotEqual(orig_max_size, 1024)
# change the sources and recompile
os.system("sed -ri 's?gboolean bd_lvm_check_deps \(void\) \{?gboolean bd_lvm_check_deps (void) { return FALSE;//test-change?' src/plugins/lvm.c > /dev/null")
os.system("make -C src/plugins/ libbd_lvm.la >/dev/null 2>&1")
# proclaim the new build a different plugin
os.system("cp src/plugins/.libs/libbd_lvm.so src/plugins/.libs/libbd_lvm2.so.2")
self.addCleanup(os.system, "rm -f src/plugins/.libs/libbd_lvm2.so")
# change the sources back and recompile
os.system("sed -ri 's?gboolean bd_lvm_check_deps \(void\) \{ return FALSE;//test-change?gboolean bd_lvm_check_deps (void) {?' src/plugins/lvm.c > /dev/null")
os.system("make -C src/plugins/ libbd_lvm.la >/dev/null 2>&1")
# now reinit the library with the config preferring the new build
orig_conf_dir = os.environ.get("LIBBLOCKDEV_CONFIG_DIR")
os.environ["LIBBLOCKDEV_CONFIG_DIR"] = "tests/plugin_prio_conf.d"
self.assertTrue(BlockDev.reinit(self.requested_plugins, True, None))
# the original plugin should be loaded because the new one should fail
# to load (due to check() returning FALSE)
self.assertEqual(BlockDev.get_plugin_soname(BlockDev.Plugin.LVM), "libbd_lvm.so.2")
self.assertEqual(BlockDev.lvm_get_max_lv_size(), orig_max_size)
# reinit with the original config
if orig_conf_dir:
os.environ["LIBBLOCKDEV_CONFIG_DIR"] = orig_conf_dir
else:
del os.environ["LIBBLOCKDEV_CONFIG_DIR"]
self.assertTrue(BlockDev.reinit(self.requested_plugins, True, None))
self.assertEqual(BlockDev.lvm_get_max_lv_size(), orig_max_size)
# now reinit the library with the another config preferring the new
# build
orig_conf_dir = os.environ.get("LIBBLOCKDEV_CONFIG_DIR")
os.environ["LIBBLOCKDEV_CONFIG_DIR"] = "tests/plugin_multi_conf.d"
self.assertTrue(BlockDev.reinit(self.requested_plugins, True, None))
# the original plugin should be loaded because the new one should fail
# to load (due to check() returning FALSE)
self.assertEqual(BlockDev.get_plugin_soname(BlockDev.Plugin.LVM), "libbd_lvm.so.2")
self.assertEqual(BlockDev.lvm_get_max_lv_size(), orig_max_size)
# reinit with the original config
if orig_conf_dir:
os.environ["LIBBLOCKDEV_CONFIG_DIR"] = orig_conf_dir
else:
del os.environ["LIBBLOCKDEV_CONFIG_DIR"]
self.assertTrue(BlockDev.reinit(self.requested_plugins, True, None))
self.assertEqual(BlockDev.lvm_get_max_lv_size(), orig_max_size)
def my_log_func(self, level, msg):
# not much to verify here
self.assertTrue(isinstance(level, int))
self.assertTrue(isinstance(msg, str))
self.log += msg + "\n"
@tag_test(TestTags.CORE)
def test_logging_setup(self):
"""Verify that setting up logging works as expected"""
self.assertTrue(BlockDev.reinit(self.requested_plugins, False, self.my_log_func))
succ = BlockDev.utils_exec_and_report_error(["true"])
self.assertTrue(succ)
# reinit with no logging function should change nothing about logging
self.assertTrue(BlockDev.reinit(self.requested_plugins, False, None))
succ, out = BlockDev.utils_exec_and_capture_output(["echo", "hi"])
self.assertTrue(succ)
self.assertEqual(out, "hi\n")
match = re.search(r'Running \[(\d+)\] true', self.log)
self.assertIsNot(match, None)
task_id1 = match.group(1)
match = re.search(r'Running \[(\d+)\] echo hi', self.log)
self.assertIsNot(match, None)
task_id2 = match.group(1)
self.assertIn("...done [%s] (exit code: 0)" % task_id1, self.log)
self.assertIn("stdout[%s]:" % task_id1, self.log)
self.assertIn("stderr[%s]:" % task_id1, self.log)
self.assertIn("stdout[%s]: hi" % task_id2, self.log)
self.assertIn("stderr[%s]:" % task_id2, self.log)
self.assertIn("...done [%s] (exit code: 0)" % task_id2, self.log)
@tag_test(TestTags.CORE)
def test_require_plugins(self):
"""Verify that loading only required plugins works as expected"""
ps = BlockDev.PluginSpec()
ps.name = BlockDev.Plugin.SWAP
ps.so_name = ""
self.assertTrue(BlockDev.reinit([ps], True, None))
self.assertEqual(BlockDev.get_available_plugin_names(), ["swap"])
self.assertTrue(BlockDev.reinit(self.requested_plugins, True, None))
@tag_test(TestTags.CORE)
def test_not_implemented(self):
"""Verify that unloaded/unimplemented functions report errors"""
# should be loaded and working
self.assertTrue(BlockDev.lvm_get_max_lv_size() > 0)
ps = BlockDev.PluginSpec()
ps.name = BlockDev.Plugin.SWAP
ps.so_name = ""
self.assertTrue(BlockDev.reinit([ps], True, None))
self.assertEqual(BlockDev.get_available_plugin_names(), ["swap"])
# no longer loaded
with self.assertRaises(GLib.GError):
BlockDev.lvm_get_max_lv_size()
self.assertTrue(BlockDev.reinit(self.requested_plugins, True, None))
# loaded again
self.assertTrue(BlockDev.lvm_get_max_lv_size() > 0)
def test_ensure_init(self):
"""Verify that ensure_init just returns when already initialized"""
# the library is already initialized, ensure_init() shonuld do nothing
avail_plugs = BlockDev.get_available_plugin_names()
self.assertTrue(BlockDev.ensure_init(self.requested_plugins, None))
self.assertEqual(avail_plugs, BlockDev.get_available_plugin_names())
# reinit with a subset of plugins
plugins = BlockDev.plugin_specs_from_names(["swap", "lvm"])
self.assertTrue(BlockDev.reinit(plugins, True, None))
self.assertEqual(set(BlockDev.get_available_plugin_names()), set(["swap", "lvm"]))
# ensure_init with the same subset -> nothing should change
self.assertTrue(BlockDev.ensure_init(plugins, None))
self.assertEqual(set(BlockDev.get_available_plugin_names()), set(["swap", "lvm"]))
# ensure_init with more plugins -> extra plugins should be loaded
plugins = BlockDev.plugin_specs_from_names(["swap", "lvm", "crypto"])
self.assertTrue(BlockDev.ensure_init(plugins, None))
self.assertEqual(set(BlockDev.get_available_plugin_names()), set(["swap", "lvm", "crypto"]))
# reinit to unload all plugins
self.assertTrue(BlockDev.reinit([], True, None))
self.assertEqual(BlockDev.get_available_plugin_names(), [])
# ensure_init to load all plugins back
self.assertTrue(BlockDev.ensure_init(self.requested_plugins, None))
self.assertGreaterEqual(len(BlockDev.get_available_plugin_names()), 8)
def test_try_reinit(self):
"""Verify that try_reinit() works as expected"""
# try reinitializing with only some utilities being available and thus
# only some plugins able to load
with fake_path("tests/lib_missing_utils", keep_utils=["swapon", "swapoff", "mkswap", "lvm", "swaplabel"]):
succ, loaded = BlockDev.try_reinit(self.requested_plugins, True, None)
self.assertFalse(succ)
for plug_name in ("swap", "lvm", "crypto"):
self.assertIn(plug_name, loaded)
# reset back to all plugins
self.assertTrue(BlockDev.reinit(self.requested_plugins, True, None))
# now the same with a subset of plugins requested
plugins = BlockDev.plugin_specs_from_names(["lvm", "swap", "crypto"])
with fake_path("tests/lib_missing_utils", keep_utils=["swapon", "swapoff", "mkswap", "lvm","swaplabel"]):
succ, loaded = BlockDev.try_reinit(plugins, True, None)
self.assertTrue(succ)
self.assertEqual(set(loaded), set(["swap", "lvm", "crypto"]))
def test_non_en_init(self):
"""Verify that the library initializes with lang different from en_US"""
orig_lang = os.environ.get("LANG")
os.environ["LANG"] = "cs.CZ_UTF-8"
self.assertTrue(BlockDev.reinit(self.requested_plugins, True, None))
if orig_lang:
os.environ["LANG"] = orig_lang
else:
del os.environ["LANG"]
self.assertTrue(BlockDev.reinit(self.requested_plugins, True, None))
def test_dep_checks_disabled(self):
"""Verify that disabling runtime dep checks works"""
with fake_path(all_but="mkswap"):
# should fail because of 'mkswap' missing
with self.assertRaises(GLib.GError):
BlockDev.reinit(self.requested_plugins, True, None)
os.environ["LIBBLOCKDEV_SKIP_DEP_CHECKS"] = ""
self.addCleanup(os.environ.pop, "LIBBLOCKDEV_SKIP_DEP_CHECKS")
with fake_path(all_but="mkswap"):
# should load just fine, skipping the runtime dep checks
self.assertTrue(BlockDev.reinit(self.requested_plugins, True, None))
|