test_constants.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.config import constants as module
  4. @pytest.mark.parametrize(
  5. 'value,expected_value',
  6. (
  7. ('3', 3),
  8. ('0', 0),
  9. ('-3', -3),
  10. ('1234', 1234),
  11. ('true', True),
  12. ('True', True),
  13. ('false', False),
  14. ('False', False),
  15. ('thing', 'thing'),
  16. ({}, {}),
  17. ({'foo': 'bar'}, {'foo': 'bar'}),
  18. ([], []),
  19. (['foo', 'bar'], ['foo', 'bar']),
  20. ),
  21. )
  22. def test_coerce_scalar_converts_value(value, expected_value):
  23. assert module.coerce_scalar(value) == expected_value
  24. def test_apply_constants_with_empty_constants_passes_through_value():
  25. assert module.apply_constants(value='thing', constants={}) == 'thing'
  26. @pytest.mark.parametrize(
  27. 'value,expected_value',
  28. (
  29. (None, None),
  30. ('thing', 'thing'),
  31. ('{foo}', 'bar'),
  32. ('abc{foo}', 'abcbar'),
  33. ('{foo}xyz', 'barxyz'),
  34. ('{foo}{baz}', 'barquux'),
  35. ('{int}', '3'),
  36. ('{bool}', 'True'),
  37. (['thing', 'other'], ['thing', 'other']),
  38. (['thing', '{foo}'], ['thing', 'bar']),
  39. (['{foo}', '{baz}'], ['bar', 'quux']),
  40. ({'key': 'value'}, {'key': 'value'}),
  41. ({'key': '{foo}'}, {'key': 'bar'}),
  42. ({'key': '{inject}'}, {'key': 'echo hi; naughty-command'}),
  43. ({'before_backup': '{inject}'}, {'before_backup': "'echo hi; naughty-command'"}),
  44. ({'after_backup': '{inject}'}, {'after_backup': "'echo hi; naughty-command'"}),
  45. ({'on_error': '{inject}'}, {'on_error': "'echo hi; naughty-command'"}),
  46. (
  47. {
  48. 'before_backup': '{env_pass}',
  49. 'postgresql_databases': [{'name': 'users', 'password': '{env_pass}'}],
  50. },
  51. {
  52. 'before_backup': "'${PASS}'",
  53. 'postgresql_databases': [{'name': 'users', 'password': '${PASS}'}],
  54. },
  55. ),
  56. (3, 3),
  57. (True, True),
  58. (False, False),
  59. ),
  60. )
  61. def test_apply_constants_makes_string_substitutions(value, expected_value):
  62. flexmock(module).should_receive('coerce_scalar').replace_with(lambda value: value)
  63. constants = {
  64. 'foo': 'bar',
  65. 'baz': 'quux',
  66. 'int': 3,
  67. 'bool': True,
  68. 'inject': 'echo hi; naughty-command',
  69. 'env_pass': '${PASS}',
  70. }
  71. assert module.apply_constants(value, constants) == expected_value