diff options
author | Andrew Brookins <andrew.brookins@redislabs.com> | 2020-07-09 16:15:23 -0700 |
---|---|---|
committer | Andrew Brookins <andrew.brookins@redislabs.com> | 2020-07-09 16:15:23 -0700 |
commit | c31ea12e89141dd90a69180060ae386948831f19 (patch) | |
tree | aceb6becaa0832b791dec5e0e72d89723030949e /tests/test_sentinel.py | |
parent | 58ca166a9c7228f24fdd074c4785fe7303851cd4 (diff) | |
download | redis-py-c31ea12e89141dd90a69180060ae386948831f19.tar.gz |
Pass the master hostname to tests
Diffstat (limited to 'tests/test_sentinel.py')
-rw-r--r-- | tests/test_sentinel.py | 35 |
1 files changed, 21 insertions, 14 deletions
diff --git a/tests/test_sentinel.py b/tests/test_sentinel.py index 1081e2b..c247c72 100644 --- a/tests/test_sentinel.py +++ b/tests/test_sentinel.py @@ -1,3 +1,5 @@ +import socket + import pytest from redis import exceptions @@ -7,6 +9,11 @@ from redis._compat import next import redis.sentinel +@pytest.fixture(scope="module") +def master_ip(master_host): + yield socket.gethostbyname(master_host) + + class SentinelTestClient(object): def __init__(self, cluster, id): self.cluster = cluster @@ -54,10 +61,10 @@ class SentinelTestCluster(object): @pytest.fixture() -def cluster(request): +def cluster(request, master_ip): def teardown(): redis.sentinel.Redis = saved_Redis - cluster = SentinelTestCluster() + cluster = SentinelTestCluster(ip=master_ip) saved_Redis = redis.sentinel.Redis redis.sentinel.Redis = cluster.client request.addfinalizer(teardown) @@ -69,9 +76,9 @@ def sentinel(request, cluster): return Sentinel([('foo', 26379), ('bar', 26379)]) -def test_discover_master(sentinel): +def test_discover_master(sentinel, master_ip): address = sentinel.discover_master('mymaster') - assert address == ('127.0.0.1', 6379) + assert address == (master_ip, 6379) def test_discover_master_error(sentinel): @@ -79,32 +86,32 @@ def test_discover_master_error(sentinel): sentinel.discover_master('xxx') -def test_discover_master_sentinel_down(cluster, sentinel): +def test_discover_master_sentinel_down(cluster, sentinel, master_ip): # Put first sentinel 'foo' down cluster.nodes_down.add(('foo', 26379)) address = sentinel.discover_master('mymaster') - assert address == ('127.0.0.1', 6379) + assert address == (master_ip, 6379) # 'bar' is now first sentinel assert sentinel.sentinels[0].id == ('bar', 26379) -def test_discover_master_sentinel_timeout(cluster, sentinel): +def test_discover_master_sentinel_timeout(cluster, sentinel, master_ip): # Put first sentinel 'foo' down cluster.nodes_timeout.add(('foo', 26379)) address = sentinel.discover_master('mymaster') - assert address == ('127.0.0.1', 6379) + assert address == (master_ip, 6379) # 'bar' is now first sentinel assert sentinel.sentinels[0].id == ('bar', 26379) -def test_master_min_other_sentinels(cluster): +def test_master_min_other_sentinels(cluster, master_ip): sentinel = Sentinel([('foo', 26379)], min_other_sentinels=1) # min_other_sentinels with pytest.raises(MasterNotFoundError): sentinel.discover_master('mymaster') cluster.master['num-other-sentinels'] = 2 address = sentinel.discover_master('mymaster') - assert address == ('127.0.0.1', 6379) + assert address == (master_ip, 6379) def test_master_odown(cluster, sentinel): @@ -153,10 +160,10 @@ def test_discover_slaves(cluster, sentinel): ('slave0', 1234), ('slave1', 1234)] -def test_master_for(cluster, sentinel): +def test_master_for(cluster, sentinel, master_ip): master = sentinel.master_for('mymaster', db=9) assert master.ping() - assert master.connection_pool.master_address == ('127.0.0.1', 6379) + assert master.connection_pool.master_address == (master_ip, 6379) # Use internal connection check master = sentinel.master_for('mymaster', db=9, check_connection=True) @@ -179,7 +186,7 @@ def test_slave_for_slave_not_found_error(cluster, sentinel): slave.ping() -def test_slave_round_robin(cluster, sentinel): +def test_slave_round_robin(cluster, sentinel, master_ip): cluster.slaves = [ {'ip': 'slave0', 'port': 6379, 'is_odown': False, 'is_sdown': False}, {'ip': 'slave1', 'port': 6379, 'is_odown': False, 'is_sdown': False}, @@ -189,6 +196,6 @@ def test_slave_round_robin(cluster, sentinel): assert next(rotator) in (('slave0', 6379), ('slave1', 6379)) assert next(rotator) in (('slave0', 6379), ('slave1', 6379)) # Fallback to master - assert next(rotator) == ('127.0.0.1', 6379) + assert next(rotator) == (master_ip, 6379) with pytest.raises(SlaveNotFoundError): next(rotator) |