test_loader.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """
  2. Tests for salt.loader
  3. """
  4. import os
  5. import shutil
  6. import sys
  7. import pytest
  8. import salt.loader
  9. import salt.loader_context
  10. import salt.utils.files
  11. from tests.support.helpers import dedent
  12. @pytest.fixture
  13. def loader_dir(tmp_path):
  14. """
  15. Create a simple directory with a couple modules to load and run tests
  16. against.
  17. """
  18. mod_content = dedent(
  19. """
  20. def __virtual__():
  21. return True
  22. def set_context(key, value):
  23. __context__[key] = value
  24. def get_context(key):
  25. return __context__[key]
  26. """
  27. )
  28. tmp_path = str(tmp_path)
  29. with salt.utils.files.fopen(os.path.join(tmp_path, "mod_a.py"), "w") as fp:
  30. fp.write(mod_content)
  31. with salt.utils.files.fopen(os.path.join(tmp_path, "mod_b.py"), "w") as fp:
  32. fp.write(mod_content)
  33. try:
  34. yield tmp_path
  35. finally:
  36. shutil.rmtree(tmp_path)
  37. def test_loaders_have_uniq_context(loader_dir):
  38. """
  39. Loaded functions run in the LazyLoader's context.
  40. """
  41. opts = {"optimization_order": [0, 1, 2]}
  42. loader_1 = salt.loader.LazyLoader([loader_dir], opts,)
  43. loader_2 = salt.loader.LazyLoader([loader_dir], opts,)
  44. loader_1._load_all()
  45. loader_2._load_all()
  46. assert loader_1.pack["__context__"] == {}
  47. assert loader_2.pack["__context__"] == {}
  48. loader_1["mod_a.set_context"]("foo", "bar")
  49. assert loader_1.pack["__context__"] == {"foo": "bar"}
  50. assert loader_1["mod_b.get_context"]("foo") == "bar"
  51. with pytest.raises(KeyError):
  52. loader_2["mod_a.get_context"]("foo")
  53. assert loader_2.pack["__context__"] == {}
  54. def test_loaded_methods_are_loaded_func(loader_dir):
  55. """
  56. Functions loaded from LazyLoader's item lookups are LoadedFunc objects
  57. """
  58. opts = {"optimization_order": [0, 1, 2]}
  59. loader_1 = salt.loader.LazyLoader([loader_dir], opts,)
  60. fun = loader_1["mod_a.get_context"]
  61. assert isinstance(fun, salt.loader.LoadedFunc)
  62. def test_loaded_modules_are_loaded_mods(loader_dir):
  63. """
  64. Modules looked up as attributes of LazyLoaders are LoadedMod objects.
  65. """
  66. opts = {"optimization_order": [0, 1, 2]}
  67. loader_1 = salt.loader.LazyLoader([loader_dir], opts,)
  68. mod = loader_1.mod_a
  69. assert isinstance(mod, salt.loader.LoadedMod)
  70. def test_loaders_create_named_loader_contexts(loader_dir):
  71. """
  72. LazyLoader's create NamedLoaderContexts on the modules the load.
  73. """
  74. opts = {"optimization_order": [0, 1, 2]}
  75. loader_1 = salt.loader.LazyLoader([loader_dir], opts,)
  76. mod = loader_1.mod_a
  77. assert isinstance(mod.mod, dict)
  78. func = mod.set_context
  79. assert isinstance(func, salt.loader.LoadedFunc)
  80. module_name = func.func.__module__
  81. module = sys.modules[module_name]
  82. assert isinstance(module.__context__, salt.loader_context.NamedLoaderContext)