test_thin.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: :email:`Bo Maryniuk <bo@suse.de>`
  4. '''
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import os
  7. import sys
  8. from tests.support.unit import TestCase, skipIf
  9. from tests.support.helpers import TstSuiteLoggingHandler
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch)
  13. import salt.exceptions
  14. import salt.utils.json
  15. from salt.utils import thin
  16. import salt.utils.stringutils
  17. import salt.utils.platform
  18. from salt.utils.stringutils import to_bytes as bts
  19. from salt.ext.six.moves import range
  20. import salt.ext.six
  21. try:
  22. import pytest
  23. except ImportError:
  24. pytest = None
  25. @skipIf(pytest is None, 'PyTest is missing')
  26. class SSHThinTestCase(TestCase):
  27. '''
  28. TestCase for SaltSSH-related parts.
  29. '''
  30. def _popen(self, return_value=None, side_effect=None, returncode=0):
  31. '''
  32. Fake subprocess.Popen
  33. :return:
  34. '''
  35. proc = MagicMock()
  36. proc.communicate = MagicMock(return_value=return_value, side_effect=side_effect)
  37. proc.returncode = returncode
  38. popen = MagicMock(return_value=proc)
  39. return popen
  40. def _version_info(self, major=None, minor=None):
  41. '''
  42. Fake version info.
  43. :param major:
  44. :param minor:
  45. :return:
  46. '''
  47. class VersionInfo(tuple):
  48. pass
  49. vi = VersionInfo([major, minor])
  50. vi.major = major or sys.version_info.major
  51. vi.minor = minor or sys.version_info.minor
  52. return vi
  53. def _tarfile(self, getinfo=False):
  54. '''
  55. Fake tarfile handler.
  56. :return:
  57. '''
  58. spec = ['add', 'close']
  59. if getinfo:
  60. spec.append('getinfo')
  61. tf = MagicMock()
  62. tf.open = MagicMock(return_value=MagicMock(spec=spec))
  63. return tf
  64. @patch('salt.exceptions.SaltSystemExit', Exception)
  65. @patch('salt.utils.thin.log', MagicMock())
  66. @patch('salt.utils.thin.os.path.isfile', MagicMock(return_value=False))
  67. def test_get_ext_tops_cfg_missing_dependencies(self):
  68. '''
  69. Test thin.get_ext_tops contains all required dependencies.
  70. :return:
  71. '''
  72. cfg = {'namespace': {'py-version': [0, 0], 'path': '/foo', 'dependencies': []}}
  73. with pytest.raises(Exception) as err:
  74. thin.get_ext_tops(cfg)
  75. self.assertIn('Missing dependencies', str(err.value))
  76. self.assertTrue(thin.log.error.called)
  77. self.assertIn('Missing dependencies', thin.log.error.call_args[0][0])
  78. self.assertIn('jinja2, yaml, tornado, msgpack',
  79. thin.log.error.call_args[0][0])
  80. @patch('salt.exceptions.SaltSystemExit', Exception)
  81. @patch('salt.utils.thin.log', MagicMock())
  82. @patch('salt.utils.thin.os.path.isfile', MagicMock(return_value=False))
  83. def test_get_ext_tops_cfg_missing_interpreter(self):
  84. '''
  85. Test thin.get_ext_tops contains interpreter configuration.
  86. :return:
  87. '''
  88. cfg = {'namespace': {'path': '/foo',
  89. 'dependencies': []}}
  90. with pytest.raises(salt.exceptions.SaltSystemExit) as err:
  91. thin.get_ext_tops(cfg)
  92. self.assertIn('missing specific locked Python version', str(err.value))
  93. @patch('salt.exceptions.SaltSystemExit', Exception)
  94. @patch('salt.utils.thin.log', MagicMock())
  95. @patch('salt.utils.thin.os.path.isfile', MagicMock(return_value=False))
  96. def test_get_ext_tops_cfg_wrong_interpreter(self):
  97. '''
  98. Test thin.get_ext_tops contains correct interpreter configuration.
  99. :return:
  100. '''
  101. cfg = {'namespace': {'path': '/foo',
  102. 'py-version': 2,
  103. 'dependencies': []}}
  104. with pytest.raises(salt.exceptions.SaltSystemExit) as err:
  105. thin.get_ext_tops(cfg)
  106. self.assertIn('specific locked Python version should be a list of '
  107. 'major/minor version', str(err.value))
  108. @patch('salt.exceptions.SaltSystemExit', Exception)
  109. @patch('salt.utils.thin.log', MagicMock())
  110. @patch('salt.utils.thin.os.path.isfile', MagicMock(return_value=False))
  111. def test_get_ext_tops_cfg_interpreter(self):
  112. '''
  113. Test thin.get_ext_tops interpreter configuration.
  114. :return:
  115. '''
  116. cfg = {'namespace': {'path': '/foo',
  117. 'py-version': [2, 6],
  118. 'dependencies': {'jinja2': '',
  119. 'yaml': '',
  120. 'tornado': '',
  121. 'msgpack': ''}}}
  122. with pytest.raises(salt.exceptions.SaltSystemExit):
  123. thin.get_ext_tops(cfg)
  124. assert len(thin.log.warning.mock_calls) == 4
  125. assert sorted([x[1][1] for x in thin.log.warning.mock_calls]) == ['jinja2', 'msgpack', 'tornado', 'yaml']
  126. assert 'Module test has missing configuration' == thin.log.warning.mock_calls[0][1][0] % 'test'
  127. @patch('salt.exceptions.SaltSystemExit', Exception)
  128. @patch('salt.utils.thin.log', MagicMock())
  129. @patch('salt.utils.thin.os.path.isfile', MagicMock(return_value=False))
  130. def test_get_ext_tops_dependency_config_check(self):
  131. '''
  132. Test thin.get_ext_tops dependencies are importable
  133. :return:
  134. '''
  135. cfg = {'namespace': {'path': '/foo',
  136. 'py-version': [2, 6],
  137. 'dependencies': {'jinja2': '/jinja/foo.py',
  138. 'yaml': '/yaml/',
  139. 'tornado': '/tornado/wrong.rb',
  140. 'msgpack': 'msgpack.sh'}}}
  141. with pytest.raises(salt.exceptions.SaltSystemExit) as err:
  142. thin.get_ext_tops(cfg)
  143. self.assertIn('Missing dependencies for the alternative version in the '
  144. 'external configuration', str(err.value))
  145. messages = {}
  146. for cl in thin.log.warning.mock_calls:
  147. messages[cl[1][1]] = cl[1][0] % (cl[1][1], cl[1][2])
  148. for mod in ['tornado', 'yaml', 'msgpack']:
  149. self.assertIn('not a Python importable module', messages[mod])
  150. self.assertIn('configured with not a file or does not exist',
  151. messages['jinja2'])
  152. @patch('salt.exceptions.SaltSystemExit', Exception)
  153. @patch('salt.utils.thin.log', MagicMock())
  154. @patch('salt.utils.thin.os.path.isfile', MagicMock(return_value=True))
  155. def test_get_ext_tops_config_pass(self):
  156. '''
  157. Test thin.get_ext_tops configuration
  158. :return:
  159. '''
  160. cfg = {'namespace': {'path': '/foo',
  161. 'py-version': [2, 6],
  162. 'dependencies': {'jinja2': '/jinja/foo.py',
  163. 'yaml': '/yaml/',
  164. 'tornado': '/tornado/tornado.py',
  165. 'msgpack': 'msgpack.py'}}}
  166. out = thin.get_ext_tops(cfg)
  167. assert out['namespace']['py-version'] == cfg['namespace']['py-version']
  168. assert out['namespace']['path'] == cfg['namespace']['path']
  169. assert sorted(out['namespace']['dependencies']) == sorted(['/tornado/tornado.py',
  170. '/jinja/foo.py', '/yaml/', 'msgpack.py'])
  171. @patch('salt.utils.thin.sys.argv', [None, '{"foo": "bar"}'])
  172. @patch('salt.utils.thin.get_tops', lambda **kw: kw)
  173. def test_gte(self):
  174. '''
  175. Test thin.gte external call for processing the info about tops per interpreter.
  176. :return:
  177. '''
  178. assert salt.utils.json.loads(thin.gte()).get('foo') == 'bar'
  179. def test_add_dep_path(self):
  180. '''
  181. Test thin._add_dependency function to setup dependency paths
  182. :return:
  183. '''
  184. container = []
  185. for pth in ['/foo/bar.py', '/something/else/__init__.py']:
  186. thin._add_dependency(container, type(str('obj'), (), {'__file__': pth})())
  187. assert '__init__' not in container[1]
  188. assert container == ['/foo/bar.py', '/something/else']
  189. def test_thin_path(self):
  190. '''
  191. Test thin.thin_path returns the expected path.
  192. :return:
  193. '''
  194. path = os.sep + os.path.join('path', 'to')
  195. expected = os.path.join(path, 'thin', 'thin.tgz')
  196. self.assertEqual(thin.thin_path(path), expected)
  197. def test_get_salt_call_script(self):
  198. '''
  199. Test get salt-call script rendered.
  200. :return:
  201. '''
  202. out = thin._get_salt_call('foo', 'bar', py26=[2, 6], py27=[2, 7], py34=[3, 4])
  203. for line in salt.utils.stringutils.to_str(out).split(os.linesep):
  204. if line.startswith('namespaces = {'):
  205. data = salt.utils.json.loads(line.replace('namespaces = ', '').strip())
  206. assert data.get('py26') == [2, 6]
  207. assert data.get('py27') == [2, 7]
  208. assert data.get('py34') == [3, 4]
  209. if line.startswith('syspaths = '):
  210. data = salt.utils.json.loads(line.replace('syspaths = ', ''))
  211. assert data == ['foo', 'bar']
  212. def test_get_ext_namespaces_empty(self):
  213. '''
  214. Test thin._get_ext_namespaces function returns an empty dictionary on nothing
  215. :return:
  216. '''
  217. for obj in [None, {}, []]:
  218. assert thin._get_ext_namespaces(obj) == {}
  219. def test_get_ext_namespaces(self):
  220. '''
  221. Test thin._get_ext_namespaces function returns namespaces properly out of the config.
  222. :return:
  223. '''
  224. cfg = {'ns': {'py-version': [2, 7]}}
  225. assert thin._get_ext_namespaces(cfg).get('ns') == (2, 7,)
  226. assert isinstance(thin._get_ext_namespaces(cfg).get('ns'), tuple)
  227. def test_get_ext_namespaces_failure(self):
  228. '''
  229. Test thin._get_ext_namespaces function raises an exception
  230. if python major/minor version is not configured.
  231. :return:
  232. '''
  233. with pytest.raises(salt.exceptions.SaltSystemExit):
  234. thin._get_ext_namespaces({'ns': {}})
  235. @patch('salt.utils.thin.salt', type(str('salt'), (), {'__file__': '/site-packages/salt'}))
  236. @patch('salt.utils.thin.jinja2', type(str('jinja2'), (), {'__file__': '/site-packages/jinja2'}))
  237. @patch('salt.utils.thin.yaml', type(str('yaml'), (), {'__file__': '/site-packages/yaml'}))
  238. @patch('salt.utils.thin.tornado', type(str('tornado'), (), {'__file__': '/site-packages/tornado'}))
  239. @patch('salt.utils.thin.msgpack', type(str('msgpack'), (), {'__file__': '/site-packages/msgpack'}))
  240. @patch('salt.utils.thin.certifi', type(str('certifi'), (), {'__file__': '/site-packages/certifi'}))
  241. @patch('salt.utils.thin.singledispatch', type(str('singledispatch'), (), {'__file__': '/site-packages/sdp'}))
  242. @patch('salt.utils.thin.singledispatch_helpers', type(str('singledispatch_helpers'), (), {'__file__': '/site-packages/sdp_hlp'}))
  243. @patch('salt.utils.thin.ssl_match_hostname', type(str('ssl_match_hostname'), (), {'__file__': '/site-packages/ssl_mh'}))
  244. @patch('salt.utils.thin.markupsafe', type(str('markupsafe'), (), {'__file__': '/site-packages/markupsafe'}))
  245. @patch('salt.utils.thin.backports_abc', type(str('backports_abc'), (), {'__file__': '/site-packages/backports_abc'}))
  246. @patch('salt.utils.thin.concurrent', type(str('concurrent'), (), {'__file__': '/site-packages/concurrent'}))
  247. @patch('salt.utils.thin.log', MagicMock())
  248. def test_get_tops(self):
  249. '''
  250. Test thin.get_tops to get top directories, based on the interpreter.
  251. :return:
  252. '''
  253. base_tops = ['/site-packages/salt', '/site-packages/jinja2', '/site-packages/yaml',
  254. '/site-packages/tornado', '/site-packages/msgpack', '/site-packages/certifi',
  255. '/site-packages/sdp', '/site-packages/sdp_hlp', '/site-packages/ssl_mh',
  256. '/site-packages/markupsafe', '/site-packages/backports_abc', '/site-packages/concurrent']
  257. tops = thin.get_tops()
  258. assert len(tops) == len(base_tops)
  259. assert sorted(tops) == sorted(base_tops)
  260. @patch('salt.utils.thin.salt', type(str('salt'), (), {'__file__': '/site-packages/salt'}))
  261. @patch('salt.utils.thin.jinja2', type(str('jinja2'), (), {'__file__': '/site-packages/jinja2'}))
  262. @patch('salt.utils.thin.yaml', type(str('yaml'), (), {'__file__': '/site-packages/yaml'}))
  263. @patch('salt.utils.thin.tornado', type(str('tornado'), (), {'__file__': '/site-packages/tornado'}))
  264. @patch('salt.utils.thin.msgpack', type(str('msgpack'), (), {'__file__': '/site-packages/msgpack'}))
  265. @patch('salt.utils.thin.certifi', type(str('certifi'), (), {'__file__': '/site-packages/certifi'}))
  266. @patch('salt.utils.thin.singledispatch', type(str('singledispatch'), (), {'__file__': '/site-packages/sdp'}))
  267. @patch('salt.utils.thin.singledispatch_helpers', type(str('singledispatch_helpers'), (), {'__file__': '/site-packages/sdp_hlp'}))
  268. @patch('salt.utils.thin.ssl_match_hostname', type(str('ssl_match_hostname'), (), {'__file__': '/site-packages/ssl_mh'}))
  269. @patch('salt.utils.thin.markupsafe', type(str('markupsafe'), (), {'__file__': '/site-packages/markupsafe'}))
  270. @patch('salt.utils.thin.backports_abc', type(str('backports_abc'), (), {'__file__': '/site-packages/backports_abc'}))
  271. @patch('salt.utils.thin.concurrent', type(str('concurrent'), (), {'__file__': '/site-packages/concurrent'}))
  272. @patch('salt.utils.thin.log', MagicMock())
  273. def test_get_tops_extra_mods(self):
  274. '''
  275. Test thin.get_tops to get extra-modules alongside the top directories, based on the interpreter.
  276. :return:
  277. '''
  278. base_tops = ['/site-packages/salt',
  279. '/site-packages/jinja2',
  280. '/site-packages/yaml',
  281. '/site-packages/tornado',
  282. '/site-packages/msgpack',
  283. '/site-packages/certifi',
  284. '/site-packages/sdp',
  285. '/site-packages/sdp_hlp',
  286. '/site-packages/ssl_mh',
  287. '/site-packages/concurrent',
  288. '/site-packages/markupsafe',
  289. '/site-packages/backports_abc',
  290. os.sep + os.path.join('custom', 'foo'),
  291. os.sep + os.path.join('custom', 'bar.py')]
  292. builtins = sys.version_info.major == 3 and 'builtins' or '__builtin__'
  293. foo = {'__file__': os.sep + os.path.join('custom', 'foo', '__init__.py')}
  294. bar = {'__file__': os.sep + os.path.join('custom', 'bar')}
  295. with patch('{}.__import__'.format(builtins),
  296. MagicMock(side_effect=[type(str('foo'), (), foo),
  297. type(str('bar'), (), bar)])):
  298. tops = thin.get_tops(extra_mods='foo,bar')
  299. self.assertEqual(len(tops), len(base_tops))
  300. self.assertListEqual(sorted(tops), sorted(base_tops))
  301. @patch('salt.utils.thin.salt', type(str('salt'), (), {'__file__': '/site-packages/salt'}))
  302. @patch('salt.utils.thin.jinja2', type(str('jinja2'), (), {'__file__': '/site-packages/jinja2'}))
  303. @patch('salt.utils.thin.yaml', type(str('yaml'), (), {'__file__': '/site-packages/yaml'}))
  304. @patch('salt.utils.thin.tornado', type(str('tornado'), (), {'__file__': '/site-packages/tornado'}))
  305. @patch('salt.utils.thin.msgpack', type(str('msgpack'), (), {'__file__': '/site-packages/msgpack'}))
  306. @patch('salt.utils.thin.certifi', type(str('certifi'), (), {'__file__': '/site-packages/certifi'}))
  307. @patch('salt.utils.thin.singledispatch', type(str('singledispatch'), (), {'__file__': '/site-packages/sdp'}))
  308. @patch('salt.utils.thin.singledispatch_helpers', type(str('singledispatch_helpers'), (), {'__file__': '/site-packages/sdp_hlp'}))
  309. @patch('salt.utils.thin.ssl_match_hostname', type(str('ssl_match_hostname'), (), {'__file__': '/site-packages/ssl_mh'}))
  310. @patch('salt.utils.thin.markupsafe', type(str('markupsafe'), (), {'__file__': '/site-packages/markupsafe'}))
  311. @patch('salt.utils.thin.backports_abc', type(str('backports_abc'), (), {'__file__': '/site-packages/backports_abc'}))
  312. @patch('salt.utils.thin.concurrent', type(str('concurrent'), (), {'__file__': '/site-packages/concurrent'}))
  313. @patch('salt.utils.thin.log', MagicMock())
  314. def test_get_tops_so_mods(self):
  315. '''
  316. Test thin.get_tops to get extra-modules alongside the top directories, based on the interpreter.
  317. :return:
  318. '''
  319. base_tops = ['/site-packages/salt', '/site-packages/jinja2', '/site-packages/yaml',
  320. '/site-packages/tornado', '/site-packages/msgpack', '/site-packages/certifi',
  321. '/site-packages/sdp', '/site-packages/sdp_hlp', '/site-packages/ssl_mh', '/site-packages/concurrent',
  322. '/site-packages/markupsafe', '/site-packages/backports_abc', '/custom/foo.so', '/custom/bar.so']
  323. builtins = sys.version_info.major == 3 and 'builtins' or '__builtin__'
  324. with patch('{}.__import__'.format(builtins),
  325. MagicMock(side_effect=[type(str('salt'), (), {'__file__': '/custom/foo.so'}),
  326. type(str('salt'), (), {'__file__': '/custom/bar.so'})])):
  327. tops = thin.get_tops(so_mods='foo,bar')
  328. assert len(tops) == len(base_tops)
  329. assert sorted(tops) == sorted(base_tops)
  330. @patch('salt.utils.thin.gen_thin', MagicMock(return_value='/path/to/thin/thin.tgz'))
  331. @patch('salt.utils.hashutils.get_hash', MagicMock(return_value=12345))
  332. def test_thin_sum(self):
  333. '''
  334. Test thin.thin_sum function.
  335. :return:
  336. '''
  337. assert thin.thin_sum('/cachedir', form='sha256')[1] == 12345
  338. thin.salt.utils.hashutils.get_hash.assert_called()
  339. assert thin.salt.utils.hashutils.get_hash.call_count == 1
  340. path, form = thin.salt.utils.hashutils.get_hash.call_args[0]
  341. assert path == '/path/to/thin/thin.tgz'
  342. assert form == 'sha256'
  343. @patch('salt.utils.thin.gen_min', MagicMock(return_value='/path/to/thin/min.tgz'))
  344. @patch('salt.utils.hashutils.get_hash', MagicMock(return_value=12345))
  345. def test_min_sum(self):
  346. '''
  347. Test thin.thin_sum function.
  348. :return:
  349. '''
  350. assert thin.min_sum('/cachedir', form='sha256') == 12345
  351. thin.salt.utils.hashutils.get_hash.assert_called()
  352. assert thin.salt.utils.hashutils.get_hash.call_count == 1
  353. path, form = thin.salt.utils.hashutils.get_hash.call_args[0]
  354. assert path == '/path/to/thin/min.tgz'
  355. assert form == 'sha256'
  356. @patch('salt.utils.thin.sys.version_info', (2, 5))
  357. @patch('salt.exceptions.SaltSystemExit', Exception)
  358. def test_gen_thin_fails_ancient_python_version(self):
  359. '''
  360. Test thin.gen_thin function raises an exception
  361. if Python major/minor version is lower than 2.6
  362. :return:
  363. '''
  364. with pytest.raises(salt.exceptions.SaltSystemExit) as err:
  365. thin.sys.exc_clear = lambda: None
  366. thin.gen_thin('')
  367. self.assertIn('The minimum required python version to run salt-ssh is '
  368. '"2.6"', str(err.value))
  369. @skipIf(salt.utils.platform.is_windows() and thin._six.PY2,
  370. 'Dies on Python2 on Windows')
  371. @patch('salt.exceptions.SaltSystemExit', Exception)
  372. @patch('salt.utils.thin.os.makedirs', MagicMock())
  373. @patch('salt.utils.files.fopen', MagicMock())
  374. @patch('salt.utils.thin._get_salt_call', MagicMock())
  375. @patch('salt.utils.thin._get_ext_namespaces', MagicMock())
  376. @patch('salt.utils.thin.get_tops', MagicMock(return_value=['/foo3', '/bar3']))
  377. @patch('salt.utils.thin.get_ext_tops', MagicMock(return_value={}))
  378. @patch('salt.utils.thin.os.path.isfile', MagicMock())
  379. @patch('salt.utils.thin.os.path.isdir', MagicMock(return_value=True))
  380. @patch('salt.utils.thin.os.remove', MagicMock())
  381. @patch('salt.utils.thin.os.path.exists', MagicMock())
  382. @patch('salt.utils.path.os_walk', MagicMock(return_value=[]))
  383. @patch('salt.utils.thin.subprocess.Popen',
  384. _popen(None, side_effect=[(bts('2.7'), bts('')), (bts('["/foo27", "/bar27"]'), bts(''))]))
  385. @patch('salt.utils.thin.tarfile', MagicMock())
  386. @patch('salt.utils.thin.zipfile', MagicMock())
  387. @patch('salt.utils.thin.os.getcwd', MagicMock())
  388. @patch('salt.utils.thin.os.chdir', MagicMock())
  389. @patch('salt.utils.thin.tempfile.mkdtemp', MagicMock())
  390. @patch('salt.utils.thin.tempfile.mkstemp', MagicMock(return_value=(3, ".temporary")))
  391. @patch('salt.utils.thin.shutil', MagicMock())
  392. @patch('salt.utils.path.which', MagicMock(return_value=''))
  393. @patch('salt.utils.thin._get_thintar_prefix', MagicMock())
  394. def test_gen_thin_python_exist_or_not(self):
  395. '''
  396. Test thin.gen_thin function if the opposite python
  397. binary does not exist
  398. '''
  399. with TstSuiteLoggingHandler() as handler:
  400. thin.gen_thin('')
  401. salt.utils.thin.subprocess.Popen.assert_not_called()
  402. if salt.ext.six.PY2:
  403. self.assertIn('DEBUG:python3 binary does not exist. Will not attempt to generate '
  404. 'tops for Python 3',
  405. handler.messages)
  406. if salt.ext.six.PY3:
  407. self.assertIn('DEBUG:python2 binary does not exist. Will not '
  408. 'detect Python 2 version',
  409. handler.messages)
  410. self.assertIn('DEBUG:python2 binary does not exist. Will not attempt to generate '
  411. 'tops for Python 2',
  412. handler.messages)
  413. @skipIf(salt.utils.platform.is_windows() and thin._six.PY2,
  414. 'Dies on Python2 on Windows')
  415. @patch('salt.exceptions.SaltSystemExit', Exception)
  416. @patch('salt.utils.thin.log', MagicMock())
  417. @patch('salt.utils.thin.os.makedirs', MagicMock())
  418. @patch('salt.utils.files.fopen', MagicMock())
  419. @patch('salt.utils.thin._get_salt_call', MagicMock())
  420. @patch('salt.utils.thin._get_ext_namespaces', MagicMock())
  421. @patch('salt.utils.thin.get_tops', MagicMock(return_value=['/foo3', '/bar3']))
  422. @patch('salt.utils.thin.get_ext_tops', MagicMock(return_value={}))
  423. @patch('salt.utils.thin.os.path.isfile', MagicMock())
  424. @patch('salt.utils.thin.os.path.isdir', MagicMock(return_value=True))
  425. @patch('salt.utils.thin.log', MagicMock())
  426. @patch('salt.utils.thin.os.remove', MagicMock())
  427. @patch('salt.utils.thin.os.path.exists', MagicMock())
  428. @patch('salt.utils.path.os_walk', MagicMock(return_value=[]))
  429. @patch('salt.utils.thin.subprocess.Popen',
  430. _popen(None, side_effect=[(bts('2.7'), bts('')), (bts('["/foo27", "/bar27"]'), bts(''))]))
  431. @patch('salt.utils.thin.tarfile', MagicMock())
  432. @patch('salt.utils.thin.zipfile', MagicMock())
  433. @patch('salt.utils.thin.os.getcwd', MagicMock())
  434. @patch('salt.utils.thin.os.chdir', MagicMock())
  435. @patch('salt.utils.thin.os.close', MagicMock())
  436. @patch('salt.utils.thin.tempfile.mkdtemp', MagicMock())
  437. @patch('salt.utils.thin.tempfile.mkstemp', MagicMock(return_value=(3, ".temporary")))
  438. @patch('salt.utils.thin.shutil', MagicMock())
  439. @patch('salt.utils.thin._six.PY3', True)
  440. @patch('salt.utils.thin._six.PY2', False)
  441. @patch('salt.utils.thin.sys.version_info', _version_info(None, 3, 6))
  442. @patch('salt.utils.path.which', MagicMock(return_value='/usr/bin/python'))
  443. def test_gen_thin_compression_fallback_py3(self):
  444. '''
  445. Test thin.gen_thin function if fallbacks to the gzip compression, once setup wrong.
  446. NOTE: Py2 version of this test is not required, as code shares the same spot across the versions.
  447. :return:
  448. '''
  449. thin.gen_thin('', compress='arj')
  450. thin.log.warning.assert_called()
  451. pt, msg = thin.log.warning.mock_calls[0][1]
  452. assert pt % msg == 'Unknown compression type: "arj". Falling back to "gzip" compression.'
  453. thin.zipfile.ZipFile.assert_not_called()
  454. thin.tarfile.open.assert_called()
  455. @patch('salt.exceptions.SaltSystemExit', Exception)
  456. @patch('salt.utils.thin.log', MagicMock())
  457. @patch('salt.utils.thin.os.makedirs', MagicMock())
  458. @patch('salt.utils.files.fopen', MagicMock())
  459. @patch('salt.utils.thin._get_salt_call', MagicMock())
  460. @patch('salt.utils.thin._get_ext_namespaces', MagicMock())
  461. @patch('salt.utils.thin.get_tops', MagicMock(return_value=['/foo3', '/bar3']))
  462. @patch('salt.utils.thin.get_ext_tops', MagicMock(return_value={}))
  463. @patch('salt.utils.thin.os.path.isfile', MagicMock())
  464. @patch('salt.utils.thin.os.path.isdir', MagicMock(return_value=False))
  465. @patch('salt.utils.thin.log', MagicMock())
  466. @patch('salt.utils.thin.os.remove', MagicMock())
  467. @patch('salt.utils.thin.os.path.exists', MagicMock())
  468. @patch('salt.utils.path.os_walk', MagicMock(return_value=[]))
  469. @patch('salt.utils.thin.subprocess.Popen',
  470. _popen(None, side_effect=[(bts('2.7'), bts('')), (bts('["/foo27", "/bar27"]'), bts(''))]))
  471. @patch('salt.utils.thin.tarfile', MagicMock())
  472. @patch('salt.utils.thin.zipfile', MagicMock())
  473. @patch('salt.utils.thin.os.getcwd', MagicMock())
  474. @patch('salt.utils.thin.os.chdir', MagicMock())
  475. @patch('salt.utils.thin.os.close', MagicMock())
  476. @patch('salt.utils.thin.tempfile.mkdtemp', MagicMock(return_value=''))
  477. @patch('salt.utils.thin.tempfile.mkstemp', MagicMock(return_value=(3, ".temporary")))
  478. @patch('salt.utils.thin.shutil', MagicMock())
  479. @patch('salt.utils.thin._six.PY3', True)
  480. @patch('salt.utils.thin._six.PY2', False)
  481. @patch('salt.utils.thin.sys.version_info', _version_info(None, 3, 6))
  482. @patch('salt.utils.path.which', MagicMock(return_value='/usr/bin/python'))
  483. def test_gen_thin_control_files_written_py3(self):
  484. '''
  485. Test thin.gen_thin function if control files are written (version, salt-call etc).
  486. NOTE: Py2 version of this test is not required, as code shares the same spot across the versions.
  487. :return:
  488. '''
  489. thin.gen_thin('')
  490. arc_name, arc_mode = thin.tarfile.method_calls[0][1]
  491. self.assertEqual(arc_name, ".temporary")
  492. self.assertEqual(arc_mode, 'w:gz')
  493. for idx, fname in enumerate(['version', '.thin-gen-py-version', 'salt-call', 'supported-versions']):
  494. name = thin.tarfile.open().method_calls[idx + 4][1][0]
  495. self.assertEqual(name, fname)
  496. thin.tarfile.open().close.assert_called()
  497. @patch('salt.exceptions.SaltSystemExit', Exception)
  498. @patch('salt.utils.thin.log', MagicMock())
  499. @patch('salt.utils.thin.os.makedirs', MagicMock())
  500. @patch('salt.utils.files.fopen', MagicMock())
  501. @patch('salt.utils.thin._get_salt_call', MagicMock())
  502. @patch('salt.utils.thin._get_ext_namespaces', MagicMock())
  503. @patch('salt.utils.thin.get_tops', MagicMock(return_value=['/salt', '/bar3']))
  504. @patch('salt.utils.thin.get_ext_tops', MagicMock(return_value={}))
  505. @patch('salt.utils.thin.os.path.isfile', MagicMock())
  506. @patch('salt.utils.thin.os.path.isdir', MagicMock(return_value=True))
  507. @patch('salt.utils.thin.log', MagicMock())
  508. @patch('salt.utils.thin.os.remove', MagicMock())
  509. @patch('salt.utils.thin.os.path.exists', MagicMock())
  510. @patch('salt.utils.path.os_walk',
  511. MagicMock(return_value=(('root', [], ['r1', 'r2', 'r3']), ('root2', [], ['r4', 'r5', 'r6']))))
  512. @patch('salt.utils.thin.subprocess.Popen',
  513. _popen(None, side_effect=[(bts('2.7'), bts('')), (bts('["/foo27", "/bar27"]'), bts(''))]))
  514. @patch('salt.utils.thin.tarfile', _tarfile(None))
  515. @patch('salt.utils.thin.zipfile', MagicMock())
  516. @patch('salt.utils.thin.os.getcwd', MagicMock())
  517. @patch('salt.utils.thin.os.chdir', MagicMock())
  518. @patch('salt.utils.thin.os.close', MagicMock())
  519. @patch('salt.utils.thin.tempfile.mkdtemp', MagicMock(return_value=''))
  520. @patch('salt.utils.thin.tempfile.mkstemp', MagicMock(return_value=(3, ".temporary")))
  521. @patch('salt.utils.thin.shutil', MagicMock())
  522. @patch('salt.utils.thin._six.PY3', True)
  523. @patch('salt.utils.thin._six.PY2', False)
  524. @patch('salt.utils.thin.sys.version_info', _version_info(None, 3, 6))
  525. @patch('salt.utils.hashutils.DigestCollector', MagicMock())
  526. @patch('salt.utils.path.which', MagicMock(return_value='/usr/bin/python'))
  527. def test_gen_thin_main_content_files_written_py3(self):
  528. '''
  529. Test thin.gen_thin function if main content files are written.
  530. NOTE: Py2 version of this test is not required, as code shares the same spot across the versions.
  531. :return:
  532. '''
  533. thin.gen_thin('')
  534. files = []
  535. for py in ('py2', 'py2', 'py3', 'pyall'):
  536. for i in range(1, 4):
  537. files.append(os.path.join(py, 'root', 'r{0}'.format(i)))
  538. for i in range(4, 7):
  539. files.append(os.path.join(py, 'root2', 'r{0}'.format(i)))
  540. for cl in thin.tarfile.open().method_calls[:-6]:
  541. arcname = cl[2].get('arcname')
  542. self.assertIn(arcname, files)
  543. files.pop(files.index(arcname))
  544. self.assertFalse(files)
  545. @patch('salt.exceptions.SaltSystemExit', Exception)
  546. @patch('salt.utils.thin.log', MagicMock())
  547. @patch('salt.utils.thin.os.makedirs', MagicMock())
  548. @patch('salt.utils.files.fopen', MagicMock())
  549. @patch('salt.utils.thin._get_salt_call', MagicMock())
  550. @patch('salt.utils.thin._get_ext_namespaces', MagicMock())
  551. @patch('salt.utils.thin.get_tops', MagicMock(return_value=[]))
  552. @patch('salt.utils.thin.get_ext_tops',
  553. MagicMock(return_value={'namespace': {'py-version': [2, 7],
  554. 'path': '/opt/2015.8/salt',
  555. 'dependencies': ['/opt/certifi', '/opt/whatever']}}))
  556. @patch('salt.utils.thin.os.path.isfile', MagicMock())
  557. @patch('salt.utils.thin.os.path.isdir', MagicMock(return_value=True))
  558. @patch('salt.utils.thin.log', MagicMock())
  559. @patch('salt.utils.thin.os.remove', MagicMock())
  560. @patch('salt.utils.thin.os.path.exists', MagicMock())
  561. @patch('salt.utils.path.os_walk',
  562. MagicMock(return_value=(('root', [], ['r1', 'r2', 'r3']), ('root2', [], ['r4', 'r5', 'r6']))))
  563. @patch('salt.utils.thin.subprocess.Popen',
  564. _popen(None, side_effect=[(bts('2.7'), bts('')), (bts('["/foo27", "/bar27"]'), bts(''))]))
  565. @patch('salt.utils.thin.tarfile', _tarfile(None))
  566. @patch('salt.utils.thin.zipfile', MagicMock())
  567. @patch('salt.utils.thin.os.getcwd', MagicMock())
  568. @patch('salt.utils.thin.os.chdir', MagicMock())
  569. @patch('salt.utils.thin.os.close', MagicMock())
  570. @patch('salt.utils.thin.tempfile.mkdtemp', MagicMock(return_value=''))
  571. @patch('salt.utils.thin.tempfile.mkstemp', MagicMock(return_value=(3, ".temporary")))
  572. @patch('salt.utils.thin.shutil', MagicMock())
  573. @patch('salt.utils.thin._six.PY3', True)
  574. @patch('salt.utils.thin._six.PY2', False)
  575. @patch('salt.utils.thin.sys.version_info', _version_info(None, 3, 6))
  576. @patch('salt.utils.hashutils.DigestCollector', MagicMock())
  577. @patch('salt.utils.path.which', MagicMock(return_value='/usr/bin/python'))
  578. def test_gen_thin_ext_alternative_content_files_written_py3(self):
  579. '''
  580. Test thin.gen_thin function if external alternative content files are written.
  581. NOTE: Py2 version of this test is not required, as code shares the same spot across the versions.
  582. :return:
  583. '''
  584. thin.gen_thin('')
  585. files = []
  586. for py in ('pyall', 'pyall', 'py2'):
  587. for i in range(1, 4):
  588. files.append(
  589. os.path.join('namespace', py, 'root', 'r{0}'.format(i)))
  590. for i in range(4, 7):
  591. files.append(
  592. os.path.join('namespace', py, 'root2', 'r{0}'.format(i)))
  593. for idx, cl in enumerate(thin.tarfile.open().method_calls[12:-6]):
  594. arcname = cl[2].get('arcname')
  595. self.assertIn(arcname, files)
  596. files.pop(files.index(arcname))
  597. self.assertFalse(files)
  598. def test_get_supported_py_config_typecheck(self):
  599. '''
  600. Test collecting proper py-versions. Should return bytes type.
  601. :return:
  602. '''
  603. tops = {}
  604. ext_cfg = {}
  605. out = thin._get_supported_py_config(tops=tops, extended_cfg=ext_cfg)
  606. assert type(salt.utils.stringutils.to_bytes('')) == type(out)
  607. def test_get_supported_py_config_base_tops(self):
  608. '''
  609. Test collecting proper py-versions. Should return proper base tops.
  610. :return:
  611. '''
  612. tops = {'3': ['/groundkeepers', '/stole'], '2': ['/the-root', '/password']}
  613. ext_cfg = {}
  614. out = salt.utils.stringutils.to_str(thin._get_supported_py_config(
  615. tops=tops, extended_cfg=ext_cfg)).strip().split(os.linesep)
  616. self.assertEqual(len(out), 2)
  617. for t_line in ['py3:3:0', 'py2:2:7']:
  618. self.assertIn(t_line, out)
  619. def test_get_supported_py_config_ext_tops(self):
  620. '''
  621. Test collecting proper py-versions. Should return proper ext conf tops.
  622. :return:
  623. '''
  624. tops = {}
  625. ext_cfg = {
  626. 'solar-interference': {
  627. 'py-version': [2, 6]},
  628. 'second-system-effect': {
  629. 'py-version': [2, 7]}}
  630. out = salt.utils.stringutils.to_str(thin._get_supported_py_config(
  631. tops=tops, extended_cfg=ext_cfg)).strip().split(os.linesep)
  632. for t_line in ['second-system-effect:2:7', 'solar-interference:2:6']:
  633. self.assertIn(t_line, out)