paths.py 3.7 KB

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