test_normalize.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. True,
  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. (
  22. {'location': None},
  23. {'location': None},
  24. False,
  25. ),
  26. (
  27. {'storage': {'compression': 'yes_please'}},
  28. {'storage': {'compression': 'yes_please'}},
  29. False,
  30. ),
  31. (
  32. {'storage': None},
  33. {'storage': None},
  34. False,
  35. ),
  36. (
  37. {'hooks': {'healthchecks': 'https://example.com'}},
  38. {'hooks': {'healthchecks': {'ping_url': 'https://example.com'}}},
  39. True,
  40. ),
  41. (
  42. {'hooks': {'cronitor': 'https://example.com'}},
  43. {'hooks': {'cronitor': {'ping_url': 'https://example.com'}}},
  44. True,
  45. ),
  46. (
  47. {'hooks': {'pagerduty': 'https://example.com'}},
  48. {'hooks': {'pagerduty': {'integration_key': 'https://example.com'}}},
  49. True,
  50. ),
  51. (
  52. {'hooks': {'cronhub': 'https://example.com'}},
  53. {'hooks': {'cronhub': {'ping_url': 'https://example.com'}}},
  54. True,
  55. ),
  56. (
  57. {'hooks': None},
  58. {'hooks': None},
  59. False,
  60. ),
  61. (
  62. {'consistency': {'checks': ['archives']}},
  63. {'consistency': {'checks': [{'name': 'archives'}]}},
  64. True,
  65. ),
  66. (
  67. {'consistency': {'checks': ['archives']}},
  68. {'consistency': {'checks': [{'name': 'archives'}]}},
  69. True,
  70. ),
  71. (
  72. {'consistency': None},
  73. {'consistency': None},
  74. False,
  75. ),
  76. (
  77. {'location': {'numeric_owner': False}},
  78. {'location': {'numeric_ids': False}},
  79. True,
  80. ),
  81. (
  82. {'location': {'bsd_flags': False}},
  83. {'location': {'flags': False}},
  84. True,
  85. ),
  86. (
  87. {'storage': {'remote_rate_limit': False}},
  88. {'storage': {'upload_rate_limit': False}},
  89. True,
  90. ),
  91. (
  92. {'location': {'repositories': ['foo@bar:/repo']}},
  93. {'location': {'repositories': [{'path': 'ssh://foo@bar/repo'}]}},
  94. True,
  95. ),
  96. (
  97. {'location': {'repositories': ['foo@bar:repo']}},
  98. {'location': {'repositories': [{'path': 'ssh://foo@bar/./repo'}]}},
  99. True,
  100. ),
  101. (
  102. {'location': {'repositories': ['foo@bar:~/repo']}},
  103. {'location': {'repositories': [{'path': 'ssh://foo@bar/~/repo'}]}},
  104. True,
  105. ),
  106. (
  107. {'location': {'repositories': ['ssh://foo@bar:1234/repo']}},
  108. {'location': {'repositories': [{'path': 'ssh://foo@bar:1234/repo'}]}},
  109. True,
  110. ),
  111. (
  112. {'location': {'repositories': ['file:///repo']}},
  113. {'location': {'repositories': [{'path': '/repo'}]}},
  114. True,
  115. ),
  116. (
  117. {'location': {'repositories': [{'path': 'foo@bar:/repo', 'label': 'foo'}]}},
  118. {'location': {'repositories': [{'path': 'ssh://foo@bar/repo', 'label': 'foo'}]}},
  119. True,
  120. ),
  121. (
  122. {'location': {'repositories': [{'path': 'file:///repo', 'label': 'foo'}]}},
  123. {'location': {'repositories': [{'path': '/repo', 'label': 'foo'}]}},
  124. False,
  125. ),
  126. (
  127. {'location': {'repositories': [{'path': '/repo', 'label': 'foo'}]}},
  128. {'location': {'repositories': [{'path': '/repo', 'label': 'foo'}]}},
  129. False,
  130. ),
  131. (
  132. {'consistency': {'prefix': 'foo'}},
  133. {'consistency': {'prefix': 'foo'}},
  134. True,
  135. ),
  136. (
  137. {'retention': {'prefix': 'foo'}},
  138. {'retention': {'prefix': 'foo'}},
  139. True,
  140. ),
  141. ),
  142. )
  143. def test_normalize_applies_hard_coded_normalization_to_config(
  144. config, expected_config, produces_logs
  145. ):
  146. logs = module.normalize('test.yaml', config)
  147. assert config == expected_config
  148. if produces_logs:
  149. assert logs
  150. else:
  151. assert logs == []
  152. def test_normalize_raises_error_if_repository_data_is_not_consistent():
  153. with pytest.raises(TypeError):
  154. module.normalize(
  155. 'test.yaml',
  156. {
  157. 'location': {
  158. 'repositories': [{'path': 'foo@bar:/repo', 'label': 'foo'}, 'file:///repo']
  159. }
  160. },
  161. )