Esempio n. 1
0
    def test_set_cache_redis_failure(
        self, geoip_db, http_session, raven, session, source, metricsmock
    ):
        cell = CellShardFactory.build()
        mock_redis_client = _mock_redis_client()
        mock_redis_client.mget.return_value = []
        mock_redis_client.mset.side_effect = RedisError()
        mock_redis_client.expire.side_effect = RedisError()
        mock_redis_client.execute.side_effect = RedisError()

        with requests_mock.Mocker() as mock_request:
            mock_request.register_uri(
                "POST", requests_mock.ANY, json=self.fallback_result
            )

            with mock.patch.object(
                source.caches[DEFAULT_SCHEMA], "redis_client", mock_redis_client
            ):
                query = self.model_query(geoip_db, http_session, session, cells=[cell])
                results = source.search(query)
                self.check_model_results(results, [self.fallback_model])

            assert mock_redis_client.mget.called
            assert mock_redis_client.mset.called
            assert mock_request.called

        raven.check([("RedisError", 1)])
        assert metricsmock.has_record(
            "incr",
            "locate.fallback.cache",
            value=1,
            tags=[self.fallback_tag, "status:miss"],
        )
Esempio n. 2
0
    def test_set_cache_redis_failure(self, geoip_db, http_session, raven,
                                     session, source, stats):
        cell = CellShardFactory.build()
        mock_redis_client = self._mock_redis_client()
        mock_redis_client.mget.return_value = []
        mock_redis_client.mset.side_effect = RedisError()
        mock_redis_client.expire.side_effect = RedisError()
        mock_redis_client.execute.side_effect = RedisError()

        with requests_mock.Mocker() as mock_request:
            mock_request.register_uri('POST',
                                      requests_mock.ANY,
                                      json=self.fallback_result)

            with mock.patch.object(source.cache, 'redis_client',
                                   mock_redis_client):
                query = self.model_query(geoip_db,
                                         http_session,
                                         session,
                                         stats,
                                         cells=[cell])
                results = source.search(query)
                self.check_model_results(results, [self.fallback_model])

            assert mock_redis_client.mget.called
            assert mock_redis_client.mset.called
            assert mock_request.called

        raven.check([('RedisError', 1)])
        stats.check(counter=[
            ('locate.fallback.cache', ['status:miss']),
        ])
Esempio n. 3
0
 def multi(self):
     if self.explicit_transaction:
         raise RedisError('Cannot issue nested calls to MULTI')
     if self.command_stack:
         raise RedisError('Commands without an initial WATCH have already '
                          'been issued')
     self.explicit_transaction = False
    def test_healthcheck_redis_error(self, fake_ldap3, fake_strict_redis):
        """Catching an RedisError is how we determine if the Redis server is offline"""
        fake_strict_redis.side_effect = RedisError('testing')
        resp = self.app.get('/api/1/auth/healthcheck')
        expected = 500

        self.assertEqual(resp.status_code, expected)
Esempio n. 5
0
    def test_get_redis_down(self, fake_logger, fake_strict_redis):
        """GET on /api/1/auth/token returns 503 when RedisError is encounted"""
        fake_strict_redis.side_effect = RedisError('testing')

        resp = self.app.get('/api/1/auth/token?token=asdfasdfasdfasdfsdf')

        self.assertEqual(resp.status_code, 503)
Esempio n. 6
0
    def test_get_network_error(self, name, get_cache, default_value):
        m_set, m_get, m_flush, cache = get_cache()

        m_get.side_effect = RedisError()

        with self.assertRaises(CacheGetError):
            cache.get('FOO')
Esempio n. 7
0
    def test_added_token_to_redis_down(self, fake_StrictRedis):
        """_added_token_to_redis returns False when RedisError is raised"""
        fake_StrictRedis.side_effect = RedisError('testing')
        result = token._added_token_to_redis(token='asdf',
                                             username='******',
                                             log=MagicMock())

        self.assertFalse(result)
Esempio n. 8
0
    def test_delete_redis_down(self, fake_logger, fake_strict_redis):
        """DELETE on /api/1/auth/token returns 503 when it cannot connect to Redis"""
        fake_strict_redis.side_effect = RedisError('testing')
        resp = self.app.delete('/api/1/auth/token',
                               content_type='application/json',
                               data=ujson.dumps({'token': 'asdfasdf'}))

        self.assertEqual(resp.status_code, 503)
Esempio n. 9
0
 def flush(self):
     if not self._in_transaction:
         raise RedisError('not in transaction')
     self._in_transaction = False
     if not self.pipeline.command_stack:
         self.pipeline.reset()
     else:
         self.pipeline.execute()
         self.pipeline.reset()
