test_extract.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_calls_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_calls_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_calls_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_calls_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_calls_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_calls_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)
  75. def test_extract_archive_calls_borg_with_restore_path_parameters():
  76. insert_subprocess_mock(('borg', 'extract', 'repo::archive', 'path1', 'path2'))
  77. module.extract_archive(
  78. dry_run=False,
  79. repository='repo',
  80. archive='archive',
  81. restore_paths=['path1', 'path2'],
  82. storage_config={},
  83. )
  84. def test_extract_archive_calls_borg_with_remote_path_parameters():
  85. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--remote-path', 'borg1'))
  86. module.extract_archive(
  87. dry_run=False,
  88. repository='repo',
  89. archive='archive',
  90. restore_paths=None,
  91. storage_config={},
  92. remote_path='borg1',
  93. )
  94. def test_extract_archive_calls_borg_with_umask_parameters():
  95. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--umask', '0770'))
  96. module.extract_archive(
  97. dry_run=False,
  98. repository='repo',
  99. archive='archive',
  100. restore_paths=None,
  101. storage_config={'umask': '0770'},
  102. )
  103. def test_extract_archive_calls_borg_with_lock_wait_parameters():
  104. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--lock-wait', '5'))
  105. module.extract_archive(
  106. dry_run=False,
  107. repository='repo',
  108. archive='archive',
  109. restore_paths=None,
  110. storage_config={'lock_wait': '5'},
  111. )
  112. def test_extract_archive_with_log_info_calls_borg_with_info_parameter():
  113. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--info'))
  114. insert_logging_mock(logging.INFO)
  115. module.extract_archive(
  116. dry_run=False, repository='repo', archive='archive', restore_paths=None, storage_config={}
  117. )
  118. def test_extract_archive_with_log_debug_calls_borg_with_debug_parameters():
  119. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--debug', '--list', '--show-rc'))
  120. insert_logging_mock(logging.DEBUG)
  121. module.extract_archive(
  122. dry_run=False, repository='repo', archive='archive', restore_paths=None, storage_config={}
  123. )
  124. def test_extract_archive_calls_borg_with_dry_run_parameter():
  125. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--dry-run'))
  126. module.extract_archive(
  127. dry_run=True, repository='repo', archive='archive', restore_paths=None, storage_config={}
  128. )
  129. def test_extract_archive_calls_borg_with_progress_parameter():
  130. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--progress'))
  131. module.extract_archive(
  132. dry_run=False,
  133. repository='repo',
  134. archive='archive',
  135. restore_paths=None,
  136. storage_config={},
  137. progress=True,
  138. )