diff options
author | laixintao <laixintaoo@gmail.com> | 2020-08-28 14:04:58 +0800 |
---|---|---|
committer | laixintao <laixintaoo@gmail.com> | 2020-08-28 14:04:58 +0800 |
commit | 81a07c455335fb700781a738fc140eb432c39365 (patch) | |
tree | efe2edd4a38c64b8ddebe7854cbc00accf7cf416 | |
parent | 8f9287c3392623f608e4915fd43e5d302cf6fe74 (diff) |
testcase for auto-redirect.
-rw-r--r-- | iredis/client.py | 39 | ||||
-rw-r--r-- | tests/unittests/test_client.py | 52 |
2 files changed, 69 insertions, 22 deletions
diff --git a/iredis/client.py b/iredis/client.py index ae093f6..61613dc 100644 --- a/iredis/client.py +++ b/iredis/client.py @@ -260,28 +260,27 @@ class Client: print(response, file=sys.stderr) + connection = self.create_connection(ip, port) # if user sets dsn for dest node # use username and password from dsn settings - for dsn_name, dsn_url in config.alias_dsn.items(): - dsn = parse_url(dsn_url) - if dsn.host == ip and dsn.port == port: - print( - f"Connect {ip}:{port} via dns settings of {dsn_name}", - file=sys.stderr, - ) - connection = self.create_connection( - dsn.host, - dsn.port, - dsn.db, - dsn.password, - dsn.path, - dsn.scheme, - dsn.username, - ) - break - else: - # create a new connection for redirection - connection = self.create_connection(ip, port) + if config.alias_dsn: + for dsn_name, dsn_url in config.alias_dsn.items(): + dsn = parse_url(dsn_url) + if dsn.host == ip and dsn.port == port: + print( + f"Connect {ip}:{port} via dns settings of {dsn_name}", + file=sys.stderr, + ) + connection = self.create_connection( + dsn.host, + dsn.port, + dsn.db, + dsn.password, + dsn.path, + dsn.scheme, + dsn.username, + ) + break connection.connect() return self.execute_by_connection(connection, *args, **kwargs) diff --git a/tests/unittests/test_client.py b/tests/unittests/test_client.py index 362bd2d..e78da17 100644 --- a/tests/unittests/test_client.py +++ b/tests/unittests/test_client.py @@ -1,12 +1,13 @@ import re import pytest import redis -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch +from textwrap import dedent from prompt_toolkit.formatted_text import FormattedText from iredis.client import Client -from iredis.config import config +from iredis.config import config, load_config_files from iredis.completers import IRedisCompleter from iredis.entry import Rainbow, prompt_message from iredis.exceptions import NotSupport @@ -454,3 +455,50 @@ def test_mem_not_called_when_cant_get_server_version( clean_redis.set("foo", "bar") result = list(iredis_client.do_peek("foo")) assert result[0][1] == ("", "string (embstr), ttl: -1") + + +def test_reissue_command_on_redis_cluster(iredis_client, clean_redis): + mock_response = iredis_client.connection = MagicMock() + mock_response.read_response.side_effect = redis.exceptions.ResponseError( + "MOVED 12182 127.0.0.1:7002" + ) + iredis_client.reissue_with_redirect = MagicMock() + iredis_client.execute("set", "foo", "bar") + assert iredis_client.reissue_with_redirect.call_args == ( + ( + "MOVED 12182 127.0.0.1:7002", + "set", + "foo", + "bar", + ), + ) + + +def test_reissue_command_on_redis_cluster_with_password_in_dsn( + iredis_client, clean_redis +): + config_content = dedent( + """ + [main] + log_location = /tmp/iredis1.log + no_info=True + [alias_dsn] + cluster-7003=redis://foo:bar@127.0.0.1:7003 + """ + ) + with open("/tmp/iredisrc", "w+") as etc_config: + etc_config.write(config_content) + + config_obj = load_config_files("/tmp/iredisrc") + config.alias_dsn = config_obj["alias_dsn"] + + mock_execute_by_connection = iredis_client.execute_by_connection = MagicMock() + with patch("redis.connection.Connection.connect"): + iredis_client.reissue_with_redirect( + "MOVED 12182 127.0.0.1:7003", "set", "foo", "bar" + ) + + call_args = mock_execute_by_connection.call_args[0] + print(call_args) + assert list(call_args[1:]) == ["set", "foo", "bar"] + assert call_args[0].password == "bar" |