test_normalize.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. (
  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. False,
  40. ),
  41. (
  42. {'hooks': {'cronitor': 'https://example.com'}},
  43. {'hooks': {'cronitor': {'ping_url': 'https://example.com'}}},
  44. False,
  45. ),
  46. (
  47. {'hooks': {'pagerduty': 'https://example.com'}},
  48. {'hooks': {'pagerduty': {'integration_key': 'https://example.com'}}},
  49. False,
  50. ),
  51. (
  52. {'hooks': {'cronhub': 'https://example.com'}},
  53. {'hooks': {'cronhub': {'ping_url': 'https://example.com'}}},
  54. False,
  55. ),
  56. (
  57. {'hooks': None},
  58. {'hooks': None},
  59. False,
  60. ),
  61. (
  62. {'consistency': {'checks': ['archives']}},
  63. {'consistency': {'checks': [{'name': 'archives'}]}},
  64. False,
  65. ),
  66. (
  67. {'consistency': {'checks': ['archives']}},
  68. {'consistency': {'checks': [{'name': 'archives'}]}},
  69. False,
  70. ),
  71. (
  72. {'consistency': None},
  73. {'consistency': None},
  74. False,
  75. ),
  76. (
  77. {'location': {'numeric_owner': False}},
  78. {'location': {'numeric_ids': False}},
  79. False,
  80. ),
  81. (
  82. {'location': {'bsd_flags': False}},
  83. {'location': {'flags': False}},
  84. False,
  85. ),
  86. (
  87. {'storage': {'remote_rate_limit': False}},
  88. {'storage': {'upload_rate_limit': False}},
  89. False,
  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. False,
  110. ),
  111. (
  112. {'location': {'repositories': ['file:///repo']}},
  113. {'location': {'repositories': [{'path': '/repo'}]}},
  114. False,
  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. )
  133. def test_normalize_applies_hard_coded_normalization_to_config(
  134. config, expected_config, produces_logs
  135. ):
  136. logs = module.normalize('test.yaml', config)
  137. assert config == expected_config
  138. if produces_logs:
  139. assert logs
  140. else:
  141. assert logs == []
  142. def test_normalize_raises_error_if_repository_data_is_not_consistent():
  143. with pytest.raises(TypeError):
  144. module.normalize(
  145. 'test.yaml',
  146. {
  147. 'location': {
  148. 'repositories': [{'path': 'foo@bar:/repo', 'label': 'foo'}, 'file:///repo']
  149. }
  150. },
  151. )