2022-08-26 10:54:38 +00:00
|
|
|
import time
|
|
|
|
import pytest
|
2022-10-24 18:22:43 +00:00
|
|
|
import os
|
|
|
|
import glob
|
2023-04-17 11:59:44 +00:00
|
|
|
import aioredis
|
2022-08-26 10:54:38 +00:00
|
|
|
from pathlib import Path
|
2023-04-20 04:30:42 +00:00
|
|
|
import aioredis
|
2022-11-06 14:27:43 +00:00
|
|
|
|
|
|
|
from . import dfly_args
|
2023-01-09 20:31:15 +00:00
|
|
|
from .utility import DflySeeder, wait_available_async
|
2022-08-26 10:54:38 +00:00
|
|
|
|
2022-10-31 14:39:20 +00:00
|
|
|
BASIC_ARGS = {"dir": "{DRAGONFLY_TMP}/"}
|
2023-01-09 20:31:15 +00:00
|
|
|
|
2023-01-12 11:38:05 +00:00
|
|
|
SEEDER_ARGS = dict(keys=12_000, dbcount=5, multi_transaction_probability=0)
|
2022-10-24 18:22:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SnapshotTestBase:
|
2022-08-26 10:54:38 +00:00
|
|
|
def setup(self, tmp_dir: Path):
|
2022-10-24 18:22:43 +00:00
|
|
|
self.tmp_dir = tmp_dir
|
2022-08-26 10:54:38 +00:00
|
|
|
|
2023-04-17 11:59:44 +00:00
|
|
|
def get_main_file(self, pattern):
|
2023-04-20 04:30:42 +00:00
|
|
|
def is_main(f): return "summary" in f if pattern.endswith(
|
|
|
|
"dfs") else True
|
2023-04-17 11:59:44 +00:00
|
|
|
files = glob.glob(str(self.tmp_dir.absolute()) + '/' + pattern)
|
|
|
|
possible_mains = list(filter(is_main, files))
|
|
|
|
assert len(possible_mains) == 1, possible_mains
|
|
|
|
return possible_mains[0]
|
2022-10-24 18:22:43 +00:00
|
|
|
|
|
|
|
|
2023-04-17 11:59:44 +00:00
|
|
|
@dfly_args({**BASIC_ARGS, "dbfilename": "test-rdb-{{timestamp}}"})
|
2022-10-24 18:22:43 +00:00
|
|
|
class TestRdbSnapshot(SnapshotTestBase):
|
|
|
|
"""Test single file rdb snapshot"""
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup(self, tmp_dir: Path):
|
|
|
|
super().setup(tmp_dir)
|
|
|
|
|
2023-01-09 20:31:15 +00:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_snapshot(self, df_seeder_factory, async_client, df_server):
|
|
|
|
seeder = df_seeder_factory.create(port=df_server.port, **SEEDER_ARGS)
|
|
|
|
await seeder.run(target_deviation=0.1)
|
|
|
|
|
|
|
|
start_capture = await seeder.capture()
|
2022-10-24 18:22:43 +00:00
|
|
|
|
|
|
|
# save + flush + load
|
2023-02-15 08:15:38 +00:00
|
|
|
await async_client.execute_command("SAVE RDB")
|
2023-01-09 20:31:15 +00:00
|
|
|
assert await async_client.flushall()
|
2023-04-17 11:59:44 +00:00
|
|
|
await async_client.execute_command("DEBUG LOAD " + super().get_main_file("test-rdb-*.rdb"))
|
|
|
|
|
|
|
|
assert await seeder.compare(start_capture)
|
|
|
|
|
|
|
|
|
2023-04-18 09:48:30 +00:00
|
|
|
@dfly_args({**BASIC_ARGS, "dbfilename": "test-rdbexact.rdb", "nodf_snapshot_format": None})
|
2023-04-17 11:59:44 +00:00
|
|
|
class TestRdbSnapshotExactFilename(SnapshotTestBase):
|
|
|
|
"""Test single file rdb snapshot without a timestamp"""
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup(self, tmp_dir: Path):
|
|
|
|
super().setup(tmp_dir)
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_snapshot(self, df_seeder_factory, async_client, df_server):
|
|
|
|
seeder = df_seeder_factory.create(port=df_server.port, **SEEDER_ARGS)
|
|
|
|
await seeder.run(target_deviation=0.1)
|
|
|
|
|
|
|
|
start_capture = await seeder.capture()
|
|
|
|
|
|
|
|
# save + flush + load
|
|
|
|
await async_client.execute_command("SAVE RDB")
|
|
|
|
assert await async_client.flushall()
|
|
|
|
main_file = super().get_main_file("test-rdbexact.rdb")
|
|
|
|
await async_client.execute_command("DEBUG LOAD " + main_file)
|
2022-10-24 18:22:43 +00:00
|
|
|
|
2023-01-09 20:31:15 +00:00
|
|
|
assert await seeder.compare(start_capture)
|
2022-10-24 18:22:43 +00:00
|
|
|
|
2022-10-31 14:39:20 +00:00
|
|
|
|
2023-01-09 20:31:15 +00:00
|
|
|
@dfly_args({**BASIC_ARGS, "dbfilename": "test-dfs"})
|
2022-10-24 18:22:43 +00:00
|
|
|
class TestDflySnapshot(SnapshotTestBase):
|
|
|
|
"""Test multi file snapshot"""
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup(self, tmp_dir: Path):
|
|
|
|
self.tmp_dir = tmp_dir
|
|
|
|
|
2023-01-09 20:31:15 +00:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_snapshot(self, df_seeder_factory, async_client, df_server):
|
|
|
|
seeder = df_seeder_factory.create(port=df_server.port, **SEEDER_ARGS)
|
|
|
|
await seeder.run(target_deviation=0.1)
|
|
|
|
|
|
|
|
start_capture = await seeder.capture()
|
2022-10-24 18:22:43 +00:00
|
|
|
|
|
|
|
# save + flush + load
|
2023-01-09 20:31:15 +00:00
|
|
|
await async_client.execute_command("SAVE DF")
|
|
|
|
assert await async_client.flushall()
|
2023-04-17 11:59:44 +00:00
|
|
|
await async_client.execute_command("DEBUG LOAD " + super().get_main_file("test-dfs-summary.dfs"))
|
2022-10-24 18:22:43 +00:00
|
|
|
|
2023-01-09 20:31:15 +00:00
|
|
|
assert await seeder.compare(start_capture)
|
2022-10-24 18:22:43 +00:00
|
|
|
|
2023-04-17 21:12:38 +00:00
|
|
|
# We spawn instances manually, so reduce memory usage of default to minimum
|
2023-04-20 04:30:42 +00:00
|
|
|
|
|
|
|
|
2023-04-17 21:12:38 +00:00
|
|
|
@dfly_args({"proactor_threads": "1"})
|
2023-04-17 11:59:44 +00:00
|
|
|
class TestDflyAutoLoadSnapshot(SnapshotTestBase):
|
|
|
|
"""Test automatic loading of dump files on startup with timestamp"""
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup(self, tmp_dir: Path):
|
|
|
|
self.tmp_dir = tmp_dir
|
|
|
|
|
|
|
|
cases = [
|
|
|
|
("rdb", "test-autoload1-{{timestamp}}"),
|
|
|
|
("df", "test-autoload2-{{timestamp}}"),
|
|
|
|
("rdb", "test-autoload3-{{timestamp}}.rdb"),
|
|
|
|
("rdb", "test-autoload4"),
|
|
|
|
("df", "test-autoload5"),
|
|
|
|
("rdb", "test-autoload6.rdb"),
|
|
|
|
]
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
@pytest.mark.parametrize("save_type, dbfilename", cases)
|
|
|
|
async def test_snapshot(self, df_local_factory, save_type, dbfilename):
|
2023-04-17 21:12:38 +00:00
|
|
|
df_args = {"dbfilename": dbfilename, **BASIC_ARGS, "port": 1111}
|
2023-04-17 11:59:44 +00:00
|
|
|
if save_type == 'rdb':
|
|
|
|
df_args['nodf_snapshot_format'] = ""
|
|
|
|
df_server = df_local_factory.create(**df_args)
|
|
|
|
df_server.start()
|
|
|
|
|
|
|
|
client = aioredis.Redis(port=df_server.port)
|
|
|
|
await client.set("TEST", hash(dbfilename))
|
|
|
|
await client.execute_command("SAVE " + save_type)
|
|
|
|
df_server.stop()
|
|
|
|
|
|
|
|
df_server2 = df_local_factory.create(**df_args)
|
|
|
|
df_server2.start()
|
|
|
|
client = aioredis.Redis(port=df_server.port)
|
|
|
|
response = await client.get("TEST")
|
|
|
|
assert response.decode("utf-8") == str(hash(dbfilename))
|
|
|
|
|
|
|
|
|
|
|
|
@dfly_args({**BASIC_ARGS, "dbfilename": "test-periodic", "save_schedule": "*:*"})
|
2022-10-24 18:22:43 +00:00
|
|
|
class TestPeriodicSnapshot(SnapshotTestBase):
|
|
|
|
"""Test periodic snapshotting"""
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup(self, tmp_dir: Path):
|
|
|
|
super().setup(tmp_dir)
|
|
|
|
|
2023-01-09 20:31:15 +00:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_snapshot(self, df_seeder_factory, df_server):
|
2023-04-20 04:30:42 +00:00
|
|
|
seeder = df_seeder_factory.create(
|
|
|
|
port=df_server.port, keys=10, multi_transaction_probability=0)
|
2023-01-09 20:31:15 +00:00
|
|
|
await seeder.run(target_deviation=0.5)
|
2022-08-26 10:54:38 +00:00
|
|
|
|
|
|
|
time.sleep(60)
|
|
|
|
|
2023-04-17 11:59:44 +00:00
|
|
|
assert super().get_main_file("test-periodic-summary.dfs")
|
2023-04-19 04:21:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dfly_args({**BASIC_ARGS})
|
|
|
|
class TestPathEscapes(SnapshotTestBase):
|
|
|
|
"""Test that we don't allow path escapes. We just check that df_server.start()
|
|
|
|
fails because we don't have a much better way to test that."""
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup(self, tmp_dir: Path):
|
|
|
|
super().setup(tmp_dir)
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_snapshot(self, df_local_factory):
|
2023-04-20 04:30:42 +00:00
|
|
|
df_server = df_local_factory.create(
|
|
|
|
dbfilename="../../../../etc/passwd")
|
2023-04-19 04:21:23 +00:00
|
|
|
try:
|
|
|
|
df_server.start()
|
|
|
|
assert False, "Server should not start correctly"
|
|
|
|
except Exception as e:
|
|
|
|
pass
|
2023-04-20 04:30:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dfly_args({**BASIC_ARGS, "dbfilename": "test-shutdown"})
|
|
|
|
class TestDflySnapshotOnShutdown(SnapshotTestBase):
|
|
|
|
"""Test multi file snapshot"""
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup(self, tmp_dir: Path):
|
|
|
|
self.tmp_dir = tmp_dir
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_snapshot(self, df_seeder_factory, df_server):
|
|
|
|
seeder = df_seeder_factory.create(port=df_server.port, **SEEDER_ARGS)
|
|
|
|
await seeder.run(target_deviation=0.1)
|
|
|
|
|
|
|
|
start_capture = await seeder.capture()
|
|
|
|
|
|
|
|
df_server.stop()
|
|
|
|
df_server.start()
|
|
|
|
|
|
|
|
a_client = aioredis.Redis(port=df_server.port)
|
|
|
|
await wait_available_async(a_client)
|
|
|
|
await a_client.connection_pool.disconnect()
|
|
|
|
|
|
|
|
assert await seeder.compare(start_capture)
|