void replication_service_test_app::read_backup_metadata_test()
{
    cold_backup_context_ptr backup_context =
        new cold_backup_context(nullptr, request, concurrent_uploading_file_cnt);

    backup_context->start_check();
    backup_context->block_service = block_service.get();
    backup_context->backup_root = backup_root;

    backup_context->_status.store(cold_backup_status::ColdBackupUploading);
    // case1: metadata is valid
    //  stop with create current_chkpt_file fail
    {
        std::cout << "testing read_backup_metadata_file, with context of metadata is valid..."
                  << std::endl;
        blob buf = ::json::json_forwarder<cold_backup_metadata>::encode(backup_context->_metadata);
        std::string context(buf.data(), buf.length());
        backup_metadata_file->set_context(context);
        backup_metadata_file->file_exist("test_md5", 10);
        block_service->enable_create_file_fail = true;
        ref_ptr<block_file> file_handle = backup_metadata_file.get();
        backup_context->read_backup_metadata(file_handle);
        ASSERT_TRUE(backup_context->status() == cold_backup_status::ColdBackupFailed);
        block_service->enable_create_file_fail = false;
        backup_metadata_file->clear_context();
        backup_metadata_file->clear_file_exist();
    }
    backup_context->_status.store(cold_backup_status::ColdBackupUploading);
    // case2: metadata is invalid
    //  stop with create current_chkpt_file fail
    {
        std::cout << "testing read_backup_metada_file, with context of metadata is invalid..."
                  << std::endl;
        backup_metadata_file->file_exist("test_md5", 10);
        backup_metadata_file->set_context("{\"key\":value\"");
        block_service->enable_create_file_fail = true;
        ref_ptr<block_file> file_handle = backup_metadata_file.get();
        backup_context->read_backup_metadata(file_handle);
        ASSERT_TRUE(backup_context->status() == cold_backup_status::ColdBackupFailed);
        block_service->enable_create_file_fail = false;
        backup_metadata_file->clear_file_exist();
        backup_metadata_file->clear_context();
    }
    // case3: read metadata fail
    //  this case has been already tested before, here just ignore
    ASSERT_TRUE(backup_context->get_count() == 1);
    ASSERT_TRUE(current_chkpt_file->get_count() == 1);
    ASSERT_TRUE(backup_metadata_file->get_count() == 1);
    ASSERT_TRUE(regular_file->get_count() == 1);
}
void replication_service_test_app::read_current_chkpt_file_test()
{
    cold_backup_context_ptr backup_context =
        new cold_backup_context(nullptr, request, concurrent_uploading_file_cnt);
    backup_context->_status.store(cold_backup_status::ColdBackupChecking);
    backup_context->block_service = block_service.get();
    backup_context->backup_root = backup_root;
    // read current_chkpt_file fail has been already tested in check_backup_on_remote_test()
    // case1: current_chkpt_file is not exist
    {
        std::cout << "testing read_current_chkpt_file(file not exist)..." << std::endl;
        current_chkpt_file->clear_file_exist();
        block_file_ptr file_handle = current_chkpt_file.get();
        backup_context->read_current_chkpt_file(file_handle);
        ASSERT_TRUE(backup_context->status() == cold_backup_status::ColdBackupChecked);
    }
    backup_context->_status.store(cold_backup_status::ColdBackupChecking);
    // case2: current_chkpt_file exist
    //  this case will call remote_chkpt_dir_exist(), so we make list_dir fail to stop
    {
        std::cout
            << "testing read_current_chkpt_file(file exist and check whether chkpt_dir is exist)..."
            << std::endl;
        current_chkpt_file->file_exist("123", 10);
        current_chkpt_file->set_context("test_dir");
        block_service->enable_list_dir_fail = true;
        block_file_ptr file_handle = current_chkpt_file.get();
        backup_context->read_current_chkpt_file(file_handle);
        ASSERT_TRUE(backup_context->status() == cold_backup_status::ColdBackupFailed);
        current_chkpt_file->clear_file_exist();
        current_chkpt_file->clear_context();
        block_service->enable_list_dir_fail = false;
    }
    ASSERT_TRUE(backup_context->get_count() == 1);
    ASSERT_TRUE(current_chkpt_file->get_count() == 1);
    ASSERT_TRUE(backup_metadata_file->get_count() == 1);
    ASSERT_TRUE(regular_file->get_count() == 1);
}
void replication_service_test_app::remote_chkpt_dir_exist_test()
{
    gpid mock_gpid(1, 2);
    std::string mock_app_name("mock_app");
    int64_t mock_backup_id(1000);
    std::string mock_backup_provider_name("mock_backup_provider_name");
    request.__set_pid(mock_gpid);
    request.__set_app_name(mock_app_name);
    request.__set_backup_id(mock_backup_id);
    policy_info mock_policy_info;
    mock_policy_info.__set_backup_provider_type("mock_service");
    mock_policy_info.__set_policy_name("mock_policy");
    request.__set_policy(mock_policy_info);
    // the case that list_dir fail has been already tested
    cold_backup_context_ptr backup_context =
        new cold_backup_context(nullptr, request, concurrent_uploading_file_cnt);

    backup_context->start_check();
    backup_context->block_service = block_service.get();
    backup_context->backup_root = backup_root;
    backup_context->_status.store(cold_backup_status::ColdBackupChecking);

    // case1: directory is exist
    {
        std::cout << "testing remote checkpoint directory is exist..." << std::endl;
        std::string dir_name = std::string("test_dir");
        current_chkpt_file->file_exist("123", 10);
        current_chkpt_file->set_context(dir_name);

        std::string parent_dir = cold_backup::get_replica_backup_path(
            backup_root, mock_policy_info.policy_name, mock_app_name, mock_gpid, mock_backup_id);

        std::vector<ls_entry> entries;
        entries.emplace_back(ls_entry{std::string(dir_name), true});
        // remote_chkpt_dir_exist() function judge whether the dir-A is exist through listing
        //      the dir-A's parent path
        block_service->dir_files.insert(
            std::make_pair(::dsn::utils::filesystem::get_file_name(parent_dir), entries));
        backup_context->remote_chkpt_dir_exist(dir_name);
        ASSERT_TRUE(backup_context->status() == cold_backup_status::ColdBackupCompleted);
        current_chkpt_file->clear_file_exist();
        current_chkpt_file->clear_context();
        block_service->dir_files.clear();
    }
    backup_context->_status.store(cold_backup_status::ColdBackupChecking);
    // case2: directory is not exist
    {
        std::cout << "testing remote checkpoint directory is not exist..." << std::endl;
        std::string dir_name = std::string("test_dir");
        current_chkpt_file->file_exist("123", 10);
        current_chkpt_file->set_context(dir_name);
        backup_context->remote_chkpt_dir_exist(dir_name);
        ASSERT_TRUE(backup_context->status() == cold_backup_status::ColdBackupChecked);
        current_chkpt_file->clear_file_exist();
        current_chkpt_file->clear_context();
    }
    ASSERT_TRUE(backup_context->get_count() == 1);
    ASSERT_TRUE(current_chkpt_file->get_count() == 1);
    ASSERT_TRUE(backup_metadata_file->get_count() == 1);
    ASSERT_TRUE(regular_file->get_count() == 1);
}