1
0

paths.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. MULTIMASTER_TEST_DIR = os.path.join(TESTS_DIR, 'multimaster')
  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(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()) if not sys.platform.startswith('darwin') else '/tmp'
  45. ))
  46. TMP = os.path.join(SYS_TMP_DIR, 'salt-tests-tmpdir')
  47. TMP_ROOT_DIR = os.path.join(TMP, 'rootdir')
  48. FILES = os.path.join(INTEGRATION_TEST_DIR, 'files')
  49. BASE_FILES = os.path.join(INTEGRATION_TEST_DIR, 'files', 'file', 'base')
  50. PROD_FILES = os.path.join(INTEGRATION_TEST_DIR, 'files', 'file', 'prod')
  51. PYEXEC = 'python{0}.{1}'.format(*sys.version_info)
  52. MOCKBIN = os.path.join(INTEGRATION_TEST_DIR, 'mockbin')
  53. SCRIPT_DIR = os.path.join(CODE_DIR, 'scripts')
  54. TMP_STATE_TREE = os.path.join(SYS_TMP_DIR, 'salt-temp-state-tree')
  55. TMP_PILLAR_TREE = os.path.join(SYS_TMP_DIR, 'salt-temp-pillar-tree')
  56. TMP_PRODENV_STATE_TREE = os.path.join(SYS_TMP_DIR, 'salt-temp-prodenv-state-tree')
  57. TMP_CONF_DIR = os.path.join(TMP, 'config')
  58. TMP_SUB_MINION_CONF_DIR = os.path.join(TMP_CONF_DIR, 'sub-minion')
  59. TMP_SYNDIC_MINION_CONF_DIR = os.path.join(TMP_CONF_DIR, 'syndic-minion')
  60. TMP_SYNDIC_MASTER_CONF_DIR = os.path.join(TMP_CONF_DIR, 'syndic-master')
  61. TMP_MM_CONF_DIR = os.path.join(TMP_CONF_DIR, 'multimaster')
  62. TMP_MM_SUB_CONF_DIR = os.path.join(TMP_CONF_DIR, 'sub-multimaster')
  63. TMP_PROXY_CONF_DIR = os.path.join(TMP_CONF_DIR, 'proxy')
  64. CONF_DIR = os.path.join(INTEGRATION_TEST_DIR, 'files', 'conf')
  65. PILLAR_DIR = os.path.join(FILES, 'pillar')
  66. TMP_SCRIPT_DIR = os.path.join(TMP, 'scripts')
  67. ENGINES_DIR = os.path.join(FILES, 'engines')
  68. LOG_HANDLERS_DIR = os.path.join(FILES, 'log_handlers')
  69. def list_test_mods():
  70. '''
  71. A generator which returns all of the test files
  72. '''
  73. test_re = re.compile(r'^test_.+\.py$')
  74. for dirname in (UNIT_TEST_DIR, INTEGRATION_TEST_DIR, MULTIMASTER_TEST_DIR):
  75. test_type = os.path.basename(dirname)
  76. for root, _, files in salt.utils.path.os_walk(dirname):
  77. parent_mod = root[len(dirname):].lstrip(os.sep).replace(os.sep, '.')
  78. for filename in files:
  79. if test_re.match(filename):
  80. mod_name = test_type
  81. if parent_mod:
  82. mod_name += '.' + parent_mod
  83. mod_name += '.' + filename[:-3]
  84. yield mod_name