Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(regression-tests): sentinel_test.py: Ditch docker whcih is complex on CI in favour of local redis binary #755

Merged
merged 2 commits into from
Feb 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 19 additions & 23 deletions tests/dragonfly/sentinel_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pathlib
import subprocess
from typing import Awaitable
import aioredis
Expand Down Expand Up @@ -43,32 +44,29 @@ async def await_for(func, pred, timeout_sec, timeout_msg=""):


class Sentinel:
def __init__(self) -> None:
def __init__(self, config_dir) -> None:
self.config_file = pathlib.Path(config_dir).joinpath("sentinel.conf")
self.port = 5555
self.image = "bitnami/redis-sentinel:latest"
self.container_name = "sentinel_test_py_sentinel"
self.default_deployment = "my_deployment"
self.initial_master_port = 1111
self.proc = None

def start(self):
self.stop() # cleanup potential zombies

cmd = [
"docker", "run",
"--network=host",
"-d",
f"--name={self.container_name}",
"-e", "REDIS_MASTER_HOST=localhost",
"-e", f"REDIS_MASTER_PORT_NUMBER={self.initial_master_port}",
"-e", f"REDIS_MASTER_SET={self.default_deployment}",
"-e", f"REDIS_SENTINEL_PORT_NUMBER={self.port}",
"-e", "REDIS_SENTINEL_QUORUM=1",
"-e", "REDIS_SENTINEL_DOWN_AFTER_MILLISECONDS=3000",
"bitnami/redis-sentinel:latest"]
assert subprocess.run(cmd).returncode == 0, "docker run failed."
config = [
f"port {self.port}",
f"sentinel monitor {self.default_deployment} 127.0.0.1 {self.initial_master_port} 1",
f"sentinel down-after-milliseconds {self.default_deployment} 3000"
]
self.config_file.write_text("\n".join(config))

print(self.config_file.read_text())

self.proc = subprocess.Popen(["redis-server", f"{self.config_file.absolute()}", "--sentinel"])

def stop(self):
assert subprocess.run(["docker", "rm", "-f", "-v", f"{self.container_name}"]).returncode == 0, "docker rm failed."
self.proc.terminate()

def run_cmd(self, args, sentinel_cmd=True, capture_output=False, assert_ok=True) -> subprocess.CompletedProcess:
run_args = ["redis-cli", "-p", f"{self.port}"]
Expand Down Expand Up @@ -110,16 +108,15 @@ def failover(self, deployment=""):


@pytest.fixture(scope="function") # Sentinel has state which we don't want carried over form test to test.
def sentinel() -> Sentinel:
s = Sentinel()
def sentinel(tmp_dir) -> Sentinel:
s = Sentinel(tmp_dir)
s.start()
s.wait_ready()
yield s
s.stop()


@pytest.mark.asyncio
@pytest.mark.skip(reason="Fails on CI")
async def test_failover(df_local_factory, sentinel):
master = df_local_factory.create(port=sentinel.initial_master_port)
replica = df_local_factory.create(port=master.port + 1)
Expand All @@ -146,7 +143,7 @@ async def test_failover(df_local_factory, sentinel):
await await_for(
lambda: sentinel.live_master_port(),
lambda p: p == replica.port,
timeout_sec=3, timeout_msg="Timeout waiting for sentinel to report replica as master."
timeout_sec=10, timeout_msg="Timeout waiting for sentinel to report replica as master."
)
assert sentinel.slaves()[0]["port"] == str(master.port)

Expand All @@ -160,7 +157,6 @@ async def test_failover(df_local_factory, sentinel):


@pytest.mark.asyncio
@pytest.mark.skip(reason="Fails on CI")
async def test_master_failure(df_local_factory, sentinel):
master = df_local_factory.create(port=sentinel.initial_master_port)
replica = df_local_factory.create(port=master.port + 1)
Expand All @@ -187,7 +183,7 @@ async def test_master_failure(df_local_factory, sentinel):
await await_for(
lambda: sentinel.live_master_port(),
lambda p: p == replica.port,
timeout_sec=1000, timeout_msg="Timeout waiting for sentinel to report replica as master."
timeout_sec=300, timeout_msg="Timeout waiting for sentinel to report replica as master."
)

# Verify we can now write to replica.
Expand Down