Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
Correct error message on sentinel discovery of master/slave with pass…
Browse files Browse the repository at this point in the history
…word.

Pyenv's .python-version config added to .gitignore.
  • Loading branch information
azinoviev authored and popravich committed Nov 14, 2017
1 parent 9ec600f commit 8b50684
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ docs/_build/
# Vim
.vim
tests/ssl

# pyenv
.python-version
13 changes: 12 additions & 1 deletion aioredis/sentinel/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from ..log import sentinel_logger
from ..pubsub import Receiver
from ..pool import create_pool, ConnectionsPool
from ..errors import MasterNotFoundError, SlaveNotFoundError, PoolClosedError
from ..errors import (
MasterNotFoundError,
SlaveNotFoundError,
PoolClosedError,
ReplyError
)


# Address marker for discovery
Expand Down Expand Up @@ -284,6 +289,9 @@ async def discover_master(self, service, timeout):
sentinel, service, err)
await asyncio.sleep(idle_timeout, loop=self._loop)
continue
except ReplyError as err:
raise ReplyError(
"Master {} error: {}".format(service, err)) from err
except Exception:
# TODO: clear (drop) connections to schedule reconnect
await asyncio.sleep(idle_timeout, loop=self._loop)
Expand Down Expand Up @@ -317,6 +325,9 @@ async def discover_slave(self, service, timeout, **kwargs):
except DiscoverError:
await asyncio.sleep(idle_timeout, loop=self._loop)
continue
except ReplyError as err:
raise ReplyError(
"Slave {} error: {}".format(service, err)) from err
except Exception:
await asyncio.sleep(idle_timeout, loop=self._loop)
continue
Expand Down
21 changes: 17 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

TCPAddress = namedtuple('TCPAddress', 'host port')

RedisServer = namedtuple('RedisServer', 'name tcp_address unixsocket version')
RedisServer = namedtuple('RedisServer',
'name tcp_address unixsocket version password')

SentinelServer = namedtuple('SentinelServer',
'name tcp_address unixsocket version masters')
Expand Down Expand Up @@ -278,7 +279,7 @@ def timeout(t):
yield True
raise RuntimeError("Redis startup timeout expired")

def maker(name, config_lines=None, *, slaveof=None):
def maker(name, config_lines=None, *, slaveof=None, password=None):
assert slaveof is None or isinstance(slaveof, RedisServer), slaveof
if name in servers:
return servers[name]
Expand All @@ -305,12 +306,16 @@ def maker(name, config_lines=None, *, slaveof=None):
if unixsocket:
write('unixsocket', unixsocket)
tmp_files.append(unixsocket)
if password:
write('requirepass "{}"'.format(password))
write('# extra config')
for line in config_lines:
write(line)
if slaveof is not None:
write("slaveof {0.tcp_address.host} {0.tcp_address.port}"
.format(slaveof))
if password:
write('masterauth "{}"'.format(password))
args = [config]
tmp_files.append(config)
else:
Expand All @@ -324,13 +329,20 @@ def maker(name, config_lines=None, *, slaveof=None):
args += [
'--unixsocket', unixsocket,
]
if password:
args += [
'--requirepass "{}"'.format(password)
]
if slaveof is not None:
args += [
'--slaveof',
str(slaveof.tcp_address.host),
str(slaveof.tcp_address.port),
]

if password:
args += [
'--masterauth "{}"'.format(password)
]
f = open(stdout_file, 'w')
atexit.register(f.close)
proc = _proc(server_bin, *args,
Expand All @@ -353,7 +365,7 @@ def maker(name, config_lines=None, *, slaveof=None):
print(name, ":", log, end='')
if 'sync: Finished with success' in log:
break
info = RedisServer(name, tcp_address, unixsocket, version)
info = RedisServer(name, tcp_address, unixsocket, version, password)
servers.setdefault(name, info)
return info

Expand Down Expand Up @@ -405,6 +417,7 @@ def maker(name, *masters, quorum=1, noslaves=False):
'127.0.0.1', master.tcp_address.port, quorum)
write('sentinel down-after-milliseconds', master.name, '3000')
write('sentinel failover-timeout', master.name, '3000')
write('sentinel auth-pass', master.name, master.password)

f = open(stdout_file, 'w')
atexit.register(f.close)
Expand Down
28 changes: 28 additions & 0 deletions tests/sentinel_commands_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,34 @@ async def test_master_info(redis_sentinel, sentinel):
assert 'link-refcount' in info


@pytest.mark.run_loop
async def test_master__auth(create_sentinel, start_sentinel,
start_server, loop):
master = start_server('master_1', password='123')
start_server('slave_1', slaveof=master, password='123')

sentinel = start_sentinel('auth_sentinel_1', master)
client1 = await create_sentinel(
[sentinel.tcp_address], password='123', loop=loop)

client2 = await create_sentinel(
[sentinel.tcp_address], password='111', loop=loop)

client3 = await create_sentinel(
[sentinel.tcp_address], loop=loop)

m1 = client1.master_for(master.name)
await m1.set('mykey', 'myval')

with pytest.raises(ReplyError):
m2 = client2.master_for(master.name)
await m2.set('mykey', 'myval')

with pytest.raises(ReplyError):
m3 = client3.master_for(master.name)
await m3.set('mykey', 'myval')


@pytest.mark.run_loop
async def test_master__unknown(redis_sentinel):
with pytest.raises(ReplyError):
Expand Down

0 comments on commit 8b50684

Please sign in to comment.