test_bootstrap.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.actions.config import bootstrap as module
  4. def test_get_config_paths_returns_list_of_config_paths():
  5. bootstrap_arguments = flexmock(
  6. borgmatic_source_directory=None,
  7. repository='repo',
  8. archive='archive',
  9. )
  10. global_arguments = flexmock(
  11. dry_run=False,
  12. )
  13. local_borg_version = flexmock()
  14. extract_process = flexmock(
  15. stdout=flexmock(
  16. read=lambda: '{"config_paths": ["/borgmatic/config.yaml"]}',
  17. ),
  18. )
  19. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  20. extract_process
  21. )
  22. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  23. 'archive'
  24. )
  25. assert module.get_config_paths(bootstrap_arguments, global_arguments, local_borg_version) == [
  26. '/borgmatic/config.yaml'
  27. ]
  28. def test_get_config_paths_with_missing_manifest_raises_value_error():
  29. bootstrap_arguments = flexmock(
  30. borgmatic_source_directory=None,
  31. repository='repo',
  32. archive='archive',
  33. )
  34. global_arguments = flexmock(
  35. dry_run=False,
  36. )
  37. local_borg_version = flexmock()
  38. extract_process = flexmock(stdout=flexmock(read=lambda: ''))
  39. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  40. extract_process
  41. )
  42. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  43. 'archive'
  44. )
  45. with pytest.raises(ValueError):
  46. module.get_config_paths(bootstrap_arguments, global_arguments, local_borg_version)
  47. def test_get_config_paths_with_broken_json_raises_value_error():
  48. bootstrap_arguments = flexmock(
  49. borgmatic_source_directory=None,
  50. repository='repo',
  51. archive='archive',
  52. )
  53. global_arguments = flexmock(
  54. dry_run=False,
  55. )
  56. local_borg_version = flexmock()
  57. extract_process = flexmock(
  58. stdout=flexmock(read=lambda: '{"config_paths": ["/oops'),
  59. )
  60. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  61. extract_process
  62. )
  63. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  64. 'archive'
  65. )
  66. with pytest.raises(ValueError):
  67. module.get_config_paths(bootstrap_arguments, global_arguments, local_borg_version)
  68. def test_get_config_paths_with_json_missing_key_raises_value_error():
  69. bootstrap_arguments = flexmock(
  70. borgmatic_source_directory=None,
  71. repository='repo',
  72. archive='archive',
  73. )
  74. global_arguments = flexmock(
  75. dry_run=False,
  76. )
  77. local_borg_version = flexmock()
  78. extract_process = flexmock(
  79. stdout=flexmock(read=lambda: '{}'),
  80. )
  81. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  82. extract_process
  83. )
  84. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  85. 'archive'
  86. )
  87. with pytest.raises(ValueError):
  88. module.get_config_paths(bootstrap_arguments, global_arguments, local_borg_version)
  89. def test_run_bootstrap_does_not_raise():
  90. bootstrap_arguments = flexmock(
  91. repository='repo',
  92. archive='archive',
  93. destination='dest',
  94. strip_components=1,
  95. progress=False,
  96. borgmatic_source_directory='/borgmatic',
  97. )
  98. global_arguments = flexmock(
  99. dry_run=False,
  100. )
  101. local_borg_version = flexmock()
  102. extract_process = flexmock(
  103. stdout=flexmock(
  104. read=lambda: '{"config_paths": ["/borgmatic/config.yaml"]}',
  105. ),
  106. )
  107. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  108. extract_process
  109. ).twice()
  110. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  111. 'archive'
  112. )
  113. module.run_bootstrap(bootstrap_arguments, global_arguments, local_borg_version)