test_override.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import pytest
  2. import ruamel.yaml
  3. from flexmock import flexmock
  4. from borgmatic.config import override as module
  5. def test_set_values_with_empty_keys_bails():
  6. config = {}
  7. module.set_values(config, keys=(), value='value')
  8. assert config == {}
  9. def test_set_values_with_one_key_sets_it_into_config():
  10. config = {}
  11. module.set_values(config, keys=('key',), value='value')
  12. assert config == {'key': 'value'}
  13. def test_set_values_with_one_key_overwrites_existing_key():
  14. config = {'key': 'old_value', 'other': 'other_value'}
  15. module.set_values(config, keys=('key',), value='value')
  16. assert config == {'key': 'value', 'other': 'other_value'}
  17. def test_set_values_with_multiple_keys_creates_hierarchy():
  18. config = {}
  19. module.set_values(config, ('option', 'suboption'), 'value')
  20. assert config == {'option': {'suboption': 'value'}}
  21. def test_set_values_with_multiple_keys_updates_hierarchy():
  22. config = {'option': {'other': 'other_value'}}
  23. module.set_values(config, ('option', 'key'), 'value')
  24. assert config == {'option': {'key': 'value', 'other': 'other_value'}}
  25. @pytest.mark.parametrize(
  26. 'schema,option_keys,expected_type',
  27. (
  28. ({'properties': {'foo': {'type': 'array'}}}, ('foo',), 'array'),
  29. (
  30. {'properties': {'foo': {'properties': {'bar': {'type': 'array'}}}}},
  31. ('foo', 'bar'),
  32. 'array',
  33. ),
  34. ({'properties': {'foo': {'type': 'array'}}}, ('other',), None),
  35. ({'properties': {'foo': {'description': 'stuff'}}}, ('foo',), None),
  36. ({}, ('foo',), None),
  37. ),
  38. )
  39. def test_type_for_option_grabs_type_if_found_in_schema(schema, option_keys, expected_type):
  40. assert module.type_for_option(schema, option_keys) == expected_type
  41. @pytest.mark.parametrize(
  42. 'key,expected_key',
  43. (
  44. (('foo', 'bar'), ('foo', 'bar')),
  45. (('location', 'foo'), ('foo',)),
  46. (('storage', 'foo'), ('foo',)),
  47. (('retention', 'foo'), ('foo',)),
  48. (('consistency', 'foo'), ('foo',)),
  49. (('output', 'foo'), ('foo',)),
  50. (('hooks', 'foo', 'bar'), ('foo', 'bar')),
  51. (('foo', 'hooks'), ('foo', 'hooks')),
  52. ),
  53. )
  54. def test_strip_section_names_passes_through_key_without_section_name(key, expected_key):
  55. assert module.strip_section_names(key) == expected_key
  56. def test_parse_overrides_splits_keys_and_values():
  57. flexmock(module).should_receive('strip_section_names').replace_with(lambda value: value)
  58. flexmock(module).should_receive('type_for_option').and_return('string')
  59. flexmock(module).should_receive('convert_value_type').replace_with(
  60. lambda value, option_type: value
  61. )
  62. raw_overrides = ['option.my_option=value1', 'other_option=value2']
  63. expected_result = (
  64. (('option', 'my_option'), 'value1'),
  65. (('other_option'), 'value2'),
  66. )
  67. module.parse_overrides(raw_overrides, schema={}) == expected_result
  68. def test_parse_overrides_allows_value_with_equal_sign():
  69. flexmock(module).should_receive('strip_section_names').replace_with(lambda value: value)
  70. flexmock(module).should_receive('type_for_option').and_return('string')
  71. flexmock(module).should_receive('convert_value_type').replace_with(
  72. lambda value, option_type: value
  73. )
  74. raw_overrides = ['option=this===value']
  75. expected_result = ((('option',), 'this===value'),)
  76. module.parse_overrides(raw_overrides, schema={}) == expected_result
  77. def test_parse_overrides_raises_on_missing_equal_sign():
  78. flexmock(module).should_receive('strip_section_names').replace_with(lambda value: value)
  79. flexmock(module).should_receive('type_for_option').and_return('string')
  80. flexmock(module).should_receive('convert_value_type').replace_with(
  81. lambda value, option_type: value
  82. )
  83. raw_overrides = ['option']
  84. with pytest.raises(ValueError):
  85. module.parse_overrides(raw_overrides, schema={})
  86. def test_parse_overrides_raises_on_invalid_override_value():
  87. flexmock(module).should_receive('strip_section_names').replace_with(lambda value: value)
  88. flexmock(module).should_receive('type_for_option').and_return('string')
  89. flexmock(module).should_receive('convert_value_type').and_raise(ruamel.yaml.parser.ParserError)
  90. raw_overrides = ['option=[in valid]']
  91. with pytest.raises(ValueError):
  92. module.parse_overrides(raw_overrides, schema={})
  93. def test_parse_overrides_allows_value_with_single_key():
  94. flexmock(module).should_receive('strip_section_names').replace_with(lambda value: value)
  95. flexmock(module).should_receive('type_for_option').and_return('string')
  96. flexmock(module).should_receive('convert_value_type').replace_with(
  97. lambda value, option_type: value
  98. )
  99. raw_overrides = ['option=value']
  100. expected_result = ((('option',), 'value'),)
  101. module.parse_overrides(raw_overrides, schema={}) == expected_result
  102. def test_parse_overrides_handles_empty_overrides():
  103. module.parse_overrides(raw_overrides=None, schema={}) == ()