paths.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. TMP_SSH_CONF_DIR = TMP_MINION_CONF_DIR = os.path.join(TMP, "ssh")
  71. CONF_DIR = os.path.join(INTEGRATION_TEST_DIR, "files", "conf")
  72. PILLAR_DIR = os.path.join(FILES, "pillar")
  73. TMP_SCRIPT_DIR = os.path.join(TMP, "scripts")
  74. ENGINES_DIR = os.path.join(FILES, "engines")
  75. LOG_HANDLERS_DIR = os.path.join(FILES, "log_handlers")
  76. def list_test_mods():
  77. """
  78. A generator which returns all of the test files
  79. """
  80. test_re = re.compile(r"^test_.+\.py$")
  81. for dirname in (UNIT_TEST_DIR, INTEGRATION_TEST_DIR, MULTIMASTER_TEST_DIR):
  82. test_type = os.path.basename(dirname)
  83. for root, _, files in salt.utils.path.os_walk(dirname):
  84. parent_mod = root[len(dirname) :].lstrip(os.sep).replace(os.sep, ".")
  85. for filename in files:
  86. if test_re.match(filename):
  87. mod_name = test_type
  88. if parent_mod:
  89. mod_name += "." + parent_mod
  90. mod_name += "." + filename[:-3]
  91. yield mod_name