1
0

test_path.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Tests for salt.utils.path
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. import sys
  9. import posixpath
  10. import ntpath
  11. import platform
  12. import tempfile
  13. # Import Salt Testing libs
  14. from tests.support.unit import TestCase, skipIf
  15. from tests.support.mock import patch, NO_MOCK, NO_MOCK_REASON
  16. # Import Salt libs
  17. import salt.utils.compat
  18. import salt.utils.path
  19. import salt.utils.platform
  20. from salt.exceptions import CommandNotFoundError
  21. # Import 3rd-party libs
  22. from salt.ext import six
  23. class PathJoinTestCase(TestCase):
  24. PLATFORM_FUNC = platform.system
  25. BUILTIN_MODULES = sys.builtin_module_names
  26. NIX_PATHS = (
  27. (('/', 'key'), '/key'),
  28. (('/etc/salt', '/etc/salt/pki'), '/etc/salt/etc/salt/pki'),
  29. (('/usr/local', '/etc/salt/pki'), '/usr/local/etc/salt/pki')
  30. )
  31. WIN_PATHS = (
  32. (('c:', 'temp', 'foo'), 'c:\\temp\\foo'),
  33. (('c:', r'\temp', r'\foo'), 'c:\\temp\\foo'),
  34. (('c:\\', r'\temp', r'\foo'), 'c:\\temp\\foo'),
  35. ((r'c:\\', r'\temp', r'\foo'), 'c:\\temp\\foo'),
  36. (('c:', r'\temp', r'\foo', 'bar'), 'c:\\temp\\foo\\bar'),
  37. (('c:', r'\temp', r'\foo\bar'), 'c:\\temp\\foo\\bar'),
  38. )
  39. @skipIf(True, 'Skipped until properly mocked')
  40. def test_nix_paths(self):
  41. if platform.system().lower() == "windows":
  42. self.skipTest(
  43. "Windows platform found. not running *nix salt.utils.path.join tests"
  44. )
  45. for idx, (parts, expected) in enumerate(self.NIX_PATHS):
  46. path = salt.utils.path.join(*parts)
  47. self.assertEqual(
  48. '{0}: {1}'.format(idx, path),
  49. '{0}: {1}'.format(idx, expected)
  50. )
  51. @skipIf(True, 'Skipped until properly mocked')
  52. def test_windows_paths(self):
  53. if platform.system().lower() != "windows":
  54. self.skipTest(
  55. 'Non windows platform found. not running non patched os.path '
  56. 'salt.utils.path.join tests'
  57. )
  58. for idx, (parts, expected) in enumerate(self.WIN_PATHS):
  59. path = salt.utils.path.join(*parts)
  60. self.assertEqual(
  61. '{0}: {1}'.format(idx, path),
  62. '{0}: {1}'.format(idx, expected)
  63. )
  64. @skipIf(True, 'Skipped until properly mocked')
  65. def test_windows_paths_patched_path_module(self):
  66. if platform.system().lower() == "windows":
  67. self.skipTest(
  68. 'Windows platform found. not running patched os.path '
  69. 'salt.utils.path.join tests'
  70. )
  71. self.__patch_path()
  72. for idx, (parts, expected) in enumerate(self.WIN_PATHS):
  73. path = salt.utils.path.join(*parts)
  74. self.assertEqual(
  75. '{0}: {1}'.format(idx, path),
  76. '{0}: {1}'.format(idx, expected)
  77. )
  78. self.__unpatch_path()
  79. @skipIf(salt.utils.platform.is_windows(), '*nix-only test')
  80. def test_mixed_unicode_and_binary(self):
  81. '''
  82. This tests joining paths that contain a mix of components with unicode
  83. strings and non-unicode strings with the unicode characters as binary.
  84. This is no longer something we need to concern ourselves with in
  85. Python 3, but the test should nonetheless pass on Python 3. Really what
  86. we're testing here is that we don't get a UnicodeDecodeError when
  87. running on Python 2.
  88. '''
  89. a = u'/foo/bar'
  90. b = 'Д'
  91. expected = u'/foo/bar/\u0414'
  92. actual = salt.utils.path.join(a, b)
  93. self.assertEqual(actual, expected)
  94. def __patch_path(self):
  95. import imp
  96. modules = list(self.BUILTIN_MODULES[:])
  97. modules.pop(modules.index('posix'))
  98. modules.append('nt')
  99. code = """'''Salt unittest loaded NT module'''"""
  100. module = imp.new_module('nt')
  101. six.exec_(code, module.__dict__)
  102. sys.modules['nt'] = module
  103. sys.builtin_module_names = modules
  104. platform.system = lambda: "windows"
  105. for module in (ntpath, os, os.path, tempfile):
  106. salt.utils.compat.reload(module)
  107. def __unpatch_path(self):
  108. del sys.modules['nt']
  109. sys.builtin_module_names = self.BUILTIN_MODULES[:]
  110. platform.system = self.PLATFORM_FUNC
  111. for module in (posixpath, os, os.path, tempfile, platform):
  112. salt.utils.compat.reload(module)
  113. @skipIf(NO_MOCK, NO_MOCK_REASON)
  114. class PathTestCase(TestCase):
  115. def test_which_bin(self):
  116. ret = salt.utils.path.which_bin('str')
  117. self.assertIs(None, ret)
  118. test_exes = ['ls', 'echo']
  119. with patch('salt.utils.path.which', return_value='/tmp/dummy_path'):
  120. ret = salt.utils.path.which_bin(test_exes)
  121. self.assertEqual(ret, '/tmp/dummy_path')
  122. ret = salt.utils.path.which_bin([])
  123. self.assertIs(None, ret)
  124. with patch('salt.utils.path.which', return_value=''):
  125. ret = salt.utils.path.which_bin(test_exes)
  126. self.assertIs(None, ret)
  127. def test_sanitize_win_path(self):
  128. p = '\\windows\\system'
  129. self.assertEqual(salt.utils.path.sanitize_win_path('\\windows\\system'), '\\windows\\system')
  130. self.assertEqual(salt.utils.path.sanitize_win_path('\\bo:g|us\\p?at*h>'), '\\bo_g_us\\p_at_h_')
  131. @skipIf(NO_MOCK, NO_MOCK_REASON)
  132. def test_check_or_die(self):
  133. self.assertRaises(CommandNotFoundError, salt.utils.path.check_or_die, None)
  134. with patch('salt.utils.path.which', return_value=False):
  135. self.assertRaises(CommandNotFoundError, salt.utils.path.check_or_die, 'FAKE COMMAND')
  136. @skipIf(NO_MOCK, NO_MOCK_REASON)
  137. def test_join(self):
  138. with patch('salt.utils.platform.is_windows', return_value=False) as is_windows_mock:
  139. self.assertFalse(is_windows_mock.return_value)
  140. expected_path = os.path.join(os.sep + 'a', 'b', 'c', 'd')
  141. ret = salt.utils.path.join('/a/b/c', 'd')
  142. self.assertEqual(ret, expected_path)
  143. @skipIf(NO_MOCK, NO_MOCK_REASON)
  144. class TestWhich(TestCase):
  145. '''
  146. Tests salt.utils.path.which function to ensure that it returns True as
  147. expected.
  148. '''
  149. # The mock patch below will make sure that ALL calls to the which function
  150. # returns None
  151. def test_missing_binary_in_linux(self):
  152. with patch('salt.utils.path.which', lambda exe: None):
  153. self.assertTrue(
  154. salt.utils.path.which('this-binary-does-not-exist') is None
  155. )
  156. # The mock patch below will make sure that ALL calls to the which function
  157. # return whatever is sent to it
  158. def test_existing_binary_in_linux(self):
  159. with patch('salt.utils.path.which', lambda exe: exe):
  160. self.assertTrue(salt.utils.path.which('this-binary-exists-under-linux'))
  161. def test_existing_binary_in_windows(self):
  162. with patch('os.access') as osaccess:
  163. # We define the side_effect attribute on the mocked object in order to
  164. # specify which calls return which values. First call to os.access
  165. # returns X, the second Y, the third Z, etc...
  166. osaccess.side_effect = [
  167. # The first os.access should return False(the abspath one)
  168. False,
  169. # The second, iterating through $PATH, should also return False,
  170. # still checking for Linux
  171. False,
  172. # We will now also return False once so we get a .EXE back from
  173. # the function, see PATHEXT below.
  174. False,
  175. # Lastly return True, this is the windows check.
  176. True
  177. ]
  178. # Let's patch os.environ to provide a custom PATH variable
  179. with patch.dict(os.environ, {'PATH': os.sep + 'bin',
  180. 'PATHEXT': '.COM;.EXE;.BAT;.CMD'}):
  181. # Let's also patch is_windows to return True
  182. with patch('salt.utils.platform.is_windows', lambda: True):
  183. with patch('os.path.isfile', lambda x: True):
  184. self.assertEqual(
  185. salt.utils.path.which('this-binary-exists-under-windows'),
  186. os.path.join(os.sep + 'bin', 'this-binary-exists-under-windows.EXE')
  187. )
  188. def test_missing_binary_in_windows(self):
  189. with patch('os.access') as osaccess:
  190. osaccess.side_effect = [
  191. # The first os.access should return False(the abspath one)
  192. False,
  193. # The second, iterating through $PATH, should also return False,
  194. # still checking for Linux
  195. # which() will add 4 extra paths to the given one, os.access will
  196. # be called 5 times
  197. False, False, False, False, False
  198. ]
  199. # Let's patch os.environ to provide a custom PATH variable
  200. with patch.dict(os.environ, {'PATH': os.sep + 'bin'}):
  201. # Let's also patch is_widows to return True
  202. with patch('salt.utils.platform.is_windows', lambda: True):
  203. self.assertEqual(
  204. # Since we're passing the .exe suffix, the last True above
  205. # will not matter. The result will be None
  206. salt.utils.path.which('this-binary-is-missing-in-windows.exe'),
  207. None
  208. )
  209. def test_existing_binary_in_windows_pathext(self):
  210. with patch('os.access') as osaccess:
  211. # We define the side_effect attribute on the mocked object in order to
  212. # specify which calls return which values. First call to os.access
  213. # returns X, the second Y, the third Z, etc...
  214. osaccess.side_effect = [
  215. # The first os.access should return False(the abspath one)
  216. False,
  217. # The second, iterating through $PATH, should also return False,
  218. # still checking for Linux
  219. False,
  220. # We will now also return False 3 times so we get a .CMD back from
  221. # the function, see PATHEXT below.
  222. # Lastly return True, this is the windows check.
  223. False, False, False,
  224. True
  225. ]
  226. # Let's patch os.environ to provide a custom PATH variable
  227. with patch.dict(os.environ, {'PATH': os.sep + 'bin',
  228. 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;'
  229. '.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY'}):
  230. # Let's also patch is_windows to return True
  231. with patch('salt.utils.platform.is_windows', lambda: True):
  232. with patch('os.path.isfile', lambda x: True):
  233. self.assertEqual(
  234. salt.utils.path.which('this-binary-exists-under-windows'),
  235. os.path.join(os.sep + 'bin', 'this-binary-exists-under-windows.CMD')
  236. )