From 18e378dc83f4bba6191a9faf4f59e6d1a9954c4b Mon Sep 17 00:00:00 2001 From: Yingchun Lai Date: Wed, 6 Dec 2023 10:51:53 +0800 Subject: [PATCH] refactor(function_test): make [backup_]restore_test derive from test_util (#1710) There is no functional changes in this patch, but only make class backup_restore_test and restore_test derive from test_util. --- run.sh | 2 +- .../backup_restore/CMakeLists.txt | 2 +- .../test_backup_and_restore.cpp | 115 ++++++------------ src/test/function_test/config.ini | 2 - .../function_test/restore/test_restore.cpp | 87 ++++--------- src/test/function_test/utils/test_util.cpp | 2 +- src/test/kill_test/config.ini | 1 - 7 files changed, 64 insertions(+), 147 deletions(-) diff --git a/run.sh b/run.sh index dd81061022..5e46da9e47 100755 --- a/run.sh +++ b/run.sh @@ -473,7 +473,7 @@ function run_test() opts="cold_backup_disabled=false,cold_backup_checkpoint_reserve_minutes=0,cold_backup_root=onebox" fi if [ "${module}" == "restore_test" ]; then - opts="cold_backup_disabled=false,cold_backup_checkpoint_reserve_minutes=0,cold_backup_root=mycluster" + opts="cold_backup_disabled=false,cold_backup_checkpoint_reserve_minutes=0,cold_backup_root=onebox" fi [ -z ${onebox_opts} ] || opts="${opts},${onebox_opts}" if ! run_start_onebox -m ${m_count} -w -c --opts ${opts}; then diff --git a/src/test/function_test/backup_restore/CMakeLists.txt b/src/test/function_test/backup_restore/CMakeLists.txt index 51fea317ea..4b12451ece 100644 --- a/src/test/function_test/backup_restore/CMakeLists.txt +++ b/src/test/function_test/backup_restore/CMakeLists.txt @@ -24,12 +24,12 @@ set(MY_PROJ_LIBS dsn_client dsn_replication_common dsn_utils - pegasus_client_static gtest sasl2 gssapi_krb5 krb5 function_test_utils + pegasus_client_static ) set(MY_BOOST_LIBS Boost::system Boost::filesystem) diff --git a/src/test/function_test/backup_restore/test_backup_and_restore.cpp b/src/test/function_test/backup_restore/test_backup_and_restore.cpp index d5a6cefda4..12dceb930b 100644 --- a/src/test/function_test/backup_restore/test_backup_and_restore.cpp +++ b/src/test/function_test/backup_restore/test_backup_and_restore.cpp @@ -25,12 +25,12 @@ #include "backup_types.h" #include "base/pegasus_const.h" #include "client/replication_ddl_client.h" -#include "common/replication_other_types.h" #include "dsn.layer2_types.h" #include "gtest/gtest.h" #include "include/pegasus/client.h" #include "include/pegasus/error.h" #include "runtime/rpc/rpc_address.h" +#include "test/function_test/utils/test_util.h" #include "utils/error_code.h" #include "utils/errors.h" #include "utils/utils.h" @@ -39,54 +39,21 @@ using namespace dsn; using namespace dsn::replication; using namespace pegasus; -// TODO(yingchun): backup & restore festure is on refactoring, we can refactor the related function -// test later. -class backup_restore_test : public testing::Test +class backup_restore_test : public test_util { public: - backup_restore_test() - : _ddl_client(nullptr), - _num_of_rows(1000), - _check_interval_sec(10), - _cluster_name("onebox"), - _old_app_name("test_app"), - _new_app_name("new_app"), - _provider("local_service") - { - } - - static void SetUpTestCase() { ASSERT_TRUE(pegasus_client_factory::initialize("config.ini")); } - - void SetUp() override - { - // initialize ddl_client - std::vector meta_list; - ASSERT_TRUE(replica_helper::load_meta_servers( - meta_list, PEGASUS_CLUSTER_SECTION_NAME.c_str(), _cluster_name.c_str())); - ASSERT_FALSE(meta_list.empty()); - _ddl_client = std::make_shared(meta_list); - ASSERT_TRUE(_ddl_client != nullptr); - } - void TearDown() override { - ASSERT_EQ(ERR_OK, _ddl_client->drop_app(_old_app_name, 0)); - ASSERT_EQ(ERR_OK, _ddl_client->drop_app(_new_app_name, 0)); + ASSERT_EQ(ERR_OK, ddl_client_->drop_app(app_name_, 0)); + ASSERT_EQ(ERR_OK, ddl_client_->drop_app(s_new_app_name, 0)); } bool write_data() { - pegasus::pegasus_client *client = pegasus::pegasus_client_factory::get_client( - _cluster_name.c_str(), _old_app_name.c_str()); - if (client == nullptr) { - std::cout << "get pegasus client failed" << std::endl; - return false; - } - - for (int i = 0; i < _num_of_rows; ++i) { - int ret = client->set("hashkey_" + std::to_string(i), - "sortkey_" + std::to_string(i), - "value_" + std::to_string(i)); + for (int i = 0; i < s_num_of_rows; ++i) { + int ret = client_->set("hashkey_" + std::to_string(i), + "sortkey_" + std::to_string(i), + "value_" + std::to_string(i)); if (ret != pegasus::PERR_OK) { std::cout << "write data failed. " << std::endl; return false; @@ -97,18 +64,11 @@ class backup_restore_test : public testing::Test bool verify_data(const std::string &app_name) { - pegasus::pegasus_client *client = - pegasus::pegasus_client_factory::get_client(_cluster_name.c_str(), app_name.c_str()); - if (client == nullptr) { - std::cout << "get pegasus client failed" << std::endl; - return false; - } - - for (int i = 0; i < _num_of_rows; ++i) { + for (int i = 0; i < s_num_of_rows; ++i) { const std::string &expected_value = "value_" + std::to_string(i); std::string value; int ret = - client->get("hashkey_" + std::to_string(i), "sortkey_" + std::to_string(i), value); + client_->get("hashkey_" + std::to_string(i), "sortkey_" + std::to_string(i), value); if (ret != pegasus::PERR_OK) { return false; } @@ -121,23 +81,23 @@ class backup_restore_test : public testing::Test start_backup_app_response start_backup(const std::string &user_specified_path = "") { - return _ddl_client->backup_app(_old_app_id, _provider, user_specified_path).get_value(); + return ddl_client_->backup_app(app_id_, s_provider_type, user_specified_path).get_value(); } query_backup_status_response query_backup(int64_t backup_id) { - return _ddl_client->query_backup(_old_app_id, backup_id).get_value(); + return ddl_client_->query_backup(app_id_, backup_id).get_value(); } error_code start_restore(int64_t backup_id, const std::string &user_specified_path = "") { - return _ddl_client->do_restore(_provider, - _cluster_name, + return ddl_client_->do_restore(s_provider_type, + cluster_name_, /*policy_name=*/"", backup_id, - _old_app_name, - _old_app_id, - _new_app_name, + app_name_, + app_id_, + s_new_app_name, /*skip_bad_partition=*/false, user_specified_path); } @@ -148,8 +108,8 @@ class backup_restore_test : public testing::Test bool is_backup_complete = false; while (!is_backup_complete && sleep_sec <= max_sleep_seconds) { std::cout << "sleep a while to wait backup complete." << std::endl; - sleep(_check_interval_sec); - sleep_sec += _check_interval_sec; + sleep(s_check_interval_sec); + sleep_sec += s_check_interval_sec; auto resp = query_backup(backup_id); if (resp.err != ERR_OK) { @@ -168,13 +128,12 @@ class backup_restore_test : public testing::Test bool is_app_healthy = false; while (!is_app_healthy && sleep_sec <= max_sleep_seconds) { std::cout << "sleep a while to wait app become healthy." << std::endl; - sleep(_check_interval_sec); - sleep_sec += _check_interval_sec; + sleep(s_check_interval_sec); + sleep_sec += s_check_interval_sec; - int32_t new_app_id; int32_t partition_count; std::vector partitions; - auto err = _ddl_client->list_app(app_name, _old_app_id, partition_count, partitions); + auto err = ddl_client_->list_app(app_name, app_id_, partition_count, partitions); if (err != ERR_OK) { std::cout << "list app " + app_name + " failed" << std::endl; return false; @@ -196,37 +155,31 @@ class backup_restore_test : public testing::Test void test_backup_and_restore(const std::string &user_specified_path = "") { - error_code err = _ddl_client->create_app(_old_app_name, "pegasus", 4, 3, {}, false); - ASSERT_EQ(ERR_OK, err); - ASSERT_TRUE(wait_app_become_healthy(_old_app_name, 180)); + ASSERT_TRUE(wait_app_become_healthy(app_name_, 180)); ASSERT_TRUE(write_data()); - ASSERT_TRUE(verify_data(_old_app_name)); + ASSERT_TRUE(verify_data(app_name_)); auto resp = start_backup(user_specified_path); ASSERT_EQ(ERR_OK, resp.err); int64_t backup_id = resp.backup_id; ASSERT_TRUE(wait_backup_complete(backup_id, 180)); - err = start_restore(backup_id, user_specified_path); - ASSERT_EQ(ERR_OK, err); - ASSERT_TRUE(wait_app_become_healthy(_new_app_name, 180)); + ASSERT_EQ(ERR_OK, start_restore(backup_id, user_specified_path)); + ASSERT_TRUE(wait_app_become_healthy(s_new_app_name, 180)); - ASSERT_TRUE(verify_data(_new_app_name)); + ASSERT_TRUE(verify_data(s_new_app_name)); } private: - std::shared_ptr _ddl_client; - - const uint32_t _num_of_rows; - const uint8_t _check_interval_sec; - const std::string _cluster_name; - const std::string _old_app_name; - const std::string _new_app_name; - const std::string _provider; - - int32_t _old_app_id; + static const uint32_t s_num_of_rows = 1000; + static const uint8_t s_check_interval_sec = 10; + static const std::string s_new_app_name; + static const std::string s_provider_type; }; +const std::string backup_restore_test::s_new_app_name = "new_app"; +const std::string backup_restore_test::s_provider_type = "local_service"; + TEST_F(backup_restore_test, test_backup_and_restore) { test_backup_and_restore(); } TEST_F(backup_restore_test, test_backup_and_restore_with_user_specified_path) diff --git a/src/test/function_test/config.ini b/src/test/function_test/config.ini index 01c7bae703..21ede3ff24 100644 --- a/src/test/function_test/config.ini +++ b/src/test/function_test/config.ini @@ -75,8 +75,6 @@ rpc_timeout_milliseconds = 5000 lb_interval_ms = 3000 [pegasus.clusters] -; TODO(yingchun): leave only 1 cluster config -mycluster = 127.0.0.1:34601,127.0.0.1:34602,127.0.0.1:34603 onebox = 127.0.0.1:34601,127.0.0.1:34602,127.0.0.1:34603 single_master_cluster = 127.0.0.1:34601 diff --git a/src/test/function_test/restore/test_restore.cpp b/src/test/function_test/restore/test_restore.cpp index 3ee5003b75..a2350a4088 100644 --- a/src/test/function_test/restore/test_restore.cpp +++ b/src/test/function_test/restore/test_restore.cpp @@ -33,7 +33,6 @@ #include "client/partition_resolver.h" #include "client/replication_ddl_client.h" #include "common/gpid.h" -#include "common/replication_other_types.h" #include "dsn.layer2_types.h" #include "gtest/gtest.h" #include "include/pegasus/client.h" @@ -41,6 +40,7 @@ #include "runtime/api_layer1.h" #include "runtime/rpc/rpc_address.h" #include "test/function_test/utils/global_env.h" +#include "test/function_test/utils/test_util.h" #include "test_util/test_util.h" #include "utils/error_code.h" #include "utils/filesystem.h" @@ -51,57 +51,32 @@ using namespace ::dsn; using namespace ::dsn::replication; using namespace pegasus; -// TODO(yingchun): backup & restore festure is on refactoring, we can refactor the related function -// test later. -class restore_test : public testing::Test +class restore_test : public test_util { public: - static void SetUpTestCase() { ASSERT_TRUE(pegasus_client_factory::initialize("config.ini")); } - void SetUp() override { + test_util::SetUp(); + std::string provider_dir = "block_service/local_service"; policy_dir = "onebox/" + provider_dir + '/' + - dsn::utils::filesystem::path_combine(cluster_name, policy_name); - backup_dir = "onebox/" + provider_dir + '/' + cluster_name; - - std::vector meta_list; - ASSERT_TRUE(replica_helper::load_meta_servers( - meta_list, PEGASUS_CLUSTER_SECTION_NAME.c_str(), cluster_name.c_str())); - ASSERT_FALSE(meta_list.empty()); - ddl_client = std::make_shared(meta_list); - ASSERT_TRUE(ddl_client != nullptr); - error_code err = - ddl_client->create_app(app_name, "pegasus", default_partition_cnt, 3, {}, false); - ASSERT_EQ(err, ERR_OK); - int32_t app_id = 0; - { - int32_t partition_id; - std::vector partitions; - err = ddl_client->list_app(app_name, app_id, partition_id, partitions); - ASSERT_EQ(err, ERR_OK); - old_app_id = app_id; - } - ASSERT_GE(app_id, 0); - pg_client = - pegasus::pegasus_client_factory::get_client(cluster_name.c_str(), app_name.c_str()); - ASSERT_NE(pg_client, nullptr); + dsn::utils::filesystem::path_combine(cluster_name_, policy_name); + backup_dir = "onebox/" + provider_dir + '/' + cluster_name_; write_data(); - std::vector app_ids; - app_ids.emplace_back(app_id); - err = ddl_client->add_backup_policy(policy_name, - backup_provider_name, - app_ids, - backup_interval_seconds, - backup_history_count_to_keep, - start_time); + std::vector app_ids({app_id_}); + auto err = ddl_client_->add_backup_policy(policy_name, + backup_provider_name, + app_ids, + backup_interval_seconds, + backup_history_count_to_keep, + start_time); std::cout << "add backup policy complete with err = " << err.to_string() << std::endl; ASSERT_EQ(err, ERR_OK); } - void TearDown() override { ASSERT_EQ(ERR_OK, ddl_client->drop_app(app_name, 0)); } + void TearDown() override { ASSERT_EQ(ERR_OK, ddl_client_->drop_app(app_name_, 0)); } void write_data() { @@ -109,13 +84,13 @@ class restore_test : public testing::Test << std::endl; int64_t start = dsn_now_ms(); int err = PERR_OK; - ASSERT_NE(pg_client, nullptr); + ASSERT_NE(client_, nullptr); for (int i = 1; i <= kv_pair_cnt; i++) { std::string index = std::to_string(i); std::string h_key = hash_key_prefix + "_" + index; std::string s_key = sort_key_prefix + "_" + index; std::string value = value_prefix + "_" + index; - err = pg_client->set(h_key, s_key, value); + err = client_->set(h_key, s_key, value); ASSERT_EQ(err, PERR_OK); } int64_t end = dsn_now_ms(); @@ -127,8 +102,8 @@ class restore_test : public testing::Test { std::cout << "start to get " << kv_pair_cnt << " key-value pairs, using get()..." << std::endl; - new_pg_client = - pegasus::pegasus_client_factory::get_client(cluster_name.c_str(), new_app_name.c_str()); + pegasus_client *new_pg_client = pegasus::pegasus_client_factory::get_client( + cluster_name_.c_str(), new_app_name.c_str()); ASSERT_NE(nullptr, new_pg_client); int64_t start = dsn_now_ms(); @@ -151,14 +126,14 @@ class restore_test : public testing::Test { std::this_thread::sleep_for(std::chrono::seconds(30)); ASSERT_EQ(ERR_OK, - ddl_client->do_restore(backup_provider_name, - cluster_name, - /*old_policy_name=*/"", - time_stamp, - app_name, - old_app_id, - new_app_name, - false)); + ddl_client_->do_restore(backup_provider_name, + cluster_name_, + /*old_policy_name=*/"", + time_stamp, + app_name_, + app_id_, + new_app_name, + false)); ASSERT_NO_FATAL_FAILURE(wait_app_healthy()); } @@ -170,7 +145,7 @@ class restore_test : public testing::Test int32_t partition_cnt = 0; std::vector p_confs; ASSERT_EQ(ERR_OK, - ddl_client->list_app(new_app_name, app_id, partition_cnt, p_confs)); + ddl_client_->list_app(new_app_name, app_id, partition_cnt, p_confs)); for (int i = 0; i < p_confs.size(); i++) { const auto &pc = p_confs[i]; ASSERT_FALSE(pc.primary.is_invalid()); @@ -245,15 +220,10 @@ class restore_test : public testing::Test } public: - pegasus_client *pg_client; - pegasus_client *new_pg_client; - std::shared_ptr ddl_client; std::string policy_dir; std::string backup_dir; - const std::string cluster_name = "mycluster"; const std::string new_app_name = "backup_test_new"; - int32_t old_app_id; int64_t time_stamp; const std::string policy_name = "policy_1"; @@ -269,14 +239,11 @@ class restore_test : public testing::Test const int backup_history_count_to_keep = 6; const std::string start_time = "24:0"; - const std::string app_name = "backup_test"; - const std::string hash_key_prefix = "hash_key"; const std::string sort_key_prefix = "sort_key"; const std::string value_prefix = "value"; const int kv_pair_cnt = 10000; - const int default_partition_cnt = 8; }; TEST_F(restore_test, restore) diff --git a/src/test/function_test/utils/test_util.cpp b/src/test/function_test/utils/test_util.cpp index 81fded73b4..3a7cb3870b 100644 --- a/src/test/function_test/utils/test_util.cpp +++ b/src/test/function_test/utils/test_util.cpp @@ -54,7 +54,7 @@ using std::vector; namespace pegasus { test_util::test_util(map create_envs) - : cluster_name_("mycluster"), app_name_("temp"), create_envs_(std::move(create_envs)) + : cluster_name_("onebox"), app_name_("temp"), create_envs_(std::move(create_envs)) { } diff --git a/src/test/kill_test/config.ini b/src/test/kill_test/config.ini index a40ac94910..426bd2a61e 100644 --- a/src/test/kill_test/config.ini +++ b/src/test/kill_test/config.ini @@ -114,5 +114,4 @@ onebox_run_path = @ONEBOX_RUN_PATH@ [pegasus.clusters] onebox = @LOCAL_HOSTNAME@:34601,@LOCAL_HOSTNAME@:34602,@LOCAL_HOSTNAME@:34603 -mycluster = @LOCAL_HOSTNAME@:34601,@LOCALOCAL_HOSTNAMEL_IP@:34602,@LOCAL_HOSTNAME@:34603