test_execute.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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.borgmatic.logger).should_receive('Log_prefix').and_return(flexmock())
  153. flexmock(module).should_receive('log_outputs')
  154. output = module.execute_command(full_command)
  155. assert output is None
  156. def test_execute_command_calls_full_command_with_output_file():
  157. full_command = ['foo', 'bar']
  158. output_file = flexmock(name='test')
  159. flexmock(module).should_receive('log_command')
  160. flexmock(module.os, environ={'a': 'b'})
  161. flexmock(module.subprocess).should_receive('Popen').with_args(
  162. full_command,
  163. stdin=None,
  164. stdout=output_file,
  165. stderr=module.subprocess.PIPE,
  166. shell=False,
  167. env=None,
  168. cwd=None,
  169. ).and_return(flexmock(stderr=None)).once()
  170. flexmock(module.borgmatic.logger).should_receive('Log_prefix').and_return(flexmock())
  171. flexmock(module).should_receive('log_outputs')
  172. output = module.execute_command(full_command, output_file=output_file)
  173. assert output is None
  174. def test_execute_command_calls_full_command_without_capturing_output():
  175. full_command = ['foo', 'bar']
  176. flexmock(module).should_receive('log_command')
  177. flexmock(module.os, environ={'a': 'b'})
  178. flexmock(module.subprocess).should_receive('Popen').with_args(
  179. full_command, stdin=None, stdout=None, stderr=None, shell=False, env=None, cwd=None
  180. ).and_return(flexmock(wait=lambda: 0)).once()
  181. flexmock(module).should_receive('interpret_exit_code').and_return(module.Exit_status.SUCCESS)
  182. flexmock(module.borgmatic.logger).should_receive('Log_prefix').and_return(flexmock())
  183. flexmock(module).should_receive('log_outputs')
  184. output = module.execute_command(full_command, output_file=module.DO_NOT_CAPTURE)
  185. assert output is None
  186. def test_execute_command_calls_full_command_with_input_file():
  187. full_command = ['foo', 'bar']
  188. input_file = flexmock(name='test')
  189. flexmock(module).should_receive('log_command')
  190. flexmock(module.os, environ={'a': 'b'})
  191. flexmock(module.subprocess).should_receive('Popen').with_args(
  192. full_command,
  193. stdin=input_file,
  194. stdout=module.subprocess.PIPE,
  195. stderr=module.subprocess.STDOUT,
  196. shell=False,
  197. env=None,
  198. cwd=None,
  199. ).and_return(flexmock(stdout=None)).once()
  200. flexmock(module.borgmatic.logger).should_receive('Log_prefix').and_return(flexmock())
  201. flexmock(module).should_receive('log_outputs')
  202. output = module.execute_command(full_command, input_file=input_file)
  203. assert output is None
  204. def test_execute_command_calls_full_command_with_shell():
  205. full_command = ['foo', 'bar']
  206. flexmock(module).should_receive('log_command')
  207. flexmock(module.os, environ={'a': 'b'})
  208. flexmock(module.subprocess).should_receive('Popen').with_args(
  209. ' '.join(full_command),
  210. stdin=None,
  211. stdout=module.subprocess.PIPE,
  212. stderr=module.subprocess.STDOUT,
  213. shell=True,
  214. env=None,
  215. cwd=None,
  216. ).and_return(flexmock(stdout=None)).once()
  217. flexmock(module.borgmatic.logger).should_receive('Log_prefix').and_return(flexmock())
  218. flexmock(module).should_receive('log_outputs')
  219. output = module.execute_command(full_command, shell=True)
  220. assert output is None
  221. def test_execute_command_calls_full_command_with_extra_environment():
  222. full_command = ['foo', 'bar']
  223. flexmock(module).should_receive('log_command')
  224. flexmock(module.os, environ={'a': 'b'})
  225. flexmock(module.subprocess).should_receive('Popen').with_args(
  226. full_command,
  227. stdin=None,
  228. stdout=module.subprocess.PIPE,
  229. stderr=module.subprocess.STDOUT,
  230. shell=False,
  231. env={'a': 'b', 'c': 'd'},
  232. cwd=None,
  233. ).and_return(flexmock(stdout=None)).once()
  234. flexmock(module.borgmatic.logger).should_receive('Log_prefix').and_return(flexmock())
  235. flexmock(module).should_receive('log_outputs')
  236. output = module.execute_command(full_command, extra_environment={'c': 'd'})
  237. assert output is None
  238. def test_execute_command_calls_full_command_with_working_directory():
  239. full_command = ['foo', 'bar']
  240. flexmock(module).should_receive('log_command')
  241. flexmock(module.os, environ={'a': 'b'})
  242. flexmock(module.subprocess).should_receive('Popen').with_args(
  243. full_command,
  244. stdin=None,
  245. stdout=module.subprocess.PIPE,
  246. stderr=module.subprocess.STDOUT,
  247. shell=False,
  248. env=None,
  249. cwd='/working',
  250. ).and_return(flexmock(stdout=None)).once()
  251. flexmock(module.borgmatic.logger).should_receive('Log_prefix').and_return(flexmock())
  252. flexmock(module).should_receive('log_outputs')
  253. output = module.execute_command(full_command, working_directory='/working')
  254. assert output is None
  255. def test_execute_command_without_run_to_completion_returns_process():
  256. full_command = ['foo', 'bar']
  257. process = flexmock()
  258. flexmock(module).should_receive('log_command')
  259. flexmock(module.os, environ={'a': 'b'})
  260. flexmock(module.subprocess).should_receive('Popen').with_args(
  261. full_command,
  262. stdin=None,
  263. stdout=module.subprocess.PIPE,
  264. stderr=module.subprocess.STDOUT,
  265. shell=False,
  266. env=None,
  267. cwd=None,
  268. ).and_return(process).once()
  269. flexmock(module.borgmatic.logger).should_receive('Log_prefix').and_return(flexmock())
  270. flexmock(module).should_receive('log_outputs')
  271. assert module.execute_command(full_command, run_to_completion=False) == process
  272. def test_execute_command_and_capture_output_returns_stdout():
  273. full_command = ['foo', 'bar']
  274. expected_output = '[]'
  275. flexmock(module).should_receive('log_command')
  276. flexmock(module.os, environ={'a': 'b'})
  277. flexmock(module.subprocess).should_receive('check_output').with_args(
  278. full_command, stderr=None, shell=False, env=None, cwd=None
  279. ).and_return(flexmock(decode=lambda: expected_output)).once()
  280. output = module.execute_command_and_capture_output(full_command)
  281. assert output == expected_output
  282. def test_execute_command_and_capture_output_with_capture_stderr_returns_stderr():
  283. full_command = ['foo', 'bar']
  284. expected_output = '[]'
  285. flexmock(module).should_receive('log_command')
  286. flexmock(module.os, environ={'a': 'b'})
  287. flexmock(module.subprocess).should_receive('check_output').with_args(
  288. full_command, stderr=module.subprocess.STDOUT, shell=False, env=None, cwd=None
  289. ).and_return(flexmock(decode=lambda: expected_output)).once()
  290. output = module.execute_command_and_capture_output(full_command, capture_stderr=True)
  291. assert output == expected_output
  292. def test_execute_command_and_capture_output_returns_output_when_process_error_is_not_considered_an_error():
  293. full_command = ['foo', 'bar']
  294. expected_output = '[]'
  295. err_output = b'[]'
  296. flexmock(module).should_receive('log_command')
  297. flexmock(module.os, environ={'a': 'b'})
  298. flexmock(module.subprocess).should_receive('check_output').with_args(
  299. full_command, stderr=None, shell=False, env=None, cwd=None
  300. ).and_raise(subprocess.CalledProcessError(1, full_command, err_output)).once()
  301. flexmock(module).should_receive('interpret_exit_code').and_return(
  302. module.Exit_status.SUCCESS
  303. ).once()
  304. output = module.execute_command_and_capture_output(full_command)
  305. assert output == expected_output
  306. def test_execute_command_and_capture_output_raises_when_command_errors():
  307. full_command = ['foo', 'bar']
  308. expected_output = '[]'
  309. flexmock(module).should_receive('log_command')
  310. flexmock(module.os, environ={'a': 'b'})
  311. flexmock(module.subprocess).should_receive('check_output').with_args(
  312. full_command, stderr=None, shell=False, env=None, cwd=None
  313. ).and_raise(subprocess.CalledProcessError(2, full_command, expected_output)).once()
  314. flexmock(module).should_receive('interpret_exit_code').and_return(
  315. module.Exit_status.ERROR
  316. ).once()
  317. with pytest.raises(subprocess.CalledProcessError):
  318. module.execute_command_and_capture_output(full_command)
  319. def test_execute_command_and_capture_output_returns_output_with_shell():
  320. full_command = ['foo', 'bar']
  321. expected_output = '[]'
  322. flexmock(module).should_receive('log_command')
  323. flexmock(module.os, environ={'a': 'b'})
  324. flexmock(module.subprocess).should_receive('check_output').with_args(
  325. 'foo bar', stderr=None, shell=True, env=None, cwd=None
  326. ).and_return(flexmock(decode=lambda: expected_output)).once()
  327. output = module.execute_command_and_capture_output(full_command, shell=True)
  328. assert output == expected_output
  329. def test_execute_command_and_capture_output_returns_output_with_extra_environment():
  330. full_command = ['foo', 'bar']
  331. expected_output = '[]'
  332. flexmock(module).should_receive('log_command')
  333. flexmock(module.os, environ={'a': 'b'})
  334. flexmock(module.subprocess).should_receive('check_output').with_args(
  335. full_command,
  336. stderr=None,
  337. shell=False,
  338. env={'a': 'b', 'c': 'd'},
  339. cwd=None,
  340. ).and_return(flexmock(decode=lambda: expected_output)).once()
  341. output = module.execute_command_and_capture_output(
  342. full_command, shell=False, extra_environment={'c': 'd'}
  343. )
  344. assert output == expected_output
  345. def test_execute_command_and_capture_output_returns_output_with_working_directory():
  346. full_command = ['foo', 'bar']
  347. expected_output = '[]'
  348. flexmock(module).should_receive('log_command')
  349. flexmock(module.os, environ={'a': 'b'})
  350. flexmock(module.subprocess).should_receive('check_output').with_args(
  351. full_command, stderr=None, shell=False, env=None, cwd='/working'
  352. ).and_return(flexmock(decode=lambda: expected_output)).once()
  353. output = module.execute_command_and_capture_output(
  354. full_command, shell=False, working_directory='/working'
  355. )
  356. assert output == expected_output
  357. def test_execute_command_with_processes_calls_full_command():
  358. full_command = ['foo', 'bar']
  359. processes = (flexmock(),)
  360. flexmock(module).should_receive('log_command')
  361. flexmock(module.os, environ={'a': 'b'})
  362. flexmock(module.subprocess).should_receive('Popen').with_args(
  363. full_command,
  364. stdin=None,
  365. stdout=module.subprocess.PIPE,
  366. stderr=module.subprocess.STDOUT,
  367. shell=False,
  368. env=None,
  369. cwd=None,
  370. ).and_return(flexmock(stdout=None)).once()
  371. flexmock(module).should_receive('log_outputs')
  372. output = module.execute_command_with_processes(full_command, processes)
  373. assert output is None
  374. def test_execute_command_with_processes_returns_output_with_output_log_level_none():
  375. full_command = ['foo', 'bar']
  376. processes = (flexmock(),)
  377. flexmock(module).should_receive('log_command')
  378. flexmock(module.os, environ={'a': 'b'})
  379. process = flexmock(stdout=None)
  380. flexmock(module.subprocess).should_receive('Popen').with_args(
  381. full_command,
  382. stdin=None,
  383. stdout=module.subprocess.PIPE,
  384. stderr=module.subprocess.STDOUT,
  385. shell=False,
  386. env=None,
  387. cwd=None,
  388. ).and_return(process).once()
  389. flexmock(module).should_receive('log_outputs').and_return({process: 'out'})
  390. output = module.execute_command_with_processes(full_command, processes, output_log_level=None)
  391. assert output == 'out'
  392. def test_execute_command_with_processes_calls_full_command_with_output_file():
  393. full_command = ['foo', 'bar']
  394. processes = (flexmock(),)
  395. output_file = flexmock(name='test')
  396. flexmock(module).should_receive('log_command')
  397. flexmock(module.os, environ={'a': 'b'})
  398. flexmock(module.subprocess).should_receive('Popen').with_args(
  399. full_command,
  400. stdin=None,
  401. stdout=output_file,
  402. stderr=module.subprocess.PIPE,
  403. shell=False,
  404. env=None,
  405. cwd=None,
  406. ).and_return(flexmock(stderr=None)).once()
  407. flexmock(module).should_receive('log_outputs')
  408. output = module.execute_command_with_processes(full_command, processes, output_file=output_file)
  409. assert output is None
  410. def test_execute_command_with_processes_calls_full_command_without_capturing_output():
  411. full_command = ['foo', 'bar']
  412. processes = (flexmock(),)
  413. flexmock(module).should_receive('log_command')
  414. flexmock(module.os, environ={'a': 'b'})
  415. flexmock(module.subprocess).should_receive('Popen').with_args(
  416. full_command, stdin=None, stdout=None, stderr=None, shell=False, env=None, cwd=None
  417. ).and_return(flexmock(wait=lambda: 0)).once()
  418. flexmock(module).should_receive('interpret_exit_code').and_return(module.Exit_status.SUCCESS)
  419. flexmock(module).should_receive('log_outputs')
  420. output = module.execute_command_with_processes(
  421. full_command, processes, output_file=module.DO_NOT_CAPTURE
  422. )
  423. assert output is None
  424. def test_execute_command_with_processes_calls_full_command_with_input_file():
  425. full_command = ['foo', 'bar']
  426. processes = (flexmock(),)
  427. input_file = flexmock(name='test')
  428. flexmock(module).should_receive('log_command')
  429. flexmock(module.os, environ={'a': 'b'})
  430. flexmock(module.subprocess).should_receive('Popen').with_args(
  431. full_command,
  432. stdin=input_file,
  433. stdout=module.subprocess.PIPE,
  434. stderr=module.subprocess.STDOUT,
  435. shell=False,
  436. env=None,
  437. cwd=None,
  438. ).and_return(flexmock(stdout=None)).once()
  439. flexmock(module).should_receive('log_outputs')
  440. output = module.execute_command_with_processes(full_command, processes, input_file=input_file)
  441. assert output is None
  442. def test_execute_command_with_processes_calls_full_command_with_shell():
  443. full_command = ['foo', 'bar']
  444. processes = (flexmock(),)
  445. flexmock(module).should_receive('log_command')
  446. flexmock(module.os, environ={'a': 'b'})
  447. flexmock(module.subprocess).should_receive('Popen').with_args(
  448. ' '.join(full_command),
  449. stdin=None,
  450. stdout=module.subprocess.PIPE,
  451. stderr=module.subprocess.STDOUT,
  452. shell=True,
  453. env=None,
  454. cwd=None,
  455. ).and_return(flexmock(stdout=None)).once()
  456. flexmock(module).should_receive('log_outputs')
  457. output = module.execute_command_with_processes(full_command, processes, shell=True)
  458. assert output is None
  459. def test_execute_command_with_processes_calls_full_command_with_extra_environment():
  460. full_command = ['foo', 'bar']
  461. processes = (flexmock(),)
  462. flexmock(module).should_receive('log_command')
  463. flexmock(module.os, environ={'a': 'b'})
  464. flexmock(module.subprocess).should_receive('Popen').with_args(
  465. full_command,
  466. stdin=None,
  467. stdout=module.subprocess.PIPE,
  468. stderr=module.subprocess.STDOUT,
  469. shell=False,
  470. env={'a': 'b', 'c': 'd'},
  471. cwd=None,
  472. ).and_return(flexmock(stdout=None)).once()
  473. flexmock(module).should_receive('log_outputs')
  474. output = module.execute_command_with_processes(
  475. full_command, processes, extra_environment={'c': 'd'}
  476. )
  477. assert output is None
  478. def test_execute_command_with_processes_calls_full_command_with_working_directory():
  479. full_command = ['foo', 'bar']
  480. processes = (flexmock(),)
  481. flexmock(module).should_receive('log_command')
  482. flexmock(module.os, environ={'a': 'b'})
  483. flexmock(module.subprocess).should_receive('Popen').with_args(
  484. full_command,
  485. stdin=None,
  486. stdout=module.subprocess.PIPE,
  487. stderr=module.subprocess.STDOUT,
  488. shell=False,
  489. env=None,
  490. cwd='/working',
  491. ).and_return(flexmock(stdout=None)).once()
  492. flexmock(module).should_receive('log_outputs')
  493. output = module.execute_command_with_processes(
  494. full_command, processes, working_directory='/working'
  495. )
  496. assert output is None
  497. def test_execute_command_with_processes_kills_processes_on_error():
  498. full_command = ['foo', 'bar']
  499. flexmock(module).should_receive('log_command')
  500. process = flexmock(stdout=flexmock(read=lambda count: None))
  501. process.should_receive('poll')
  502. process.should_receive('kill').once()
  503. processes = (process,)
  504. flexmock(module.os, environ={'a': 'b'})
  505. flexmock(module.subprocess).should_receive('Popen').with_args(
  506. full_command,
  507. stdin=None,
  508. stdout=module.subprocess.PIPE,
  509. stderr=module.subprocess.STDOUT,
  510. shell=False,
  511. env=None,
  512. cwd=None,
  513. ).and_raise(subprocess.CalledProcessError(1, full_command, 'error')).once()
  514. flexmock(module).should_receive('log_outputs').never()
  515. with pytest.raises(subprocess.CalledProcessError):
  516. module.execute_command_with_processes(full_command, processes)