test_flags.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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, ()),
  58. (None, '', True, ()),
  59. (
  60. 're:foo-.*',
  61. '{hostname}-{now}',
  62. True,
  63. ('--match-archives', 're:foo-.*'),
  64. ), # noqa: FS003
  65. (
  66. 'sh:foo-*',
  67. '{hostname}-{now}',
  68. False,
  69. ('--glob-archives', 'foo-*'),
  70. ), # noqa: FS003
  71. (
  72. 'foo-*',
  73. '{hostname}-{now}',
  74. False,
  75. ('--glob-archives', 'foo-*'),
  76. ), # noqa: FS003
  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. (None, '{utcnow}-docs-{user}', False, ('--glob-archives', '*-docs-{user}')), # noqa: FS003
  103. ),
  104. )
  105. def test_make_match_archives_flags_makes_flags_with_globs(
  106. match_archives, archive_name_format, feature_available, expected_result
  107. ):
  108. flexmock(module.feature).should_receive('available').and_return(feature_available)
  109. assert (
  110. module.make_match_archives_flags(
  111. match_archives, archive_name_format, local_borg_version=flexmock()
  112. )
  113. == expected_result
  114. )