test_execute.py 15 KB

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