2
0

test_validate.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.config import validate as module
  4. def test_format_json_error_path_element_formats_array_index():
  5. module.format_json_error_path_element(3) == '[3]'
  6. def test_format_json_error_path_element_formats_property():
  7. module.format_json_error_path_element('foo') == '.foo'
  8. def test_format_json_error_formats_error_including_path():
  9. flexmock(module).format_json_error_path_element = lambda element: f'.{element}'
  10. error = flexmock(message='oops', path=['foo', 'bar'])
  11. assert module.format_json_error(error) == "At 'foo.bar': oops"
  12. def test_format_json_error_formats_error_without_path():
  13. flexmock(module).should_receive('format_json_error_path_element').never()
  14. error = flexmock(message='oops', path=[])
  15. assert module.format_json_error(error) == 'At the top level: oops'
  16. def test_validation_error_string_contains_errors():
  17. flexmock(module).format_json_error = lambda error: error.message
  18. error = module.Validation_error('config.yaml', ('oops', 'uh oh'))
  19. result = str(error)
  20. assert 'config.yaml' in result
  21. assert 'oops' in result
  22. assert 'uh oh' in result
  23. def test_apply_logical_validation_raises_if_unknown_repository_in_check_repositories():
  24. flexmock(module).format_json_error = lambda error: error.message
  25. with pytest.raises(module.Validation_error):
  26. module.apply_logical_validation(
  27. 'config.yaml',
  28. {
  29. 'location': {'repositories': ['repo.borg', 'other.borg']},
  30. 'retention': {'keep_secondly': 1000},
  31. 'consistency': {'check_repositories': ['repo.borg', 'unknown.borg']},
  32. },
  33. )
  34. def test_apply_logical_validation_does_not_raise_if_known_repository_path_in_check_repositories():
  35. module.apply_logical_validation(
  36. 'config.yaml',
  37. {
  38. 'location': {'repositories': [{'path': 'repo.borg'}, {'path': 'other.borg'}]},
  39. 'retention': {'keep_secondly': 1000},
  40. 'consistency': {'check_repositories': ['repo.borg']},
  41. },
  42. )
  43. def test_apply_logical_validation_does_not_raise_if_known_repository_label_in_check_repositories():
  44. module.apply_logical_validation(
  45. 'config.yaml',
  46. {
  47. 'location': {
  48. 'repositories': [
  49. {'path': 'repo.borg', 'label': 'my_repo'},
  50. {'path': 'other.borg', 'label': 'other_repo'},
  51. ]
  52. },
  53. 'retention': {'keep_secondly': 1000},
  54. 'consistency': {'check_repositories': ['my_repo']},
  55. },
  56. )
  57. def test_apply_logical_validation_does_not_raise_if_archive_name_format_and_prefix_present():
  58. module.apply_logical_validation(
  59. 'config.yaml',
  60. {
  61. 'storage': {'archive_name_format': '{hostname}-{now}'}, # noqa: FS003
  62. 'retention': {'prefix': '{hostname}-'}, # noqa: FS003
  63. 'consistency': {'prefix': '{hostname}-'}, # noqa: FS003
  64. },
  65. )
  66. def test_apply_logical_validation_does_not_raise_otherwise():
  67. module.apply_logical_validation('config.yaml', {'retention': {'keep_secondly': 1000}})
  68. def test_normalize_repository_path_passes_through_remote_repository():
  69. repository = 'example.org:test.borg'
  70. module.normalize_repository_path(repository) == repository
  71. def test_normalize_repository_path_passes_through_file_repository():
  72. repository = 'file:///foo/bar/test.borg'
  73. flexmock(module.os.path).should_receive('abspath').and_return('/foo/bar/test.borg')
  74. module.normalize_repository_path(repository) == '/foo/bar/test.borg'
  75. def test_normalize_repository_path_passes_through_absolute_repository():
  76. repository = '/foo/bar/test.borg'
  77. flexmock(module.os.path).should_receive('abspath').and_return(repository)
  78. module.normalize_repository_path(repository) == repository
  79. def test_normalize_repository_path_resolves_relative_repository():
  80. repository = 'test.borg'
  81. absolute = '/foo/bar/test.borg'
  82. flexmock(module.os.path).should_receive('abspath').and_return(absolute)
  83. module.normalize_repository_path(repository) == absolute
  84. def test_repositories_match_does_not_raise():
  85. flexmock(module).should_receive('normalize_repository_path')
  86. module.repositories_match('foo', 'bar')
  87. def test_guard_configuration_contains_repository_does_not_raise_when_repository_in_config():
  88. flexmock(module).should_receive('repositories_match').replace_with(
  89. lambda first, second: first == second
  90. )
  91. module.guard_configuration_contains_repository(
  92. repository='repo', configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  93. )
  94. def test_guard_configuration_contains_repository_does_not_raise_when_repository_label_in_config():
  95. module.guard_configuration_contains_repository(
  96. repository='repo',
  97. configurations={
  98. 'config.yaml': {'location': {'repositories': [{'path': 'foo/bar', 'label': 'repo'}]}}
  99. },
  100. )
  101. def test_guard_configuration_contains_repository_does_not_raise_when_repository_not_given():
  102. module.guard_configuration_contains_repository(
  103. repository=None, configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  104. )
  105. def test_guard_configuration_contains_repository_errors_when_repository_missing_from_config():
  106. flexmock(module).should_receive('repositories_match').replace_with(
  107. lambda first, second: first == second
  108. )
  109. with pytest.raises(ValueError):
  110. module.guard_configuration_contains_repository(
  111. repository='nope',
  112. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  113. )
  114. def test_guard_configuration_contains_repository_errors_when_repository_matches_config_twice():
  115. flexmock(module).should_receive('repositories_match').replace_with(
  116. lambda first, second: first == second
  117. )
  118. with pytest.raises(ValueError):
  119. module.guard_configuration_contains_repository(
  120. repository='repo',
  121. configurations={
  122. 'config.yaml': {'location': {'repositories': ['repo', 'repo2']}},
  123. 'other.yaml': {'location': {'repositories': ['repo']}},
  124. },
  125. )
  126. def test_guard_single_repository_selected_raises_when_multiple_repositories_configured_and_none_selected():
  127. with pytest.raises(ValueError):
  128. module.guard_single_repository_selected(
  129. repository=None,
  130. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  131. )
  132. def test_guard_single_repository_selected_does_not_raise_when_single_repository_configured_and_none_selected():
  133. module.guard_single_repository_selected(
  134. repository=None, configurations={'config.yaml': {'location': {'repositories': ['repo']}}},
  135. )
  136. def test_guard_single_repository_selected_does_not_raise_when_no_repositories_configured_and_one_selected():
  137. module.guard_single_repository_selected(
  138. repository='repo', configurations={'config.yaml': {'location': {'repositories': []}}},
  139. )
  140. def test_guard_single_repository_selected_does_not_raise_when_repositories_configured_and_one_selected():
  141. module.guard_single_repository_selected(
  142. repository='repo',
  143. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  144. )