test_validate.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.config import validate as module
  4. def test_validation_error_str_contains_error_messages_and_config_filename():
  5. error = module.Validation_error('config.yaml', ('oops', 'uh oh'))
  6. result = str(error)
  7. assert 'config.yaml' in result
  8. assert 'oops' in result
  9. assert 'uh oh' in result
  10. def test_apply_logical_validation_raises_if_archive_name_format_present_without_prefix():
  11. with pytest.raises(module.Validation_error):
  12. module.apply_logical_validation(
  13. 'config.yaml',
  14. {
  15. 'storage': {'archive_name_format': '{hostname}-{now}'},
  16. 'retention': {'keep_daily': 7},
  17. },
  18. )
  19. def test_apply_logical_validation_raises_if_archive_name_format_present_without_retention_prefix():
  20. with pytest.raises(module.Validation_error):
  21. module.apply_logical_validation(
  22. 'config.yaml',
  23. {
  24. 'storage': {'archive_name_format': '{hostname}-{now}'},
  25. 'retention': {'keep_daily': 7},
  26. 'consistency': {'prefix': '{hostname}-'},
  27. },
  28. )
  29. def test_apply_locical_validation_raises_if_unknown_repository_in_check_repositories():
  30. with pytest.raises(module.Validation_error):
  31. module.apply_logical_validation(
  32. 'config.yaml',
  33. {
  34. 'location': {'repositories': ['repo.borg', 'other.borg']},
  35. 'retention': {'keep_secondly': 1000},
  36. 'consistency': {'check_repositories': ['repo.borg', 'unknown.borg']},
  37. },
  38. )
  39. def test_apply_locical_validation_does_not_raise_if_known_repository_in_check_repositories():
  40. module.apply_logical_validation(
  41. 'config.yaml',
  42. {
  43. 'location': {'repositories': ['repo.borg', 'other.borg']},
  44. 'retention': {'keep_secondly': 1000},
  45. 'consistency': {'check_repositories': ['repo.borg']},
  46. },
  47. )
  48. def test_apply_logical_validation_does_not_raise_if_archive_name_format_and_prefix_present():
  49. module.apply_logical_validation(
  50. 'config.yaml',
  51. {
  52. 'storage': {'archive_name_format': '{hostname}-{now}'},
  53. 'retention': {'prefix': '{hostname}-'},
  54. 'consistency': {'prefix': '{hostname}-'},
  55. },
  56. )
  57. def test_apply_logical_validation_does_not_raise_otherwise():
  58. module.apply_logical_validation('config.yaml', {'retention': {'keep_secondly': 1000}})
  59. def test_remove_examples_strips_examples_from_map():
  60. schema = {
  61. 'map': {
  62. 'foo': {'desc': 'thing1', 'example': 'bar'},
  63. 'baz': {'desc': 'thing2', 'example': 'quux'},
  64. }
  65. }
  66. module.remove_examples(schema)
  67. assert schema == {'map': {'foo': {'desc': 'thing1'}, 'baz': {'desc': 'thing2'}}}
  68. def test_remove_examples_strips_examples_from_sequence_of_maps():
  69. schema = {'seq': [{'map': {'foo': {'desc': 'thing', 'example': 'bar'}}, 'example': 'stuff'}]}
  70. module.remove_examples(schema)
  71. assert schema == {'seq': [{'map': {'foo': {'desc': 'thing'}}}]}
  72. def test_normalize_repository_path_passes_through_remote_repository():
  73. repository = 'example.org:test.borg'
  74. module.normalize_repository_path(repository) == repository
  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_not_given():
  95. module.guard_configuration_contains_repository(
  96. repository=None, configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
  97. )
  98. def test_guard_configuration_contains_repository_errors_when_repository_assumed_to_match_config_twice():
  99. with pytest.raises(ValueError):
  100. module.guard_configuration_contains_repository(
  101. repository=None,
  102. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  103. )
  104. def test_guard_configuration_contains_repository_errors_when_repository_missing_from_config():
  105. flexmock(module).should_receive('repositories_match').replace_with(
  106. lambda first, second: first == second
  107. )
  108. with pytest.raises(ValueError):
  109. module.guard_configuration_contains_repository(
  110. repository='nope',
  111. configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
  112. )
  113. def test_guard_configuration_contains_repository_errors_when_repository_matches_config_twice():
  114. flexmock(module).should_receive('repositories_match').replace_with(
  115. lambda first, second: first == second
  116. )
  117. with pytest.raises(ValueError):
  118. module.guard_configuration_contains_repository(
  119. repository='repo',
  120. configurations={
  121. 'config.yaml': {'location': {'repositories': ['repo', 'repo2']}},
  122. 'other.yaml': {'location': {'repositories': ['repo']}},
  123. },
  124. )