Esempio n. 10
0
def test_suppress_redis_failure_logs_exceptions(caplog):
    caplog.set_level(logging.INFO)

    flag = False
    with SuppressRedisFailure():
        raise RedisError("Failure to be suppressed.")
        flag = True

    assert len(caplog.records) == 1
    assert not flag
Esempio n. 11
0
 def insert_dict_to_database(self, dictionary):
     """
        insert information to redis
     """
     try:
         self.connection.mset(dictionary)
     except AttributeError as e:
         LOGGER.error("Exception occurred, please, send dictionary input" +
                      str(e))
         raise RedisError(
             "Please, send only the dictionary for insertion in to redis")
Esempio n. 12
0
    def redis_client(self):
        redis_mode = str(self.context.config.REDIS_QUEUE_MODE).lower()

        if redis_mode == SINGLE_NODE:
            return self.__redis_single_node_client()
        if redis_mode == SENTINEL:
            return self.__redis_sentinel_client()

        raise RedisError(
            f"REDIS_QUEUE_MODE must be {SINGLE_NODE} or {SENTINEL}"
        )
Esempio n. 13
0
    def get_keys(self):
        """
           get the keys 
        """

        try:
            return self.connection.keys()
        except Exception as e:
            LOGGER.error("Exception occurred, please, send dictionary input" +
                         str(e))
            raise RedisError(
                "Please, send only the dictionary for insertion in to redis")
Esempio n. 14
0
    def test_error_redis_failure(self):
        mock_queue = mock.Mock()
        mock_queue.side_effect = RedisError()

        with mock.patch('ichnaea.queue.DataQueue.enqueue', mock_queue):
            res = self._post_one_cell(status=503)
            self.assertEqual(res.json, ServiceUnavailable.json_body())

        self.assertTrue(mock_queue.called)
        self.check_stats(counter=[
            ('data.batch.upload', 0),
            ('request', [self.metric_path, 'method:post', 'status:503']),
        ])
Esempio n. 15
0
    def get_database_config(self):
        """
            Get the redis configuration pool
        """

        try:
            data = self.connection.config_get()
            assert 'maxmemory' in data
            assert data['maxmemory'].isdigit()
            return (DictionaryObject(data))
        except Exception as e:
            raise RedisError(
                "Exception occurred while getting the configuration for redis")
Esempio n. 16
0
    def test_error_redis_failure(self, app, raven, metricsmock):
        mock_queue = mock.Mock()
        mock_queue.side_effect = RedisError()

        with mock.patch("ichnaea.queue.DataQueue.enqueue", mock_queue):
            res = self._post_one_cell(app, status=503)
            assert res.json == ServiceUnavailable().json_body()

        assert mock_queue.called
        raven.check([("ServiceUnavailable", 1)])
        metricsmock.assert_not_incr("data.batch.upload")
        metricsmock.assert_incr_once(
            "request", tags=[self.metric_path, "method:post", "status:503"])
Esempio n. 17
0
    def test_error_redis_failure(self, app, raven, stats):
        mock_queue = mock.Mock()
        mock_queue.side_effect = RedisError()

        with mock.patch('ichnaea.queue.DataQueue.enqueue', mock_queue):
            res = self._call(app, {'items': []}, status=503)
            assert res.json == ServiceUnavailable.json_body()

        assert mock_queue.called
        raven.check([('ServiceUnavailable', 1)])
        stats.check(counter=[
            ('request', [self.metric_path, 'method:post', 'status:503']),
        ])
Esempio n. 18
0
    def initialize_heat_switch(self):
        try:
            desired_position = get_redis_value(
                self.redis, 'device-settings:currentduino:heatswitch')
            current_position = get_redis_value(self.redis, 'status:heatswitch')
        except RedisError as e:
            raise RedisError(e)

        getLogger(__name__).debug(
            f"Desired position is {desired_position} and currently the heat switch is {current_position}"
        )

        if desired_position[HEATSWITCH_MOVE_KEY] == current_position[
                HEATSWITCH_STATUS_KEY]:
            getLogger(__name__).info(
                f"Initial heat switch position is: {current_position}")
            self.heat_switch_position = current_position
        else:
            if desired_position[HEATSWITCH_MOVE_KEY] == 'open':
                getLogger(__name__).info("Opening heat switch")
                self.heat_switch_position = self.open_heat_switch()
                getLogger(__name__).info(
                    f"Heat switch set to {self.heat_switch_position}")
            elif desired_position[HEATSWITCH_MOVE_KEY] == 'close':
                getLogger(__name__).info("Closing heat switch")
                self.heat_switch_position = self.close_heat_switch()
                getLogger(__name__).info(
                    f"Heat switch set to {self.heat_switch_position}")

        try:
            getLogger(__name__).debug(
                f"Storing heat switch position to redis: {self.heat_switch_position}"
            )
            store_redis_data(self.redis, self.heat_switch_position)
        except RedisError as e:
            raise RedisError(e)
