test_extract.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import sys
  2. from flexmock import flexmock
  3. from borgmatic.borg import extract as module
  4. from borgmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
  5. def insert_subprocess_mock(check_call_command, **kwargs):
  6. subprocess = flexmock(module.subprocess)
  7. subprocess.should_receive('check_call').with_args(check_call_command, **kwargs).once()
  8. def insert_subprocess_never():
  9. subprocess = flexmock(module.subprocess)
  10. subprocess.should_receive('check_call').never()
  11. def insert_subprocess_check_output_mock(check_output_command, result, **kwargs):
  12. subprocess = flexmock(module.subprocess)
  13. subprocess.should_receive('check_output').with_args(check_output_command, **kwargs).and_return(result).once()
  14. def test_extract_last_archive_dry_run_should_call_borg_with_last_archive():
  15. flexmock(sys.stdout).encoding = 'utf-8'
  16. insert_subprocess_check_output_mock(
  17. ('borg', 'list', '--short', 'repo'),
  18. result='archive1\narchive2\n'.encode('utf-8'),
  19. )
  20. insert_subprocess_mock(
  21. ('borg', 'extract', '--dry-run', 'repo::archive2'),
  22. )
  23. module.extract_last_archive_dry_run(
  24. verbosity=None,
  25. repository='repo',
  26. lock_wait=None,
  27. )
  28. def test_extract_last_archive_dry_run_without_any_archives_should_bail():
  29. flexmock(sys.stdout).encoding = 'utf-8'
  30. insert_subprocess_check_output_mock(
  31. ('borg', 'list', '--short', 'repo'),
  32. result='\n'.encode('utf-8'),
  33. )
  34. insert_subprocess_never()
  35. module.extract_last_archive_dry_run(
  36. verbosity=None,
  37. repository='repo',
  38. lock_wait=None,
  39. )
  40. def test_extract_last_archive_dry_run_with_verbosity_some_should_call_borg_with_info_parameter():
  41. flexmock(sys.stdout).encoding = 'utf-8'
  42. insert_subprocess_check_output_mock(
  43. ('borg', 'list', '--short', 'repo', '--info'),
  44. result='archive1\narchive2\n'.encode('utf-8'),
  45. )
  46. insert_subprocess_mock(
  47. ('borg', 'extract', '--dry-run', 'repo::archive2', '--info'),
  48. )
  49. module.extract_last_archive_dry_run(
  50. verbosity=VERBOSITY_SOME,
  51. repository='repo',
  52. lock_wait=None,
  53. )
  54. def test_extract_last_archive_dry_run_with_verbosity_lots_should_call_borg_with_debug_parameter():
  55. flexmock(sys.stdout).encoding = 'utf-8'
  56. insert_subprocess_check_output_mock(
  57. ('borg', 'list', '--short', 'repo', '--debug', '--show-rc'),
  58. result='archive1\narchive2\n'.encode('utf-8'),
  59. )
  60. insert_subprocess_mock(
  61. ('borg', 'extract', '--dry-run', 'repo::archive2', '--debug', '--show-rc', '--list'),
  62. )
  63. module.extract_last_archive_dry_run(
  64. verbosity=VERBOSITY_LOTS,
  65. repository='repo',
  66. lock_wait=None,
  67. )
  68. def test_extract_last_archive_dry_run_should_call_borg_via_local_path():
  69. flexmock(sys.stdout).encoding = 'utf-8'
  70. insert_subprocess_check_output_mock(
  71. ('borg1', 'list', '--short', 'repo'),
  72. result='archive1\narchive2\n'.encode('utf-8'),
  73. )
  74. insert_subprocess_mock(
  75. ('borg1', 'extract', '--dry-run', 'repo::archive2'),
  76. )
  77. module.extract_last_archive_dry_run(
  78. verbosity=None,
  79. repository='repo',
  80. lock_wait=None,
  81. local_path='borg1',
  82. )
  83. def test_extract_last_archive_dry_run_should_call_borg_with_remote_path_parameters():
  84. flexmock(sys.stdout).encoding = 'utf-8'
  85. insert_subprocess_check_output_mock(
  86. ('borg', 'list', '--short', 'repo', '--remote-path', 'borg1'),
  87. result='archive1\narchive2\n'.encode('utf-8'),
  88. )
  89. insert_subprocess_mock(
  90. ('borg', 'extract', '--dry-run', 'repo::archive2', '--remote-path', 'borg1'),
  91. )
  92. module.extract_last_archive_dry_run(
  93. verbosity=None,
  94. repository='repo',
  95. lock_wait=None,
  96. remote_path='borg1',
  97. )
  98. def test_extract_last_archive_dry_run_should_call_borg_with_lock_wait_parameters():
  99. flexmock(sys.stdout).encoding = 'utf-8'
  100. insert_subprocess_check_output_mock(
  101. ('borg', 'list', '--short', 'repo', '--lock-wait', '5'),
  102. result='archive1\narchive2\n'.encode('utf-8'),
  103. )
  104. insert_subprocess_mock(
  105. ('borg', 'extract', '--dry-run', 'repo::archive2', '--lock-wait', '5'),
  106. )
  107. module.extract_last_archive_dry_run(
  108. verbosity=None,
  109. repository='repo',
  110. lock_wait=5,
  111. )