paths.py 3.8 KB

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