Skip to content

Commit

Permalink
test for authenticating & sentinel
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaSkriblovsky committed Dec 25, 2017
1 parent 6a5ca55 commit e0dcd23
Showing 1 changed file with 72 additions and 3 deletions.
75 changes: 72 additions & 3 deletions tests/test_sentinel.py
Expand Up @@ -30,11 +30,37 @@ def send_error(self, msg):
self.transport.write(b''.join([b"-ERR ", msg.encode("utf-8"), b"\r\n"]))

def replyReceived(self, request):
if request[0] == "ROLE":
self.send_reply(self.factory.role)
if isinstance(request, list):
if request[0] == "ROLE":
self.send_reply(self.factory.role)

else:
self.send_error("Command not supported")
else:
BaseRedisProtocol.replyReceived(self, request)


class FakeAuthenticatedRedisProtocol(FakeRedisProtocol):
def __init__(self, requirepass):
FakeRedisProtocol.__init__(self)
self.requirepass = requirepass
self.authenticated = False

def replyReceived(self, request):
if isinstance(request, list):
if request[0] == "AUTH":
if request[1] == self.requirepass:
self.authenticated = True
self.send_reply("OK")
else:
self.send_error("invalid password")
else:
if self.authenticated:
FakeRedisProtocol.replyReceived(self, request)
else:
self.send_error("authentication required")
else:
self.send_error("Command not supported")
FakeRedisProtocol.replyReceived(self, request)


class FakeSentinelProtocol(FakeRedisProtocol):
Expand Down Expand Up @@ -80,6 +106,17 @@ def addConnection(self, conn): pass
def delConnection(self, conn): pass


class FakeAuthenticatedRedisFactory(FakeRedisFactory):
def __init__(self, requirepass):
FakeRedisFactory.__init__(self)
self.requirepass = requirepass

def buildProtocol(self, addr):
proto = FakeAuthenticatedRedisProtocol(self.requirepass)
proto.factory = self
return proto


class FakeSentinelFactory(FakeRedisFactory):
protocol = FakeSentinelProtocol

Expand Down Expand Up @@ -287,3 +324,35 @@ def test_drop_all_when_master_changes(self):
self.assertTrue(all(addr.port == self.slave_port for addr in addrs))

yield conn.disconnect()


class TestAuthViaSentinel(TestCase):
master_port = 36379
sentinel_port = 46379

def setUp(self):
self.fake_master = FakeAuthenticatedRedisFactory('secret!')
self.master_listener = reactor.listenTCP(self.master_port, self.fake_master)

self.fake_sentinel = FakeSentinelFactory()
self.fake_sentinel.master_addr = ("127.0.0.1", self.master_port)
self.fake_sentinel.slave_addrs = []
self.fake_sentinel.slave_flags = []
self.sentinel_listener = reactor.listenTCP(self.sentinel_port, self.fake_sentinel)

self.client = Sentinel([("127.0.0.1", self.sentinel_port)])
self.client.discovery_timeout = 1

@defer.inlineCallbacks
def tearDown(self):
yield self.client.disconnect()
self.sentinel_listener.stopListening()
self.master_listener.stopListening()


@defer.inlineCallbacks
def test_auth(self):
conn = self.client.master_for("test", password='secret!')
reply = yield conn.role()
self.assertEqual(reply[0], "master")
yield conn.disconnect()

0 comments on commit e0dcd23

Please sign in to comment.