2
0

test_execute.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. import subprocess
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic import execute as module
  5. @pytest.mark.parametrize(
  6. 'process,exit_code,borg_local_path,expected_result',
  7. (
  8. (flexmock(args=['grep']), 2, None, True),
  9. (flexmock(args=['grep']), 2, 'borg', True),
  10. (flexmock(args=['borg']), 2, 'borg', True),
  11. (flexmock(args=['borg1']), 2, 'borg1', True),
  12. (flexmock(args=['grep']), 1, None, True),
  13. (flexmock(args=['grep']), 1, 'borg', True),
  14. (flexmock(args=['borg']), 1, 'borg', False),
  15. (flexmock(args=['borg1']), 1, 'borg1', False),
  16. (flexmock(args=['grep']), 0, None, False),
  17. (flexmock(args=['grep']), 0, 'borg', False),
  18. (flexmock(args=['borg']), 0, 'borg', False),
  19. (flexmock(args=['borg1']), 0, 'borg1', False),
  20. # -9 exit code occurs when child process get SIGKILLed.
  21. (flexmock(args=['grep']), -9, None, True),
  22. (flexmock(args=['grep']), -9, 'borg', True),
  23. (flexmock(args=['borg']), -9, 'borg', True),
  24. (flexmock(args=['borg1']), -9, 'borg1', True),
  25. (flexmock(args=['borg']), None, None, False),
  26. ),
  27. )
  28. def test_exit_code_indicates_error_respects_exit_code_and_borg_local_path(
  29. process, exit_code, borg_local_path, expected_result
  30. ):
  31. assert module.exit_code_indicates_error(process, 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_with_raise_on_exit_code_one_false():
  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. output = module.execute_command_and_capture_output(full_command, raise_on_exit_code_one=False)
  194. assert output == expected_output
  195. def test_execute_command_and_capture_output_returns_output_with_raise_on_exit_code_one_false_and_exit_code_not_one():
  196. full_command = ['foo', 'bar']
  197. expected_output = '[]'
  198. flexmock(module.os, environ={'a': 'b'})
  199. flexmock(module.subprocess).should_receive('check_output').with_args(
  200. full_command, stderr=None, shell=False, env=None, cwd=None
  201. ).and_raise(subprocess.CalledProcessError(2, full_command, expected_output)).once()
  202. with pytest.raises(subprocess.CalledProcessError):
  203. module.execute_command_and_capture_output(full_command, raise_on_exit_code_one=False)
  204. def test_execute_command_and_capture_output_returns_output_with_shell():
  205. full_command = ['foo', 'bar']
  206. expected_output = '[]'
  207. flexmock(module.os, environ={'a': 'b'})
  208. flexmock(module.subprocess).should_receive('check_output').with_args(
  209. 'foo bar', stderr=None, shell=True, env=None, cwd=None
  210. ).and_return(flexmock(decode=lambda: expected_output)).once()
  211. output = module.execute_command_and_capture_output(full_command, shell=True)
  212. assert output == expected_output
  213. def test_execute_command_and_capture_output_returns_output_with_extra_environment():
  214. full_command = ['foo', 'bar']
  215. expected_output = '[]'
  216. flexmock(module.os, environ={'a': 'b'})
  217. flexmock(module.subprocess).should_receive('check_output').with_args(
  218. full_command, stderr=None, shell=False, env={'a': 'b', 'c': 'd'}, cwd=None,
  219. ).and_return(flexmock(decode=lambda: expected_output)).once()
  220. output = module.execute_command_and_capture_output(
  221. full_command, shell=False, extra_environment={'c': 'd'}
  222. )
  223. assert output == expected_output
  224. def test_execute_command_and_capture_output_returns_output_with_working_directory():
  225. full_command = ['foo', 'bar']
  226. expected_output = '[]'
  227. flexmock(module.os, environ={'a': 'b'})
  228. flexmock(module.subprocess).should_receive('check_output').with_args(
  229. full_command, stderr=None, shell=False, env=None, cwd='/working'
  230. ).and_return(flexmock(decode=lambda: expected_output)).once()
  231. output = module.execute_command_and_capture_output(
  232. full_command, shell=False, working_directory='/working'
  233. )
  234. assert output == expected_output
  235. def test_execute_command_with_processes_calls_full_command():
  236. full_command = ['foo', 'bar']
  237. processes = (flexmock(),)
  238. flexmock(module.os, environ={'a': 'b'})
  239. flexmock(module.subprocess).should_receive('Popen').with_args(
  240. full_command,
  241. stdin=None,
  242. stdout=module.subprocess.PIPE,
  243. stderr=module.subprocess.STDOUT,
  244. shell=False,
  245. env=None,
  246. cwd=None,
  247. ).and_return(flexmock(stdout=None)).once()
  248. flexmock(module).should_receive('log_outputs')
  249. output = module.execute_command_with_processes(full_command, processes)
  250. assert output is None
  251. def test_execute_command_with_processes_returns_output_with_output_log_level_none():
  252. full_command = ['foo', 'bar']
  253. processes = (flexmock(),)
  254. flexmock(module.os, environ={'a': 'b'})
  255. process = flexmock(stdout=None)
  256. flexmock(module.subprocess).should_receive('Popen').with_args(
  257. full_command,
  258. stdin=None,
  259. stdout=module.subprocess.PIPE,
  260. stderr=module.subprocess.STDOUT,
  261. shell=False,
  262. env=None,
  263. cwd=None,
  264. ).and_return(process).once()
  265. flexmock(module).should_receive('log_outputs').and_return({process: 'out'})
  266. output = module.execute_command_with_processes(full_command, processes, output_log_level=None)
  267. assert output == 'out'
  268. def test_execute_command_with_processes_calls_full_command_with_output_file():
  269. full_command = ['foo', 'bar']
  270. processes = (flexmock(),)
  271. output_file = flexmock(name='test')
  272. flexmock(module.os, environ={'a': 'b'})
  273. flexmock(module.subprocess).should_receive('Popen').with_args(
  274. full_command,
  275. stdin=None,
  276. stdout=output_file,
  277. stderr=module.subprocess.PIPE,
  278. shell=False,
  279. env=None,
  280. cwd=None,
  281. ).and_return(flexmock(stderr=None)).once()
  282. flexmock(module).should_receive('log_outputs')
  283. output = module.execute_command_with_processes(full_command, processes, output_file=output_file)
  284. assert output is None
  285. def test_execute_command_with_processes_calls_full_command_without_capturing_output():
  286. full_command = ['foo', 'bar']
  287. processes = (flexmock(),)
  288. flexmock(module.os, environ={'a': 'b'})
  289. flexmock(module.subprocess).should_receive('Popen').with_args(
  290. full_command, stdin=None, stdout=None, stderr=None, shell=False, env=None, cwd=None
  291. ).and_return(flexmock(wait=lambda: 0)).once()
  292. flexmock(module).should_receive('exit_code_indicates_error').and_return(False)
  293. flexmock(module).should_receive('log_outputs')
  294. output = module.execute_command_with_processes(
  295. full_command, processes, output_file=module.DO_NOT_CAPTURE
  296. )
  297. assert output is None
  298. def test_execute_command_with_processes_calls_full_command_with_input_file():
  299. full_command = ['foo', 'bar']
  300. processes = (flexmock(),)
  301. input_file = flexmock(name='test')
  302. flexmock(module.os, environ={'a': 'b'})
  303. flexmock(module.subprocess).should_receive('Popen').with_args(
  304. full_command,
  305. stdin=input_file,
  306. stdout=module.subprocess.PIPE,
  307. stderr=module.subprocess.STDOUT,
  308. shell=False,
  309. env=None,
  310. cwd=None,
  311. ).and_return(flexmock(stdout=None)).once()
  312. flexmock(module).should_receive('log_outputs')
  313. output = module.execute_command_with_processes(full_command, processes, input_file=input_file)
  314. assert output is None
  315. def test_execute_command_with_processes_calls_full_command_with_shell():
  316. full_command = ['foo', 'bar']
  317. processes = (flexmock(),)
  318. flexmock(module.os, environ={'a': 'b'})
  319. flexmock(module.subprocess).should_receive('Popen').with_args(
  320. ' '.join(full_command),
  321. stdin=None,
  322. stdout=module.subprocess.PIPE,
  323. stderr=module.subprocess.STDOUT,
  324. shell=True,
  325. env=None,
  326. cwd=None,
  327. ).and_return(flexmock(stdout=None)).once()
  328. flexmock(module).should_receive('log_outputs')
  329. output = module.execute_command_with_processes(full_command, processes, shell=True)
  330. assert output is None
  331. def test_execute_command_with_processes_calls_full_command_with_extra_environment():
  332. full_command = ['foo', 'bar']
  333. processes = (flexmock(),)
  334. flexmock(module.os, environ={'a': 'b'})
  335. flexmock(module.subprocess).should_receive('Popen').with_args(
  336. full_command,
  337. stdin=None,
  338. stdout=module.subprocess.PIPE,
  339. stderr=module.subprocess.STDOUT,
  340. shell=False,
  341. env={'a': 'b', 'c': 'd'},
  342. cwd=None,
  343. ).and_return(flexmock(stdout=None)).once()
  344. flexmock(module).should_receive('log_outputs')
  345. output = module.execute_command_with_processes(
  346. full_command, processes, extra_environment={'c': 'd'}
  347. )
  348. assert output is None
  349. def test_execute_command_with_processes_calls_full_command_with_working_directory():
  350. full_command = ['foo', 'bar']
  351. processes = (flexmock(),)
  352. flexmock(module.os, environ={'a': 'b'})
  353. flexmock(module.subprocess).should_receive('Popen').with_args(
  354. full_command,
  355. stdin=None,
  356. stdout=module.subprocess.PIPE,
  357. stderr=module.subprocess.STDOUT,
  358. shell=False,
  359. env=None,
  360. cwd='/working',
  361. ).and_return(flexmock(stdout=None)).once()
  362. flexmock(module).should_receive('log_outputs')
  363. output = module.execute_command_with_processes(
  364. full_command, processes, working_directory='/working'
  365. )
  366. assert output is None
  367. def test_execute_command_with_processes_kills_processes_on_error():
  368. full_command = ['foo', 'bar']
  369. process = flexmock(stdout=flexmock(read=lambda count: None))
  370. process.should_receive('poll')
  371. process.should_receive('kill').once()
  372. processes = (process,)
  373. flexmock(module.os, environ={'a': 'b'})
  374. flexmock(module.subprocess).should_receive('Popen').with_args(
  375. full_command,
  376. stdin=None,
  377. stdout=module.subprocess.PIPE,
  378. stderr=module.subprocess.STDOUT,
  379. shell=False,
  380. env=None,
  381. cwd=None,
  382. ).and_raise(subprocess.CalledProcessError(1, full_command, 'error')).once()
  383. flexmock(module).should_receive('log_outputs').never()
  384. with pytest.raises(subprocess.CalledProcessError):
  385. module.execute_command_with_processes(full_command, processes)