def test_default_data_produces_expected_dict(self):
     default_data = funcs.default_data()
     self.assertIn(u"pid", list(default_data.keys()))
     self.assertIn(u"created", list(default_data.keys()))
     self.assertEqual(os.getpid(), default_data['pid'])
     self.assertRegexpMatches(
         default_data['created'],
         r'[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}')
 def test_default_data_produces_expected_dict(self):
     default_data = funcs.default_data()
     self.assertIn(u"pid", list(default_data.keys()))
     self.assertIn(u"created", list(default_data.keys()))
     self.assertEqual(os.getpid(), default_data['pid'])
     self.assertRegexpMatches(
         default_data['created'],
         r'[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}'
     )
    def _update(self, data):
        """Updates the DataNode objects data values.

        If the values change via the Watcher object, update the DataNode cached
        settings so that we know what the current state of Zookeeper is.

        These are updated with the true values in Zookeeper (regardless of what
        the initial object was created with) so that when the set_data()
        method is called, we only make updates to Zookeeper if necessary."""

        log.debug('[%s] Received updated data: %s' % (self._path, data))

        # If the returned 'stat' is empty, then the node was deleted in
        # Zookeeper and we should update our local 'state'.
        if not data['stat']:
            log.debug("[%s] No stat supplied, setting local state "
                      "to false." % self._path)
            self._state = False

        # Quickly check that if data['data'] is none, we just clear our
        # settings and jump out of this method.
        if not data['data']:
            log.error("[%s] No data supplied at all. Wiping out "
                      "local data cache." % self._path)
            self._encoded_data = funcs.encode(None)
            self._decoded_data = None
            self._data = None
            return

        # First, store the directly supplied data as our _decoded_data
        # (string), and then decode that into a proper hash and store it
        # as our _encoded_data.
        self._encoded_data = funcs.encode(data['data'])
        self._decoded_data = dict(data['data'])

        # Strictly speaking, the self._data object should contain the user
        # supplied data object, without any of the additional data
        # automatically supplied by funcs.default_data(). If this DataNode
        # object receives an updated bunch of data from Zookeeper, it will
        # include these additional data parameters. We need to strip those
        # out first, to ensure that we're only storing the user-supplied
        # parameters.
        self._data = dict(data['data'])
        for k in funcs.default_data().keys():
            self._data.pop(k, None)

        # If the only key left in the self._data object is 'string_value',
        # then the supplied user data was actually in string format -- so
        # thats actually what we want to save.
        if self._data.keys() == ['string_value']:
            self._data = self._data['string_value']
Example #4
0
    def _update(self, data):
        """Updates the DataNode objects data values.

        If the values change via the Watcher object, update the DataNode cached
        settings so that we know what the current state of Zookeeper is.

        These are updated with the true values in Zookeeper (regardless of what
        the initial object was created with) so that when the set_data()
        method is called, we only make updates to Zookeeper if necessary."""

        log.debug('[%s] Received updated data: %s' % (self._path, data))

        # If the returned 'stat' is empty, then the node was deleted in
        # Zookeeper and we should update our local 'state'.
        if not data['stat']:
            log.debug("[%s] No stat supplied, setting local state "
                      "to false." % self._path)
            self._state = False

        # Quickly check that if data['data'] is none, we just clear our
        # settings and jump out of this method.
        if not data['data']:
            log.error("[%s] No data supplied at all. Wiping out "
                      "local data cache." % self._path)
            self._encoded_data = funcs.encode(None)
            self._decoded_data = None
            self._data = None
            return

        # First, store the directly supplied data as our _decoded_data
        # (string), and then decode that into a proper hash and store it
        # as our _encoded_data.
        self._encoded_data = funcs.encode(data['data'])
        self._decoded_data = dict(data['data'])

        # Strictly speaking, the self._data object should contain the user
        # supplied data object, without any of the additional data
        # automatically supplied by funcs.default_data(). If this DataNode
        # object receives an updated bunch of data from Zookeeper, it will
        # include these additional data parameters. We need to strip those
        # out first, to ensure that we're only storing the user-supplied
        # parameters.
        self._data = dict(data['data'])
        for k in funcs.default_data().keys():
            self._data.pop(k, None)

        # If the only key left in the self._data object is 'string_value',
        # then the supplied user data was actually in string format -- so
        # thats actually what we want to save.
        if self._data.keys() == ['string_value']:
            self._data = self._data['string_value']