paths.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """
  2. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  3. :copyright: Copyright 2017 by the SaltStack Team, see AUTHORS for more details.
  4. :license: Apache 2.0, see LICENSE for more details.
  5. tests.support.paths
  6. ~~~~~~~~~~~~~~~~~~~
  7. Tests related paths
  8. """
  9. import logging
  10. import os
  11. import re
  12. import sys
  13. import tempfile
  14. import salt
  15. import salt.utils.path
  16. log = logging.getLogger(__name__)
  17. SALT_CODE_DIR = os.path.dirname(os.path.normpath(os.path.abspath(salt.__file__)))
  18. TESTS_DIR = os.path.dirname(
  19. os.path.dirname(os.path.normpath(os.path.abspath(__file__)))
  20. )
  21. if TESTS_DIR.startswith("//"):
  22. # Have we been given an initial double forward slash? Ditch it!
  23. TESTS_DIR = TESTS_DIR[1:]
  24. if sys.platform.startswith("win"):
  25. TESTS_DIR = os.path.normcase(TESTS_DIR)
  26. CODE_DIR = os.path.dirname(TESTS_DIR)
  27. if sys.platform.startswith("win"):
  28. CODE_DIR = CODE_DIR.replace("\\", "\\\\")
  29. UNIT_TEST_DIR = os.path.join(TESTS_DIR, "unit")
  30. INTEGRATION_TEST_DIR = os.path.join(TESTS_DIR, "integration")
  31. MULTIMASTER_TEST_DIR = os.path.join(TESTS_DIR, "multimaster")
  32. # Let's inject CODE_DIR so salt is importable if not there already
  33. if TESTS_DIR in sys.path:
  34. sys.path.remove(TESTS_DIR)
  35. if CODE_DIR in sys.path and sys.path[0] != CODE_DIR:
  36. sys.path.remove(CODE_DIR)
  37. if CODE_DIR not in sys.path:
  38. sys.path.insert(0, CODE_DIR)
  39. if TESTS_DIR not in sys.path:
  40. sys.path.insert(1, TESTS_DIR)
  41. SYS_TMP_DIR = os.path.abspath(
  42. os.path.realpath(
  43. # Avoid ${TMPDIR} and gettempdir() on MacOS as they yield a base path too long
  44. # for unix sockets: ``error: AF_UNIX path too long``
  45. # Gentoo Portage prefers ebuild tests are rooted in ${TMPDIR}
  46. os.environ.get("TMPDIR", tempfile.gettempdir())
  47. if not sys.platform.startswith("darwin")
  48. else "/tmp"
  49. )
  50. )
  51. TMP = os.path.join(SYS_TMP_DIR, "salt-tests-tmpdir")
  52. TMP_ROOT_DIR = os.path.join(TMP, "rootdir")
  53. FILES = os.path.join(INTEGRATION_TEST_DIR, "files")
  54. BASE_FILES = os.path.join(INTEGRATION_TEST_DIR, "files", "file", "base")
  55. PROD_FILES = os.path.join(INTEGRATION_TEST_DIR, "files", "file", "prod")
  56. PYEXEC = "python{}.{}".format(*sys.version_info)
  57. MOCKBIN = os.path.join(INTEGRATION_TEST_DIR, "mockbin")
  58. SCRIPT_DIR = os.path.join(CODE_DIR, "scripts")
  59. TMP_STATE_TREE = os.path.join(SYS_TMP_DIR, "salt-temp-state-tree")
  60. TMP_PILLAR_TREE = os.path.join(SYS_TMP_DIR, "salt-temp-pillar-tree")
  61. TMP_PRODENV_STATE_TREE = os.path.join(SYS_TMP_DIR, "salt-temp-prodenv-state-tree")
  62. TMP_PRODENV_PILLAR_TREE = os.path.join(SYS_TMP_DIR, "salt-temp-prodenv-pillar-tree")
  63. TMP_CONF_DIR = TMP_MINION_CONF_DIR = os.path.join(TMP, "config")
  64. TMP_SUB_MINION_CONF_DIR = os.path.join(TMP_CONF_DIR, "sub-minion")
  65. TMP_SYNDIC_MINION_CONF_DIR = os.path.join(TMP_CONF_DIR, "syndic-minion")
  66. TMP_SYNDIC_MASTER_CONF_DIR = os.path.join(TMP_CONF_DIR, "syndic-master")
  67. TMP_MM_CONF_DIR = TMP_MM_MINION_CONF_DIR = os.path.join(TMP_CONF_DIR, "multimaster")
  68. TMP_MM_SUB_CONF_DIR = TMP_MM_SUB_MINION_CONF_DIR = os.path.join(
  69. TMP_CONF_DIR, "sub-multimaster"
  70. )
  71. TMP_PROXY_CONF_DIR = TMP_CONF_DIR
  72. TMP_SSH_CONF_DIR = TMP_MINION_CONF_DIR
  73. CONF_DIR = os.path.join(INTEGRATION_TEST_DIR, "files", "conf")
  74. PILLAR_DIR = os.path.join(FILES, "pillar")
  75. TMP_SCRIPT_DIR = os.path.join(TMP, "scripts")
  76. ENGINES_DIR = os.path.join(FILES, "engines")
  77. LOG_HANDLERS_DIR = os.path.join(FILES, "log_handlers")
  78. def list_test_mods():
  79. """
  80. A generator which returns all of the test files
  81. """
  82. test_re = re.compile(r"^test_.+\.py$")
  83. for dirname in (UNIT_TEST_DIR, INTEGRATION_TEST_DIR, MULTIMASTER_TEST_DIR):
  84. test_type = os.path.basename(dirname)
  85. for root, _, files in salt.utils.path.os_walk(dirname):
  86. parent_mod = root[len(dirname) :].lstrip(os.sep).replace(os.sep, ".")
  87. for filename in files:
  88. if test_re.match(filename):
  89. mod_name = test_type
  90. if parent_mod:
  91. mod_name += "." + parent_mod
  92. mod_name += "." + filename[:-3]
  93. yield mod_name