123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143 |
- import io
- import sys
- import pytest
- from flexmock import flexmock
- from borgmatic.config import load as module
- def test_load_configuration_parses_contents():
- builtins = flexmock(sys.modules['builtins'])
- config_file = io.StringIO('key: value')
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = {'other.yaml'}
- assert module.load_configuration('config.yaml', config_paths) == {'key': 'value'}
- assert config_paths == {'config.yaml', 'other.yaml'}
- def test_load_configuration_with_only_integer_value_does_not_raise():
- builtins = flexmock(sys.modules['builtins'])
- config_file = io.StringIO('33')
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = {'other.yaml'}
- assert module.load_configuration('config.yaml', config_paths) == 33
- assert config_paths == {'config.yaml', 'other.yaml'}
- def test_load_configuration_inlines_include_relative_to_current_directory():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').and_return(False)
- flexmock(module.os.path).should_receive('exists').and_return(True)
- include_file = io.StringIO('value')
- include_file.name = 'include.yaml'
- builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
- config_file = io.StringIO('key: !include include.yaml')
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = {'other.yaml'}
- assert module.load_configuration('config.yaml', config_paths) == {'key': 'value'}
- assert config_paths == {'config.yaml', '/tmp/include.yaml', 'other.yaml'}
- def test_load_configuration_inlines_include_relative_to_config_parent_directory():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').with_args('/etc').and_return(True)
- flexmock(module.os.path).should_receive('isabs').with_args('/etc/config.yaml').and_return(True)
- flexmock(module.os.path).should_receive('isabs').with_args('include.yaml').and_return(False)
- flexmock(module.os.path).should_receive('exists').with_args('/tmp/include.yaml').and_return(
- False
- )
- flexmock(module.os.path).should_receive('exists').with_args('/etc/include.yaml').and_return(
- True
- )
- include_file = io.StringIO('value')
- include_file.name = 'include.yaml'
- builtins.should_receive('open').with_args('/etc/include.yaml').and_return(include_file)
- config_file = io.StringIO('key: !include include.yaml')
- config_file.name = '/etc/config.yaml'
- builtins.should_receive('open').with_args('/etc/config.yaml').and_return(config_file)
- config_paths = {'other.yaml'}
- assert module.load_configuration('/etc/config.yaml', config_paths) == {'key': 'value'}
- assert config_paths == {'/etc/config.yaml', '/etc/include.yaml', 'other.yaml'}
- def test_load_configuration_raises_if_relative_include_does_not_exist():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').with_args('/etc').and_return(True)
- flexmock(module.os.path).should_receive('isabs').with_args('/etc/config.yaml').and_return(True)
- flexmock(module.os.path).should_receive('isabs').with_args('include.yaml').and_return(False)
- flexmock(module.os.path).should_receive('exists').and_return(False)
- config_file = io.StringIO('key: !include include.yaml')
- config_file.name = '/etc/config.yaml'
- builtins.should_receive('open').with_args('/etc/config.yaml').and_return(config_file)
- config_paths = set()
- with pytest.raises(FileNotFoundError):
- module.load_configuration('/etc/config.yaml', config_paths)
- def test_load_configuration_inlines_absolute_include():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').and_return(True)
- flexmock(module.os.path).should_receive('exists').never()
- include_file = io.StringIO('value')
- include_file.name = '/root/include.yaml'
- builtins.should_receive('open').with_args('/root/include.yaml').and_return(include_file)
- config_file = io.StringIO('key: !include /root/include.yaml')
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = {'other.yaml'}
- assert module.load_configuration('config.yaml', config_paths) == {'key': 'value'}
- assert config_paths == {'config.yaml', '/root/include.yaml', 'other.yaml'}
- def test_load_configuration_raises_if_absolute_include_does_not_exist():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').and_return(True)
- builtins.should_receive('open').with_args('/root/include.yaml').and_raise(FileNotFoundError)
- config_file = io.StringIO('key: !include /root/include.yaml')
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = set()
- with pytest.raises(FileNotFoundError):
- assert module.load_configuration('config.yaml', config_paths)
- def test_load_configuration_inlines_multiple_file_include_as_list():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').and_return(True)
- flexmock(module.os.path).should_receive('exists').never()
- include1_file = io.StringIO('value1')
- include1_file.name = '/root/include1.yaml'
- builtins.should_receive('open').with_args('/root/include1.yaml').and_return(include1_file)
- include2_file = io.StringIO('value2')
- include2_file.name = '/root/include2.yaml'
- builtins.should_receive('open').with_args('/root/include2.yaml').and_return(include2_file)
- config_file = io.StringIO('key: !include [/root/include1.yaml, /root/include2.yaml]')
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = {'other.yaml'}
- assert module.load_configuration('config.yaml', config_paths) == {'key': ['value2', 'value1']}
- assert config_paths == {
- 'config.yaml',
- '/root/include1.yaml',
- '/root/include2.yaml',
- 'other.yaml',
- }
- def test_load_configuration_include_with_unsupported_filename_type_raises():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').and_return(True)
- flexmock(module.os.path).should_receive('exists').never()
- config_file = io.StringIO('key: !include {path: /root/include.yaml}')
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = set()
- with pytest.raises(ValueError):
- module.load_configuration('config.yaml', config_paths)
- def test_load_configuration_merges_include():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').and_return(False)
- flexmock(module.os.path).should_receive('exists').and_return(True)
- include_file = io.StringIO(
- '''
- foo: bar
- baz: quux
- '''
- )
- include_file.name = 'include.yaml'
- builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
- config_file = io.StringIO(
- '''
- foo: override
- <<: !include include.yaml
- '''
- )
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = {'other.yaml'}
- assert module.load_configuration('config.yaml', config_paths) == {
- 'foo': 'override',
- 'baz': 'quux',
- }
- assert config_paths == {'config.yaml', '/tmp/include.yaml', 'other.yaml'}
- def test_load_configuration_merges_multiple_file_include():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').and_return(False)
- flexmock(module.os.path).should_receive('exists').and_return(True)
- include1_file = io.StringIO(
- '''
- foo: bar
- baz: quux
- original: yes
- '''
- )
- include1_file.name = 'include1.yaml'
- builtins.should_receive('open').with_args('/tmp/include1.yaml').and_return(include1_file)
- include2_file = io.StringIO(
- '''
- baz: second
- '''
- )
- include2_file.name = 'include2.yaml'
- builtins.should_receive('open').with_args('/tmp/include2.yaml').and_return(include2_file)
- config_file = io.StringIO(
- '''
- foo: override
- <<: !include [include1.yaml, include2.yaml]
- '''
- )
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = {'other.yaml'}
- assert module.load_configuration('config.yaml', config_paths) == {
- 'foo': 'override',
- 'baz': 'second',
- 'original': 'yes',
- }
- assert config_paths == {'config.yaml', '/tmp/include1.yaml', '/tmp/include2.yaml', 'other.yaml'}
- def test_load_configuration_with_retain_tag_merges_include_but_keeps_local_values():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').and_return(False)
- flexmock(module.os.path).should_receive('exists').and_return(True)
- include_file = io.StringIO(
- '''
- stuff:
- foo: bar
- baz: quux
- other:
- a: b
- c: d
- '''
- )
- include_file.name = 'include.yaml'
- builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
- config_file = io.StringIO(
- '''
- stuff: !retain
- foo: override
- other:
- a: override
- <<: !include include.yaml
- '''
- )
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = {'other.yaml'}
- assert module.load_configuration('config.yaml', config_paths) == {
- 'stuff': {'foo': 'override'},
- 'other': {'a': 'override', 'c': 'd'},
- }
- assert config_paths == {'config.yaml', '/tmp/include.yaml', 'other.yaml'}
- def test_load_configuration_with_retain_tag_but_without_merge_include_raises():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').and_return(False)
- flexmock(module.os.path).should_receive('exists').and_return(True)
- include_file = io.StringIO(
- '''
- stuff: !retain
- foo: bar
- baz: quux
- '''
- )
- include_file.name = 'include.yaml'
- builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
- config_file = io.StringIO(
- '''
- stuff:
- foo: override
- <<: !include include.yaml
- '''
- )
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = set()
- with pytest.raises(ValueError):
- module.load_configuration('config.yaml', config_paths)
- def test_load_configuration_with_retain_tag_on_scalar_raises():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').and_return(False)
- flexmock(module.os.path).should_receive('exists').and_return(True)
- include_file = io.StringIO(
- '''
- stuff:
- foo: bar
- baz: quux
- '''
- )
- include_file.name = 'include.yaml'
- builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
- config_file = io.StringIO(
- '''
- stuff:
- foo: !retain override
- <<: !include include.yaml
- '''
- )
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = set()
- with pytest.raises(ValueError):
- module.load_configuration('config.yaml', config_paths)
- def test_load_configuration_with_omit_tag_merges_include_and_omits_requested_values():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').and_return(False)
- flexmock(module.os.path).should_receive('exists').and_return(True)
- include_file = io.StringIO(
- '''
- stuff:
- - a
- - b
- - c
- '''
- )
- include_file.name = 'include.yaml'
- builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
- config_file = io.StringIO(
- '''
- stuff:
- - x
- - !omit b
- - y
- <<: !include include.yaml
- '''
- )
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = {'other.yaml'}
- assert module.load_configuration('config.yaml', config_paths) == {'stuff': ['a', 'c', 'x', 'y']}
- assert config_paths == {'config.yaml', '/tmp/include.yaml', 'other.yaml'}
- def test_load_configuration_with_omit_tag_on_unknown_value_merges_include_and_does_not_raise():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').and_return(False)
- flexmock(module.os.path).should_receive('exists').and_return(True)
- include_file = io.StringIO(
- '''
- stuff:
- - a
- - b
- - c
- '''
- )
- include_file.name = 'include.yaml'
- builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
- config_file = io.StringIO(
- '''
- stuff:
- - x
- - !omit q
- - y
- <<: !include include.yaml
- '''
- )
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = {'other.yaml'}
- assert module.load_configuration('config.yaml', config_paths) == {
- 'stuff': ['a', 'b', 'c', 'x', 'y']
- }
- assert config_paths == {'config.yaml', '/tmp/include.yaml', 'other.yaml'}
- def test_load_configuration_with_omit_tag_on_non_list_item_raises():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').and_return(False)
- flexmock(module.os.path).should_receive('exists').and_return(True)
- include_file = io.StringIO(
- '''
- stuff:
- - a
- - b
- - c
- '''
- )
- include_file.name = 'include.yaml'
- builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
- config_file = io.StringIO(
- '''
- stuff: !omit
- - x
- - y
- <<: !include include.yaml
- '''
- )
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = set()
- with pytest.raises(ValueError):
- module.load_configuration('config.yaml', config_paths)
- def test_load_configuration_with_omit_tag_on_non_scalar_list_item_raises():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').and_return(False)
- flexmock(module.os.path).should_receive('exists').and_return(True)
- include_file = io.StringIO(
- '''
- stuff:
- - foo: bar
- baz: quux
- '''
- )
- include_file.name = 'include.yaml'
- builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
- config_file = io.StringIO(
- '''
- stuff:
- - !omit foo: bar
- baz: quux
- <<: !include include.yaml
- '''
- )
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = set()
- with pytest.raises(ValueError):
- module.load_configuration('config.yaml', config_paths)
- def test_load_configuration_with_omit_tag_but_without_merge_raises():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').and_return(False)
- flexmock(module.os.path).should_receive('exists').and_return(True)
- include_file = io.StringIO(
- '''
- stuff:
- - a
- - !omit b
- - c
- '''
- )
- include_file.name = 'include.yaml'
- builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
- config_file = io.StringIO(
- '''
- stuff:
- - x
- - y
- <<: !include include.yaml
- '''
- )
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = set()
- with pytest.raises(ValueError):
- module.load_configuration('config.yaml', config_paths)
- def test_load_configuration_does_not_merge_include_list():
- builtins = flexmock(sys.modules['builtins'])
- flexmock(module.os).should_receive('getcwd').and_return('/tmp')
- flexmock(module.os.path).should_receive('isabs').and_return(False)
- flexmock(module.os.path).should_receive('exists').and_return(True)
- include_file = io.StringIO(
- '''
- - one
- - two
- '''
- )
- include_file.name = 'include.yaml'
- builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
- config_file = io.StringIO(
- '''
- foo: bar
- repositories:
- <<: !include include.yaml
- '''
- )
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- config_paths = set()
- with pytest.raises(module.ruamel.yaml.error.YAMLError):
- assert module.load_configuration('config.yaml', config_paths)
- @pytest.mark.parametrize(
- 'node_class',
- (
- module.ruamel.yaml.nodes.MappingNode,
- module.ruamel.yaml.nodes.SequenceNode,
- module.ruamel.yaml.nodes.ScalarNode,
- ),
- )
- def test_raise_retain_node_error_raises(node_class):
- with pytest.raises(ValueError):
- module.raise_retain_node_error(
- loader=flexmock(), node=node_class(tag=flexmock(), value=flexmock())
- )
- def test_raise_omit_node_error_raises():
- with pytest.raises(ValueError):
- module.raise_omit_node_error(loader=flexmock(), node=flexmock())
- def test_filter_omitted_nodes_discards_values_with_omit_tag_and_also_equal_values():
- nodes = [flexmock(), flexmock()]
- values = [
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='a'),
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='b'),
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='c'),
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='a'),
- module.ruamel.yaml.nodes.ScalarNode(tag='!omit', value='b'),
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='c'),
- ]
- result = module.filter_omitted_nodes(nodes, values)
- assert [item.value for item in result] == ['a', 'c', 'a', 'c']
- def test_filter_omitted_nodes_keeps_all_values_when_given_only_one_node():
- nodes = [flexmock()]
- values = [
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='a'),
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='b'),
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='c'),
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='a'),
- module.ruamel.yaml.nodes.ScalarNode(tag='!omit', value='b'),
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='c'),
- ]
- result = module.filter_omitted_nodes(nodes, values)
- assert [item.value for item in result] == ['a', 'b', 'c', 'a', 'b', 'c']
- def test_merge_values_combines_mapping_values():
- nodes = [
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='option'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='keep_hourly'
- ),
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:int', value='24'
- ),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='keep_daily'
- ),
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:int', value='7'),
- ),
- ],
- ),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='option'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='keep_daily'
- ),
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:int', value='25'
- ),
- ),
- ],
- ),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='option'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='keep_nanosecondly'
- ),
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:int', value='1000'
- ),
- ),
- ],
- ),
- ),
- ]
- values = module.merge_values(nodes)
- assert len(values) == 4
- assert values[0][0].value == 'keep_hourly'
- assert values[0][1].value == '24'
- assert values[1][0].value == 'keep_daily'
- assert values[1][1].value == '7'
- assert values[2][0].value == 'keep_daily'
- assert values[2][1].value == '25'
- assert values[3][0].value == 'keep_nanosecondly'
- assert values[3][1].value == '1000'
- def test_merge_values_combines_sequence_values():
- nodes = [
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='option'),
- module.ruamel.yaml.nodes.SequenceNode(
- tag='tag:yaml.org,2002:seq',
- value=[
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:int', value='1'),
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:int', value='2'),
- ],
- ),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='option'),
- module.ruamel.yaml.nodes.SequenceNode(
- tag='tag:yaml.org,2002:seq',
- value=[
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:int', value='3'),
- ],
- ),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='option'),
- module.ruamel.yaml.nodes.SequenceNode(
- tag='tag:yaml.org,2002:seq',
- value=[
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:int', value='4'),
- ],
- ),
- ),
- ]
- values = module.merge_values(nodes)
- assert len(values) == 4
- assert values[0].value == '1'
- assert values[1].value == '2'
- assert values[2].value == '3'
- assert values[3].value == '4'
- def test_deep_merge_nodes_replaces_colliding_scalar_values():
- node_values = [
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='retention'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='keep_hourly'
- ),
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:int', value='24'
- ),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='keep_daily'
- ),
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:int', value='7'),
- ),
- ],
- ),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='retention'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='keep_daily'
- ),
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:int', value='5'),
- ),
- ],
- ),
- ),
- ]
- result = module.deep_merge_nodes(node_values)
- assert len(result) == 1
- (section_key, section_value) = result[0]
- assert section_key.value == 'retention'
- options = section_value.value
- assert len(options) == 2
- assert options[0][0].value == 'keep_daily'
- assert options[0][1].value == '5'
- assert options[1][0].value == 'keep_hourly'
- assert options[1][1].value == '24'
- def test_deep_merge_nodes_keeps_non_colliding_scalar_values():
- node_values = [
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='retention'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='keep_hourly'
- ),
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:int', value='24'
- ),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='keep_daily'
- ),
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:int', value='7'),
- ),
- ],
- ),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='retention'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='keep_minutely'
- ),
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:int', value='10'
- ),
- ),
- ],
- ),
- ),
- ]
- result = module.deep_merge_nodes(node_values)
- assert len(result) == 1
- (section_key, section_value) = result[0]
- assert section_key.value == 'retention'
- options = section_value.value
- assert len(options) == 3
- assert options[0][0].value == 'keep_daily'
- assert options[0][1].value == '7'
- assert options[1][0].value == 'keep_hourly'
- assert options[1][1].value == '24'
- assert options[2][0].value == 'keep_minutely'
- assert options[2][1].value == '10'
- def test_deep_merge_nodes_keeps_deeply_nested_values():
- node_values = [
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='storage'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='lock_wait'
- ),
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:int', value='5'),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='extra_borg_options'
- ),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='init'
- ),
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='--init-option'
- ),
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='storage'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='extra_borg_options'
- ),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='prune'
- ),
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='--prune-option'
- ),
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- ]
- result = module.deep_merge_nodes(node_values)
- assert len(result) == 1
- (section_key, section_value) = result[0]
- assert section_key.value == 'storage'
- options = section_value.value
- assert len(options) == 2
- assert options[0][0].value == 'extra_borg_options'
- assert options[1][0].value == 'lock_wait'
- assert options[1][1].value == '5'
- nested_options = options[0][1].value
- assert len(nested_options) == 2
- assert nested_options[0][0].value == 'init'
- assert nested_options[0][1].value == '--init-option'
- assert nested_options[1][0].value == 'prune'
- assert nested_options[1][1].value == '--prune-option'
- def test_deep_merge_nodes_appends_colliding_sequence_values():
- node_values = [
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='hooks'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='before_backup'
- ),
- module.ruamel.yaml.nodes.SequenceNode(
- tag='tag:yaml.org,2002:seq',
- value=[
- module.ruamel.yaml.ScalarNode(
- tag='tag:yaml.org,2002:str', value='echo 1'
- ),
- module.ruamel.yaml.ScalarNode(
- tag='tag:yaml.org,2002:str', value='echo 2'
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='hooks'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='before_backup'
- ),
- module.ruamel.yaml.nodes.SequenceNode(
- tag='tag:yaml.org,2002:seq',
- value=[
- module.ruamel.yaml.ScalarNode(
- tag='tag:yaml.org,2002:str', value='echo 3'
- ),
- module.ruamel.yaml.ScalarNode(
- tag='tag:yaml.org,2002:str', value='echo 4'
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- ]
- result = module.deep_merge_nodes(node_values)
- assert len(result) == 1
- (section_key, section_value) = result[0]
- assert section_key.value == 'hooks'
- options = section_value.value
- assert len(options) == 1
- assert options[0][0].value == 'before_backup'
- assert [item.value for item in options[0][1].value] == ['echo 1', 'echo 2', 'echo 3', 'echo 4']
- def test_deep_merge_nodes_errors_on_colliding_values_of_different_types():
- node_values = [
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='hooks'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='before_backup'
- ),
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='echo oopsie daisy'
- ),
- ),
- ],
- ),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='hooks'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='before_backup'
- ),
- module.ruamel.yaml.nodes.SequenceNode(
- tag='tag:yaml.org,2002:seq',
- value=[
- module.ruamel.yaml.ScalarNode(
- tag='tag:yaml.org,2002:str', value='echo 3'
- ),
- module.ruamel.yaml.ScalarNode(
- tag='tag:yaml.org,2002:str', value='echo 4'
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- ]
- with pytest.raises(ValueError):
- module.deep_merge_nodes(node_values)
- def test_deep_merge_nodes_only_keeps_mapping_values_tagged_with_retain():
- node_values = [
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='retention'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='keep_hourly'
- ),
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:int', value='24'
- ),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='keep_daily'
- ),
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:int', value='7'),
- ),
- ],
- ),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='retention'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='!retain',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='keep_daily'
- ),
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:int', value='5'),
- ),
- ],
- ),
- ),
- ]
- result = module.deep_merge_nodes(node_values)
- assert len(result) == 1
- (section_key, section_value) = result[0]
- assert section_key.value == 'retention'
- assert section_value.tag == 'tag:yaml.org,2002:map'
- options = section_value.value
- assert len(options) == 1
- assert options[0][0].value == 'keep_daily'
- assert options[0][1].value == '5'
- def test_deep_merge_nodes_only_keeps_sequence_values_tagged_with_retain():
- node_values = [
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='hooks'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='before_backup'
- ),
- module.ruamel.yaml.nodes.SequenceNode(
- tag='tag:yaml.org,2002:seq',
- value=[
- module.ruamel.yaml.ScalarNode(
- tag='tag:yaml.org,2002:str', value='echo 1'
- ),
- module.ruamel.yaml.ScalarNode(
- tag='tag:yaml.org,2002:str', value='echo 2'
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='hooks'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='before_backup'
- ),
- module.ruamel.yaml.nodes.SequenceNode(
- tag='!retain',
- value=[
- module.ruamel.yaml.ScalarNode(
- tag='tag:yaml.org,2002:str', value='echo 3'
- ),
- module.ruamel.yaml.ScalarNode(
- tag='tag:yaml.org,2002:str', value='echo 4'
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- ]
- result = module.deep_merge_nodes(node_values)
- assert len(result) == 1
- (section_key, section_value) = result[0]
- assert section_key.value == 'hooks'
- options = section_value.value
- assert len(options) == 1
- assert options[0][0].value == 'before_backup'
- assert options[0][1].tag == 'tag:yaml.org,2002:seq'
- assert [item.value for item in options[0][1].value] == ['echo 3', 'echo 4']
- def test_deep_merge_nodes_skips_sequence_values_tagged_with_omit():
- node_values = [
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='hooks'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='before_backup'
- ),
- module.ruamel.yaml.nodes.SequenceNode(
- tag='tag:yaml.org,2002:seq',
- value=[
- module.ruamel.yaml.ScalarNode(
- tag='tag:yaml.org,2002:str', value='echo 1'
- ),
- module.ruamel.yaml.ScalarNode(
- tag='tag:yaml.org,2002:str', value='echo 2'
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- (
- module.ruamel.yaml.nodes.ScalarNode(tag='tag:yaml.org,2002:str', value='hooks'),
- module.ruamel.yaml.nodes.MappingNode(
- tag='tag:yaml.org,2002:map',
- value=[
- (
- module.ruamel.yaml.nodes.ScalarNode(
- tag='tag:yaml.org,2002:str', value='before_backup'
- ),
- module.ruamel.yaml.nodes.SequenceNode(
- tag='tag:yaml.org,2002:seq',
- value=[
- module.ruamel.yaml.ScalarNode(tag='!omit', value='echo 2'),
- module.ruamel.yaml.ScalarNode(
- tag='tag:yaml.org,2002:str', value='echo 3'
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- ]
- result = module.deep_merge_nodes(node_values)
- assert len(result) == 1
- (section_key, section_value) = result[0]
- assert section_key.value == 'hooks'
- options = section_value.value
- assert len(options) == 1
- assert options[0][0].value == 'before_backup'
- assert [item.value for item in options[0][1].value] == ['echo 1', 'echo 3']
|