From 75837152cb06eadcfb0c3aaf202d80edd01fb5ce Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Thu, 29 Feb 2024 12:46:12 +0100 Subject: [PATCH] better test --- src/Coordination/tests/gtest_coordination.cpp | 16 +------ .../integration/test_keeper_snapshots/test.py | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/Coordination/tests/gtest_coordination.cpp b/src/Coordination/tests/gtest_coordination.cpp index 2e640ddbbcd..07dfac0670e 100644 --- a/src/Coordination/tests/gtest_coordination.cpp +++ b/src/Coordination/tests/gtest_coordination.cpp @@ -1790,7 +1790,6 @@ void testLogAndStateMachine( keeper_context); changelog.init(state_machine->last_commit_index() + 1, settings->reserved_log_items); - std::optional latest_snapshot; for (size_t i = 1; i < total_logs + 1; ++i) { std::shared_ptr request = std::make_shared(); @@ -1819,7 +1818,7 @@ void testLogAndStateMachine( bool pop_result = snapshots_queue.pop(snapshot_task); EXPECT_TRUE(pop_result); - latest_snapshot = snapshot_task.create_snapshot(std::move(snapshot_task.snapshot)); + snapshot_task.create_snapshot(std::move(snapshot_task.snapshot)); } if (snapshot_created && changelog.size() > settings->reserved_log_items) @@ -1862,19 +1861,6 @@ void testLogAndStateMachine( auto path = "/hello_" + std::to_string(i); EXPECT_EQ(source_storage.container.getValue(path).getData(), restored_storage.container.getValue(path).getData()); } - - if (latest_snapshot.has_value()) - { - const auto & [path, disk] = *latest_snapshot; - EXPECT_TRUE(disk->exists(path)); - DB::WriteBufferFromFile plain_buf( - fs::path("./snapshots") / path, DB::DBMS_DEFAULT_BUFFER_SIZE, O_APPEND | O_CREAT | O_WRONLY); - plain_buf.truncate(0); - SnapshotsQueue snapshots_queue2{1}; - keeper_context = get_keeper_context(); - auto invalid_snapshot_machine = std::make_shared(queue, snapshots_queue2, keeper_context, nullptr); - ASSERT_DEATH(invalid_snapshot_machine->init(), "Aborting because of failure to load from latest snapshot with"); - } } TEST_P(CoordinationTest, TestStateMachineAndLogStore) diff --git a/tests/integration/test_keeper_snapshots/test.py b/tests/integration/test_keeper_snapshots/test.py index 2e126ed1152..afdc1a80e3e 100644 --- a/tests/integration/test_keeper_snapshots/test.py +++ b/tests/integration/test_keeper_snapshots/test.py @@ -161,3 +161,45 @@ def test_ephemeral_after_restart(started_cluster): node_zk2.close() except: pass + + +def test_invalid_snapshot(started_cluster): + keeper_utils.wait_until_connected(started_cluster, node) + node_zk = None + try: + node_zk = get_connection_zk("node") + node_zk.create("/test_invalid_snapshot", b"somevalue") + keeper_utils.send_4lw_cmd(started_cluster, node, "csnp") + node.stop_clickhouse() + snapshots = ( + node.exec_in_container(["ls", "/var/lib/clickhouse/coordination/snapshots"]) + .strip() + .split("\n") + ) + + def snapshot_sort_key(snapshot_name): + snapshot_prefix_size = len("snapshot_") + last_log_idx = snapshot_name.split(".")[0][snapshot_prefix_size:] + return int(last_log_idx) + + snapshots.sort(key=snapshot_sort_key) + last_snapshot = snapshots[-1] + node.exec_in_container( + [ + "truncate", + "-s", + "0", + f"/var/lib/clickhouse/coordination/snapshots/{last_snapshot}", + ] + ) + node.start_clickhouse(expected_to_fail=True) + assert node.contains_in_log( + "Aborting because of failure to load from latest snapshot with index" + ) + finally: + try: + if node_zk is not None: + node_zk.stop() + node_zk.close() + except: + pass