From 84f10fd78b08db80fb1d9bd03f2598e3f3757f02 Mon Sep 17 00:00:00 2001 From: Gautham B A Date: Tue, 5 Oct 2021 21:46:42 +0530 Subject: [PATCH] HDFS-16250. Refactor AllowSnapshotMock using GMock (#3513) --- .../tests/tools/hdfs-allow-snapshot-mock.cc | 11 +--------- .../tests/tools/hdfs-allow-snapshot-mock.h | 18 +++++++--------- .../libhdfspp/tests/tools/hdfs-tool-test.h | 21 ++++++++++++++----- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-allow-snapshot-mock.cc b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-allow-snapshot-mock.cc index bbfecef247..2539c3ca48 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-allow-snapshot-mock.cc +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-allow-snapshot-mock.cc @@ -17,17 +17,8 @@ under the License. */ -#include - -#include - #include "hdfs-allow-snapshot-mock.h" namespace hdfs::tools::test { -bool AllowSnapshotMock::HandleHelp() const { return true; } - -bool AllowSnapshotMock::HandlePath(const std::string &path) const { - EXPECT_STREQ(path.c_str(), "a/b/c") << "Expecting the path a/b/c here"; - return true; -} +AllowSnapshotMock::~AllowSnapshotMock() {} } // namespace hdfs::tools::test \ No newline at end of file diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-allow-snapshot-mock.h b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-allow-snapshot-mock.h index c080a00fb7..5c399b6b68 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-allow-snapshot-mock.h +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-allow-snapshot-mock.h @@ -21,6 +21,8 @@ #include +#include + #include "hdfs-allow-snapshot.h" namespace hdfs::tools::test { @@ -36,21 +38,15 @@ public: AllowSnapshotMock(const int argc, char **argv) : AllowSnapshot(argc, argv) {} // Abiding to the Rule of 5 - AllowSnapshotMock(const AllowSnapshotMock &) = default; - AllowSnapshotMock(AllowSnapshotMock &&) = default; + AllowSnapshotMock(const AllowSnapshotMock &) = delete; + AllowSnapshotMock(AllowSnapshotMock &&) = delete; AllowSnapshotMock &operator=(const AllowSnapshotMock &) = delete; AllowSnapshotMock &operator=(AllowSnapshotMock &&) = delete; - ~AllowSnapshotMock() override = default; + ~AllowSnapshotMock() override; - /** - * {@inheritdoc} - */ - [[nodiscard]] bool HandleHelp() const override; + MOCK_METHOD(bool, HandleHelp, (), (const, override)); - /** - * {@inheritdoc} - */ - [[nodiscard]] bool HandlePath(const std::string &path) const override; + MOCK_METHOD(bool, HandlePath, (const std::string &), (const, override)); }; } // namespace hdfs::tools::test diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-tool-test.h b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-tool-test.h index be695b653f..3dac945118 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-tool-test.h +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-tool-test.h @@ -25,6 +25,7 @@ #include #include +#include #include #include "hdfs-tool.h" @@ -81,25 +82,35 @@ TEST_P(HdfsToolNegativeTest, RunTool) { EXPECT_ANY_THROW({ std::ignore = this->hdfs_tool_->Do(); }); } -template std::unique_ptr PassAPath() { +template std::unique_ptr PassAPath() { constexpr auto argc = 2; static std::string exe("hdfs_tool_name"); static std::string arg1("a/b/c"); static char *argv[] = {exe.data(), arg1.data()}; - return std::make_unique(argc, argv); + + auto hdfs_tool = std::make_unique(argc, argv); + EXPECT_CALL(*hdfs_tool, HandlePath(arg1)) + .Times(1) + .WillOnce(testing::Return(true)); + return hdfs_tool; } -template std::unique_ptr CallHelp() { +template std::unique_ptr CallHelp() { constexpr auto argc = 2; static std::string exe("hdfs_tool_name"); static std::string arg1("-h"); static char *argv[] = {exe.data(), arg1.data()}; - return std::make_unique(argc, argv); + + auto hdfs_tool = std::make_unique(argc, argv); + EXPECT_CALL(*hdfs_tool, HandleHelp()) + .Times(1) + .WillOnce(testing::Return(true)); + return hdfs_tool; } -template std::unique_ptr Pass2Paths() { +template std::unique_ptr Pass2Paths() { constexpr auto argc = 3; static std::string exe("hdfs_tool_name"); static std::string arg1("a/b/c");