test_execute.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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,expected_result',
  7. (
  8. (['grep'], 2, None, True),
  9. (['grep'], 2, 'borg', True),
  10. (['borg'], 2, 'borg', True),
  11. (['borg1'], 2, 'borg1', True),
  12. (['grep'], 1, None, True),
  13. (['grep'], 1, 'borg', True),
  14. (['borg'], 1, 'borg', False),
  15. (['borg1'], 1, 'borg1', False),
  16. (['grep'], 0, None, False),
  17. (['grep'], 0, 'borg', False),
  18. (['borg'], 0, 'borg', False),
  19. (['borg1'], 0, 'borg1', False),
  20. # -9 exit code occurs when child process get SIGKILLed.
  21. (['grep'], -9, None, True),
  22. (['grep'], -9, 'borg', True),
  23. (['borg'], -9, 'borg', True),
  24. (['borg1'], -9, 'borg1', True),
  25. (['borg'], None, None, False),
  26. ),
  27. )
  28. def test_exit_code_indicates_error_respects_exit_code_and_borg_local_path(
  29. command, exit_code, borg_local_path, expected_result
  30. ):
  31. assert module.exit_code_indicates_error(command, exit_code, borg_local_path) is expected_result
  32. def test_command_for_process_converts_sequence_command_to_string():
  33. process = flexmock(args=['foo', 'bar', 'baz'])
  34. assert module.command_for_process(process) == 'foo bar baz'
  35. def test_command_for_process_passes_through_string_command():
  36. process = flexmock(args='foo bar baz')
  37. assert module.command_for_process(process) == 'foo bar baz'
  38. def test_output_buffer_for_process_returns_stderr_when_stdout_excluded():
  39. stdout = flexmock()
  40. stderr = flexmock()
  41. process = flexmock(stdout=stdout, stderr=stderr)
  42. assert module.output_buffer_for_process(process, exclude_stdouts=[flexmock(), stdout]) == stderr
  43. def test_output_buffer_for_process_returns_stdout_when_not_excluded():
  44. stdout = flexmock()
  45. process = flexmock(stdout=stdout)
  46. assert (
  47. module.output_buffer_for_process(process, exclude_stdouts=[flexmock(), flexmock()])
  48. == stdout
  49. )
  50. def test_append_last_lines_under_max_line_count_appends():
  51. last_lines = ['last']
  52. flexmock(module.logger).should_receive('log').once()
  53. module.append_last_lines(
  54. last_lines, captured_output=flexmock(), line='line', output_log_level=flexmock()
  55. )
  56. assert last_lines == ['last', 'line']
  57. def test_append_last_lines_over_max_line_count_trims_and_appends():
  58. original_last_lines = [str(number) for number in range(0, module.ERROR_OUTPUT_MAX_LINE_COUNT)]
  59. last_lines = list(original_last_lines)
  60. flexmock(module.logger).should_receive('log').once()
  61. module.append_last_lines(
  62. last_lines, captured_output=flexmock(), line='line', output_log_level=flexmock()
  63. )
  64. assert last_lines == original_last_lines[1:] + ['line']
  65. def test_append_last_lines_with_output_log_level_none_appends_captured_output():
  66. last_lines = ['last']
  67. captured_output = ['captured']
  68. flexmock(module.logger).should_receive('log').never()
  69. module.append_last_lines(
  70. last_lines, captured_output=captured_output, line='line', output_log_level=None
  71. )
  72. assert captured_output == ['captured', 'line']
  73. def test_execute_command_calls_full_command():
  74. full_command = ['foo', 'bar']
  75. flexmock(module.os, environ={'a': 'b'})
  76. flexmock(module.subprocess).should_receive('Popen').with_args(
  77. full_command,
  78. stdin=None,
  79. stdout=module.subprocess.PIPE,
  80. stderr=module.subprocess.STDOUT,
  81. shell=False,
  82. env=None,
  83. cwd=None,
  84. ).and_return(flexmock(stdout=None)).once()
  85. flexmock(module).should_receive('log_outputs')
  86. output = module.execute_command(full_command)
  87. assert output is None
  88. def test_execute_command_calls_full_command_with_output_file():
  89. full_command = ['foo', 'bar']
  90. output_file = flexmock(name='test')
  91. flexmock(module.os, environ={'a': 'b'})
  92. flexmock(module.subprocess).should_receive('Popen').with_args(
  93. full_command,
  94. stdin=None,
  95. stdout=output_file,
  96. stderr=module.subprocess.PIPE,
  97. shell=False,
  98. env=None,
  99. cwd=None,
  100. ).and_return(flexmock(stderr=None)).once()
  101. flexmock(module).should_receive('log_outputs')
  102. output = module.execute_command(full_command, output_file=output_file)
  103. assert output is None
  104. def test_execute_command_calls_full_command_without_capturing_output():
  105. full_command = ['foo', 'bar']
  106. flexmock(module.os, environ={'a': 'b'})
  107. flexmock(module.subprocess).should_receive('Popen').with_args(
  108. full_command, stdin=None, stdout=None, stderr=None, shell=False, env=None, cwd=None
  109. ).and_return(flexmock(wait=lambda: 0)).once()
  110. flexmock(module).should_receive('exit_code_indicates_error').and_return(False)
  111. flexmock(module).should_receive('log_outputs')
  112. output = module.execute_command(full_command, output_file=module.DO_NOT_CAPTURE)
  113. assert output is None
  114. def test_execute_command_calls_full_command_with_input_file():
  115. full_command = ['foo', 'bar']
  116. input_file = flexmock(name='test')
  117. flexmock(module.os, environ={'a': 'b'})
  118. flexmock(module.subprocess).should_receive('Popen').with_args(
  119. full_command,
  120. stdin=input_file,
  121. stdout=module.subprocess.PIPE,
  122. stderr=module.subprocess.STDOUT,
  123. shell=False,
  124. env=None,
  125. cwd=None,
  126. ).and_return(flexmock(stdout=None)).once()
  127. flexmock(module).should_receive('log_outputs')
  128. output = module.execute_command(full_command, input_file=input_file)
  129. assert output is None
  130. def test_execute_command_calls_full_command_with_shell():
  131. full_command = ['foo', 'bar']
  132. flexmock(module.os, environ={'a': 'b'})
  133. flexmock(module.subprocess).should_receive('Popen').with_args(
  134. ' '.join(full_command),
  135. stdin=None,
  136. stdout=module.subprocess.PIPE,
  137. stderr=module.subprocess.STDOUT,
  138. shell=True,
  139. env=None,
  140. cwd=None,
  141. ).and_return(flexmock(stdout=None)).once()
  142. flexmock(module).should_receive('log_outputs')
  143. output = module.execute_command(full_command, shell=True)
  144. assert output is None
  145. def test_execute_command_calls_full_command_with_extra_environment():
  146. full_command = ['foo', 'bar']
  147. flexmock(module.os, environ={'a': 'b'})
  148. flexmock(module.subprocess).should_receive('Popen').with_args(
  149. full_command,
  150. stdin=None,
  151. stdout=module.subprocess.PIPE,
  152. stderr=module.subprocess.STDOUT,
  153. shell=False,
  154. env={'a': 'b', 'c': 'd'},
  155. cwd=None,
  156. ).and_return(flexmock(stdout=None)).once()
  157. flexmock(module).should_receive('log_outputs')
  158. output = module.execute_command(full_command, extra_environment={'c': 'd'})
  159. assert output is None
  160. def test_execute_command_calls_full_command_with_working_directory():
  161. full_command = ['foo', 'bar']
  162. flexmock(module.os, environ={'a': 'b'})
  163. flexmock(module.subprocess).should_receive('Popen').with_args(
  164. full_command,
  165. stdin=None,
  166. stdout=module.subprocess.PIPE,
  167. stderr=module.subprocess.STDOUT,
  168. shell=False,
  169. env=None,
  170. cwd='/working',
  171. ).and_return(flexmock(stdout=None)).once()
  172. flexmock(module).should_receive('log_outputs')
  173. output = module.execute_command(full_command, working_directory='/working')
  174. assert output is None
  175. def test_execute_command_without_run_to_completion_returns_process():
  176. full_command = ['foo', 'bar']
  177. process = flexmock()
  178. flexmock(module.os, environ={'a': 'b'})
  179. flexmock(module.subprocess).should_receive('Popen').with_args(
  180. full_command,
  181. stdin=None,
  182. stdout=module.subprocess.PIPE,
  183. stderr=module.subprocess.STDOUT,
  184. shell=False,
  185. env=None,
  186. cwd=None,
  187. ).and_return(process).once()
  188. flexmock(module).should_receive('log_outputs')
  189. assert module.execute_command(full_command, run_to_completion=False) == process
  190. def test_execute_command_and_capture_output_returns_stdout():
  191. full_command = ['foo', 'bar']
  192. expected_output = '[]'
  193. flexmock(module.os, environ={'a': 'b'})
  194. flexmock(module.subprocess).should_receive('check_output').with_args(
  195. full_command, stderr=None, shell=False, env=None, cwd=None
  196. ).and_return(flexmock(decode=lambda: expected_output)).once()
  197. output = module.execute_command_and_capture_output(full_command)
  198. assert output == expected_output
  199. def test_execute_command_and_capture_output_with_capture_stderr_returns_stderr():
  200. full_command = ['foo', 'bar']
  201. expected_output = '[]'
  202. flexmock(module.os, environ={'a': 'b'})
  203. flexmock(module.subprocess).should_receive('check_output').with_args(
  204. full_command, stderr=module.subprocess.STDOUT, shell=False, env=None, cwd=None
  205. ).and_return(flexmock(decode=lambda: expected_output)).once()
  206. output = module.execute_command_and_capture_output(full_command, capture_stderr=True)
  207. assert output == expected_output
  208. def test_execute_command_and_capture_output_returns_output_when_process_error_is_not_considered_an_error():
  209. full_command = ['foo', 'bar']
  210. expected_output = '[]'
  211. err_output = b'[]'
  212. flexmock(module.os, environ={'a': 'b'})
  213. flexmock(module.subprocess).should_receive('check_output').with_args(
  214. full_command, stderr=None, shell=False, env=None, cwd=None
  215. ).and_raise(subprocess.CalledProcessError(1, full_command, err_output)).once()
  216. flexmock(module).should_receive('exit_code_indicates_error').and_return(False).once()
  217. output = module.execute_command_and_capture_output(full_command)
  218. assert output == expected_output
  219. def test_execute_command_and_capture_output_raises_when_command_errors():
  220. full_command = ['foo', 'bar']
  221. expected_output = '[]'
  222. flexmock(module.os, environ={'a': 'b'})
  223. flexmock(module.subprocess).should_receive('check_output').with_args(
  224. full_command, stderr=None, shell=False, env=None, cwd=None
  225. ).and_raise(subprocess.CalledProcessError(2, full_command, expected_output)).once()
  226. flexmock(module).should_receive('exit_code_indicates_error').and_return(True).once()
  227. with pytest.raises(subprocess.CalledProcessError):
  228. module.execute_command_and_capture_output(full_command)
  229. def test_execute_command_and_capture_output_returns_output_with_shell():
  230. full_command = ['foo', 'bar']
  231. expected_output = '[]'
  232. flexmock(module.os, environ={'a': 'b'})
  233. flexmock(module.subprocess).should_receive('check_output').with_args(
  234. 'foo bar', stderr=None, shell=True, env=None, cwd=None
  235. ).and_return(flexmock(decode=lambda: expected_output)).once()
  236. output = module.execute_command_and_capture_output(full_command, shell=True)
  237. assert output == expected_output
  238. def test_execute_command_and_capture_output_returns_output_with_extra_environment():
  239. full_command = ['foo', 'bar']
  240. expected_output = '[]'
  241. flexmock(module.os, environ={'a': 'b'})
  242. flexmock(module.subprocess).should_receive('check_output').with_args(
  243. full_command, stderr=None, shell=False, env={'a': 'b', 'c': 'd'}, cwd=None,
  244. ).and_return(flexmock(decode=lambda: expected_output)).once()
  245. output = module.execute_command_and_capture_output(
  246. full_command, shell=False, extra_environment={'c': 'd'}
  247. )
  248. assert output == expected_output
  249. def test_execute_command_and_capture_output_returns_output_with_working_directory():
  250. full_command = ['foo', 'bar']
  251. expected_output = '[]'
  252. flexmock(module.os, environ={'a': 'b'})
  253. flexmock(module.subprocess).should_receive('check_output').with_args(
  254. full_command, stderr=None, shell=False, env=None, cwd='/working'
  255. ).and_return(flexmock(decode=lambda: expected_output)).once()
  256. output = module.execute_command_and_capture_output(
  257. full_command, shell=False, working_directory='/working'
  258. )
  259. assert output == expected_output
  260. def test_execute_command_with_processes_calls_full_command():
  261. full_command = ['foo', 'bar']
  262. processes = (flexmock(),)
  263. flexmock(module.os, environ={'a': 'b'})
  264. flexmock(module.subprocess).should_receive('Popen').with_args(
  265. full_command,
  266. stdin=None,
  267. stdout=module.subprocess.PIPE,
  268. stderr=module.subprocess.STDOUT,
  269. shell=False,
  270. env=None,
  271. cwd=None,
  272. ).and_return(flexmock(stdout=None)).once()
  273. flexmock(module).should_receive('log_outputs')
  274. output = module.execute_command_with_processes(full_command, processes)
  275. assert output is None
  276. def test_execute_command_with_processes_returns_output_with_output_log_level_none():
  277. full_command = ['foo', 'bar']
  278. processes = (flexmock(),)
  279. flexmock(module.os, environ={'a': 'b'})
  280. process = flexmock(stdout=None)
  281. flexmock(module.subprocess).should_receive('Popen').with_args(
  282. full_command,
  283. stdin=None,
  284. stdout=module.subprocess.PIPE,
  285. stderr=module.subprocess.STDOUT,
  286. shell=False,
  287. env=None,
  288. cwd=None,
  289. ).and_return(process).once()
  290. flexmock(module).should_receive('log_outputs').and_return({process: 'out'})
  291. output = module.execute_command_with_processes(full_command, processes, output_log_level=None)
  292. assert output == 'out'
  293. def test_execute_command_with_processes_calls_full_command_with_output_file():
  294. full_command = ['foo', 'bar']
  295. processes = (flexmock(),)
  296. output_file = flexmock(name='test')
  297. flexmock(module.os, environ={'a': 'b'})
  298. flexmock(module.subprocess).should_receive('Popen').with_args(
  299. full_command,
  300. stdin=None,
  301. stdout=output_file,
  302. stderr=module.subprocess.PIPE,
  303. shell=False,
  304. env=None,
  305. cwd=None,
  306. ).and_return(flexmock(stderr=None)).once()
  307. flexmock(module).should_receive('log_outputs')
  308. output = module.execute_command_with_processes(full_command, processes, output_file=output_file)
  309. assert output is None
  310. def test_execute_command_with_processes_calls_full_command_without_capturing_output():
  311. full_command = ['foo', 'bar']
  312. processes = (flexmock(),)
  313. flexmock(module.os, environ={'a': 'b'})
  314. flexmock(module.subprocess).should_receive('Popen').with_args(
  315. full_command, stdin=None, stdout=None, stderr=None, shell=False, env=None, cwd=None
  316. ).and_return(flexmock(wait=lambda: 0)).once()
  317. flexmock(module).should_receive('exit_code_indicates_error').and_return(False)
  318. flexmock(module).should_receive('log_outputs')
  319. output = module.execute_command_with_processes(
  320. full_command, processes, output_file=module.DO_NOT_CAPTURE
  321. )
  322. assert output is None
  323. def test_execute_command_with_processes_calls_full_command_with_input_file():
  324. full_command = ['foo', 'bar']
  325. processes = (flexmock(),)
  326. input_file = flexmock(name='test')
  327. flexmock(module.os, environ={'a': 'b'})
  328. flexmock(module.subprocess).should_receive('Popen').with_args(
  329. full_command,
  330. stdin=input_file,
  331. stdout=module.subprocess.PIPE,
  332. stderr=module.subprocess.STDOUT,
  333. shell=False,
  334. env=None,
  335. cwd=None,
  336. ).and_return(flexmock(stdout=None)).once()
  337. flexmock(module).should_receive('log_outputs')
  338. output = module.execute_command_with_processes(full_command, processes, input_file=input_file)
  339. assert output is None
  340. def test_execute_command_with_processes_calls_full_command_with_shell():
  341. full_command = ['foo', 'bar']
  342. processes = (flexmock(),)
  343. flexmock(module.os, environ={'a': 'b'})
  344. flexmock(module.subprocess).should_receive('Popen').with_args(
  345. ' '.join(full_command),
  346. stdin=None,
  347. stdout=module.subprocess.PIPE,
  348. stderr=module.subprocess.STDOUT,
  349. shell=True,
  350. env=None,
  351. cwd=None,
  352. ).and_return(flexmock(stdout=None)).once()
  353. flexmock(module).should_receive('log_outputs')
  354. output = module.execute_command_with_processes(full_command, processes, shell=True)
  355. assert output is None
  356. def test_execute_command_with_processes_calls_full_command_with_extra_environment():
  357. full_command = ['foo', 'bar']
  358. processes = (flexmock(),)
  359. flexmock(module.os, environ={'a': 'b'})
  360. flexmock(module.subprocess).should_receive('Popen').with_args(
  361. full_command,
  362. stdin=None,
  363. stdout=module.subprocess.PIPE,
  364. stderr=module.subprocess.STDOUT,
  365. shell=False,
  366. env={'a': 'b', 'c': 'd'},
  367. cwd=None,
  368. ).and_return(flexmock(stdout=None)).once()
  369. flexmock(module).should_receive('log_outputs')
  370. output = module.execute_command_with_processes(
  371. full_command, processes, extra_environment={'c': 'd'}
  372. )
  373. assert output is None
  374. def test_execute_command_with_processes_calls_full_command_with_working_directory():
  375. full_command = ['foo', 'bar']
  376. processes = (flexmock(),)
  377. flexmock(module.os, environ={'a': 'b'})
  378. flexmock(module.subprocess).should_receive('Popen').with_args(
  379. full_command,
  380. stdin=None,
  381. stdout=module.subprocess.PIPE,
  382. stderr=module.subprocess.STDOUT,
  383. shell=False,
  384. env=None,
  385. cwd='/working',
  386. ).and_return(flexmock(stdout=None)).once()
  387. flexmock(module).should_receive('log_outputs')
  388. output = module.execute_command_with_processes(
  389. full_command, processes, working_directory='/working'
  390. )
  391. assert output is None
  392. def test_execute_command_with_processes_kills_processes_on_error():
  393. full_command = ['foo', 'bar']
  394. process = flexmock(stdout=flexmock(read=lambda count: None))
  395. process.should_receive('poll')
  396. process.should_receive('kill').once()
  397. processes = (process,)
  398. flexmock(module.os, environ={'a': 'b'})
  399. flexmock(module.subprocess).should_receive('Popen').with_args(
  400. full_command,
  401. stdin=None,
  402. stdout=module.subprocess.PIPE,
  403. stderr=module.subprocess.STDOUT,
  404. shell=False,
  405. env=None,
  406. cwd=None,
  407. ).and_raise(subprocess.CalledProcessError(1, full_command, 'error')).once()
  408. flexmock(module).should_receive('log_outputs').never()
  409. with pytest.raises(subprocess.CalledProcessError):
  410. module.execute_command_with_processes(full_command, processes)