test_virtualname.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # -*- coding: utf-8 -*-
  2. """
  3. tests.unit.test_virtualname
  4. ~~~~~~~~~~~~~~~~~~~~
  5. """
  6. # Import Python libs
  7. from __future__ import absolute_import
  8. import logging
  9. import os
  10. # Import Salt libs
  11. import salt.ext.six as six
  12. from tests.support.runtests import RUNTIME_VARS
  13. # Import Salt Testing libs
  14. from tests.support.unit import TestCase
  15. try:
  16. import importlib.util
  17. except ImportError:
  18. import imp
  19. log = logging.getLogger(__name__)
  20. class FakeEntry(object):
  21. def __init__(self, name, path, is_file=True):
  22. self.name = name
  23. self.path = path
  24. self._is_file = is_file
  25. def is_file(self):
  26. return self._is_file
  27. class VirtualNameTestCase(TestCase):
  28. """
  29. Test that the virtualname is in the module name, to speed up lookup of
  30. modules.
  31. """
  32. maxDiff = None
  33. @staticmethod
  34. def _import_module(testpath):
  35. if six.PY3:
  36. spec = importlib.util.spec_from_file_location("tmpmodule", testpath)
  37. module = importlib.util.module_from_spec(spec)
  38. spec.loader.exec_module(module)
  39. else:
  40. fp, pathname, description = imp.find_module("tmpmodule", testpath)
  41. try:
  42. module = imp.load_module("tmpmodule", fp, pathname, description)
  43. finally:
  44. # Since we may exit via an exception, close fp explicitly.
  45. if fp:
  46. fp.close()
  47. return module
  48. def _check_modules(self, path):
  49. """
  50. check modules in directory
  51. """
  52. ret = []
  53. for entry in os.listdir(path):
  54. name, path = os.path.splitext(os.path.basename(entry))[0], entry
  55. if name.startswith(".") or name.startswith("_"):
  56. continue
  57. if os.path.isfile(path) and not name.endswith(".py"):
  58. continue
  59. testpath = (
  60. path if os.path.isfile(path) else os.path.join(path, "__init__.py")
  61. )
  62. module = self._import_module(testpath)
  63. if hasattr(module, "__virtualname__"):
  64. if module.__virtualname__ not in name:
  65. ret.append(
  66. 'Virtual name "{0}" is not in the module filename "{1}": {2}'.format(
  67. module.__virtualname__, name, path
  68. )
  69. )
  70. return ret
  71. def test_check_virtualname(self):
  72. """
  73. Test that the virtualname is in __name__ of the module
  74. """
  75. errors = []
  76. for entry in os.listdir(os.path.join(RUNTIME_VARS.CODE_DIR, "salt/")):
  77. name, path = os.path.splitext(os.path.basename(entry))[0], entry
  78. if name.startswith(".") or name.startswith("_") or not os.path.isdir(path):
  79. continue
  80. if name in ("cli", "defaults", "spm", "daemons", "ext", "templates"):
  81. continue
  82. if name == "cloud":
  83. entry = os.path.join(RUNTIME_VARS.CODE_DIR, "salt", "cloud", "clouds")
  84. errors.extend(self._check_modules(entry))
  85. for error in errors:
  86. log.critical(error)
  87. assert not errors