test_execute.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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_execute_command_calls_full_command():
  51. full_command = ['foo', 'bar']
  52. flexmock(module.os, environ={'a': 'b'})
  53. flexmock(module.subprocess).should_receive('Popen').with_args(
  54. full_command,
  55. stdin=None,
  56. stdout=module.subprocess.PIPE,
  57. stderr=module.subprocess.STDOUT,
  58. shell=False,
  59. env=None,
  60. cwd=None,
  61. ).and_return(flexmock(stdout=None)).once()
  62. flexmock(module).should_receive('log_outputs')
  63. output = module.execute_command(full_command)
  64. assert output is None
  65. def test_execute_command_calls_full_command_with_output_file():
  66. full_command = ['foo', 'bar']
  67. output_file = flexmock(name='test')
  68. flexmock(module.os, environ={'a': 'b'})
  69. flexmock(module.subprocess).should_receive('Popen').with_args(
  70. full_command,
  71. stdin=None,
  72. stdout=output_file,
  73. stderr=module.subprocess.PIPE,
  74. shell=False,
  75. env=None,
  76. cwd=None,
  77. ).and_return(flexmock(stderr=None)).once()
  78. flexmock(module).should_receive('log_outputs')
  79. output = module.execute_command(full_command, output_file=output_file)
  80. assert output is None
  81. def test_execute_command_calls_full_command_without_capturing_output():
  82. full_command = ['foo', 'bar']
  83. flexmock(module.os, environ={'a': 'b'})
  84. flexmock(module.subprocess).should_receive('Popen').with_args(
  85. full_command, stdin=None, stdout=None, stderr=None, shell=False, env=None, cwd=None
  86. ).and_return(flexmock(wait=lambda: 0)).once()
  87. flexmock(module).should_receive('exit_code_indicates_error').and_return(False)
  88. flexmock(module).should_receive('log_outputs')
  89. output = module.execute_command(full_command, output_file=module.DO_NOT_CAPTURE)
  90. assert output is None
  91. def test_execute_command_calls_full_command_with_input_file():
  92. full_command = ['foo', 'bar']
  93. input_file = flexmock(name='test')
  94. flexmock(module.os, environ={'a': 'b'})
  95. flexmock(module.subprocess).should_receive('Popen').with_args(
  96. full_command,
  97. stdin=input_file,
  98. stdout=module.subprocess.PIPE,
  99. stderr=module.subprocess.STDOUT,
  100. shell=False,
  101. env=None,
  102. cwd=None,
  103. ).and_return(flexmock(stdout=None)).once()
  104. flexmock(module).should_receive('log_outputs')
  105. output = module.execute_command(full_command, input_file=input_file)
  106. assert output is None
  107. def test_execute_command_calls_full_command_with_shell():
  108. full_command = ['foo', 'bar']
  109. flexmock(module.os, environ={'a': 'b'})
  110. flexmock(module.subprocess).should_receive('Popen').with_args(
  111. ' '.join(full_command),
  112. stdin=None,
  113. stdout=module.subprocess.PIPE,
  114. stderr=module.subprocess.STDOUT,
  115. shell=True,
  116. env=None,
  117. cwd=None,
  118. ).and_return(flexmock(stdout=None)).once()
  119. flexmock(module).should_receive('log_outputs')
  120. output = module.execute_command(full_command, shell=True)
  121. assert output is None
  122. def test_execute_command_calls_full_command_with_extra_environment():
  123. full_command = ['foo', 'bar']
  124. flexmock(module.os, environ={'a': 'b'})
  125. flexmock(module.subprocess).should_receive('Popen').with_args(
  126. full_command,
  127. stdin=None,
  128. stdout=module.subprocess.PIPE,
  129. stderr=module.subprocess.STDOUT,
  130. shell=False,
  131. env={'a': 'b', 'c': 'd'},
  132. cwd=None,
  133. ).and_return(flexmock(stdout=None)).once()
  134. flexmock(module).should_receive('log_outputs')
  135. output = module.execute_command(full_command, extra_environment={'c': 'd'})
  136. assert output is None
  137. def test_execute_command_calls_full_command_with_working_directory():
  138. full_command = ['foo', 'bar']
  139. flexmock(module.os, environ={'a': 'b'})
  140. flexmock(module.subprocess).should_receive('Popen').with_args(
  141. full_command,
  142. stdin=None,
  143. stdout=module.subprocess.PIPE,
  144. stderr=module.subprocess.STDOUT,
  145. shell=False,
  146. env=None,
  147. cwd='/working',
  148. ).and_return(flexmock(stdout=None)).once()
  149. flexmock(module).should_receive('log_outputs')
  150. output = module.execute_command(full_command, working_directory='/working')
  151. assert output is None
  152. def test_execute_command_without_run_to_completion_returns_process():
  153. full_command = ['foo', 'bar']
  154. process = flexmock()
  155. flexmock(module.os, environ={'a': 'b'})
  156. flexmock(module.subprocess).should_receive('Popen').with_args(
  157. full_command,
  158. stdin=None,
  159. stdout=module.subprocess.PIPE,
  160. stderr=module.subprocess.STDOUT,
  161. shell=False,
  162. env=None,
  163. cwd=None,
  164. ).and_return(process).once()
  165. flexmock(module).should_receive('log_outputs')
  166. assert module.execute_command(full_command, run_to_completion=False) == process
  167. def test_execute_command_and_capture_output_returns_stdout():
  168. full_command = ['foo', 'bar']
  169. expected_output = '[]'
  170. flexmock(module.os, environ={'a': 'b'})
  171. flexmock(module.subprocess).should_receive('check_output').with_args(
  172. full_command, stderr=None, shell=False, env=None, cwd=None
  173. ).and_return(flexmock(decode=lambda: expected_output)).once()
  174. output = module.execute_command_and_capture_output(full_command)
  175. assert output == expected_output
  176. def test_execute_command_and_capture_output_with_capture_stderr_returns_stderr():
  177. full_command = ['foo', 'bar']
  178. expected_output = '[]'
  179. flexmock(module.os, environ={'a': 'b'})
  180. flexmock(module.subprocess).should_receive('check_output').with_args(
  181. full_command, stderr=module.subprocess.STDOUT, shell=False, env=None, cwd=None
  182. ).and_return(flexmock(decode=lambda: expected_output)).once()
  183. output = module.execute_command_and_capture_output(full_command, capture_stderr=True)
  184. assert output == expected_output
  185. def test_execute_command_and_capture_output_returns_output_when_process_error_is_not_considered_an_error():
  186. full_command = ['foo', 'bar']
  187. expected_output = '[]'
  188. err_output = b'[]'
  189. flexmock(module.os, environ={'a': 'b'})
  190. flexmock(module.subprocess).should_receive('check_output').with_args(
  191. full_command, stderr=None, shell=False, env=None, cwd=None
  192. ).and_raise(subprocess.CalledProcessError(1, full_command, err_output)).once()
  193. flexmock(module).should_receive('exit_code_indicates_error').and_return(False).once()
  194. output = module.execute_command_and_capture_output(full_command)
  195. assert output == expected_output
  196. def test_execute_command_and_capture_output_raises_when_command_errors():
  197. full_command = ['foo', 'bar']
  198. expected_output = '[]'
  199. flexmock(module.os, environ={'a': 'b'})
  200. flexmock(module.subprocess).should_receive('check_output').with_args(
  201. full_command, stderr=None, shell=False, env=None, cwd=None
  202. ).and_raise(subprocess.CalledProcessError(2, full_command, expected_output)).once()
  203. flexmock(module).should_receive('exit_code_indicates_error').and_return(True).once()
  204. with pytest.raises(subprocess.CalledProcessError):
  205. module.execute_command_and_capture_output(full_command)
  206. def test_execute_command_and_capture_output_returns_output_with_shell():
  207. full_command = ['foo', 'bar']
  208. expected_output = '[]'
  209. flexmock(module.os, environ={'a': 'b'})
  210. flexmock(module.subprocess).should_receive('check_output').with_args(
  211. 'foo bar', stderr=None, shell=True, env=None, cwd=None
  212. ).and_return(flexmock(decode=lambda: expected_output)).once()
  213. output = module.execute_command_and_capture_output(full_command, shell=True)
  214. assert output == expected_output
  215. def test_execute_command_and_capture_output_returns_output_with_extra_environment():
  216. full_command = ['foo', 'bar']
  217. expected_output = '[]'
  218. flexmock(module.os, environ={'a': 'b'})
  219. flexmock(module.subprocess).should_receive('check_output').with_args(
  220. full_command, stderr=None, shell=False, env={'a': 'b', 'c': 'd'}, cwd=None,
  221. ).and_return(flexmock(decode=lambda: expected_output)).once()
  222. output = module.execute_command_and_capture_output(
  223. full_command, shell=False, extra_environment={'c': 'd'}
  224. )
  225. assert output == expected_output
  226. def test_execute_command_and_capture_output_returns_output_with_working_directory():
  227. full_command = ['foo', 'bar']
  228. expected_output = '[]'
  229. flexmock(module.os, environ={'a': 'b'})
  230. flexmock(module.subprocess).should_receive('check_output').with_args(
  231. full_command, stderr=None, shell=False, env=None, cwd='/working'
  232. ).and_return(flexmock(decode=lambda: expected_output)).once()
  233. output = module.execute_command_and_capture_output(
  234. full_command, shell=False, working_directory='/working'
  235. )
  236. assert output == expected_output
  237. def test_execute_command_with_processes_calls_full_command():
  238. full_command = ['foo', 'bar']
  239. processes = (flexmock(),)
  240. flexmock(module.os, environ={'a': 'b'})
  241. flexmock(module.subprocess).should_receive('Popen').with_args(
  242. full_command,
  243. stdin=None,
  244. stdout=module.subprocess.PIPE,
  245. stderr=module.subprocess.STDOUT,
  246. shell=False,
  247. env=None,
  248. cwd=None,
  249. ).and_return(flexmock(stdout=None)).once()
  250. flexmock(module).should_receive('log_outputs')
  251. output = module.execute_command_with_processes(full_command, processes)
  252. assert output is None
  253. def test_execute_command_with_processes_returns_output_with_output_log_level_none():
  254. full_command = ['foo', 'bar']
  255. processes = (flexmock(),)
  256. flexmock(module.os, environ={'a': 'b'})
  257. process = flexmock(stdout=None)
  258. flexmock(module.subprocess).should_receive('Popen').with_args(
  259. full_command,
  260. stdin=None,
  261. stdout=module.subprocess.PIPE,
  262. stderr=module.subprocess.STDOUT,
  263. shell=False,
  264. env=None,
  265. cwd=None,
  266. ).and_return(process).once()
  267. flexmock(module).should_receive('log_outputs').and_return({process: 'out'})
  268. output = module.execute_command_with_processes(full_command, processes, output_log_level=None)
  269. assert output == 'out'
  270. def test_execute_command_with_processes_calls_full_command_with_output_file():
  271. full_command = ['foo', 'bar']
  272. processes = (flexmock(),)
  273. output_file = flexmock(name='test')
  274. flexmock(module.os, environ={'a': 'b'})
  275. flexmock(module.subprocess).should_receive('Popen').with_args(
  276. full_command,
  277. stdin=None,
  278. stdout=output_file,
  279. stderr=module.subprocess.PIPE,
  280. shell=False,
  281. env=None,
  282. cwd=None,
  283. ).and_return(flexmock(stderr=None)).once()
  284. flexmock(module).should_receive('log_outputs')
  285. output = module.execute_command_with_processes(full_command, processes, output_file=output_file)
  286. assert output is None
  287. def test_execute_command_with_processes_calls_full_command_without_capturing_output():
  288. full_command = ['foo', 'bar']
  289. processes = (flexmock(),)
  290. flexmock(module.os, environ={'a': 'b'})
  291. flexmock(module.subprocess).should_receive('Popen').with_args(
  292. full_command, stdin=None, stdout=None, stderr=None, shell=False, env=None, cwd=None
  293. ).and_return(flexmock(wait=lambda: 0)).once()
  294. flexmock(module).should_receive('exit_code_indicates_error').and_return(False)
  295. flexmock(module).should_receive('log_outputs')
  296. output = module.execute_command_with_processes(
  297. full_command, processes, output_file=module.DO_NOT_CAPTURE
  298. )
  299. assert output is None
  300. def test_execute_command_with_processes_calls_full_command_with_input_file():
  301. full_command = ['foo', 'bar']
  302. processes = (flexmock(),)
  303. input_file = flexmock(name='test')
  304. flexmock(module.os, environ={'a': 'b'})
  305. flexmock(module.subprocess).should_receive('Popen').with_args(
  306. full_command,
  307. stdin=input_file,
  308. stdout=module.subprocess.PIPE,
  309. stderr=module.subprocess.STDOUT,
  310. shell=False,
  311. env=None,
  312. cwd=None,
  313. ).and_return(flexmock(stdout=None)).once()
  314. flexmock(module).should_receive('log_outputs')
  315. output = module.execute_command_with_processes(full_command, processes, input_file=input_file)
  316. assert output is None
  317. def test_execute_command_with_processes_calls_full_command_with_shell():
  318. full_command = ['foo', 'bar']
  319. processes = (flexmock(),)
  320. flexmock(module.os, environ={'a': 'b'})
  321. flexmock(module.subprocess).should_receive('Popen').with_args(
  322. ' '.join(full_command),
  323. stdin=None,
  324. stdout=module.subprocess.PIPE,
  325. stderr=module.subprocess.STDOUT,
  326. shell=True,
  327. env=None,
  328. cwd=None,
  329. ).and_return(flexmock(stdout=None)).once()
  330. flexmock(module).should_receive('log_outputs')
  331. output = module.execute_command_with_processes(full_command, processes, shell=True)
  332. assert output is None
  333. def test_execute_command_with_processes_calls_full_command_with_extra_environment():
  334. full_command = ['foo', 'bar']
  335. processes = (flexmock(),)
  336. flexmock(module.os, environ={'a': 'b'})
  337. flexmock(module.subprocess).should_receive('Popen').with_args(
  338. full_command,
  339. stdin=None,
  340. stdout=module.subprocess.PIPE,
  341. stderr=module.subprocess.STDOUT,
  342. shell=False,
  343. env={'a': 'b', 'c': 'd'},
  344. cwd=None,
  345. ).and_return(flexmock(stdout=None)).once()
  346. flexmock(module).should_receive('log_outputs')
  347. output = module.execute_command_with_processes(
  348. full_command, processes, extra_environment={'c': 'd'}
  349. )
  350. assert output is None
  351. def test_execute_command_with_processes_calls_full_command_with_working_directory():
  352. full_command = ['foo', 'bar']
  353. processes = (flexmock(),)
  354. flexmock(module.os, environ={'a': 'b'})
  355. flexmock(module.subprocess).should_receive('Popen').with_args(
  356. full_command,
  357. stdin=None,
  358. stdout=module.subprocess.PIPE,
  359. stderr=module.subprocess.STDOUT,
  360. shell=False,
  361. env=None,
  362. cwd='/working',
  363. ).and_return(flexmock(stdout=None)).once()
  364. flexmock(module).should_receive('log_outputs')
  365. output = module.execute_command_with_processes(
  366. full_command, processes, working_directory='/working'
  367. )
  368. assert output is None
  369. def test_execute_command_with_processes_kills_processes_on_error():
  370. full_command = ['foo', 'bar']
  371. process = flexmock(stdout=flexmock(read=lambda count: None))
  372. process.should_receive('poll')
  373. process.should_receive('kill').once()
  374. processes = (process,)
  375. flexmock(module.os, environ={'a': 'b'})
  376. flexmock(module.subprocess).should_receive('Popen').with_args(
  377. full_command,
  378. stdin=None,
  379. stdout=module.subprocess.PIPE,
  380. stderr=module.subprocess.STDOUT,
  381. shell=False,
  382. env=None,
  383. cwd=None,
  384. ).and_raise(subprocess.CalledProcessError(1, full_command, 'error')).once()
  385. flexmock(module).should_receive('log_outputs').never()
  386. with pytest.raises(subprocess.CalledProcessError):
  387. module.execute_command_with_processes(full_command, processes)