test_execute.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. import subprocess
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic import execute as module
  5. @pytest.mark.parametrize(
  6. 'command,exit_code,borg_local_path,borg_exit_codes,expected_result',
  7. (
  8. (['grep'], 2, None, None, module.Exit_status.ERROR),
  9. (['grep'], 2, 'borg', None, module.Exit_status.ERROR),
  10. (['borg'], 2, 'borg', None, module.Exit_status.ERROR),
  11. (['borg1'], 2, 'borg1', None, module.Exit_status.ERROR),
  12. (['grep'], 1, None, None, module.Exit_status.ERROR),
  13. (['grep'], 1, 'borg', None, module.Exit_status.ERROR),
  14. (['borg'], 1, 'borg', None, module.Exit_status.WARNING),
  15. (['borg1'], 1, 'borg1', None, module.Exit_status.WARNING),
  16. (['grep'], 100, None, None, module.Exit_status.ERROR),
  17. (['grep'], 100, 'borg', None, module.Exit_status.ERROR),
  18. (['borg'], 100, 'borg', None, module.Exit_status.WARNING),
  19. (['borg1'], 100, 'borg1', None, module.Exit_status.WARNING),
  20. (['grep'], 0, None, None, module.Exit_status.SUCCESS),
  21. (['grep'], 0, 'borg', None, module.Exit_status.SUCCESS),
  22. (['borg'], 0, 'borg', None, module.Exit_status.SUCCESS),
  23. (['borg1'], 0, 'borg1', None, module.Exit_status.SUCCESS),
  24. # -9 exit code occurs when child process get SIGKILLed.
  25. (['grep'], -9, None, None, module.Exit_status.ERROR),
  26. (['grep'], -9, 'borg', None, module.Exit_status.ERROR),
  27. (['borg'], -9, 'borg', None, module.Exit_status.ERROR),
  28. (['borg1'], -9, 'borg1', None, module.Exit_status.ERROR),
  29. (['borg'], None, None, None, module.Exit_status.STILL_RUNNING),
  30. (['borg'], 1, 'borg', [], module.Exit_status.WARNING),
  31. (['borg'], 1, 'borg', [{}], module.Exit_status.WARNING),
  32. (['borg'], 1, 'borg', [{'code': 1}], module.Exit_status.WARNING),
  33. (['grep'], 1, 'borg', [{'code': 100, 'treat_as': 'error'}], module.Exit_status.ERROR),
  34. (['borg'], 1, 'borg', [{'code': 100, 'treat_as': 'error'}], module.Exit_status.WARNING),
  35. (['borg'], 1, 'borg', [{'code': 1, 'treat_as': 'error'}], module.Exit_status.ERROR),
  36. (['borg'], 2, 'borg', [{'code': 99, 'treat_as': 'warning'}], module.Exit_status.ERROR),
  37. (['borg'], 2, 'borg', [{'code': 2, 'treat_as': 'warning'}], module.Exit_status.WARNING),
  38. (['borg'], 100, 'borg', [{'code': 1, 'treat_as': 'error'}], module.Exit_status.WARNING),
  39. (['borg'], 100, 'borg', [{'code': 100, 'treat_as': 'error'}], module.Exit_status.ERROR),
  40. ),
  41. )
  42. def test_interpret_exit_code_respects_exit_code_and_borg_local_path(
  43. command, exit_code, borg_local_path, borg_exit_codes, expected_result
  44. ):
  45. assert (
  46. module.interpret_exit_code(command, exit_code, borg_local_path, borg_exit_codes)
  47. is expected_result
  48. )
  49. def test_command_for_process_converts_sequence_command_to_string():
  50. process = flexmock(args=['foo', 'bar', 'baz'])
  51. assert module.command_for_process(process) == 'foo bar baz'
  52. def test_command_for_process_passes_through_string_command():
  53. process = flexmock(args='foo bar baz')
  54. assert module.command_for_process(process) == 'foo bar baz'
  55. def test_output_buffer_for_process_returns_stderr_when_stdout_excluded():
  56. stdout = flexmock()
  57. stderr = flexmock()
  58. process = flexmock(stdout=stdout, stderr=stderr)
  59. assert module.output_buffer_for_process(process, exclude_stdouts=[flexmock(), stdout]) == stderr
  60. def test_output_buffer_for_process_returns_stdout_when_not_excluded():
  61. stdout = flexmock()
  62. process = flexmock(stdout=stdout)
  63. assert (
  64. module.output_buffer_for_process(process, exclude_stdouts=[flexmock(), flexmock()])
  65. == stdout
  66. )
  67. def test_append_last_lines_under_max_line_count_appends():
  68. last_lines = ['last']
  69. flexmock(module.logger).should_receive('log').once()
  70. module.append_last_lines(
  71. last_lines, captured_output=flexmock(), line='line', output_log_level=flexmock()
  72. )
  73. assert last_lines == ['last', 'line']
  74. def test_append_last_lines_over_max_line_count_trims_and_appends():
  75. original_last_lines = [str(number) for number in range(0, module.ERROR_OUTPUT_MAX_LINE_COUNT)]
  76. last_lines = list(original_last_lines)
  77. flexmock(module.logger).should_receive('log').once()
  78. module.append_last_lines(
  79. last_lines, captured_output=flexmock(), line='line', output_log_level=flexmock()
  80. )
  81. assert last_lines == original_last_lines[1:] + ['line']
  82. def test_append_last_lines_with_output_log_level_none_appends_captured_output():
  83. last_lines = ['last']
  84. captured_output = ['captured']
  85. flexmock(module.logger).should_receive('log').never()
  86. module.append_last_lines(
  87. last_lines, captured_output=captured_output, line='line', output_log_level=None
  88. )
  89. assert captured_output == ['captured', 'line']
  90. def test_mask_command_secrets_masks_password_flag_value():
  91. assert module.mask_command_secrets(('cooldb', '--username', 'bob', '--password', 'pass')) == (
  92. 'cooldb',
  93. '--username',
  94. 'bob',
  95. '--password',
  96. '***',
  97. )
  98. def test_mask_command_secrets_passes_through_other_commands():
  99. assert module.mask_command_secrets(('cooldb', '--username', 'bob')) == (
  100. 'cooldb',
  101. '--username',
  102. 'bob',
  103. )
  104. @pytest.mark.parametrize(
  105. 'full_command,input_file,output_file,environment,expected_result',
  106. (
  107. (('foo', 'bar'), None, None, None, 'foo bar'),
  108. (('foo', 'bar'), flexmock(name='input'), None, None, 'foo bar < input'),
  109. (('foo', 'bar'), None, flexmock(name='output'), None, 'foo bar > output'),
  110. (
  111. ('A',) * module.MAX_LOGGED_COMMAND_LENGTH,
  112. None,
  113. None,
  114. None,
  115. 'A ' * (module.MAX_LOGGED_COMMAND_LENGTH // 2 - 2) + '...',
  116. ),
  117. (
  118. ('foo', 'bar'),
  119. flexmock(name='input'),
  120. flexmock(name='output'),
  121. None,
  122. 'foo bar < input > output',
  123. ),
  124. (
  125. ('foo', 'bar'),
  126. None,
  127. None,
  128. {'DBPASS': 'secret', 'OTHER': 'thing'},
  129. 'DBPASS=*** OTHER=*** foo bar',
  130. ),
  131. ),
  132. )
  133. def test_log_command_logs_command_constructed_from_arguments(
  134. full_command, input_file, output_file, environment, expected_result
  135. ):
  136. flexmock(module).should_receive('mask_command_secrets').replace_with(lambda command: command)
  137. flexmock(module.logger).should_receive('debug').with_args(expected_result).once()
  138. module.log_command(full_command, input_file, output_file, environment)
  139. def test_execute_command_calls_full_command():
  140. full_command = ['foo', 'bar']
  141. flexmock(module).should_receive('log_command')
  142. flexmock(module.os, environ={'a': 'b'})
  143. flexmock(module.subprocess).should_receive('Popen').with_args(
  144. full_command,
  145. stdin=None,
  146. stdout=module.subprocess.PIPE,
  147. stderr=module.subprocess.STDOUT,
  148. shell=False,
  149. env=None,
  150. cwd=None,
  151. ).and_return(flexmock(stdout=None)).once()
  152. flexmock(module).should_receive('log_outputs')
  153. output = module.execute_command(full_command)
  154. assert output is None
  155. def test_execute_command_calls_full_command_with_output_file():
  156. full_command = ['foo', 'bar']
  157. output_file = flexmock(name='test')
  158. flexmock(module).should_receive('log_command')
  159. flexmock(module.os, environ={'a': 'b'})
  160. flexmock(module.subprocess).should_receive('Popen').with_args(
  161. full_command,
  162. stdin=None,
  163. stdout=output_file,
  164. stderr=module.subprocess.PIPE,
  165. shell=False,
  166. env=None,
  167. cwd=None,
  168. ).and_return(flexmock(stderr=None)).once()
  169. flexmock(module).should_receive('log_outputs')
  170. output = module.execute_command(full_command, output_file=output_file)
  171. assert output is None
  172. def test_execute_command_calls_full_command_without_capturing_output():
  173. full_command = ['foo', 'bar']
  174. flexmock(module).should_receive('log_command')
  175. flexmock(module.os, environ={'a': 'b'})
  176. flexmock(module.subprocess).should_receive('Popen').with_args(
  177. full_command, stdin=None, stdout=None, stderr=None, shell=False, env=None, cwd=None
  178. ).and_return(flexmock(wait=lambda: 0)).once()
  179. flexmock(module).should_receive('interpret_exit_code').and_return(module.Exit_status.SUCCESS)
  180. flexmock(module).should_receive('log_outputs')
  181. output = module.execute_command(full_command, output_file=module.DO_NOT_CAPTURE)
  182. assert output is None
  183. def test_execute_command_calls_full_command_with_input_file():
  184. full_command = ['foo', 'bar']
  185. input_file = flexmock(name='test')
  186. flexmock(module).should_receive('log_command')
  187. flexmock(module.os, environ={'a': 'b'})
  188. flexmock(module.subprocess).should_receive('Popen').with_args(
  189. full_command,
  190. stdin=input_file,
  191. stdout=module.subprocess.PIPE,
  192. stderr=module.subprocess.STDOUT,
  193. shell=False,
  194. env=None,
  195. cwd=None,
  196. ).and_return(flexmock(stdout=None)).once()
  197. flexmock(module).should_receive('log_outputs')
  198. output = module.execute_command(full_command, input_file=input_file)
  199. assert output is None
  200. def test_execute_command_calls_full_command_with_shell():
  201. full_command = ['foo', 'bar']
  202. flexmock(module).should_receive('log_command')
  203. flexmock(module.os, environ={'a': 'b'})
  204. flexmock(module.subprocess).should_receive('Popen').with_args(
  205. ' '.join(full_command),
  206. stdin=None,
  207. stdout=module.subprocess.PIPE,
  208. stderr=module.subprocess.STDOUT,
  209. shell=True,
  210. env=None,
  211. cwd=None,
  212. ).and_return(flexmock(stdout=None)).once()
  213. flexmock(module).should_receive('log_outputs')
  214. output = module.execute_command(full_command, shell=True)
  215. assert output is None
  216. def test_execute_command_calls_full_command_with_extra_environment():
  217. full_command = ['foo', 'bar']
  218. flexmock(module).should_receive('log_command')
  219. flexmock(module.os, environ={'a': 'b'})
  220. flexmock(module.subprocess).should_receive('Popen').with_args(
  221. full_command,
  222. stdin=None,
  223. stdout=module.subprocess.PIPE,
  224. stderr=module.subprocess.STDOUT,
  225. shell=False,
  226. env={'a': 'b', 'c': 'd'},
  227. cwd=None,
  228. ).and_return(flexmock(stdout=None)).once()
  229. flexmock(module).should_receive('log_outputs')
  230. output = module.execute_command(full_command, extra_environment={'c': 'd'})
  231. assert output is None
  232. def test_execute_command_calls_full_command_with_working_directory():
  233. full_command = ['foo', 'bar']
  234. flexmock(module).should_receive('log_command')
  235. flexmock(module.os, environ={'a': 'b'})
  236. flexmock(module.subprocess).should_receive('Popen').with_args(
  237. full_command,
  238. stdin=None,
  239. stdout=module.subprocess.PIPE,
  240. stderr=module.subprocess.STDOUT,
  241. shell=False,
  242. env=None,
  243. cwd='/working',
  244. ).and_return(flexmock(stdout=None)).once()
  245. flexmock(module).should_receive('log_outputs')
  246. output = module.execute_command(full_command, working_directory='/working')
  247. assert output is None
  248. def test_execute_command_without_run_to_completion_returns_process():
  249. full_command = ['foo', 'bar']
  250. process = flexmock()
  251. flexmock(module).should_receive('log_command')
  252. flexmock(module.os, environ={'a': 'b'})
  253. flexmock(module.subprocess).should_receive('Popen').with_args(
  254. full_command,
  255. stdin=None,
  256. stdout=module.subprocess.PIPE,
  257. stderr=module.subprocess.STDOUT,
  258. shell=False,
  259. env=None,
  260. cwd=None,
  261. ).and_return(process).once()
  262. flexmock(module).should_receive('log_outputs')
  263. assert module.execute_command(full_command, run_to_completion=False) == process
  264. def test_execute_command_and_capture_output_returns_stdout():
  265. full_command = ['foo', 'bar']
  266. expected_output = '[]'
  267. flexmock(module).should_receive('log_command')
  268. flexmock(module.os, environ={'a': 'b'})
  269. flexmock(module.subprocess).should_receive('check_output').with_args(
  270. full_command, stderr=None, shell=False, env=None, cwd=None
  271. ).and_return(flexmock(decode=lambda: expected_output)).once()
  272. output = module.execute_command_and_capture_output(full_command)
  273. assert output == expected_output
  274. def test_execute_command_and_capture_output_with_capture_stderr_returns_stderr():
  275. full_command = ['foo', 'bar']
  276. expected_output = '[]'
  277. flexmock(module).should_receive('log_command')
  278. flexmock(module.os, environ={'a': 'b'})
  279. flexmock(module.subprocess).should_receive('check_output').with_args(
  280. full_command, stderr=module.subprocess.STDOUT, shell=False, env=None, cwd=None
  281. ).and_return(flexmock(decode=lambda: expected_output)).once()
  282. output = module.execute_command_and_capture_output(full_command, capture_stderr=True)
  283. assert output == expected_output
  284. def test_execute_command_and_capture_output_returns_output_when_process_error_is_not_considered_an_error():
  285. full_command = ['foo', 'bar']
  286. expected_output = '[]'
  287. err_output = b'[]'
  288. flexmock(module).should_receive('log_command')
  289. flexmock(module.os, environ={'a': 'b'})
  290. flexmock(module.subprocess).should_receive('check_output').with_args(
  291. full_command, stderr=None, shell=False, env=None, cwd=None
  292. ).and_raise(subprocess.CalledProcessError(1, full_command, err_output)).once()
  293. flexmock(module).should_receive('interpret_exit_code').and_return(
  294. module.Exit_status.SUCCESS
  295. ).once()
  296. output = module.execute_command_and_capture_output(full_command)
  297. assert output == expected_output
  298. def test_execute_command_and_capture_output_raises_when_command_errors():
  299. full_command = ['foo', 'bar']
  300. expected_output = '[]'
  301. flexmock(module).should_receive('log_command')
  302. flexmock(module.os, environ={'a': 'b'})
  303. flexmock(module.subprocess).should_receive('check_output').with_args(
  304. full_command, stderr=None, shell=False, env=None, cwd=None
  305. ).and_raise(subprocess.CalledProcessError(2, full_command, expected_output)).once()
  306. flexmock(module).should_receive('interpret_exit_code').and_return(
  307. module.Exit_status.ERROR
  308. ).once()
  309. with pytest.raises(subprocess.CalledProcessError):
  310. module.execute_command_and_capture_output(full_command)
  311. def test_execute_command_and_capture_output_returns_output_with_shell():
  312. full_command = ['foo', 'bar']
  313. expected_output = '[]'
  314. flexmock(module).should_receive('log_command')
  315. flexmock(module.os, environ={'a': 'b'})
  316. flexmock(module.subprocess).should_receive('check_output').with_args(
  317. 'foo bar', stderr=None, shell=True, env=None, cwd=None
  318. ).and_return(flexmock(decode=lambda: expected_output)).once()
  319. output = module.execute_command_and_capture_output(full_command, shell=True)
  320. assert output == expected_output
  321. def test_execute_command_and_capture_output_returns_output_with_extra_environment():
  322. full_command = ['foo', 'bar']
  323. expected_output = '[]'
  324. flexmock(module).should_receive('log_command')
  325. flexmock(module.os, environ={'a': 'b'})
  326. flexmock(module.subprocess).should_receive('check_output').with_args(
  327. full_command,
  328. stderr=None,
  329. shell=False,
  330. env={'a': 'b', 'c': 'd'},
  331. cwd=None,
  332. ).and_return(flexmock(decode=lambda: expected_output)).once()
  333. output = module.execute_command_and_capture_output(
  334. full_command, shell=False, extra_environment={'c': 'd'}
  335. )
  336. assert output == expected_output
  337. def test_execute_command_and_capture_output_returns_output_with_working_directory():
  338. full_command = ['foo', 'bar']
  339. expected_output = '[]'
  340. flexmock(module).should_receive('log_command')
  341. flexmock(module.os, environ={'a': 'b'})
  342. flexmock(module.subprocess).should_receive('check_output').with_args(
  343. full_command, stderr=None, shell=False, env=None, cwd='/working'
  344. ).and_return(flexmock(decode=lambda: expected_output)).once()
  345. output = module.execute_command_and_capture_output(
  346. full_command, shell=False, working_directory='/working'
  347. )
  348. assert output == expected_output
  349. def test_execute_command_with_processes_calls_full_command():
  350. full_command = ['foo', 'bar']
  351. processes = (flexmock(),)
  352. flexmock(module).should_receive('log_command')
  353. flexmock(module.os, environ={'a': 'b'})
  354. flexmock(module.subprocess).should_receive('Popen').with_args(
  355. full_command,
  356. stdin=None,
  357. stdout=module.subprocess.PIPE,
  358. stderr=module.subprocess.STDOUT,
  359. shell=False,
  360. env=None,
  361. cwd=None,
  362. ).and_return(flexmock(stdout=None)).once()
  363. flexmock(module).should_receive('log_outputs')
  364. output = module.execute_command_with_processes(full_command, processes)
  365. assert output is None
  366. def test_execute_command_with_processes_returns_output_with_output_log_level_none():
  367. full_command = ['foo', 'bar']
  368. processes = (flexmock(),)
  369. flexmock(module).should_receive('log_command')
  370. flexmock(module.os, environ={'a': 'b'})
  371. process = flexmock(stdout=None)
  372. flexmock(module.subprocess).should_receive('Popen').with_args(
  373. full_command,
  374. stdin=None,
  375. stdout=module.subprocess.PIPE,
  376. stderr=module.subprocess.STDOUT,
  377. shell=False,
  378. env=None,
  379. cwd=None,
  380. ).and_return(process).once()
  381. flexmock(module).should_receive('log_outputs').and_return({process: 'out'})
  382. output = module.execute_command_with_processes(full_command, processes, output_log_level=None)
  383. assert output == 'out'
  384. def test_execute_command_with_processes_calls_full_command_with_output_file():
  385. full_command = ['foo', 'bar']
  386. processes = (flexmock(),)
  387. output_file = flexmock(name='test')
  388. flexmock(module).should_receive('log_command')
  389. flexmock(module.os, environ={'a': 'b'})
  390. flexmock(module.subprocess).should_receive('Popen').with_args(
  391. full_command,
  392. stdin=None,
  393. stdout=output_file,
  394. stderr=module.subprocess.PIPE,
  395. shell=False,
  396. env=None,
  397. cwd=None,
  398. ).and_return(flexmock(stderr=None)).once()
  399. flexmock(module).should_receive('log_outputs')
  400. output = module.execute_command_with_processes(full_command, processes, output_file=output_file)
  401. assert output is None
  402. def test_execute_command_with_processes_calls_full_command_without_capturing_output():
  403. full_command = ['foo', 'bar']
  404. processes = (flexmock(),)
  405. flexmock(module).should_receive('log_command')
  406. flexmock(module.os, environ={'a': 'b'})
  407. flexmock(module.subprocess).should_receive('Popen').with_args(
  408. full_command, stdin=None, stdout=None, stderr=None, shell=False, env=None, cwd=None
  409. ).and_return(flexmock(wait=lambda: 0)).once()
  410. flexmock(module).should_receive('interpret_exit_code').and_return(module.Exit_status.SUCCESS)
  411. flexmock(module).should_receive('log_outputs')
  412. output = module.execute_command_with_processes(
  413. full_command, processes, output_file=module.DO_NOT_CAPTURE
  414. )
  415. assert output is None
  416. def test_execute_command_with_processes_calls_full_command_with_input_file():
  417. full_command = ['foo', 'bar']
  418. processes = (flexmock(),)
  419. input_file = flexmock(name='test')
  420. flexmock(module).should_receive('log_command')
  421. flexmock(module.os, environ={'a': 'b'})
  422. flexmock(module.subprocess).should_receive('Popen').with_args(
  423. full_command,
  424. stdin=input_file,
  425. stdout=module.subprocess.PIPE,
  426. stderr=module.subprocess.STDOUT,
  427. shell=False,
  428. env=None,
  429. cwd=None,
  430. ).and_return(flexmock(stdout=None)).once()
  431. flexmock(module).should_receive('log_outputs')
  432. output = module.execute_command_with_processes(full_command, processes, input_file=input_file)
  433. assert output is None
  434. def test_execute_command_with_processes_calls_full_command_with_shell():
  435. full_command = ['foo', 'bar']
  436. processes = (flexmock(),)
  437. flexmock(module).should_receive('log_command')
  438. flexmock(module.os, environ={'a': 'b'})
  439. flexmock(module.subprocess).should_receive('Popen').with_args(
  440. ' '.join(full_command),
  441. stdin=None,
  442. stdout=module.subprocess.PIPE,
  443. stderr=module.subprocess.STDOUT,
  444. shell=True,
  445. env=None,
  446. cwd=None,
  447. ).and_return(flexmock(stdout=None)).once()
  448. flexmock(module).should_receive('log_outputs')
  449. output = module.execute_command_with_processes(full_command, processes, shell=True)
  450. assert output is None
  451. def test_execute_command_with_processes_calls_full_command_with_extra_environment():
  452. full_command = ['foo', 'bar']
  453. processes = (flexmock(),)
  454. flexmock(module).should_receive('log_command')
  455. flexmock(module.os, environ={'a': 'b'})
  456. flexmock(module.subprocess).should_receive('Popen').with_args(
  457. full_command,
  458. stdin=None,
  459. stdout=module.subprocess.PIPE,
  460. stderr=module.subprocess.STDOUT,
  461. shell=False,
  462. env={'a': 'b', 'c': 'd'},
  463. cwd=None,
  464. ).and_return(flexmock(stdout=None)).once()
  465. flexmock(module).should_receive('log_outputs')
  466. output = module.execute_command_with_processes(
  467. full_command, processes, extra_environment={'c': 'd'}
  468. )
  469. assert output is None
  470. def test_execute_command_with_processes_calls_full_command_with_working_directory():
  471. full_command = ['foo', 'bar']
  472. processes = (flexmock(),)
  473. flexmock(module).should_receive('log_command')
  474. flexmock(module.os, environ={'a': 'b'})
  475. flexmock(module.subprocess).should_receive('Popen').with_args(
  476. full_command,
  477. stdin=None,
  478. stdout=module.subprocess.PIPE,
  479. stderr=module.subprocess.STDOUT,
  480. shell=False,
  481. env=None,
  482. cwd='/working',
  483. ).and_return(flexmock(stdout=None)).once()
  484. flexmock(module).should_receive('log_outputs')
  485. output = module.execute_command_with_processes(
  486. full_command, processes, working_directory='/working'
  487. )
  488. assert output is None
  489. def test_execute_command_with_processes_kills_processes_on_error():
  490. full_command = ['foo', 'bar']
  491. flexmock(module).should_receive('log_command')
  492. process = flexmock(stdout=flexmock(read=lambda count: None))
  493. process.should_receive('poll')
  494. process.should_receive('kill').once()
  495. processes = (process,)
  496. flexmock(module.os, environ={'a': 'b'})
  497. flexmock(module.subprocess).should_receive('Popen').with_args(
  498. full_command,
  499. stdin=None,
  500. stdout=module.subprocess.PIPE,
  501. stderr=module.subprocess.STDOUT,
  502. shell=False,
  503. env=None,
  504. cwd=None,
  505. ).and_raise(subprocess.CalledProcessError(1, full_command, 'error')).once()
  506. flexmock(module).should_receive('log_outputs').never()
  507. with pytest.raises(subprocess.CalledProcessError):
  508. module.execute_command_with_processes(full_command, processes)