test_bootstrap.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. ssh_command=None,
  10. )
  11. global_arguments = flexmock(
  12. dry_run=False,
  13. )
  14. local_borg_version = flexmock()
  15. extract_process = flexmock(
  16. stdout=flexmock(
  17. read=lambda: '{"config_paths": ["/borgmatic/config.yaml"]}',
  18. ),
  19. )
  20. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  21. extract_process
  22. )
  23. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  24. 'archive'
  25. )
  26. assert module.get_config_paths(bootstrap_arguments, global_arguments, local_borg_version) == [
  27. '/borgmatic/config.yaml'
  28. ]
  29. def test_get_config_paths_translates_ssh_command_argument_to_config():
  30. bootstrap_arguments = flexmock(
  31. borgmatic_source_directory=None,
  32. repository='repo',
  33. archive='archive',
  34. ssh_command='ssh -i key',
  35. )
  36. global_arguments = flexmock(
  37. dry_run=False,
  38. )
  39. local_borg_version = flexmock()
  40. extract_process = flexmock(
  41. stdout=flexmock(
  42. read=lambda: '{"config_paths": ["/borgmatic/config.yaml"]}',
  43. ),
  44. )
  45. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').with_args(
  46. False,
  47. 'repo',
  48. 'archive',
  49. object,
  50. {'ssh_command': 'ssh -i key'},
  51. object,
  52. object,
  53. extract_to_stdout=True,
  54. ).and_return(extract_process)
  55. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').with_args(
  56. 'repo', 'archive', {'ssh_command': 'ssh -i key'}, object, object
  57. ).and_return('archive')
  58. assert module.get_config_paths(bootstrap_arguments, global_arguments, local_borg_version) == [
  59. '/borgmatic/config.yaml'
  60. ]
  61. def test_get_config_paths_with_missing_manifest_raises_value_error():
  62. bootstrap_arguments = flexmock(
  63. borgmatic_source_directory=None,
  64. repository='repo',
  65. archive='archive',
  66. ssh_command=None,
  67. )
  68. global_arguments = flexmock(
  69. dry_run=False,
  70. )
  71. local_borg_version = flexmock()
  72. extract_process = flexmock(stdout=flexmock(read=lambda: ''))
  73. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  74. extract_process
  75. )
  76. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  77. 'archive'
  78. )
  79. with pytest.raises(ValueError):
  80. module.get_config_paths(bootstrap_arguments, global_arguments, local_borg_version)
  81. def test_get_config_paths_with_broken_json_raises_value_error():
  82. bootstrap_arguments = flexmock(
  83. borgmatic_source_directory=None,
  84. repository='repo',
  85. archive='archive',
  86. ssh_command=None,
  87. )
  88. global_arguments = flexmock(
  89. dry_run=False,
  90. )
  91. local_borg_version = flexmock()
  92. extract_process = flexmock(
  93. stdout=flexmock(read=lambda: '{"config_paths": ["/oops'),
  94. )
  95. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  96. extract_process
  97. )
  98. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  99. 'archive'
  100. )
  101. with pytest.raises(ValueError):
  102. module.get_config_paths(bootstrap_arguments, global_arguments, local_borg_version)
  103. def test_get_config_paths_with_json_missing_key_raises_value_error():
  104. bootstrap_arguments = flexmock(
  105. borgmatic_source_directory=None,
  106. repository='repo',
  107. archive='archive',
  108. ssh_command=None,
  109. )
  110. global_arguments = flexmock(
  111. dry_run=False,
  112. )
  113. local_borg_version = flexmock()
  114. extract_process = flexmock(
  115. stdout=flexmock(read=lambda: '{}'),
  116. )
  117. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  118. extract_process
  119. )
  120. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  121. 'archive'
  122. )
  123. with pytest.raises(ValueError):
  124. module.get_config_paths(bootstrap_arguments, global_arguments, local_borg_version)
  125. def test_run_bootstrap_does_not_raise():
  126. flexmock(module).should_receive('get_config_paths').and_return(['/borgmatic/config.yaml'])
  127. bootstrap_arguments = flexmock(
  128. repository='repo',
  129. archive='archive',
  130. destination='dest',
  131. strip_components=1,
  132. progress=False,
  133. borgmatic_source_directory='/borgmatic',
  134. ssh_command=None,
  135. )
  136. global_arguments = flexmock(
  137. dry_run=False,
  138. )
  139. local_borg_version = flexmock()
  140. extract_process = flexmock(
  141. stdout=flexmock(
  142. read=lambda: '{"config_paths": ["borgmatic/config.yaml"]}',
  143. ),
  144. )
  145. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  146. extract_process
  147. ).once()
  148. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  149. 'archive'
  150. )
  151. module.run_bootstrap(bootstrap_arguments, global_arguments, local_borg_version)
  152. def test_run_bootstrap_translates_ssh_command_argument_to_config():
  153. flexmock(module).should_receive('get_config_paths').and_return(['/borgmatic/config.yaml'])
  154. bootstrap_arguments = flexmock(
  155. repository='repo',
  156. archive='archive',
  157. destination='dest',
  158. strip_components=1,
  159. progress=False,
  160. borgmatic_source_directory='/borgmatic',
  161. ssh_command='ssh -i key',
  162. )
  163. global_arguments = flexmock(
  164. dry_run=False,
  165. )
  166. local_borg_version = flexmock()
  167. extract_process = flexmock(
  168. stdout=flexmock(
  169. read=lambda: '{"config_paths": ["borgmatic/config.yaml"]}',
  170. ),
  171. )
  172. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').with_args(
  173. False,
  174. 'repo',
  175. 'archive',
  176. object,
  177. {'ssh_command': 'ssh -i key'},
  178. object,
  179. object,
  180. extract_to_stdout=False,
  181. destination_path='dest',
  182. strip_components=1,
  183. progress=False,
  184. ).and_return(extract_process).once()
  185. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').with_args(
  186. 'repo', 'archive', {'ssh_command': 'ssh -i key'}, object, object
  187. ).and_return('archive')
  188. module.run_bootstrap(bootstrap_arguments, global_arguments, local_borg_version)