test_validate.py 7.4 KB

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