1
0

test_thin.py 33 KB

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