paths.py 3.5 KB

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