test_extract.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import logging
  2. import sys
  3. from flexmock import flexmock
  4. from borgmatic.borg import extract as module
  5. from ..test_verbosity import insert_logging_mock
  6. def insert_subprocess_mock(check_call_command, **kwargs):
  7. subprocess = flexmock(module.subprocess)
  8. subprocess.should_receive('check_call').with_args(check_call_command, **kwargs).once()
  9. def insert_subprocess_never():
  10. subprocess = flexmock(module.subprocess)
  11. subprocess.should_receive('check_call').never()
  12. def insert_subprocess_check_output_mock(check_output_command, result, **kwargs):
  13. subprocess = flexmock(module.subprocess)
  14. subprocess.should_receive('check_output').with_args(check_output_command, **kwargs).and_return(
  15. result
  16. ).once()
  17. def test_extract_last_archive_dry_run_should_call_borg_with_last_archive():
  18. flexmock(sys.stdout).encoding = 'utf-8'
  19. insert_subprocess_check_output_mock(
  20. ('borg', 'list', '--short', 'repo'), result='archive1\narchive2\n'.encode('utf-8')
  21. )
  22. insert_subprocess_mock(('borg', 'extract', '--dry-run', 'repo::archive2'))
  23. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  24. def test_extract_last_archive_dry_run_without_any_archives_should_bail():
  25. flexmock(sys.stdout).encoding = 'utf-8'
  26. insert_subprocess_check_output_mock(
  27. ('borg', 'list', '--short', 'repo'), result='\n'.encode('utf-8')
  28. )
  29. insert_subprocess_never()
  30. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  31. def test_extract_last_archive_dry_run_with_log_info_should_call_borg_with_info_parameter():
  32. flexmock(sys.stdout).encoding = 'utf-8'
  33. insert_subprocess_check_output_mock(
  34. ('borg', 'list', '--short', 'repo', '--info'), result='archive1\narchive2\n'.encode('utf-8')
  35. )
  36. insert_subprocess_mock(('borg', 'extract', '--dry-run', 'repo::archive2', '--info'))
  37. insert_logging_mock(logging.INFO)
  38. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  39. def test_extract_last_archive_dry_run_with_log_debug_should_call_borg_with_debug_parameter():
  40. flexmock(sys.stdout).encoding = 'utf-8'
  41. insert_subprocess_check_output_mock(
  42. ('borg', 'list', '--short', 'repo', '--debug', '--show-rc'),
  43. result='archive1\narchive2\n'.encode('utf-8'),
  44. )
  45. insert_subprocess_mock(
  46. ('borg', 'extract', '--dry-run', 'repo::archive2', '--debug', '--show-rc', '--list')
  47. )
  48. insert_logging_mock(logging.DEBUG)
  49. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  50. def test_extract_last_archive_dry_run_should_call_borg_via_local_path():
  51. flexmock(sys.stdout).encoding = 'utf-8'
  52. insert_subprocess_check_output_mock(
  53. ('borg1', 'list', '--short', 'repo'), result='archive1\narchive2\n'.encode('utf-8')
  54. )
  55. insert_subprocess_mock(('borg1', 'extract', '--dry-run', 'repo::archive2'))
  56. module.extract_last_archive_dry_run(repository='repo', lock_wait=None, local_path='borg1')
  57. def test_extract_last_archive_dry_run_should_call_borg_with_remote_path_parameters():
  58. flexmock(sys.stdout).encoding = 'utf-8'
  59. insert_subprocess_check_output_mock(
  60. ('borg', 'list', '--short', 'repo', '--remote-path', 'borg1'),
  61. result='archive1\narchive2\n'.encode('utf-8'),
  62. )
  63. insert_subprocess_mock(
  64. ('borg', 'extract', '--dry-run', 'repo::archive2', '--remote-path', 'borg1')
  65. )
  66. module.extract_last_archive_dry_run(repository='repo', lock_wait=None, remote_path='borg1')
  67. def test_extract_last_archive_dry_run_should_call_borg_with_lock_wait_parameters():
  68. flexmock(sys.stdout).encoding = 'utf-8'
  69. insert_subprocess_check_output_mock(
  70. ('borg', 'list', '--short', 'repo', '--lock-wait', '5'),
  71. result='archive1\narchive2\n'.encode('utf-8'),
  72. )
  73. insert_subprocess_mock(('borg', 'extract', '--dry-run', 'repo::archive2', '--lock-wait', '5'))
  74. module.extract_last_archive_dry_run(repository='repo', lock_wait=5)