test_execute.py 24 KB

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