test_normalize.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import pytest
  2. from borgmatic.config import normalize as module
  3. @pytest.mark.parametrize(
  4. 'config,expected_config,produces_logs',
  5. (
  6. (
  7. {'location': {'exclude_if_present': '.nobackup'}},
  8. {'location': {'exclude_if_present': ['.nobackup']}},
  9. False,
  10. ),
  11. (
  12. {'location': {'exclude_if_present': ['.nobackup']}},
  13. {'location': {'exclude_if_present': ['.nobackup']}},
  14. False,
  15. ),
  16. (
  17. {'location': {'source_directories': ['foo', 'bar']}},
  18. {'location': {'source_directories': ['foo', 'bar']}},
  19. False,
  20. ),
  21. ({'location': None}, {'location': None}, False,),
  22. (
  23. {'storage': {'compression': 'yes_please'}},
  24. {'storage': {'compression': 'yes_please'}},
  25. False,
  26. ),
  27. ({'storage': None}, {'storage': None}, False,),
  28. (
  29. {'hooks': {'healthchecks': 'https://example.com'}},
  30. {'hooks': {'healthchecks': {'ping_url': 'https://example.com'}}},
  31. False,
  32. ),
  33. (
  34. {'hooks': {'cronitor': 'https://example.com'}},
  35. {'hooks': {'cronitor': {'ping_url': 'https://example.com'}}},
  36. False,
  37. ),
  38. (
  39. {'hooks': {'pagerduty': 'https://example.com'}},
  40. {'hooks': {'pagerduty': {'integration_key': 'https://example.com'}}},
  41. False,
  42. ),
  43. (
  44. {'hooks': {'cronhub': 'https://example.com'}},
  45. {'hooks': {'cronhub': {'ping_url': 'https://example.com'}}},
  46. False,
  47. ),
  48. ({'hooks': None}, {'hooks': None}, False,),
  49. (
  50. {'consistency': {'checks': ['archives']}},
  51. {'consistency': {'checks': [{'name': 'archives'}]}},
  52. False,
  53. ),
  54. (
  55. {'consistency': {'checks': ['archives']}},
  56. {'consistency': {'checks': [{'name': 'archives'}]}},
  57. False,
  58. ),
  59. ({'consistency': None}, {'consistency': None}, False,),
  60. ({'location': {'numeric_owner': False}}, {'location': {'numeric_ids': False}}, False,),
  61. ({'location': {'bsd_flags': False}}, {'location': {'flags': False}}, False,),
  62. (
  63. {'storage': {'remote_rate_limit': False}},
  64. {'storage': {'upload_rate_limit': False}},
  65. False,
  66. ),
  67. (
  68. {'location': {'repositories': ['foo@bar:/repo']}},
  69. {'location': {'repositories': [{'path': 'ssh://foo@bar/repo'}]}},
  70. True,
  71. ),
  72. (
  73. {'location': {'repositories': ['foo@bar:repo']}},
  74. {'location': {'repositories': [{'path': 'ssh://foo@bar/./repo'}]}},
  75. True,
  76. ),
  77. (
  78. {'location': {'repositories': ['foo@bar:~/repo']}},
  79. {'location': {'repositories': [{'path': 'ssh://foo@bar/~/repo'}]}},
  80. True,
  81. ),
  82. (
  83. {'location': {'repositories': ['ssh://foo@bar:1234/repo']}},
  84. {'location': {'repositories': [{'path': 'ssh://foo@bar:1234/repo'}]}},
  85. False,
  86. ),
  87. (
  88. {'location': {'repositories': ['file:///repo']}},
  89. {'location': {'repositories': [{'path': '/repo'}]}},
  90. False,
  91. ),
  92. (
  93. {'location': {'repositories': [{'path': 'foo@bar:/repo', 'label': 'foo'}]}},
  94. {'location': {'repositories': [{'path': 'ssh://foo@bar/repo', 'label': 'foo'}]}},
  95. True,
  96. ),
  97. (
  98. {'location': {'repositories': [{'path': 'file:///repo', 'label': 'foo'}]}},
  99. {'location': {'repositories': [{'path': '/repo', 'label': 'foo'}]}},
  100. False,
  101. ),
  102. (
  103. {'location': {'repositories': [{'path': '/repo', 'label': 'foo'}]}},
  104. {'location': {'repositories': [{'path': '/repo', 'label': 'foo'}]}},
  105. False,
  106. ),
  107. ),
  108. )
  109. def test_normalize_applies_hard_coded_normalization_to_config(
  110. config, expected_config, produces_logs
  111. ):
  112. logs = module.normalize('test.yaml', config)
  113. assert config == expected_config
  114. if produces_logs:
  115. assert logs
  116. else:
  117. assert logs == []
  118. def test_normalize_raises_error_if_repository_data_is_not_consistent():
  119. with pytest.raises(TypeError):
  120. module.normalize(
  121. 'test.yaml',
  122. {
  123. 'location': {
  124. 'repositories': [{'path': 'foo@bar:/repo', 'label': 'foo'}, 'file:///repo']
  125. }
  126. },
  127. )