Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(function_test): make [backup_]restore_test derive from test_util #1710

Merged
merged 4 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/function_test/backup_restore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
115 changes: 34 additions & 81 deletions src/test/function_test/backup_restore/test_backup_and_restore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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<rpc_address> 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<replication_ddl_client>(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;
Expand All @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -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) {
Expand All @@ -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<partition_configuration> 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;
Expand All @@ -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<replication_ddl_client> _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)
Expand Down
2 changes: 0 additions & 2 deletions src/test/function_test/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading
Loading