2
0

test_execute.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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,
  244. stderr=None,
  245. shell=False,
  246. env={'a': 'b', 'c': 'd'},
  247. cwd=None,
  248. ).and_return(flexmock(decode=lambda: expected_output)).once()
  249. output = module.execute_command_and_capture_output(
  250. full_command, shell=False, extra_environment={'c': 'd'}
  251. )
  252. assert output == expected_output
  253. def test_execute_command_and_capture_output_returns_output_with_working_directory():
  254. full_command = ['foo', 'bar']
  255. expected_output = '[]'
  256. flexmock(module.os, environ={'a': 'b'})
  257. flexmock(module.subprocess).should_receive('check_output').with_args(
  258. full_command, stderr=None, shell=False, env=None, cwd='/working'
  259. ).and_return(flexmock(decode=lambda: expected_output)).once()
  260. output = module.execute_command_and_capture_output(
  261. full_command, shell=False, working_directory='/working'
  262. )
  263. assert output == expected_output
  264. def test_execute_command_with_processes_calls_full_command():
  265. full_command = ['foo', 'bar']
  266. processes = (flexmock(),)
  267. flexmock(module.os, environ={'a': 'b'})
  268. flexmock(module.subprocess).should_receive('Popen').with_args(
  269. full_command,
  270. stdin=None,
  271. stdout=module.subprocess.PIPE,
  272. stderr=module.subprocess.STDOUT,
  273. shell=False,
  274. env=None,
  275. cwd=None,
  276. ).and_return(flexmock(stdout=None)).once()
  277. flexmock(module).should_receive('log_outputs')
  278. output = module.execute_command_with_processes(full_command, processes)
  279. assert output is None
  280. def test_execute_command_with_processes_returns_output_with_output_log_level_none():
  281. full_command = ['foo', 'bar']
  282. processes = (flexmock(),)
  283. flexmock(module.os, environ={'a': 'b'})
  284. process = flexmock(stdout=None)
  285. flexmock(module.subprocess).should_receive('Popen').with_args(
  286. full_command,
  287. stdin=None,
  288. stdout=module.subprocess.PIPE,
  289. stderr=module.subprocess.STDOUT,
  290. shell=False,
  291. env=None,
  292. cwd=None,
  293. ).and_return(process).once()
  294. flexmock(module).should_receive('log_outputs').and_return({process: 'out'})
  295. output = module.execute_command_with_processes(full_command, processes, output_log_level=None)
  296. assert output == 'out'
  297. def test_execute_command_with_processes_calls_full_command_with_output_file():
  298. full_command = ['foo', 'bar']
  299. processes = (flexmock(),)
  300. output_file = flexmock(name='test')
  301. flexmock(module.os, environ={'a': 'b'})
  302. flexmock(module.subprocess).should_receive('Popen').with_args(
  303. full_command,
  304. stdin=None,
  305. stdout=output_file,
  306. stderr=module.subprocess.PIPE,
  307. shell=False,
  308. env=None,
  309. cwd=None,
  310. ).and_return(flexmock(stderr=None)).once()
  311. flexmock(module).should_receive('log_outputs')
  312. output = module.execute_command_with_processes(full_command, processes, output_file=output_file)
  313. assert output is None
  314. def test_execute_command_with_processes_calls_full_command_without_capturing_output():
  315. full_command = ['foo', 'bar']
  316. processes = (flexmock(),)
  317. flexmock(module.os, environ={'a': 'b'})
  318. flexmock(module.subprocess).should_receive('Popen').with_args(
  319. full_command, stdin=None, stdout=None, stderr=None, shell=False, env=None, cwd=None
  320. ).and_return(flexmock(wait=lambda: 0)).once()
  321. flexmock(module).should_receive('exit_code_indicates_error').and_return(False)
  322. flexmock(module).should_receive('log_outputs')
  323. output = module.execute_command_with_processes(
  324. full_command, processes, output_file=module.DO_NOT_CAPTURE
  325. )
  326. assert output is None
  327. def test_execute_command_with_processes_calls_full_command_with_input_file():
  328. full_command = ['foo', 'bar']
  329. processes = (flexmock(),)
  330. input_file = flexmock(name='test')
  331. flexmock(module.os, environ={'a': 'b'})
  332. flexmock(module.subprocess).should_receive('Popen').with_args(
  333. full_command,
  334. stdin=input_file,
  335. stdout=module.subprocess.PIPE,
  336. stderr=module.subprocess.STDOUT,
  337. shell=False,
  338. env=None,
  339. cwd=None,
  340. ).and_return(flexmock(stdout=None)).once()
  341. flexmock(module).should_receive('log_outputs')
  342. output = module.execute_command_with_processes(full_command, processes, input_file=input_file)
  343. assert output is None
  344. def test_execute_command_with_processes_calls_full_command_with_shell():
  345. full_command = ['foo', 'bar']
  346. processes = (flexmock(),)
  347. flexmock(module.os, environ={'a': 'b'})
  348. flexmock(module.subprocess).should_receive('Popen').with_args(
  349. ' '.join(full_command),
  350. stdin=None,
  351. stdout=module.subprocess.PIPE,
  352. stderr=module.subprocess.STDOUT,
  353. shell=True,
  354. env=None,
  355. cwd=None,
  356. ).and_return(flexmock(stdout=None)).once()
  357. flexmock(module).should_receive('log_outputs')
  358. output = module.execute_command_with_processes(full_command, processes, shell=True)
  359. assert output is None
  360. def test_execute_command_with_processes_calls_full_command_with_extra_environment():
  361. full_command = ['foo', 'bar']
  362. processes = (flexmock(),)
  363. flexmock(module.os, environ={'a': 'b'})
  364. flexmock(module.subprocess).should_receive('Popen').with_args(
  365. full_command,
  366. stdin=None,
  367. stdout=module.subprocess.PIPE,
  368. stderr=module.subprocess.STDOUT,
  369. shell=False,
  370. env={'a': 'b', 'c': 'd'},
  371. cwd=None,
  372. ).and_return(flexmock(stdout=None)).once()
  373. flexmock(module).should_receive('log_outputs')
  374. output = module.execute_command_with_processes(
  375. full_command, processes, extra_environment={'c': 'd'}
  376. )
  377. assert output is None
  378. def test_execute_command_with_processes_calls_full_command_with_working_directory():
  379. full_command = ['foo', 'bar']
  380. processes = (flexmock(),)
  381. flexmock(module.os, environ={'a': 'b'})
  382. flexmock(module.subprocess).should_receive('Popen').with_args(
  383. full_command,
  384. stdin=None,
  385. stdout=module.subprocess.PIPE,
  386. stderr=module.subprocess.STDOUT,
  387. shell=False,
  388. env=None,
  389. cwd='/working',
  390. ).and_return(flexmock(stdout=None)).once()
  391. flexmock(module).should_receive('log_outputs')
  392. output = module.execute_command_with_processes(
  393. full_command, processes, working_directory='/working'
  394. )
  395. assert output is None
  396. def test_execute_command_with_processes_kills_processes_on_error():
  397. full_command = ['foo', 'bar']
  398. process = flexmock(stdout=flexmock(read=lambda count: None))
  399. process.should_receive('poll')
  400. process.should_receive('kill').once()
  401. processes = (process,)
  402. flexmock(module.os, environ={'a': 'b'})
  403. flexmock(module.subprocess).should_receive('Popen').with_args(
  404. full_command,
  405. stdin=None,
  406. stdout=module.subprocess.PIPE,
  407. stderr=module.subprocess.STDOUT,
  408. shell=False,
  409. env=None,
  410. cwd=None,
  411. ).and_raise(subprocess.CalledProcessError(1, full_command, 'error')).once()
  412. flexmock(module).should_receive('log_outputs').never()
  413. with pytest.raises(subprocess.CalledProcessError):
  414. module.execute_command_with_processes(full_command, processes)