Skip to content

Commit

Permalink
fix snaptool bug
Browse files Browse the repository at this point in the history
  • Loading branch information
cw123 authored and YunhuiChen committed Aug 17, 2020
1 parent b27c892 commit a9c30ec
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/common/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ static bool StringToUll(const std::string &value, uint64_t *out) {
}
}

static bool StringToInt(const std::string &value, int32_t *out) {
try {
*out = std::stoi(value);
return true;
} catch (std::invalid_argument &e) {
LOG(ERROR) << "decode string:{" << value << "} to number err:"
<< e.what();
return false;
} catch (std::out_of_range &e) {
LOG(ERROR) << "decode string:{" << value << "} to number err:"
<< e.what();
return false;
}
}

} // namespace common
} // namespace curve

Expand Down
20 changes: 18 additions & 2 deletions src/snapshotcloneserver/clone/clone_service_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "src/snapshotcloneserver/common/snapshotclone_metric.h"
#include "src/snapshotcloneserver/common/define.h"
#include "src/common/string_util.h"

namespace curve {
namespace snapshotcloneserver {
Expand Down Expand Up @@ -418,15 +419,30 @@ bool CloneFilterCondition::IsMatchCondition(const CloneInfo &cloneInfo) {
return false;
}

int status;
if (status_ != nullptr
&& std::stoi(*status_) != static_cast<int>(cloneInfo.GetStatus())) {
&& common::StringToInt(*status_, &status) == false) {
return false;
}

if (status_ != nullptr
&& common::StringToInt(*status_, &status) == true
&& status != static_cast<int>(cloneInfo.GetStatus())) {
return false;
}

int type;
if (type_ != nullptr
&& std::stoi(*type_) != static_cast<int>(cloneInfo.GetTaskType())) {
&& common::StringToInt(*type_, &type) == false) {
return false;
}

if (type_ != nullptr
&& common::StringToInt(*type_, &type) == true
&& type != static_cast<int>(cloneInfo.GetTaskType())) {
return false;
}

return true;
}

Expand Down
3 changes: 2 additions & 1 deletion src/snapshotcloneserver/clone/clone_service_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ class CloneFilterCondition {
type_(type) {}
bool IsMatchCondition(const CloneInfo &cloneInfo);

private:
void SetUuid(const std::string *uuid) {
uuid_ = uuid;
}
Expand All @@ -143,6 +142,8 @@ class CloneFilterCondition {
void SetType(const std::string *type) {
type_ = type;
}

private:
const std::string *uuid_;
const std::string *source_;
const std::string *destination_;
Expand Down
10 changes: 9 additions & 1 deletion src/snapshotcloneserver/snapshot/snapshot_service_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "src/snapshotcloneserver/snapshot/snapshot_service_manager.h"

#include <glog/logging.h>
#include "src/common/string_util.h"

namespace curve {
namespace snapshotcloneserver {
Expand Down Expand Up @@ -275,8 +276,15 @@ bool SnapshotFilterCondition::IsMatchCondition(const SnapshotInfo &snapInfo) {
return false;
}

int status;
if (status_ != nullptr
&& std::stoi(*status_) != static_cast<int>(snapInfo.GetStatus())) {
&& common::StringToInt(*status_, &status) == false) {
return false;
}

if (status_ != nullptr
&& common::StringToInt(*status_, &status) == true
&& status != static_cast<int>(snapInfo.GetStatus())) {
return false;
}

Expand Down
40 changes: 40 additions & 0 deletions test/snapshotcloneserver/test_clone_service_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,46 @@ TEST_F(TestCloneServiceManager, GetCloneTaskInfoByFilterSuccess) {
ASSERT_EQ(kProgressCloneComplete, infos[0].GetCloneProgress());
}

TEST_F(TestCloneServiceManager, GetCloneTaskInfoByFilterFail) {
const UUID source = "uuid1";
const std::string user = "user1";
const std::string destination = "file1";
bool lazyFlag = true;

CloneInfo cloneInfo("uuid1", user, CloneTaskType::kClone,
source, destination, CloneFileType::kSnapshot, lazyFlag);
cloneInfo.SetStatus(CloneStatus::done);

std::vector<CloneInfo> cloneInfos;
cloneInfos.push_back(cloneInfo);
EXPECT_CALL(*cloneCore_, GetCloneInfoList(_))
.WillRepeatedly(DoAll(SetArgPointee<0>(cloneInfos),
Return(kErrCodeSuccess)));

std::string type = "wrongType";
CloneFilterCondition filter;
filter.SetType(&type);
std::vector<TaskCloneInfo> infos;
auto ret = manager_->GetCloneTaskInfoByFilter(filter, &infos);

ASSERT_EQ(kErrCodeSuccess, ret);
ASSERT_EQ(0, infos.size());

std::string type1 = "1";
filter.SetType(&type1);
ret = manager_->GetCloneTaskInfoByFilter(filter, &infos);

ASSERT_EQ(kErrCodeSuccess, ret);
ASSERT_EQ(0, infos.size());

std::string type2 = "0";
filter.SetType(&type2);
ret = manager_->GetCloneTaskInfoByFilter(filter, &infos);

ASSERT_EQ(kErrCodeSuccess, ret);
ASSERT_EQ(1, infos.size());
}

TEST_F(TestCloneServiceManager, TestGetCloneTaskInfoByUUIDSuccess) {
const UUID uuid = "uuid1";
const UUID source = "src";
Expand Down

0 comments on commit a9c30ec

Please sign in to comment.