def test_round_trip_empty(self): """Serializing - deserializing empty HostInfo keeps it unchanged.""" info = host_info.HostInfo() serialized_fp = cStringIO.StringIO() host_info.json_serialize(info, serialized_fp) serialized_fp.seek(0) got = host_info.json_deserialize(serialized_fp) self.assertEqual(got, info)
def _commit_impl_locked(self, info): """Same as _commit_impl, but assumes relevant files are locked.""" try: with open(self._store_file, 'w') as fp: host_info.json_serialize(info, fp) except IOError as e: raise host_info.StoreError('Failed to write backing file (%s) : %r' % (self._store_file, e))
def test_round_trip_non_empty(self): """Serializing - deserializing non-empty HostInfo keeps it unchanged.""" info = host_info.HostInfo(labels=['label1'], attributes={'attrib': 'val'}) serialized_fp = cStringIO.StringIO() host_info.json_serialize(info, serialized_fp) serialized_fp.seek(0) got = host_info.json_deserialize(serialized_fp) self.assertEqual(got, info)
def test_serialize_empty(self): """Serializing empty HostInfo results in the expected json.""" info = host_info.HostInfo() file_obj = cStringIO.StringIO() host_info.json_serialize(info, file_obj) file_obj.seek(0) expected_dict = { 'serializer_version': self.CURRENT_SERIALIZATION_VERSION, 'attributes': {}, 'labels': [], } self.assertEqual(json.load(file_obj), expected_dict)
def test_deserialize_malformed_host_info_raises(self): """Deserializing a malformed host_info raises.""" info = host_info.HostInfo() serialized_fp = cStringIO.StringIO() host_info.json_serialize(info, serialized_fp) serialized_fp.seek(0) serialized_dict = json.load(serialized_fp) del serialized_dict['labels'] serialized_no_version_str = json.dumps(serialized_dict) with self.assertRaises(host_info.DeserializationError): host_info.json_deserialize( cStringIO.StringIO(serialized_no_version_str))
def test_serialize_non_empty(self): """Serializing a populated HostInfo results in expected json.""" info = host_info.HostInfo(labels=['label1'], attributes={'attrib': 'val'}) file_obj = cStringIO.StringIO() host_info.json_serialize(info, file_obj) file_obj.seek(0) expected_dict = { 'serializer_version': self.CURRENT_SERIALIZATION_VERSION, 'attributes': { 'attrib': 'val' }, 'labels': ['label1'], } self.assertEqual(json.load(file_obj), expected_dict)
def test_serialize_pretty_print(self): """Serializing a host_info dumps the json in human-friendly format""" info = host_info.HostInfo(labels=['label1'], attributes={'attrib': 'val'}) serialized_fp = cStringIO.StringIO() host_info.json_serialize(info, serialized_fp) expected = """{ "attributes": { "attrib": "val" }, "labels": [ "label1" ], "serializer_version": %d }""" % self.CURRENT_SERIALIZATION_VERSION self.assertEqual(serialized_fp.getvalue(), inspect.cleandoc(expected))