1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129 |
- 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)
- assert module.load_configuration('config.yaml') == {'key': 'value'}
- def test_load_configuration_replaces_constants():
- builtins = flexmock(sys.modules['builtins'])
- config_file = io.StringIO(
- '''
- constants:
- key: value
- key: {key}
- '''
- )
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- assert module.load_configuration('config.yaml') == {'key': 'value'}
- def test_load_configuration_replaces_complex_constants():
- builtins = flexmock(sys.modules['builtins'])
- config_file = io.StringIO(
- '''
- constants:
- key:
- subkey: value
- key: {key}
- '''
- )
- config_file.name = 'config.yaml'
- builtins.should_receive('open').with_args('config.yaml').and_return(config_file)
- assert module.load_configuration('config.yaml') == {'key': {'subkey': 'value'}}
- 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)
- assert module.load_configuration('config.yaml') == 33
- 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)
- assert module.load_configuration('config.yaml') == {'key': 'value'}
- 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)
- assert module.load_configuration('/etc/config.yaml') == {'key': 'value'}
- 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)
- with pytest.raises(FileNotFoundError):
- module.load_configuration('/etc/config.yaml')
- 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)
- assert module.load_configuration('config.yaml') == {'key': 'value'}
- 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)
- with pytest.raises(FileNotFoundError):
- assert module.load_configuration('config.yaml')
- 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)
- assert module.load_configuration('config.yaml') == {'key': ['value2', 'value1']}
- 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)
- with pytest.raises(ValueError):
- module.load_configuration('config.yaml')
- 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)
- assert module.load_configuration('config.yaml') == {'foo': 'override', 'baz': 'quux'}
- 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)
- assert module.load_configuration('config.yaml') == {
- 'foo': 'override',
- 'baz': 'second',
- 'original': 'yes',
- }
- 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)
- assert module.load_configuration('config.yaml') == {
- 'stuff': {'foo': 'override'},
- 'other': {'a': 'override', 'c': 'd'},
- }
- 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)
- with pytest.raises(ValueError):
- module.load_configuration('config.yaml')
- 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)
- with pytest.raises(ValueError):
- module.load_configuration('config.yaml')
- 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)
- assert module.load_configuration('config.yaml') == {'stuff': ['a', 'c', 'x', 'y']}
- 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)
- assert module.load_configuration('config.yaml') == {'stuff': ['a', 'b', 'c', 'x', 'y']}
- 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)
- with pytest.raises(ValueError):
- module.load_configuration('config.yaml')
- 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)
- with pytest.raises(ValueError):
- module.load_configuration('config.yaml')
- 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)
- with pytest.raises(ValueError):
- module.load_configuration('config.yaml')
- 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)
- with pytest.raises(module.ruamel.yaml.error.YAMLError):
- assert module.load_configuration('config.yaml')
- @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']
|