paths.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. :copyright: Copyright 2017 by the SaltStack Team, see AUTHORS for more details.
  5. :license: Apache 2.0, see LICENSE for more details.
  6. tests.support.paths
  7. ~~~~~~~~~~~~~~~~~~~
  8. Tests related paths
  9. """
  10. from __future__ import absolute_import
  11. import logging
  12. import os
  13. import re
  14. import sys
  15. import tempfile
  16. import salt.utils.path
  17. log = logging.getLogger(__name__)
  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{0}.{1}".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 = os.path.join(TMP_CONF_DIR, "proxy")
  72. CONF_DIR = os.path.join(INTEGRATION_TEST_DIR, "files", "conf")
  73. PILLAR_DIR = os.path.join(FILES, "pillar")
  74. TMP_SCRIPT_DIR = os.path.join(TMP, "scripts")
  75. ENGINES_DIR = os.path.join(FILES, "engines")
  76. LOG_HANDLERS_DIR = os.path.join(FILES, "log_handlers")
  77. def list_test_mods():
  78. """
  79. A generator which returns all of the test files
  80. """
  81. test_re = re.compile(r"^test_.+\.py$")
  82. for dirname in (UNIT_TEST_DIR, INTEGRATION_TEST_DIR, MULTIMASTER_TEST_DIR):
  83. test_type = os.path.basename(dirname)
  84. for root, _, files in salt.utils.path.os_walk(dirname):
  85. parent_mod = root[len(dirname) :].lstrip(os.sep).replace(os.sep, ".")
  86. for filename in files:
  87. if test_re.match(filename):
  88. mod_name = test_type
  89. if parent_mod:
  90. mod_name += "." + parent_mod
  91. mod_name += "." + filename[:-3]
  92. yield mod_name