test_flags.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.borg import flags as module
  4. def test_make_flags_formats_string_value():
  5. assert module.make_flags('foo', 'bar') == ('--foo', 'bar')
  6. def test_make_flags_formats_integer_value():
  7. assert module.make_flags('foo', 3) == ('--foo', '3')
  8. def test_make_flags_formats_true_value():
  9. assert module.make_flags('foo', True) == ('--foo',)
  10. def test_make_flags_omits_false_value():
  11. assert module.make_flags('foo', False) == ()
  12. def test_make_flags_formats_name_with_underscore():
  13. assert module.make_flags('posix_me_harder', 'okay') == ('--posix-me-harder', 'okay')
  14. def test_make_flags_from_arguments_flattens_and_sorts_multiple_arguments():
  15. flexmock(module).should_receive('make_flags').with_args('foo', 'bar').and_return(('foo', 'bar'))
  16. flexmock(module).should_receive('make_flags').with_args('baz', 'quux').and_return(
  17. ('baz', 'quux')
  18. )
  19. arguments = flexmock(foo='bar', baz='quux')
  20. assert module.make_flags_from_arguments(arguments) == ('baz', 'quux', 'foo', 'bar')
  21. def test_make_flags_from_arguments_excludes_underscored_argument_names():
  22. flexmock(module).should_receive('make_flags').with_args('foo', 'bar').and_return(('foo', 'bar'))
  23. arguments = flexmock(foo='bar', _baz='quux')
  24. assert module.make_flags_from_arguments(arguments) == ('foo', 'bar')
  25. def test_make_flags_from_arguments_omits_excludes():
  26. flexmock(module).should_receive('make_flags').with_args('foo', 'bar').and_return(('foo', 'bar'))
  27. arguments = flexmock(foo='bar', baz='quux')
  28. assert module.make_flags_from_arguments(arguments, excludes=('baz', 'other')) == ('foo', 'bar')
  29. def test_make_repository_flags_with_borg_features_includes_repo_flag():
  30. flexmock(module.feature).should_receive('available').and_return(True)
  31. assert module.make_repository_flags(repository_path='repo', local_borg_version='1.2.3') == (
  32. '--repo',
  33. 'repo',
  34. )
  35. def test_make_repository_flags_without_borg_features_includes_omits_flag():
  36. flexmock(module.feature).should_receive('available').and_return(False)
  37. assert module.make_repository_flags(repository_path='repo', local_borg_version='1.2.3') == (
  38. 'repo',
  39. )
  40. def test_make_repository_archive_flags_with_borg_features_separates_repository_and_archive():
  41. flexmock(module.feature).should_receive('available').and_return(True)
  42. assert module.make_repository_archive_flags(
  43. repository_path='repo', archive='archive', local_borg_version='1.2.3'
  44. ) == (
  45. '--repo',
  46. 'repo',
  47. 'archive',
  48. )
  49. def test_make_repository_archive_flags_with_borg_features_joins_repository_and_archive():
  50. flexmock(module.feature).should_receive('available').and_return(False)
  51. assert module.make_repository_archive_flags(
  52. repository_path='repo', archive='archive', local_borg_version='1.2.3'
  53. ) == ('repo::archive',)
  54. @pytest.mark.parametrize(
  55. 'match_archives,archive_name_format,feature_available,expected_result',
  56. (
  57. (None, None, True, ('--match-archives', 'sh:{hostname}-*')), # noqa: FS003
  58. (None, '', True, ('--match-archives', 'sh:{hostname}-*')), # noqa: FS003
  59. (
  60. 're:foo-.*',
  61. '{hostname}-{now}', # noqa: FS003
  62. True,
  63. ('--match-archives', 're:foo-.*'),
  64. ),
  65. (
  66. 'sh:foo-*',
  67. '{hostname}-{now}', # noqa: FS003
  68. False,
  69. ('--glob-archives', 'foo-*'),
  70. ),
  71. (
  72. 'foo-*',
  73. '{hostname}-{now}', # noqa: FS003
  74. False,
  75. ('--glob-archives', 'foo-*'),
  76. ),
  77. (
  78. None,
  79. '{hostname}-docs-{now}', # noqa: FS003
  80. True,
  81. ('--match-archives', 'sh:{hostname}-docs-*'), # noqa: FS003
  82. ),
  83. (
  84. None,
  85. '{utcnow}-docs-{user}', # noqa: FS003
  86. True,
  87. ('--match-archives', 'sh:*-docs-{user}'), # noqa: FS003
  88. ),
  89. (None, '{fqdn}-{pid}', True, ('--match-archives', 'sh:{fqdn}-*')), # noqa: FS003
  90. (
  91. None,
  92. 'stuff-{now:%Y-%m-%dT%H:%M:%S.%f}', # noqa: FS003
  93. True,
  94. ('--match-archives', 'sh:stuff-*'),
  95. ),
  96. (
  97. None,
  98. '{hostname}-docs-{now}', # noqa: FS003
  99. False,
  100. ('--glob-archives', '{hostname}-docs-*'), # noqa: FS003
  101. ),
  102. (
  103. None,
  104. '{now}', # noqa: FS003
  105. False,
  106. (),
  107. ),
  108. (
  109. None,
  110. '{now}', # noqa: FS003
  111. True,
  112. (),
  113. ),
  114. (
  115. None,
  116. '{utcnow}-docs-{user}', # noqa: FS003
  117. False,
  118. ('--glob-archives', '*-docs-{user}'), # noqa: FS003
  119. ),
  120. (
  121. '*',
  122. '{now}', # noqa: FS003
  123. True,
  124. (),
  125. ),
  126. (
  127. '*',
  128. '{now}', # noqa: FS003
  129. False,
  130. (),
  131. ),
  132. (
  133. 're:.*',
  134. '{now}', # noqa: FS003
  135. True,
  136. (),
  137. ),
  138. (
  139. 'sh:*',
  140. '{now}', # noqa: FS003
  141. True,
  142. (),
  143. ),
  144. ),
  145. )
  146. def test_make_match_archives_flags_makes_flags_with_globs(
  147. match_archives, archive_name_format, feature_available, expected_result
  148. ):
  149. flexmock(module.feature).should_receive('available').and_return(feature_available)
  150. assert (
  151. module.make_match_archives_flags(
  152. match_archives, archive_name_format, local_borg_version=flexmock()
  153. )
  154. == expected_result
  155. )
  156. def test_make_match_archives_flags_accepts_default_archive_name_format():
  157. flexmock(module.feature).should_receive('available').and_return(True)
  158. assert (
  159. module.make_match_archives_flags(
  160. match_archives=None,
  161. archive_name_format=None,
  162. local_borg_version=flexmock(),
  163. default_archive_name_format='*',
  164. )
  165. == ()
  166. )
  167. def test_warn_for_aggressive_archive_flags_without_archive_flags_bails():
  168. flexmock(module.logger).should_receive('warning').never()
  169. module.warn_for_aggressive_archive_flags(('borg', '--do-stuff'), '{}')
  170. def test_warn_for_aggressive_archive_flags_with_glob_archives_and_zero_archives_warns():
  171. flexmock(module.logger).should_receive('warning').twice()
  172. module.warn_for_aggressive_archive_flags(
  173. ('borg', '--glob-archives', 'foo*'), '{"archives": []}'
  174. )
  175. def test_warn_for_aggressive_archive_flags_with_match_archives_and_zero_archives_warns():
  176. flexmock(module.logger).should_receive('warning').twice()
  177. module.warn_for_aggressive_archive_flags(
  178. ('borg', '--match-archives', 'foo*'), '{"archives": []}'
  179. )
  180. def test_warn_for_aggressive_archive_flags_with_glob_archives_and_one_archive_does_not_warn():
  181. flexmock(module.logger).should_receive('warning').never()
  182. module.warn_for_aggressive_archive_flags(
  183. ('borg', '--glob-archives', 'foo*'), '{"archives": [{"name": "foo"]}'
  184. )
  185. def test_warn_for_aggressive_archive_flags_with_match_archives_and_one_archive_does_not_warn():
  186. flexmock(module.logger).should_receive('warning').never()
  187. module.warn_for_aggressive_archive_flags(
  188. ('borg', '--match-archives', 'foo*'), '{"archives": [{"name": "foo"]}'
  189. )
  190. def test_warn_for_aggressive_archive_flags_with_glob_archives_and_invalid_json_does_not_warn():
  191. flexmock(module.logger).should_receive('warning').never()
  192. module.warn_for_aggressive_archive_flags(('borg', '--glob-archives', 'foo*'), '{"archives": [}')
  193. def test_warn_for_aggressive_archive_flags_with_glob_archives_and_json_missing_archives_does_not_warn():
  194. flexmock(module.logger).should_receive('warning').never()
  195. module.warn_for_aggressive_archive_flags(('borg', '--glob-archives', 'foo*'), '{}')