def test_empty_startup_nodes(s):
    """
    Test that exception is raised when empty providing empty startup_nodes
    """
    with pytest.raises(RedisClusterException) as ex:
        _get_client(init_slot_cache=False, startup_nodes=[])

    assert unicode(ex.value).startswith("No startup nodes provided"), unicode(ex.value)
Exemple #2
0
def test_empty_startup_nodes():
    """
    Test that exception is raised when empty providing empty startup_nodes
    """
    with pytest.raises(RedisClusterException) as ex:
        _get_client(init_slot_cache=False, startup_nodes=[])

    assert unicode(ex.value).startswith("No startup nodes provided"), unicode(ex.value)
def test_blocked_strict_redis_args():
    """
    Some arguments should explicitly be blocked because they will not work in a cluster setup
    """
    params = {"startup_nodes": [{"host": "127.0.0.1", "port": 7000}]}
    c = StrictRedisCluster(**params)
    assert c.connection_pool.connection_kwargs["socket_timeout"] == ClusterConnectionPool.RedisClusterDefaultTimeout

    with pytest.raises(RedisClusterException) as ex:
        _get_client(db=1)
    assert unicode(ex.value).startswith("Argument 'db' is not possible to use in cluster mode")
Exemple #4
0
def test_blocked_strict_redis_args():
    """
    Some arguments should explicitly be blocked because they will not work in a cluster setup
    """
    params = {'startup_nodes': [{'host': '127.0.0.1', 'port': 7000}]}
    c = StrictRedisCluster(**params)
    assert c.connection_pool.connection_kwargs["socket_timeout"] == ClusterConnectionPool.RedisClusterDefaultTimeout

    with pytest.raises(RedisClusterException) as ex:
        _get_client(db=1)
    assert unicode(ex.value).startswith("Argument 'db' is not possible to use in cluster mode")
Exemple #5
0
 def test_redis_cluster_pipeline(self):
     """
     Test that we can use a pipeline with the RedisCluster class
     """
     r = _get_client(RedisCluster)
     with r.pipeline(transaction=False) as pipe:
         pipe.get("foobar")
 def test_redis_cluster_pipeline(self):
     """
     Test that we can use a pipeline with the RedisCluster class
     """
     r = _get_client(cls=None)
     with r.pipeline(transaction=False) as pipe:
         pipe.get("foobar")
    def test_init_slots_cache_slots_collision(self):
        """
        Test that if 2 nodes do not agree on the same slots setup it should raise an error.
        In this test both nodes will say that the first slots block should be bound to different
         servers.
        """
        s = _get_client(init_slot_cache=False,
                        startup_nodes=[{
                            "host": "127.0.0.1",
                            "port": "7000"
                        }, {
                            "host": "127.0.0.1",
                            "port": "7001"
                        }])

        old_get_link_from_node = s.get_redis_link_from_node

        node_1 = old_get_link_from_node({"host": "127.0.0.1", "port": "7000"})
        node_2 = old_get_link_from_node({"host": "127.0.0.1", "port": "7001"})

        node_1_slots = [[0, 5460, [b'127.0.0.1', 7000], [b'127.0.0.1', 7003]],
                        [
                            5461, 10922, [b'127.0.0.1', 7001],
                            [b'127.0.0.1', 7004]
                        ]]

        node_2_slots = [[0, 5460, [b'127.0.0.1', 7001], [b'127.0.0.1', 7003]],
                        [
                            5461, 10922, [b'127.0.0.1', 7000],
                            [b'127.0.0.1', 7004]
                        ]]

        node_1.execute_command = lambda *args: node_1_slots
        node_2.execute_command = lambda *args: node_2_slots

        def new_get_link_from_node(node):
            # TODO: This seem like a bug that we have to exclude 'name' in first use but not in second use.
            if node == {
                    "host": "127.0.0.1",
                    "port": "7000"
            }:  # , 'name': '127.0.0.1:7000'}:
                return node_1
            elif node == {
                    "host": "127.0.0.1",
                    "port": "7001",
                    'name': '127.0.0.1:7001'
            }:
                return node_2
            else:
                raise Exception("Foo")

        s.get_redis_link_from_node = new_get_link_from_node

        with pytest.raises(RedisClusterException) as ex:
            s.initialize_slots_cache()

        assert unicode(ex.value).startswith(
            "startup_nodes could not agree on a valid slots cache."), True
Exemple #8
0
def test_password_procted_nodes():
    """
    Test that it is possible to connect to password protected nodes
    """
    startup_nodes = [{"host": "127.0.0.1", "port": "7000"}]
    password_protected_startup_nodes = [{"host": "127.0.0.1", "port": "7100"}]
    with pytest.raises(RedisClusterException) as ex:
        _get_client(startup_nodes=password_protected_startup_nodes)
    assert unicode(ex.value).startswith("ERROR sending 'cluster slots' command to redis server:")
    _get_client(startup_nodes=password_protected_startup_nodes, password='******')

    with pytest.raises(RedisClusterException) as ex:
        _get_client(startup_nodes=startup_nodes, password='******')
    assert unicode(ex.value).startswith("ERROR sending 'cluster slots' command to redis server:")
    _get_client(startup_nodes=startup_nodes)
