Example #1
0
    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))
Example #2
0
    def test_enforce_compatibility_version_1(self):
        """Tests that required fields are never dropped.

        Never change this test. If you must break compatibility, uprev the
        serializer version and add a new test for the newer version.

        Adding a field to compat_info_str means we're making the new field
        mandatory. This breaks backwards compatibility.
        Removing a field from compat_info_str means we're no longer requiring a
        field to be mandatory. This breaks forwards compatibility.
        """
        compat_dict = {'serializer_version': 1, 'attributes': {}, 'labels': []}
        serialized_str = json.dumps(compat_dict)
        serialized_fp = cStringIO.StringIO(serialized_str)
        host_info.json_deserialize(serialized_fp)
Example #3
0
 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)
Example #4
0
 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)
Example #5
0
 def _refresh_impl_locked(self):
     """Same as _refresh_impl, but assumes relevant files are locked."""
     try:
         with open(self._store_file, 'r') as fp:
             return host_info.json_deserialize(fp)
     except IOError as e:
         if e.errno == errno.ENOENT:
             raise host_info.StoreError(
                     'No backing file. You must commit to the store before '
                     'trying to read a value from it.')
         raise host_info.StoreError('Failed to read backing file (%s) : %r'
                                    % (self._store_file, e))
     except host_info.DeserializationError as e:
         raise host_info.StoreError(
                 'Failed to desrialize backing file %s: %r' %
                 (self._store_file, e))
Example #6
0
 def test_deserialize_malformed_json_raises(self):
     """Deserializing a malformed string raises."""
     with self.assertRaises(host_info.DeserializationError):
         host_info.json_deserialize(cStringIO.StringIO('{labels:['))