test_validate.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.config import validate as module
  4. def test_format_error_path_element_formats_array_index():
  5. module.format_error_path_element(3) == '[3]'
  6. def test_format_error_path_element_formats_property():
  7. module.format_error_path_element('foo') == '.foo'
  8. def test_format_error_formats_error_including_path():
  9. flexmock(module).format_error_path_element = lambda element: '.{}'.format(element)
  10. error = flexmock(message='oops', path=['foo', 'bar'])
  11. assert module.format_error(error) == "At 'foo.bar': oops"
  12. def test_format_error_formats_error_without_path():
  13. flexmock(module).should_receive('format_error_path_element').never()
  14. error = flexmock(message='oops', path=[])
  15. assert module.format_error(error) == 'At the top level: oops'
  16. def test_validation_error_string_contains_error_messages_and_config_filename():
  17. flexmock(module).format_error = lambda error: error.message
  18. error = module.Validation_error(
  19. 'config.yaml', (flexmock(message='oops', path=None), flexmock(message='uh oh'))
  20. )
  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_archive_name_format_present_without_prefix():
  26. flexmock(module).format_error = lambda error: error.message
  27. with pytest.raises(module.Validation_error):
  28. module.apply_logical_validation(
  29. 'config.yaml',
  30. {
  31. 'storage': {'archive_name_format': '{hostname}-{now}'},
  32. 'retention': {'keep_daily': 7},
  33. },
  34. )
  35. def test_apply_logical_validation_raises_if_archive_name_format_present_without_retention_prefix():
  36. flexmock(module).format_error = lambda error: error.message
  37. with pytest.raises(module.Validation_error):
  38. module.apply_logical_validation(
  39. 'config.yaml',
  40. {
  41. 'storage': {'archive_name_format': '{hostname}-{now}'},
  42. 'retention': {'keep_daily': 7},
  43. 'consistency': {'prefix': '{hostname}-'},
  44. },
  45. )
  46. def test_apply_locical_validation_raises_if_unknown_repository_in_check_repositories():
  47. flexmock(module).format_error = lambda error: error.message
  48. with pytest.raises(module.Validation_error):
  49. module.apply_logical_validation(
  50. 'config.yaml',
  51. {
  52. 'location': {'repositories': ['repo.borg', 'other.borg']},
  53. 'retention': {'keep_secondly': 1000},
  54. 'consistency': {'check_repositories': ['repo.borg', 'unknown.borg']},
  55. },
  56. )
  57. def test_apply_locical_validation_does_not_raise_if_known_repository_in_check_repositories():
  58. module.apply_logical_validation(
  59. 'config.yaml',
  60. {
  61. 'location': {'repositories': ['repo.borg', 'other.borg']},
  62. 'retention': {'keep_secondly': 1000},
  63. 'consistency': {'check_repositories': ['repo.borg']},
  64. },
  65. )
  66. def test_apply_logical_validation_does_not_raise_if_archive_name_format_and_prefix_present():
  67. module.apply_logical_validation(
  68. 'config.yaml',
  69. {
  70. 'storage': {'archive_name_format': '{hostname}-{now}'},
  71. 'retention': {'prefix': '{hostname}-'},
  72. 'consistency': {'prefix': '{hostname}-'},
  73. },
  74. )
  75. def test_apply_logical_validation_does_not_raise_otherwise():
  76. module.apply_logical_validation('config.yaml', {'retention': {'keep_secondly': 1000}})
  77. def test_normalize_repository_path_passes_through_remote_repository():
  78. repository = 'example.org:test.borg'
  79. module.normalize_repository_path(repository) == repository
  80. def test_normalize_repository_path_passes_through_absolute_repository():
  81. repository = '/foo/bar/test.borg'
  82. flexmock(module.os.path).should_receive('abspath').and_return(repository)
  83. module.normalize_repository_path(repository) == repository
  84. def test_normalize_repository_path_resolves_relative_repository():
  85. repository = 'test.borg'
  86. absolute = '/foo/bar/test.borg'
  87. flexmock(module.os.path).should_receive('abspath').and_return(absolute)
  88. module.normalize_repository_path(repository) == absolute
  89. def test_repositories_match_does_not_raise():
  90. flexmock(module).should_receive('normalize_repository_path')
  91. module.repositories_match('foo', 'bar')
  92. def test_guard_configuration_contains_repository_does_not_raise_when_repository_in_config():
  93. flexmock(module).should_receive('repositories_match').replace_with(
  94. lambda first, second: first == second
  95. )
  96. module.guard_configuration_contains_repository(
  97. repository='repo', configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  98. )
  99. def test_guard_configuration_contains_repository_does_not_raise_when_repository_not_given():
  100. module.guard_configuration_contains_repository(
  101. repository=None, configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  102. )
  103. def test_guard_configuration_contains_repository_errors_when_repository_assumed_to_match_config_twice():
  104. with pytest.raises(ValueError):
  105. module.guard_configuration_contains_repository(
  106. repository=None,
  107. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  108. )
  109. def test_guard_configuration_contains_repository_errors_when_repository_missing_from_config():
  110. flexmock(module).should_receive('repositories_match').replace_with(
  111. lambda first, second: first == second
  112. )
  113. with pytest.raises(ValueError):
  114. module.guard_configuration_contains_repository(
  115. repository='nope',
  116. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  117. )
  118. def test_guard_configuration_contains_repository_errors_when_repository_matches_config_twice():
  119. flexmock(module).should_receive('repositories_match').replace_with(
  120. lambda first, second: first == second
  121. )
  122. with pytest.raises(ValueError):
  123. module.guard_configuration_contains_repository(
  124. repository='repo',
  125. configurations={
  126. 'config.yaml': {'location': {'repositories': ['repo', 'repo2']}},
  127. 'other.yaml': {'location': {'repositories': ['repo']}},
  128. },
  129. )