2
0

test_execute.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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_shell():
  186. full_command = ['foo', 'bar']
  187. expected_output = '[]'
  188. flexmock(module.os, environ={'a': 'b'})
  189. flexmock(module.subprocess).should_receive('check_output').with_args(
  190. 'foo bar', stderr=None, shell=True, env=None, cwd=None
  191. ).and_return(flexmock(decode=lambda: expected_output)).once()
  192. output = module.execute_command_and_capture_output(full_command, shell=True)
  193. assert output == expected_output
  194. def test_execute_command_and_capture_output_returns_output_with_extra_environment():
  195. full_command = ['foo', 'bar']
  196. expected_output = '[]'
  197. flexmock(module.os, environ={'a': 'b'})
  198. flexmock(module.subprocess).should_receive('check_output').with_args(
  199. full_command, stderr=None, shell=False, env={'a': 'b', 'c': 'd'}, cwd=None,
  200. ).and_return(flexmock(decode=lambda: expected_output)).once()
  201. output = module.execute_command_and_capture_output(
  202. full_command, shell=False, extra_environment={'c': 'd'}
  203. )
  204. assert output == expected_output
  205. def test_execute_command_and_capture_output_returns_output_with_working_directory():
  206. full_command = ['foo', 'bar']
  207. expected_output = '[]'
  208. flexmock(module.os, environ={'a': 'b'})
  209. flexmock(module.subprocess).should_receive('check_output').with_args(
  210. full_command, stderr=None, shell=False, env=None, cwd='/working'
  211. ).and_return(flexmock(decode=lambda: expected_output)).once()
  212. output = module.execute_command_and_capture_output(
  213. full_command, shell=False, working_directory='/working'
  214. )
  215. assert output == expected_output
  216. def test_execute_command_with_processes_calls_full_command():
  217. full_command = ['foo', 'bar']
  218. processes = (flexmock(),)
  219. flexmock(module.os, environ={'a': 'b'})
  220. flexmock(module.subprocess).should_receive('Popen').with_args(
  221. full_command,
  222. stdin=None,
  223. stdout=module.subprocess.PIPE,
  224. stderr=module.subprocess.STDOUT,
  225. shell=False,
  226. env=None,
  227. cwd=None,
  228. ).and_return(flexmock(stdout=None)).once()
  229. flexmock(module).should_receive('log_outputs')
  230. output = module.execute_command_with_processes(full_command, processes)
  231. assert output is None
  232. def test_execute_command_with_processes_returns_output_with_output_log_level_none():
  233. full_command = ['foo', 'bar']
  234. processes = (flexmock(),)
  235. flexmock(module.os, environ={'a': 'b'})
  236. process = flexmock(stdout=None)
  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=None,
  244. cwd=None,
  245. ).and_return(process).once()
  246. flexmock(module).should_receive('log_outputs').and_return({process: 'out'})
  247. output = module.execute_command_with_processes(full_command, processes, output_log_level=None)
  248. assert output == 'out'
  249. def test_execute_command_with_processes_calls_full_command_with_output_file():
  250. full_command = ['foo', 'bar']
  251. processes = (flexmock(),)
  252. output_file = flexmock(name='test')
  253. flexmock(module.os, environ={'a': 'b'})
  254. flexmock(module.subprocess).should_receive('Popen').with_args(
  255. full_command,
  256. stdin=None,
  257. stdout=output_file,
  258. stderr=module.subprocess.PIPE,
  259. shell=False,
  260. env=None,
  261. cwd=None,
  262. ).and_return(flexmock(stderr=None)).once()
  263. flexmock(module).should_receive('log_outputs')
  264. output = module.execute_command_with_processes(full_command, processes, output_file=output_file)
  265. assert output is None
  266. def test_execute_command_with_processes_calls_full_command_without_capturing_output():
  267. full_command = ['foo', 'bar']
  268. processes = (flexmock(),)
  269. flexmock(module.os, environ={'a': 'b'})
  270. flexmock(module.subprocess).should_receive('Popen').with_args(
  271. full_command, stdin=None, stdout=None, stderr=None, shell=False, env=None, cwd=None
  272. ).and_return(flexmock(wait=lambda: 0)).once()
  273. flexmock(module).should_receive('exit_code_indicates_error').and_return(False)
  274. flexmock(module).should_receive('log_outputs')
  275. output = module.execute_command_with_processes(
  276. full_command, processes, output_file=module.DO_NOT_CAPTURE
  277. )
  278. assert output is None
  279. def test_execute_command_with_processes_calls_full_command_with_input_file():
  280. full_command = ['foo', 'bar']
  281. processes = (flexmock(),)
  282. input_file = flexmock(name='test')
  283. flexmock(module.os, environ={'a': 'b'})
  284. flexmock(module.subprocess).should_receive('Popen').with_args(
  285. full_command,
  286. stdin=input_file,
  287. stdout=module.subprocess.PIPE,
  288. stderr=module.subprocess.STDOUT,
  289. shell=False,
  290. env=None,
  291. cwd=None,
  292. ).and_return(flexmock(stdout=None)).once()
  293. flexmock(module).should_receive('log_outputs')
  294. output = module.execute_command_with_processes(full_command, processes, input_file=input_file)
  295. assert output is None
  296. def test_execute_command_with_processes_calls_full_command_with_shell():
  297. full_command = ['foo', 'bar']
  298. processes = (flexmock(),)
  299. flexmock(module.os, environ={'a': 'b'})
  300. flexmock(module.subprocess).should_receive('Popen').with_args(
  301. ' '.join(full_command),
  302. stdin=None,
  303. stdout=module.subprocess.PIPE,
  304. stderr=module.subprocess.STDOUT,
  305. shell=True,
  306. env=None,
  307. cwd=None,
  308. ).and_return(flexmock(stdout=None)).once()
  309. flexmock(module).should_receive('log_outputs')
  310. output = module.execute_command_with_processes(full_command, processes, shell=True)
  311. assert output is None
  312. def test_execute_command_with_processes_calls_full_command_with_extra_environment():
  313. full_command = ['foo', 'bar']
  314. processes = (flexmock(),)
  315. flexmock(module.os, environ={'a': 'b'})
  316. flexmock(module.subprocess).should_receive('Popen').with_args(
  317. full_command,
  318. stdin=None,
  319. stdout=module.subprocess.PIPE,
  320. stderr=module.subprocess.STDOUT,
  321. shell=False,
  322. env={'a': 'b', 'c': 'd'},
  323. cwd=None,
  324. ).and_return(flexmock(stdout=None)).once()
  325. flexmock(module).should_receive('log_outputs')
  326. output = module.execute_command_with_processes(
  327. full_command, processes, extra_environment={'c': 'd'}
  328. )
  329. assert output is None
  330. def test_execute_command_with_processes_calls_full_command_with_working_directory():
  331. full_command = ['foo', 'bar']
  332. processes = (flexmock(),)
  333. flexmock(module.os, environ={'a': 'b'})
  334. flexmock(module.subprocess).should_receive('Popen').with_args(
  335. full_command,
  336. stdin=None,
  337. stdout=module.subprocess.PIPE,
  338. stderr=module.subprocess.STDOUT,
  339. shell=False,
  340. env=None,
  341. cwd='/working',
  342. ).and_return(flexmock(stdout=None)).once()
  343. flexmock(module).should_receive('log_outputs')
  344. output = module.execute_command_with_processes(
  345. full_command, processes, working_directory='/working'
  346. )
  347. assert output is None
  348. def test_execute_command_with_processes_kills_processes_on_error():
  349. full_command = ['foo', 'bar']
  350. process = flexmock(stdout=flexmock(read=lambda count: None))
  351. process.should_receive('poll')
  352. process.should_receive('kill').once()
  353. processes = (process,)
  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=None,
  363. ).and_raise(subprocess.CalledProcessError(1, full_command, 'error')).once()
  364. flexmock(module).should_receive('log_outputs').never()
  365. with pytest.raises(subprocess.CalledProcessError):
  366. module.execute_command_with_processes(full_command, processes)