def test_recursive_dump_load_with_identical_non_recursive_types(self): """ If identical objects are nested anywhere, they should not be marked recursive unless they're one of the types we iterate over. """ payload = salt.payload.Serial("msgpack") repeating = "repeating element" data = { "a": "a", # Test CPython implementation detail. Short "b": "a", # strings are interned. "c": 13, # So are small numbers. "d": 13, "fnord": repeating, # Let's go for broke and make a crazy nested structure "repeating": [ [[[[{"one": repeating, "two": repeating}], repeating, 13, "a"]]], repeating, repeating, repeating, ], } # We need a nested dictionary to trigger the exception data["repeating"][0][0][0].append(data) # If we don't deepcopy the data it gets mutated sdata = payload.dumps(copy.deepcopy(data)) odata = payload.loads(sdata) # Delete the recursive piece - it's served its purpose, and our # other test tests that it's actually marked as recursive. del odata["repeating"][0][0][0][-1], data["repeating"][0][0][0][-1] self.assertDictEqual(odata, data)
def test_mixed_dump_load(self): ''' Test we can handle all exceptions at once ''' payload = salt.payload.Serial('msgpack') dtvalue = datetime.datetime(2001, 2, 3, 4, 5, 6, 7) od = OrderedDict() od['a'] = 'b' od['y'] = 'z' od['j'] = 'k' od['w'] = 'x' idata = { dtvalue: dtvalue, # datetime 'jid': 20180227140750302662, # long int 'dict': immutabletypes.ImmutableDict({'key': 'value'}), # immutable dict 'list': immutabletypes.ImmutableList([1, 2, 3]), # immutable list 'set': immutabletypes.ImmutableSet( ('red', 'green', 'blue')), # immutable set 'odict': od, # odict } edata = { dtvalue: dtvalue, # datetime, == input 'jid': '20180227140750302662', # string repr of long int 'dict': { 'key': 'value' }, # builtin dict 'list': [1, 2, 3], # builtin list 'set': ['red', 'green', 'blue'], # builtin set 'odict': dict(od), # builtin dict } sdata = payload.dumps(idata) odata = payload.loads(sdata) self.assertEqual(edata, odata)
def test_mixed_dump_load(self): """ Test we can handle all exceptions at once """ payload = salt.payload.Serial("msgpack") dtvalue = datetime.datetime(2001, 2, 3, 4, 5, 6, 7) od = OrderedDict() od["a"] = "b" od["y"] = "z" od["j"] = "k" od["w"] = "x" idata = { dtvalue: dtvalue, # datetime "jid": 20180227140750302662, # long int "dict": immutabletypes.ImmutableDict({"key": "value"}), # immutable dict "list": immutabletypes.ImmutableList([1, 2, 3]), # immutable list "set": immutabletypes.ImmutableSet( ("red", "green", "blue")), # immutable set "odict": od, # odict } edata = { dtvalue: dtvalue, # datetime, == input "jid": "20180227140750302662", # string repr of long int "dict": { "key": "value" }, # builtin dict "list": [1, 2, 3], # builtin list "set": ["red", "green", "blue"], # builtin set "odict": dict(od), # builtin dict } sdata = payload.dumps(idata) odata = payload.loads(sdata) self.assertEqual(edata, odata)
def test_immutable_list_dump_load(self): ''' Test immutable list encoder/decoder ''' payload = salt.payload.Serial('msgpack') idata = {'list': [1, 2, 3]} sdata = payload.dumps({'list': immutabletypes.ImmutableList(idata['list'])}) odata = payload.loads(sdata) self.assertEqual(idata, odata)
def test_immutable_dict_dump_load(self): ''' Test immutable dict encoder/decoder ''' payload = salt.payload.Serial('msgpack') idata = {'dict': {'key': 'value'}} sdata = payload.dumps({'dict': immutabletypes.ImmutableDict(idata['dict'])}) odata = payload.loads(sdata) self.assertEqual(idata, odata)
def test_immutable_set_dump_load(self): ''' Test immutable set encoder/decoder ''' payload = salt.payload.Serial('msgpack') idata = {'set': ['red', 'green', 'blue']} sdata = payload.dumps({'set': immutabletypes.ImmutableSet(idata['set'])}) odata = payload.loads(sdata) self.assertEqual(idata, odata)
def test_immutable_set_dump_load(self): """ Test immutable set encoder/decoder """ payload = salt.payload.Serial("msgpack") idata = {"set": ["red", "green", "blue"]} sdata = payload.dumps({"set": immutabletypes.ImmutableSet(idata["set"])}) odata = payload.loads(sdata) self.assertEqual(idata, odata)
def test_immutable_list_dump_load(self): """ Test immutable list encoder/decoder """ payload = salt.payload.Serial("msgpack") idata = {"list": [1, 2, 3]} sdata = payload.dumps({"list": immutabletypes.ImmutableList(idata["list"])}) odata = payload.loads(sdata) self.assertEqual(idata, odata)
def test_immutable_dict_dump_load(self): """ Test immutable dict encoder/decoder """ payload = salt.payload.Serial("msgpack") idata = {"dict": {"key": "value"}} sdata = payload.dumps({"dict": immutabletypes.ImmutableDict(idata["dict"])}) odata = payload.loads(sdata) self.assertEqual(idata, odata)
def test_list_nested_odicts(self): with patch('msgpack.version', (0, 1, 13)): msgpack.dumps = MockWraps(msgpack.dumps, 1, TypeError('ODict TypeError Forced')) payload = salt.payload.Serial('msgpack') idata = {'pillar': [OrderedDict(environment='dev')]} odata = payload.loads(payload.dumps(idata.copy())) self.assertNoOrderedDict(odata) self.assertEqual(idata, odata)
def test_verylong_dump_load(self): ''' Test verylong encoder/decoder ''' payload = salt.payload.Serial('msgpack') idata = {'jid': 20180227140750302662} sdata = payload.dumps(idata.copy()) odata = payload.loads(sdata) idata['jid'] = '{0}'.format(idata['jid']) self.assertEqual(idata, odata)
def test_list_nested_odicts(self): with patch('msgpack.version', (0, 1, 13)): msgpack.dumps = MockWraps( msgpack.dumps, 1, TypeError('ODict TypeError Forced') ) payload = salt.payload.Serial('msgpack') idata = {'pillar': [OrderedDict(environment='dev')]} odata = payload.loads(payload.dumps(idata.copy())) self.assertNoOrderedDict(odata) self.assertEqual(idata, odata)
def test_recursive_dump_load(self): ''' Test recursive payloads are (mostly) serialized ''' payload = salt.payload.Serial('msgpack') data = {'name': 'roscivs'} data['data'] = data # Data all the things! sdata = payload.dumps(data) odata = payload.loads(sdata) self.assertTrue('recursion' in odata['data'].lower())
def test_verylong_dump_load(self): """ Test verylong encoder/decoder """ payload = salt.payload.Serial("msgpack") idata = {"jid": 20180227140750302662} sdata = payload.dumps(idata.copy()) odata = payload.loads(sdata) idata["jid"] = "{}".format(idata["jid"]) self.assertEqual(idata, odata)
def test_recursive_dump_load(self): """ Test recursive payloads are (mostly) serialized """ payload = salt.payload.Serial("msgpack") data = {"name": "roscivs"} data["data"] = data # Data all the things! sdata = payload.dumps(data) odata = payload.loads(sdata) self.assertTrue("recursion" in odata["data"].lower())
def test_raw_vs_encoding_utf8(self): """ Test that we handle the new raw parameter in 5.0.2 correctly based on encoding. When encoding is utf-8 loads should return unicode """ payload = salt.payload.Serial("msgpack") dtvalue = datetime.datetime(2001, 2, 3, 4, 5, 6, 7) idata = {dtvalue: "strval"} sdata = payload.dumps(idata.copy()) odata = payload.loads(sdata, encoding="utf-8") assert isinstance(odata[dtvalue], str)
def test_raw_vs_encoding_utf8(self): ''' Test that we handle the new raw parameter in 5.0.2 correctly based on encoding. When encoding is utf-8 loads should return unicode ''' payload = salt.payload.Serial('msgpack') dtvalue = datetime.datetime(2001, 2, 3, 4, 5, 6, 7) idata = {dtvalue: 'strval'} sdata = payload.dumps(idata.copy()) odata = payload.loads(sdata, encoding='utf-8') assert isinstance(odata[dtvalue], six.text_type)
def test_datetime_dump_load(self): ''' Check the custom datetime handler can understand itself ''' payload = salt.payload.Serial('msgpack') dtvalue = datetime.datetime(2001, 2, 3, 4, 5, 6, 7) idata = {dtvalue: dtvalue} sdata = payload.dumps(idata.copy()) odata = payload.loads(sdata) self.assertEqual( sdata, b'\x81\xc7\x18N20010203T04:05:06.000007\xc7\x18N20010203T04:05:06.000007') self.assertEqual(idata, odata)
def test_odict_dump_load(self): ''' Test odict just works. It wasn't until msgpack 0.2.0 ''' payload = salt.payload.Serial('msgpack') data = OrderedDict() data['a'] = 'b' data['y'] = 'z' data['j'] = 'k' data['w'] = 'x' sdata = payload.dumps({'set': data}) odata = payload.loads(sdata) self.assertEqual({'set': dict(data)}, odata)
def test_odict_dump_load(self): """ Test odict just works. It wasn't until msgpack 0.2.0 """ payload = salt.payload.Serial("msgpack") data = OrderedDict() data["a"] = "b" data["y"] = "z" data["j"] = "k" data["w"] = "x" sdata = payload.dumps({"set": data}) odata = payload.loads(sdata) self.assertEqual({"set": dict(data)}, odata)
def test_recursive_dump_load_with_identical_non_recursive_types(self): ''' If identical objects are nested anywhere, they should not be marked recursive unless they're one of the types we iterate over. ''' payload = salt.payload.Serial('msgpack') repeating = 'repeating element' data = { 'a': 'a', # Test CPython implementation detail. Short 'b': 'a', # strings are interned. 'c': 13, # So are small numbers. 'd': 13, 'fnord': repeating, # Let's go for broke and make a crazy nested structure 'repeating': [ [[[ [{ 'one': repeating, 'two': repeating }], repeating, 13, 'a', ]]], repeating, repeating, repeating, ], } # We need a nested dictionary to trigger the exception data['repeating'][0][0][0].append(data) # If we don't deepcopy the data it gets mutated sdata = payload.dumps(copy.deepcopy(data)) odata = payload.loads(sdata) # Delete the recursive piece - it's served its purpose, and our # other test tests that it's actually marked as recursive. del odata['repeating'][0][0][0][-1], data['repeating'][0][0][0][-1] self.assertDictEqual(odata, data)
def test_list_nested_odicts(self): payload = salt.payload.Serial('msgpack') idata = {'pillar': [OrderedDict(environment='dev')]} odata = payload.loads(payload.dumps(idata.copy())) self.assertNoOrderedDict(odata) self.assertEqual(idata, odata)