test_flags.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. ) == ('--repo', 'repo', 'archive',)
  45. def test_make_repository_archive_flags_with_borg_features_joins_repository_and_archive():
  46. flexmock(module.feature).should_receive('available').and_return(False)
  47. assert module.make_repository_archive_flags(
  48. repository_path='repo', archive='archive', local_borg_version='1.2.3'
  49. ) == ('repo::archive',)
  50. @pytest.mark.parametrize(
  51. 'match_archives, archive_name_format,feature_available,expected_result',
  52. (
  53. (None, None, True, ()),
  54. (None, '', True, ()),
  55. ('re:foo-.*', '{hostname}-{now}', True, ('--match-archives', 're:foo-.*'),), # noqa: FS003
  56. ('sh:foo-*', '{hostname}-{now}', False, ('--glob-archives', 'foo-*'),), # noqa: FS003
  57. ('foo-*', '{hostname}-{now}', False, ('--glob-archives', 'foo-*'),), # noqa: FS003
  58. (
  59. None,
  60. '{hostname}-docs-{now}', # noqa: FS003
  61. True,
  62. ('--match-archives', 'sh:{hostname}-docs-*'), # noqa: FS003
  63. ),
  64. (
  65. None,
  66. '{utcnow}-docs-{user}', # noqa: FS003
  67. True,
  68. ('--match-archives', 'sh:*-docs-{user}'), # noqa: FS003
  69. ),
  70. (None, '{fqdn}-{pid}', True, ('--match-archives', 'sh:{fqdn}-*')), # noqa: FS003
  71. (
  72. None,
  73. 'stuff-{now:%Y-%m-%dT%H:%M:%S.%f}', # noqa: FS003
  74. True,
  75. ('--match-archives', 'sh:stuff-*'),
  76. ),
  77. (
  78. None,
  79. '{hostname}-docs-{now}', # noqa: FS003
  80. False,
  81. ('--glob-archives', '{hostname}-docs-*'), # noqa: FS003
  82. ),
  83. (None, '{utcnow}-docs-{user}', False, ('--glob-archives', '*-docs-{user}')), # noqa: FS003
  84. ),
  85. )
  86. def test_make_match_archives_flags_makes_flags_with_globs(
  87. match_archives, archive_name_format, feature_available, expected_result
  88. ):
  89. flexmock(module.feature).should_receive('available').and_return(feature_available)
  90. assert (
  91. module.make_match_archives_flags(
  92. match_archives, archive_name_format, local_borg_version=flexmock()
  93. )
  94. == expected_result
  95. )