Esempio n. 19
0
    def test_rate_limit_redis_failure(self, geoip_db, http_session, session, source):
        cell = CellShardFactory.build()
        mock_redis_client = _mock_redis_client()
        mock_redis_client.pipeline.side_effect = RedisError()

        with requests_mock.Mocker() as mock_request:
            mock_request.register_uri(
                "POST", requests_mock.ANY, json=self.fallback_result
            )

            with mock.patch.object(source, "redis_client", mock_redis_client):
                query = self.model_query(geoip_db, http_session, session, cells=[cell])
                results = source.search(query)
                self.check_model_results(results, None)

            assert mock_redis_client.pipeline.called
            assert not mock_request.called
Esempio n. 20
0
    def test_rate_limit_redis_failure(self):
        cell = CellShardFactory.build()
        mock_redis_client = self._mock_redis_client()
        mock_redis_client.pipeline.side_effect = RedisError()

        with requests_mock.Mocker() as mock_request:
            mock_request.register_uri('POST',
                                      requests_mock.ANY,
                                      json=self.fallback_result)

            with mock.patch.object(self.source, 'redis_client',
                                   mock_redis_client):
                query = self.model_query(cells=[cell])
                results = self.source.search(query)
                self.check_model_results(results, None)

            self.assertTrue(mock_redis_client.pipeline.called)
            self.assertFalse(mock_request.called)
Esempio n. 21
0
    def test_get_cache_redis_failure(self):
        cell = CellShardFactory.build()
        mock_redis_client = self._mock_redis_client()
        mock_redis_client.mget.side_effect = RedisError()

        with requests_mock.Mocker() as mock_request:
            mock_request.register_uri('POST',
                                      requests_mock.ANY,
                                      json=self.fallback_result)

            with mock.patch.object(self.source.cache, 'redis_client',
                                   mock_redis_client):
                query = self.model_query(cells=[cell])
                results = self.source.search(query)
                self.check_model_results(results, [self.fallback_model])

            self.assertTrue(mock_redis_client.mget.called)
            self.assertTrue(mock_request.called)

        self.check_stats(counter=[
            ('locate.fallback.cache', ['status:failure']),
        ])
Esempio n. 22
0
    def zaddnx(self, name, *args, **kwargs):
        """Like zadd but don't update the score of existing elements,

        Set any number of score, element-name pairs to the key ``name``. Pairs
        can be specified in two ways:

        As *args, in the form of: score1, name1, score2, name2, ...
        or as **kwargs, in the form of: name1=score1, name2=score2, ...

        The following example would add four values to the 'my-key' key:
        redis.zaddnx('my-key', 1.1, 'name1', 2.2, 'name2', name3=3.3, name4=4.4)
        """
        pieces = []
        if args:
            if len(args) % 2 != 0:
                raise RedisError("ZADDNX requires an equal number of "
                                 "values and scores")
            pieces.extend(args)
        for pair in iteritems(kwargs):
            pieces.append(pair[1])
            pieces.append(pair[0])

        return ZADDNX(keys=[name], args=pieces, client=self)
Esempio n. 23
0
    def test_get_multi_redis_error_1(self):
        self.redis_client.mget.side_effect = RedisError()

        with self.assertRaises(VersionGetError):
            self.version_store.get_multi([])
Esempio n. 24
0
    def test_incr_redis_error(self):
        self.redis_client.incr.side_effect = RedisError()

        with self.assertRaises(VersionIncrementError):
            self.version_store.incr('FOO')
Esempio n. 25
0
 def begin(self):
     if self._in_transaction:
         raise RedisError('already in transaction')
     self._in_transaction = True
     self.pipeline.multi()
Esempio n. 26
0
 def watch(self, name):
     if self.explicit_transaction:
         raise RedisError('Cannot issue a WATCH after a MULTI')
     self.watching = True
Esempio n. 27
0
    def test_get_multi_redis_error_3(self):
        self.redis_client.mget.return_value = [None]
        self.redis_client.get.side_effect = RedisError()

        with self.assertRaises(VersionMasterVersionError):
            self.version_store.get_multi([])
Esempio n. 28
0
 def throw(*_, **__):
     """ Throw an exception """
     raise RedisError("DB exception")