|
| 1 | +from pathlib import Path |
| 2 | +from typing import Any |
| 3 | +import pytest |
| 4 | +from common import CommonScenario, ResultCode |
| 5 | +from testing_utils import ScenarioResult, LogContainer |
| 6 | + |
| 7 | +pytestmark = pytest.mark.parametrize("version", ["rust"], scope="class") |
| 8 | + |
| 9 | + |
| 10 | +class TestSnapshotCountFirstFlush(CommonScenario): |
| 11 | + @pytest.fixture(scope="class") |
| 12 | + def scenario_name(self) -> str: |
| 13 | + return "cit.snapshots.count" |
| 14 | + |
| 15 | + @pytest.fixture(scope="class") |
| 16 | + def test_config(self, temp_dir: Path) -> dict[str, Any]: |
| 17 | + return {"kvs_parameters": {"instance_id": 1, "dir": str(temp_dir)}, "count": 1} |
| 18 | + |
| 19 | + def test_ok( |
| 20 | + self, |
| 21 | + test_config: dict[str, Any], |
| 22 | + results: ScenarioResult, |
| 23 | + logs_info_level: LogContainer, |
| 24 | + ): |
| 25 | + max_count = 3 |
| 26 | + assert results.return_code == ResultCode.SUCCESS |
| 27 | + |
| 28 | + count = test_config["count"] |
| 29 | + logs = logs_info_level.get_logs("snapshot_count") |
| 30 | + assert len(logs) == count + 1 |
| 31 | + for i in range(count): |
| 32 | + expected = min(i, max_count) |
| 33 | + assert logs[i].snapshot_count == expected |
| 34 | + |
| 35 | + assert logs[-1].snapshot_count == min(count, max_count) |
| 36 | + |
| 37 | + |
| 38 | +class TestSnapshotCountFull(TestSnapshotCountFirstFlush): |
| 39 | + @pytest.fixture(scope="class") |
| 40 | + def test_config(self, temp_dir: Path) -> dict[str, Any]: |
| 41 | + return {"kvs_parameters": {"instance_id": 1, "dir": str(temp_dir)}, "count": 4} |
| 42 | + |
| 43 | + |
| 44 | +class TestSnapshotMaxCount(CommonScenario): |
| 45 | + @pytest.fixture(scope="class") |
| 46 | + def scenario_name(self) -> str: |
| 47 | + return "cit.snapshots.max_count" |
| 48 | + |
| 49 | + @pytest.fixture(scope="class") |
| 50 | + def test_config(self, temp_dir: Path) -> dict[str, Any]: |
| 51 | + return {"kvs_parameters": {"instance_id": 1, "dir": str(temp_dir)}} |
| 52 | + |
| 53 | + def test_ok(self, results: ScenarioResult, logs_info_level: LogContainer): |
| 54 | + assert results.return_code == ResultCode.SUCCESS |
| 55 | + assert logs_info_level.find_log("max_count", value=3) is not None |
| 56 | + |
| 57 | + |
| 58 | +class TestSnapshotRestorePrevious(CommonScenario): |
| 59 | + @pytest.fixture(scope="class") |
| 60 | + def scenario_name(self) -> str: |
| 61 | + return "cit.snapshots.restore" |
| 62 | + |
| 63 | + @pytest.fixture(scope="class") |
| 64 | + def test_config(self, temp_dir: Path) -> dict[str, Any]: |
| 65 | + return { |
| 66 | + "kvs_parameters": {"instance_id": 1, "dir": str(temp_dir)}, |
| 67 | + "snapshot_id": 1, |
| 68 | + "count": 3, |
| 69 | + } |
| 70 | + |
| 71 | + def test_ok( |
| 72 | + self, |
| 73 | + results: ScenarioResult, |
| 74 | + logs_info_level: LogContainer, |
| 75 | + ): |
| 76 | + assert results.return_code == ResultCode.SUCCESS |
| 77 | + |
| 78 | + result_log = logs_info_level.find_log("result") |
| 79 | + assert result_log is not None |
| 80 | + assert result_log.result == "Ok(())" |
| 81 | + |
| 82 | + value_log = logs_info_level.find_log("value") |
| 83 | + assert value_log is not None |
| 84 | + assert value_log.value == 1 |
| 85 | + |
| 86 | + |
| 87 | +class TestSnapshotRestoreCurrent(CommonScenario): |
| 88 | + @pytest.fixture(scope="class") |
| 89 | + def scenario_name(self) -> str: |
| 90 | + return "cit.snapshots.restore" |
| 91 | + |
| 92 | + @pytest.fixture(scope="class") |
| 93 | + def test_config(self, temp_dir: Path) -> dict[str, Any]: |
| 94 | + return { |
| 95 | + "kvs_parameters": {"instance_id": 1, "dir": str(temp_dir)}, |
| 96 | + "snapshot_id": 0, |
| 97 | + "count": 3, |
| 98 | + } |
| 99 | + |
| 100 | + def capture_stderr(self) -> bool: |
| 101 | + return True |
| 102 | + |
| 103 | + def test_error( |
| 104 | + self, |
| 105 | + results: ScenarioResult, |
| 106 | + logs_info_level: LogContainer, |
| 107 | + ): |
| 108 | + assert results.return_code == ResultCode.SUCCESS |
| 109 | + |
| 110 | + assert results.stderr is not None |
| 111 | + assert "error: tried to restore current KVS as snapshot" in results.stderr |
| 112 | + |
| 113 | + result_log = logs_info_level.find_log("result") |
| 114 | + assert result_log is not None |
| 115 | + assert result_log.result == "Err(InvalidSnapshotId)" |
| 116 | + |
| 117 | + |
| 118 | +class TestSnapshotRestoreNonexistent(CommonScenario): |
| 119 | + @pytest.fixture(scope="class") |
| 120 | + def scenario_name(self) -> str: |
| 121 | + return "cit.snapshots.restore" |
| 122 | + |
| 123 | + @pytest.fixture(scope="class") |
| 124 | + def test_config(self, temp_dir: Path) -> dict[str, Any]: |
| 125 | + return { |
| 126 | + "kvs_parameters": {"instance_id": 1, "dir": str(temp_dir)}, |
| 127 | + "snapshot_id": 2, |
| 128 | + "count": 1, |
| 129 | + } |
| 130 | + |
| 131 | + def capture_stderr(self) -> bool: |
| 132 | + return True |
| 133 | + |
| 134 | + def test_error( |
| 135 | + self, |
| 136 | + results: ScenarioResult, |
| 137 | + logs_info_level: LogContainer, |
| 138 | + ): |
| 139 | + assert results.return_code == ResultCode.SUCCESS |
| 140 | + |
| 141 | + assert results.stderr is not None |
| 142 | + assert "error: tried to restore a non-existing snapshot" in results.stderr |
| 143 | + |
| 144 | + result_log = logs_info_level.find_log("result") |
| 145 | + assert result_log is not None |
| 146 | + assert result_log.result == "Err(InvalidSnapshotId)" |
| 147 | + |
| 148 | + |
| 149 | +class TestSnapshotPathsExist(CommonScenario): |
| 150 | + @pytest.fixture(scope="class") |
| 151 | + def scenario_name(self) -> str: |
| 152 | + return "cit.snapshots.paths" |
| 153 | + |
| 154 | + @pytest.fixture(scope="class") |
| 155 | + def test_config(self, temp_dir: Path) -> dict[str, Any]: |
| 156 | + return { |
| 157 | + "kvs_parameters": {"instance_id": 1, "dir": str(temp_dir)}, |
| 158 | + "snapshot_id": 1, |
| 159 | + "count": 3, |
| 160 | + } |
| 161 | + |
| 162 | + def test_ok( |
| 163 | + self, |
| 164 | + temp_dir: Path, |
| 165 | + results: ScenarioResult, |
| 166 | + logs_info_level: LogContainer, |
| 167 | + ): |
| 168 | + assert results.return_code == ResultCode.SUCCESS |
| 169 | + |
| 170 | + paths_log = logs_info_level.find_log("kvs_path") |
| 171 | + assert paths_log is not None |
| 172 | + assert paths_log.kvs_path == f'Ok("{temp_dir}/kvs_1_1.json")' |
| 173 | + assert paths_log.hash_path == f'Ok("{temp_dir}/kvs_1_1.hash")' |
| 174 | + |
| 175 | + |
| 176 | +class TestSnapshotPathsNonexistent(CommonScenario): |
| 177 | + @pytest.fixture(scope="class") |
| 178 | + def scenario_name(self) -> str: |
| 179 | + return "cit.snapshots.paths" |
| 180 | + |
| 181 | + @pytest.fixture(scope="class") |
| 182 | + def test_config(self, temp_dir: Path) -> dict[str, Any]: |
| 183 | + return { |
| 184 | + "kvs_parameters": {"instance_id": 1, "dir": str(temp_dir)}, |
| 185 | + "snapshot_id": 2, |
| 186 | + "count": 1, |
| 187 | + } |
| 188 | + |
| 189 | + def test_error( |
| 190 | + self, |
| 191 | + results: ScenarioResult, |
| 192 | + logs_info_level: LogContainer, |
| 193 | + ): |
| 194 | + assert results.return_code == ResultCode.SUCCESS |
| 195 | + |
| 196 | + paths_log = logs_info_level.find_log("kvs_path") |
| 197 | + assert paths_log is not None |
| 198 | + assert paths_log.kvs_path == "Err(FileNotFound)" |
| 199 | + assert paths_log.hash_path == "Err(FileNotFound)" |
0 commit comments