2023-02-14 11:19:33 +00:00
|
|
|
import os
|
|
|
|
import pytest
|
2023-05-13 07:44:25 +00:00
|
|
|
import redis
|
2023-06-18 18:14:28 +00:00
|
|
|
import asyncio
|
2023-05-13 07:44:25 +00:00
|
|
|
from redis import asyncio as aioredis
|
|
|
|
|
2023-09-28 07:11:11 +00:00
|
|
|
from . import dfly_multi_test_args, dfly_args
|
|
|
|
from .instance import DflyStartException
|
2023-09-18 07:23:49 +00:00
|
|
|
from .utility import batch_fill_data, gen_test_data, EnvironCntx
|
2022-10-31 14:39:20 +00:00
|
|
|
|
|
|
|
|
2023-07-17 10:13:12 +00:00
|
|
|
@dfly_multi_test_args({"keys_output_limit": 512}, {"keys_output_limit": 1024})
|
2022-10-31 14:39:20 +00:00
|
|
|
class TestKeys:
|
2023-03-24 15:22:14 +00:00
|
|
|
async def test_max_keys(self, async_client: aioredis.Redis, df_server):
|
2023-07-17 10:13:12 +00:00
|
|
|
max_keys = df_server["keys_output_limit"]
|
2023-03-24 15:22:14 +00:00
|
|
|
pipe = async_client.pipeline()
|
|
|
|
batch_fill_data(pipe, gen_test_data(max_keys * 3))
|
|
|
|
await pipe.execute()
|
|
|
|
keys = await async_client.keys()
|
2023-07-17 10:13:12 +00:00
|
|
|
assert len(keys) in range(max_keys, max_keys + 512)
|
|
|
|
|
2023-02-14 11:19:33 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def export_dfly_password() -> str:
|
2023-07-17 10:13:12 +00:00
|
|
|
pwd = "flypwd"
|
2023-12-24 20:06:57 +00:00
|
|
|
with EnvironCntx(DFLY_requirepass=pwd):
|
2023-09-18 07:23:49 +00:00
|
|
|
yield pwd
|
2023-07-17 10:13:12 +00:00
|
|
|
|
2023-02-14 11:19:33 +00:00
|
|
|
|
2024-07-02 07:26:26 +00:00
|
|
|
async def test_password(df_factory, export_dfly_password):
|
|
|
|
with df_factory.create() as dfly:
|
2023-09-18 10:52:56 +00:00
|
|
|
# Expect password form environment variable
|
|
|
|
with pytest.raises(redis.exceptions.AuthenticationError):
|
|
|
|
async with aioredis.Redis(port=dfly.port) as client:
|
|
|
|
await client.ping()
|
|
|
|
async with aioredis.Redis(password=export_dfly_password, port=dfly.port) as client:
|
2023-09-18 07:23:49 +00:00
|
|
|
await client.ping()
|
2023-02-14 11:19:33 +00:00
|
|
|
|
|
|
|
# --requirepass should take precedence over environment variable
|
2023-07-17 10:13:12 +00:00
|
|
|
requirepass = "requirepass"
|
2024-07-02 07:26:26 +00:00
|
|
|
with df_factory.create(requirepass=requirepass) as dfly:
|
2023-09-18 10:52:56 +00:00
|
|
|
# Expect password form flag
|
|
|
|
with pytest.raises(redis.exceptions.AuthenticationError):
|
|
|
|
async with aioredis.Redis(port=dfly.port, password=export_dfly_password) as client:
|
|
|
|
await client.ping()
|
|
|
|
async with aioredis.Redis(password=requirepass, port=dfly.port) as client:
|
2023-09-18 07:23:49 +00:00
|
|
|
await client.ping()
|
2023-06-18 18:14:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
Make sure that multi-hop transactions can't run OOO.
|
|
|
|
"""
|
|
|
|
|
|
|
|
MULTI_HOPS = """
|
|
|
|
for i = 0, ARGV[1] do
|
|
|
|
redis.call('INCR', KEYS[1])
|
|
|
|
end
|
|
|
|
"""
|
|
|
|
|
2023-07-17 10:13:12 +00:00
|
|
|
|
2023-06-18 18:14:28 +00:00
|
|
|
@dfly_args({"proactor_threads": 1})
|
|
|
|
async def test_txq_ooo(async_client: aioredis.Redis, df_server):
|
|
|
|
async def task1(k, h):
|
|
|
|
c = aioredis.Redis(port=df_server.port)
|
|
|
|
for _ in range(100):
|
|
|
|
await c.eval(MULTI_HOPS, 1, k, h)
|
|
|
|
|
|
|
|
async def task2(k, n):
|
|
|
|
c = aioredis.Redis(port=df_server.port)
|
|
|
|
for _ in range(100):
|
|
|
|
pipe = c.pipeline(transaction=False)
|
|
|
|
pipe.lpush(k, 1)
|
|
|
|
for _ in range(n):
|
|
|
|
pipe.blpop(k, 0.001)
|
|
|
|
await pipe.execute()
|
|
|
|
|
2023-07-17 10:13:12 +00:00
|
|
|
await asyncio.gather(
|
|
|
|
task1("i1", 2), task1("i2", 3), task2("l1", 2), task2("l1", 2), task2("l1", 5)
|
|
|
|
)
|
2023-09-13 10:02:38 +00:00
|
|
|
|
|
|
|
|
2024-07-02 07:26:26 +00:00
|
|
|
async def test_arg_from_environ_overwritten_by_cli(df_factory):
|
2023-09-18 07:23:49 +00:00
|
|
|
with EnvironCntx(DFLY_port="6378"):
|
2024-07-02 07:26:26 +00:00
|
|
|
with df_factory.create(port=6377):
|
2023-09-18 10:52:56 +00:00
|
|
|
client = aioredis.Redis(port=6377)
|
|
|
|
await client.ping()
|
2023-09-13 10:02:38 +00:00
|
|
|
|
|
|
|
|
2024-07-02 07:26:26 +00:00
|
|
|
async def test_arg_from_environ(df_factory):
|
2023-09-18 07:23:49 +00:00
|
|
|
with EnvironCntx(DFLY_requirepass="pass"):
|
2024-07-02 07:26:26 +00:00
|
|
|
with df_factory.create() as dfly:
|
2023-09-18 10:52:56 +00:00
|
|
|
# Expect password from environment variable
|
|
|
|
with pytest.raises(redis.exceptions.AuthenticationError):
|
|
|
|
client = aioredis.Redis(port=dfly.port)
|
|
|
|
await client.ping()
|
2023-09-13 10:02:38 +00:00
|
|
|
|
2023-09-18 10:52:56 +00:00
|
|
|
client = aioredis.Redis(password="pass", port=dfly.port)
|
2023-09-18 07:23:49 +00:00
|
|
|
await client.ping()
|
2023-09-13 10:02:38 +00:00
|
|
|
|
|
|
|
|
2024-07-02 07:26:26 +00:00
|
|
|
async def test_unknown_dfly_env(df_factory, export_dfly_password):
|
2023-09-18 07:23:49 +00:00
|
|
|
with EnvironCntx(DFLY_abcdef="xyz"):
|
2024-08-11 11:52:44 +00:00
|
|
|
dfly = df_factory.create()
|
2023-09-18 07:23:49 +00:00
|
|
|
with pytest.raises(DflyStartException):
|
|
|
|
dfly.start()
|
2024-08-11 11:52:44 +00:00
|
|
|
dfly.set_proc_to_none()
|
2023-09-29 15:16:06 +00:00
|
|
|
|
|
|
|
|
2024-07-02 07:26:26 +00:00
|
|
|
async def test_restricted_commands(df_factory):
|
2023-09-29 15:16:06 +00:00
|
|
|
# Restrict GET and SET, then verify non-admin clients are blocked from
|
|
|
|
# using these commands, though admin clients can use them.
|
2024-07-02 07:26:26 +00:00
|
|
|
with df_factory.create(restricted_commands="get,set", admin_port=1112) as server:
|
2023-09-29 15:16:06 +00:00
|
|
|
async with aioredis.Redis(port=server.port) as client:
|
|
|
|
with pytest.raises(redis.exceptions.ResponseError):
|
|
|
|
await client.get("foo")
|
|
|
|
|
|
|
|
with pytest.raises(redis.exceptions.ResponseError):
|
|
|
|
await client.set("foo", "bar")
|
|
|
|
|
|
|
|
async with aioredis.Redis(port=server.admin_port) as admin_client:
|
|
|
|
await admin_client.get("foo")
|
|
|
|
await admin_client.set("foo", "bar")
|
2024-01-15 08:13:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2024-07-02 07:26:26 +00:00
|
|
|
async def test_reply_guard_oom(df_factory, df_seeder_factory):
|
|
|
|
master = df_factory.create(
|
2024-09-22 10:28:24 +00:00
|
|
|
proactor_threads=1,
|
|
|
|
cache_mode="true",
|
|
|
|
maxmemory="256mb",
|
|
|
|
enable_heartbeat_eviction="false",
|
|
|
|
rss_oom_deny_ratio=2,
|
2024-01-15 08:13:10 +00:00
|
|
|
)
|
2024-07-02 07:26:26 +00:00
|
|
|
df_factory.start_all([master])
|
2024-01-15 08:13:10 +00:00
|
|
|
c_master = master.client()
|
2024-09-22 10:28:24 +00:00
|
|
|
await c_master.execute_command("DEBUG POPULATE 6000 size 40000")
|
2024-01-15 08:13:10 +00:00
|
|
|
|
|
|
|
seeder = df_seeder_factory.create(
|
|
|
|
port=master.port, keys=5000, val_size=1000, stop_on_failure=False
|
|
|
|
)
|
|
|
|
await seeder.run(target_deviation=0.1)
|
|
|
|
|
|
|
|
info = await c_master.info("stats")
|
|
|
|
assert info["evicted_keys"] > 0, "Weak testcase: policy based eviction was not triggered."
|
2024-09-22 06:32:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_denyoom_commands(df_factory):
|
|
|
|
df_server = df_factory.create(
|
|
|
|
proactor_threads=1, maxmemory="256mb", oom_deny_commands="get", oom_deny_ratio=0.7
|
|
|
|
)
|
|
|
|
df_server.start()
|
|
|
|
client = df_server.client()
|
|
|
|
await client.execute_command("DEBUG POPULATE 7000 size 44000")
|
|
|
|
|
|
|
|
min_deny = 250 * 1024 * 1024 # 250mb
|
|
|
|
info = await client.info("memory")
|
|
|
|
print(f'Used memory {info["used_memory"]}, rss {info["used_memory_rss"]}')
|
|
|
|
assert info["used_memory"] > min_deny, "Weak testcase: too little used memory"
|
|
|
|
|
|
|
|
# reject set due to oom
|
|
|
|
with pytest.raises(redis.exceptions.ResponseError):
|
|
|
|
await client.execute_command("set x y")
|
|
|
|
|
|
|
|
# reject get because it is set in oom_deny_commands
|
|
|
|
with pytest.raises(redis.exceptions.ResponseError):
|
|
|
|
await client.execute_command("get x")
|
|
|
|
|
|
|
|
# mget should not be rejected
|
|
|
|
await client.execute_command("mget x")
|