def test_sls_micking_file_merging(self): def convert(obj): return yamlex.deserialize(yamlex.serialize(obj)) # let say that we have 2 pillar files src1 = dedent(""" a: first b: !aggregate first c: subkey1: first subkey2: !aggregate first """).strip() src2 = dedent(""" a: second b: !aggregate second c: subkey2: !aggregate second subkey3: second """).strip() sls_obj1 = yamlex.deserialize(src1) sls_obj2 = yamlex.deserialize(src2) sls_obj3 = yamlex.merge_recursive(sls_obj1, sls_obj2) assert sls_obj3 == { 'a': 'second', 'b': ['first', 'second'], 'c': { 'subkey2': ['first', 'second'], 'subkey3': 'second' } }, sls_obj3
def test_sls_micking_file_merging(self): def convert(obj): return yamlex.deserialize(yamlex.serialize(obj)) # let say that we have 2 pillar files src1 = dedent( """ a: first b: !aggregate first c: subkey1: first subkey2: !aggregate first """ ).strip() src2 = dedent( """ a: second b: !aggregate second c: subkey2: !aggregate second subkey3: second """ ).strip() sls_obj1 = yamlex.deserialize(src1) sls_obj2 = yamlex.deserialize(src2) sls_obj3 = yamlex.merge_recursive(sls_obj1, sls_obj2) assert sls_obj3 == { "a": "second", "b": ["first", "second"], "c": {"subkey2": ["first", "second"], "subkey3": "second"}, }, sls_obj3
def test_compare_sls_vs_yaml_with_jinja(self): tpl = '{{ data }}' env = jinja2.Environment() src = '{foo: 1, bar: 2, baz: {qux: true}}' sls_src = env.from_string(tpl).render(data=yamlex.deserialize(src)) yml_src = env.from_string(tpl).render(data=yaml.deserialize(src)) sls_data = yamlex.deserialize(sls_src) yml_data = yaml.deserialize(yml_src) # ensure that sls & yaml have the same base assert isinstance(sls_data, dict) assert isinstance(yml_data, dict) # The below has been commented out because something the loader test # is modifying the yaml renderer to render things to unicode. Without # running the loader test, the below passes. Even reloading the module # from disk does not reset its internal state (per the Python docs). ## #assert sls_data == yml_data # ensure that sls is ordered, while yaml not assert isinstance(sls_data, OrderedDict) assert not isinstance(yml_data, OrderedDict) # prove that yaml does not handle well with OrderedDict # while sls is jinja friendly. obj = OrderedDict([ ('foo', 1), ('bar', 2), ('baz', {'qux': True}) ]) sls_obj = yamlex.deserialize(yamlex.serialize(obj)) try: yml_obj = yaml.deserialize(yaml.serialize(obj)) except SerializationError: # BLAAM! yaml was unable to serialize OrderedDict, # but it's not the purpose of the current test. yml_obj = obj.copy() sls_src = env.from_string(tpl).render(data=sls_obj) yml_src = env.from_string(tpl).render(data=yml_obj) final_obj = yaml.deserialize(sls_src) assert obj == final_obj # BLAAM! yml_src is not valid ! final_obj = OrderedDict(yaml.deserialize(yml_src)) assert obj != final_obj
def test_serialize_sls(self): data = {"foo": "bar"} serialized = yamlex.serialize(data) assert serialized == '{foo: bar}', serialized deserialized = yamlex.deserialize(serialized) assert deserialized == data, deserialized
def test_compare_sls_vs_yaml_with_jinja(self): tpl = '{{ data }}' env = jinja2.Environment() src = '{foo: 1, bar: 2, baz: {qux: true}}' sls_src = env.from_string(tpl).render(data=yamlex.deserialize(src)) yml_src = env.from_string(tpl).render(data=yaml.deserialize(src)) sls_data = yamlex.deserialize(sls_src) yml_data = yaml.deserialize(yml_src) # ensure that sls & yaml have the same base assert isinstance(sls_data, dict) assert isinstance(yml_data, dict) assert sls_data == yml_data # ensure that sls is ordered, while yaml not assert isinstance(sls_data, OrderedDict) assert not isinstance(yml_data, OrderedDict) # prove that yaml does not handle well with OrderedDict # while sls is jinja friendly. obj = OrderedDict([ ('foo', 1), ('bar', 2), ('baz', {'qux': True}) ]) sls_obj = yamlex.deserialize(yamlex.serialize(obj)) try: yml_obj = yaml.deserialize(yaml.serialize(obj)) except SerializationError: # BLAAM! yaml was unable to serialize OrderedDict, # but it's not the purpose of the current test. yml_obj = obj.copy() sls_src = env.from_string(tpl).render(data=sls_obj) yml_src = env.from_string(tpl).render(data=yml_obj) final_obj = yaml.deserialize(sls_src) assert obj == final_obj # BLAAM! yml_src is not valid ! final_obj = OrderedDict(yaml.deserialize(yml_src)) assert obj != final_obj
def test_serialize_sls(self): data = { "foo": "bar" } serialized = yamlex.serialize(data) assert serialized == '{foo: bar}', serialized deserialized = yamlex.deserialize(serialized) assert deserialized == data, deserialized
def test_sls_reset(self): src = dedent(""" placeholder: {!aggregate foo: {foo: 42}} placeholder: {!aggregate foo: {bar: null}} !reset placeholder: {!aggregate foo: {baz: inga}} """).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == {'placeholder': {'foo': {'baz': 'inga'}}}, sls_obj
def test_sls_reset(self): src = dedent(""" placeholder: {!aggregate foo: {foo: 42}} placeholder: {!aggregate foo: {bar: null}} !reset placeholder: {!aggregate foo: {baz: inga}} """).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == {"placeholder": {"foo": {"baz": "inga"}}}, sls_obj
def test_serialize_complex_sls(self): data = OrderedDict([ ("foo", 1), ("bar", 2), ("baz", True), ]) serialized = yamlex.serialize(data) assert serialized == '{foo: 1, bar: 2, baz: true}', serialized deserialized = yamlex.deserialize(serialized) assert deserialized == data, deserialized
def ext_pillar(minion_id, # pylint: disable=W0613 pillar, # pylint: disable=W0613 command): ''' Execute a command and read the output as YAMLEX ''' try: command = command.replace('%s', minion_id) return deserialize(__salt__['cmd.run']('{0}'.format(command))) except Exception: log.critical('YAML data from %s failed to parse', command) return {}
def ext_pillar( minion_id, pillar, command # pylint: disable=W0613 # pylint: disable=W0613 ): """ Execute a command and read the output as YAMLEX """ try: command = command.replace("%s", minion_id) return deserialize(__salt__["cmd.run"]("{0}".format(command))) except Exception: # pylint: disable=broad-except log.critical("YAML data from %s failed to parse", command) return {}
def test_serialize_complex_sls(): data = OrderedDict([("foo", 1), ("bar", 2), ("baz", True)]) serialized = yamlex.serialize(data) assert serialized == "{foo: 1, bar: 2, baz: true}", serialized deserialized = yamlex.deserialize(serialized) assert deserialized == data, deserialized serialized = yaml.serialize(data) assert serialized == "{bar: 2, baz: true, foo: 1}", serialized deserialized = yaml.deserialize(serialized) assert deserialized == data, deserialized
def test_compare_sls_vs_yaml(self): src = '{foo: 1, bar: 2, baz: {qux: true}}' sls_data = yamlex.deserialize(src) yml_data = yaml.deserialize(src) # ensure that sls & yaml have the same base assert isinstance(sls_data, dict) assert isinstance(yml_data, dict) assert sls_data == yml_data # ensure that sls is ordered, while yaml not assert isinstance(sls_data, OrderedDict) assert not isinstance(yml_data, OrderedDict)
def ext_pillar(minion_id, # pylint: disable=W0613 pillar, # pylint: disable=W0613 command): ''' Execute a command and read the output as YAMLEX ''' try: command = command.replace('%s', minion_id) return deserialize(__salt__['cmd.run']('{0}'.format(command))) except Exception: log.critical( 'YAML data from {0} failed to parse'.format(command) ) return {}
def test_sls_reset(self): src = dedent(""" placeholder: {!aggregate foo: {foo: 42}} placeholder: {!aggregate foo: {bar: null}} !reset placeholder: {!aggregate foo: {baz: inga}} """).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == { 'placeholder': { 'foo': { 'baz': 'inga' } } }, sls_obj
def render(sls_data, saltenv='base', sls='', **kws): ''' Accepts YAML_EX as a string or as a file object and runs it through the YAML_EX parser. :rtype: A Python data structure ''' with warnings.catch_warnings(record=True) as warn_list: data = deserialize(sls_data) or {} for item in warn_list: log.warn('{warn} found in {sls} environment={env}'.format( warn=item.message, sls=salt.utils.url.create(sls), env=saltenv)) log.debug('Results of SLS rendering: \n{0}'.format(data)) return data
def render(sls_data, saltenv='base', sls='', **kws): ''' Accepts YAML_EX as a string or as a file object and runs it through the YAML_EX parser. :rtype: A Python data structure ''' with warnings.catch_warnings(record=True) as warn_list: data = deserialize(sls_data) or {} for item in warn_list: log.warning( '%s found in %s saltenv=%s', item.message, salt.utils.url.create(sls), saltenv ) log.debug('Results of SLS rendering: \n%s', data) return data
def render(sls_data, saltenv='base', sls='', **kws): ''' Accepts YAML_EX as a string or as a file object and runs it through the YAML_EX parser. :rtype: A Python data structure ''' with warnings.catch_warnings(record=True) as warn_list: data = deserialize(sls_data) or {} for item in warn_list: log.warning( '{warn} found in {sls} saltenv={env}'.format( warn=item.message, sls=salt.utils.url.create(sls), env=saltenv ) ) log.debug('Results of SLS rendering: \n{0}'.format(data)) return data
def test_serialize_sls(self): data = {"foo": "bar"} serialized = yamlex.serialize(data) assert serialized == "{foo: bar}", serialized serialized = yamlex.serialize(data, default_flow_style=False) assert serialized == "foo: bar", serialized deserialized = yamlex.deserialize(serialized) assert deserialized == data, deserialized serialized = yaml.serialize(data) assert serialized == "{foo: bar}", serialized deserialized = yaml.deserialize(serialized) assert deserialized == data, deserialized serialized = yaml.serialize(data, default_flow_style=False) assert serialized == "foo: bar", serialized deserialized = yaml.deserialize(serialized) assert deserialized == data, deserialized
def test_sls_aggregate(self): src = dedent(""" a: lol foo: !aggregate hello bar: !aggregate [1, 2, 3] baz: !aggregate a: 42 b: 666 c: the beast """).strip() # test that !aggregate is correctly parsed sls_obj = yamlex.deserialize(src) assert sls_obj == { 'a': 'lol', 'foo': ['hello'], 'bar': [1, 2, 3], 'baz': { 'a': 42, 'b': 666, 'c': 'the beast' } }, sls_obj assert dedent(""" a: lol foo: [hello] bar: [1, 2, 3] baz: {a: 42, b: 666, c: the beast} """).strip() == yamlex.serialize(sls_obj), sls_obj # test that !aggregate aggregates scalars src = dedent(""" placeholder: !aggregate foo placeholder: !aggregate bar placeholder: !aggregate baz """).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == {'placeholder': ['foo', 'bar', 'baz']}, sls_obj # test that !aggregate aggregates lists src = dedent(""" placeholder: !aggregate foo placeholder: !aggregate [bar, baz] placeholder: !aggregate [] placeholder: !aggregate ~ """).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == {'placeholder': ['foo', 'bar', 'baz']}, sls_obj # test that !aggregate aggregates dicts src = dedent(""" placeholder: !aggregate {foo: 42} placeholder: !aggregate {bar: null} placeholder: !aggregate {baz: inga} """).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == { 'placeholder': { 'foo': 42, 'bar': None, 'baz': 'inga' } }, sls_obj # test that !aggregate aggregates deep dicts src = dedent(""" placeholder: {foo: !aggregate {foo: 42}} placeholder: {foo: !aggregate {bar: null}} placeholder: {foo: !aggregate {baz: inga}} """).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == { 'placeholder': { 'foo': { 'foo': 42, 'bar': None, 'baz': 'inga' } } }, sls_obj # test that {foo: !aggregate bar} and {!aggregate foo: bar} # are roughly equivalent. src = dedent(""" placeholder: {!aggregate foo: {foo: 42}} placeholder: {!aggregate foo: {bar: null}} placeholder: {!aggregate foo: {baz: inga}} """).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == { 'placeholder': { 'foo': { 'foo': 42, 'bar': None, 'baz': 'inga' } } }, sls_obj
def convert(obj): return yamlex.deserialize(yamlex.serialize(obj))
def test_sls_aggregate(self): src = dedent( """ a: lol foo: !aggregate hello bar: !aggregate [1, 2, 3] baz: !aggregate a: 42 b: 666 c: the beast """ ).strip() # test that !aggregate is correctly parsed sls_obj = yamlex.deserialize(src) assert sls_obj == { "a": "lol", "foo": ["hello"], "bar": [1, 2, 3], "baz": {"a": 42, "b": 666, "c": "the beast"}, }, sls_obj assert ( dedent( """ a: lol foo: [hello] bar: [1, 2, 3] baz: {a: 42, b: 666, c: the beast} """ ).strip() == yamlex.serialize(sls_obj) ), sls_obj # test that !aggregate aggregates scalars src = dedent( """ placeholder: !aggregate foo placeholder: !aggregate bar placeholder: !aggregate baz """ ).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == {"placeholder": ["foo", "bar", "baz"]}, sls_obj # test that !aggregate aggregates lists src = dedent( """ placeholder: !aggregate foo placeholder: !aggregate [bar, baz] placeholder: !aggregate [] placeholder: !aggregate ~ """ ).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == {"placeholder": ["foo", "bar", "baz"]}, sls_obj # test that !aggregate aggregates dicts src = dedent( """ placeholder: !aggregate {foo: 42} placeholder: !aggregate {bar: null} placeholder: !aggregate {baz: inga} """ ).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == { "placeholder": {"foo": 42, "bar": None, "baz": "inga"} }, sls_obj # test that !aggregate aggregates deep dicts src = dedent( """ placeholder: {foo: !aggregate {foo: 42}} placeholder: {foo: !aggregate {bar: null}} placeholder: {foo: !aggregate {baz: inga}} """ ).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == { "placeholder": {"foo": {"foo": 42, "bar": None, "baz": "inga"}} }, sls_obj # test that {foo: !aggregate bar} and {!aggregate foo: bar} # are roughly equivalent. src = dedent( """ placeholder: {!aggregate foo: {foo: 42}} placeholder: {!aggregate foo: {bar: null}} placeholder: {!aggregate foo: {baz: inga}} """ ).strip() sls_obj = yamlex.deserialize(src) assert sls_obj == { "placeholder": {"foo": {"foo": 42, "bar": None, "baz": "inga"}} }, sls_obj