def test_password_procted_nodes():
    """
    Test that it is possible to connect to password protected nodes
    """
    startup_nodes = [{"host": "127.0.0.1", "port": "7000"}]
    password_protected_startup_nodes = [{"host": "127.0.0.1", "port": "7100"}]
    with pytest.raises(RedisClusterException) as ex:
        _get_client(startup_nodes=password_protected_startup_nodes)
    assert unicode(ex.value).startswith("ERROR sending 'cluster slots' command to redis server:")
    _get_client(startup_nodes=password_protected_startup_nodes, password="******")

    with pytest.raises(RedisClusterException) as ex:
        _get_client(startup_nodes=startup_nodes, password="******")
    assert unicode(ex.value).startswith("ERROR sending 'cluster slots' command to redis server:")
    _get_client(startup_nodes=startup_nodes)
Exemple #10
0
def test_send_to_all_masters_merge_list(r):
    """
    Test to send command to only master nodes and merge result into a dict
    and returned to caller.
    """
    # empty the cluster before continue
    r.flushall()

    RedisCluster.test_send_to_all_masters_merge_list = send_to_all_masters_merge_list(StrictRedis.keys)
    r = _get_client()
    r.set("foo", 1)
    r.set("bar", 2)
    s = r.test_send_to_all_masters_merge_list()
    assert isinstance(s, list), "returned data should be of type list : {}".format(s.__class__)

    # Check that data returned from cluster is correct
    assert b"foo" in s
    assert b"bar" in s
    def test_init_slots_cache_slots_collision(self):
        """
        Test that if 2 nodes do not agree on the same slots setup it should raise an error.
        In this test both nodes will say that the first slots block should be bound to different
         servers.
        """
        s = _get_client(init_slot_cache=False, startup_nodes=[{"host": "127.0.0.1", "port": "7000"}, {"host": "127.0.0.1", "port": "7001"}])

        old_get_link_from_node = s.get_redis_link_from_node

        node_1 = old_get_link_from_node({"host": "127.0.0.1", "port": "7000"})
        node_2 = old_get_link_from_node({"host": "127.0.0.1", "port": "7001"})

        node_1_slots = [[0, 5460, [b'127.0.0.1', 7000], [b'127.0.0.1', 7003]],
                        [5461, 10922, [b'127.0.0.1', 7001], [b'127.0.0.1', 7004]]]

        node_2_slots = [[0, 5460, [b'127.0.0.1', 7001], [b'127.0.0.1', 7003]],
                        [5461, 10922, [b'127.0.0.1', 7000], [b'127.0.0.1', 7004]]]

        node_1.execute_command = lambda *args: node_1_slots
        node_2.execute_command = lambda *args: node_2_slots

        def new_get_link_from_node(node):
            # TODO: This seem like a bug that we have to exclude 'name' in first use but not in second use.
            if node == {"host": "127.0.0.1", "port": "7000"}:  # , 'name': '127.0.0.1:7000'}:
                return node_1
            elif node == {"host": "127.0.0.1", "port": "7001", 'name': '127.0.0.1:7001'}:
                return node_2
            else:
                raise Exception("Foo")

        s.get_redis_link_from_node = new_get_link_from_node

        with pytest.raises(RedisClusterException) as ex:
            s.initialize_slots_cache()

        assert unicode(ex.value).startswith("startup_nodes could not agree on a valid slots cache."), True
    def test_blocked_strict_redis_args(self):
        """
        Some arguments should explicitly be blocked because they will not work in a cluster setup
        """
        params = {'startup_nodes': [{'host': '127.0.0.1', 'port': 7000}]}
        c = RedisCluster(**params)
        assert c.opt["socket_timeout"] == RedisCluster.RedisClusterDefaultTimeout

        with pytest.raises(RedisClusterException) as ex:
            _get_client(db=1)
        assert unicode(ex.value).startswith("(error) [Remove 'db' from kwargs]"), True

        with pytest.raises(RedisClusterException) as ex:
            _get_client(host="foo.bar")
        assert unicode(ex.value).startswith("(error) [Remove 'host' from kwargs]"), True

        with pytest.raises(RedisClusterException) as ex:
            _get_client(port=1337)
        assert unicode(ex.value).startswith("(error) [Remove 'port' from kwargs]"), True
    def test_blocked_strict_redis_args(self):
        """
        Some arguments should explicitly be blocked because they will not work in a cluster setup
        """
        params = {'startup_nodes': [{'host': '127.0.0.1', 'port': 7000}]}
        c = RedisCluster(**params)
        assert c.opt[
            "socket_timeout"] == RedisCluster.RedisClusterDefaultTimeout

        with pytest.raises(RedisClusterException) as ex:
            _get_client(db=1)
        assert unicode(
            ex.value).startswith("(error) [Remove 'db' from kwargs]"), True

        with pytest.raises(RedisClusterException) as ex:
            _get_client(host="foo.bar")
        assert unicode(
            ex.value).startswith("(error) [Remove 'host' from kwargs]"), True

        with pytest.raises(RedisClusterException) as ex:
            _get_client(port=1337)
        assert unicode(
            ex.value).startswith("(error) [Remove 'port' from kwargs]"), True