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 | |
parent | 58ca166a9c7228f24fdd074c4785fe7303851cd4 (diff) | |
download | redis-py-c31ea12e89141dd90a69180060ae386948831f19.tar.gz |
Pass the master hostname to tests
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | tests/conftest.py | 7 | ||||
-rw-r--r-- | tests/test_connection_pool.py | 42 | ||||
-rw-r--r-- | tests/test_multiprocessing.py | 16 | ||||
-rw-r--r-- | tests/test_sentinel.py | 35 |
5 files changed, 63 insertions, 40 deletions
@@ -9,4 +9,5 @@ dev: docker-compose up -d test: dev - docker-compose run test tox --redis-url="redis://master:6379/9" + find . -name "*.pyc" -exec rm -f {} \; + docker-compose run test tox -- --redis-url="redis://master:6379/9" --redis-master-host=master diff --git a/tests/conftest.py b/tests/conftest.py index 4398175..4a745df 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -24,7 +24,7 @@ def pytest_addoption(parser): action="store", help="Redis connection string," " defaults to `%(default)s`") - parser.addoption('--master-host', default=DEFAULT_REDIS_MASTER_HOST, + parser.addoption('--redis-master-host', default=DEFAULT_REDIS_MASTER_HOST, action="store", help="Redis master hostname," " defaults to `%(default)s`") @@ -163,6 +163,11 @@ def mock_cluster_resp_slaves(request, **kwargs): return _gen_cluster_mock_resp(r, response) +@pytest.fixture(scope="module") +def master_host(request): + yield request.config.getoption("--redis-master-host") + + def wait_for_command(client, monitor, command): # issue a command with a key name that's local to this process. # if we find a command with our key before the command we're waiting diff --git a/tests/test_connection_pool.py b/tests/test_connection_pool.py index 080e3df..2f8ffbf 100644 --- a/tests/test_connection_pool.py +++ b/tests/test_connection_pool.py @@ -43,21 +43,25 @@ class TestConnectionPool(object): assert isinstance(connection, DummyConnection) assert connection.kwargs == connection_kwargs - def test_multiple_connections(self): - pool = self.get_pool() + def test_multiple_connections(self, master_host): + connection_kwargs = {'host': master_host} + pool = self.get_pool(connection_kwargs=connection_kwargs) c1 = pool.get_connection('_') c2 = pool.get_connection('_') assert c1 != c2 - def test_max_connections(self): - pool = self.get_pool(max_connections=2) + def test_max_connections(self, master_host): + connection_kwargs = {'host': master_host} + pool = self.get_pool(max_connections=2, + connection_kwargs=connection_kwargs) pool.get_connection('_') pool.get_connection('_') with pytest.raises(redis.ConnectionError): pool.get_connection('_') - def test_reuse_previously_released_connection(self): - pool = self.get_pool() + def test_reuse_previously_released_connection(self, master_host): + connection_kwargs = {'host': master_host} + pool = self.get_pool(connection_kwargs=connection_kwargs) c1 = pool.get_connection('_') pool.release(c1) c2 = pool.get_connection('_') @@ -98,22 +102,25 @@ class TestBlockingConnectionPool(object): **connection_kwargs) return pool - def test_connection_creation(self): - connection_kwargs = {'foo': 'bar', 'biz': 'baz'} + def test_connection_creation(self, master_host): + connection_kwargs = {'foo': 'bar', 'biz': 'baz', 'host': master_host} pool = self.get_pool(connection_kwargs=connection_kwargs) connection = pool.get_connection('_') assert isinstance(connection, DummyConnection) assert connection.kwargs == connection_kwargs - def test_multiple_connections(self): - pool = self.get_pool() + def test_multiple_connections(self, master_host): + connection_kwargs = {'host': master_host} + pool = self.get_pool(connection_kwargs=connection_kwargs) c1 = pool.get_connection('_') c2 = pool.get_connection('_') assert c1 != c2 - def test_connection_pool_blocks_until_timeout(self): + def test_connection_pool_blocks_until_timeout(self, master_host): "When out of connections, block for timeout seconds, then raise" - pool = self.get_pool(max_connections=1, timeout=0.1) + connection_kwargs = {'host': master_host} + pool = self.get_pool(max_connections=1, timeout=0.1, + connection_kwargs=connection_kwargs) pool.get_connection('_') start = time.time() @@ -122,12 +129,14 @@ class TestBlockingConnectionPool(object): # we should have waited at least 0.1 seconds assert time.time() - start >= 0.1 - def connection_pool_blocks_until_another_connection_released(self): + def test_connection_pool_blocks_until_another_connection_released(self, master_host): """ When out of connections, block until another connection is released to the pool """ - pool = self.get_pool(max_connections=1, timeout=2) + connection_kwargs = {'host': master_host} + pool = self.get_pool(max_connections=1, timeout=2, + connection_kwargs=connection_kwargs) c1 = pool.get_connection('_') def target(): @@ -139,8 +148,9 @@ class TestBlockingConnectionPool(object): pool.get_connection('_') assert time.time() - start >= 0.1 - def test_reuse_previously_released_connection(self): - pool = self.get_pool() + def test_reuse_previously_released_connection(self, master_host): + connection_kwargs = {'host': master_host} + pool = self.get_pool(connection_kwargs=connection_kwargs) c1 = pool.get_connection('_') pool.release(c1) c2 = pool.get_connection('_') diff --git a/tests/test_multiprocessing.py b/tests/test_multiprocessing.py index 3f81606..235e3ce 100644 --- a/tests/test_multiprocessing.py +++ b/tests/test_multiprocessing.py @@ -30,12 +30,12 @@ class TestMultiprocessing(object): request=request, single_connection_client=False) - def test_close_connection_in_child(self): + def test_close_connection_in_child(self, master_host): """ A connection owned by a parent and closed by a child doesn't destroy the file descriptors so a parent can still use it. """ - conn = Connection() + conn = Connection(host=master_host) conn.send_command('ping') assert conn.read_response() == b'PONG' @@ -56,12 +56,12 @@ class TestMultiprocessing(object): conn.send_command('ping') assert conn.read_response() == b'PONG' - def test_close_connection_in_parent(self): + def test_close_connection_in_parent(self, master_host): """ A connection owned by a parent is unusable by a child if the parent (the owning process) closes the connection. """ - conn = Connection() + conn = Connection(host=master_host) conn.send_command('ping') assert conn.read_response() == b'PONG' @@ -84,12 +84,12 @@ class TestMultiprocessing(object): assert proc.exitcode == 0 @pytest.mark.parametrize('max_connections', [1, 2, None]) - def test_pool(self, max_connections): + def test_pool(self, max_connections, master_host): """ A child will create its own connections when using a pool created by a parent. """ - pool = ConnectionPool.from_url('redis://localhost', + pool = ConnectionPool.from_url('redis://{}'.format(master_host), max_connections=max_connections) conn = pool.get_connection('ping') @@ -119,12 +119,12 @@ class TestMultiprocessing(object): assert conn.read_response() == b'PONG' @pytest.mark.parametrize('max_connections', [1, 2, None]) - def test_close_pool_in_main(self, max_connections): + def test_close_pool_in_main(self, max_connections, master_host): """ A child process that uses the same pool as its parent isn't affected when the parent disconnects all connections within the pool. """ - pool = ConnectionPool.from_url('redis://localhost', + pool = ConnectionPool.from_url('redis://{}'.format(master_host), max_connections=max_connections) conn = pool.get_connection('ping') 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) |