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

HDFS-16470. Make HDFS find tool cross platform #4076

Merged
merged 6 commits into from
Mar 18, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ add_executable(hdfs_tool_tests
hdfs-mkdir-mock.cc
hdfs-rm-mock.cc
hdfs-get-mock.cc
hdfs-find-mock.cc
main.cc)
target_include_directories(hdfs_tool_tests PRIVATE
../tools
Expand All @@ -56,6 +57,7 @@ target_include_directories(hdfs_tool_tests PRIVATE
../../tools/hdfs-mkdir
../../tools/hdfs-rm
../../tools/hdfs-get
../../tools/hdfs-find
../../tools/hdfs-cat)
target_link_libraries(hdfs_tool_tests PRIVATE
gmock_main
Expand All @@ -75,5 +77,6 @@ target_link_libraries(hdfs_tool_tests PRIVATE
hdfs_mkdir_lib
hdfs_rm_lib
hdfs_get_lib
hdfs_find_lib
hdfs_cat_lib)
add_test(hdfs_tool_tests hdfs_tool_tests)
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ void CreateSnapshotMock::SetExpectations(
}

if (*test_case_func == &PassNOptAndAPath<CreateSnapshotMock>) {
const auto arg1 = args[1];
const auto arg2 = std::optional{args[0]};
EXPECT_CALL(*this, HandleSnapshot(arg1, arg2))
const auto opt_n = args[0];
const auto path = args[2];
const auto opt_n_value = std::optional{args[1]};
ASSERT_EQ(opt_n, "-n");
EXPECT_CALL(*this, HandleSnapshot(path, opt_n_value))
.Times(1)
.WillOnce(testing::Return(true));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <functional>
#include <memory>
#include <string>
#include <vector>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "hdfs-find-mock.h"
#include "hdfs-tool-tests.h"
#include "hdfspp/hdfspp.h"

namespace hdfs::tools::test {
FindMock::~FindMock() = default;

void FindMock::SetExpectations(
std::function<std::unique_ptr<FindMock>()> test_case,
const std::vector<std::string> &args) const {
// Get the pointer to the function that defines the test case
const auto test_case_func =
test_case.target<std::unique_ptr<FindMock> (*)()>();
ASSERT_NE(test_case_func, nullptr);

// Set the expected method calls and their corresponding arguments for each
// test case
if (*test_case_func == &CallHelp<FindMock>) {
EXPECT_CALL(*this, HandleHelp()).Times(1).WillOnce(testing::Return(true));
return;
}

if (*test_case_func == &PassAPath<FindMock>) {
const auto arg1 = args[0];
EXPECT_CALL(*this, HandlePath(arg1, "*",
hdfs::FileSystem::GetDefaultFindMaxDepth()))
.Times(1)
.WillOnce(testing::Return(true));
}

if (*test_case_func == &PassNOptAndAPath<FindMock>) {
const auto arg1 = args[0];
const auto arg2 = args[1];
const auto arg3 = args[2];
ASSERT_EQ(arg1, "-n");
EXPECT_CALL(*this, HandlePath(arg3, arg2,
hdfs::FileSystem::GetDefaultFindMaxDepth()))
.Times(1)
.WillOnce(testing::Return(true));
}

if (*test_case_func == &PassMOptPermissionsAndAPath<FindMock>) {
const auto arg1 = args[0];
const auto arg2 = args[1];
const auto arg3 = args[2];
ASSERT_EQ(arg1, "-m");
EXPECT_CALL(*this,
HandlePath(arg3, "*", static_cast<uint32_t>(std::stoi(arg2))))
.Times(1)
.WillOnce(testing::Return(true));
}

if (*test_case_func == &PassNStrMNumAndAPath<FindMock>) {
const auto arg1 = args[0];
const auto arg2 = args[1];
const auto arg3 = args[2];
const auto arg4 = args[3];
const auto arg5 = args[4];
ASSERT_EQ(arg1, "-n");
ASSERT_EQ(arg3, "-m");
EXPECT_CALL(*this,
HandlePath(arg5, arg2, static_cast<uint32_t>(std::stoi(arg4))))
.Times(1)
.WillOnce(testing::Return(true));
}
}
} // namespace hdfs::tools::test
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef LIBHDFSPP_TOOLS_HDFS_FIND_MOCK
#define LIBHDFSPP_TOOLS_HDFS_FIND_MOCK

#include <functional>
#include <memory>
#include <string>
#include <vector>

#include <gmock/gmock.h>

#include "hdfs-find.h"

namespace hdfs::tools::test {
/**
* {@class FindMock} is an {@class Find} whereby it mocks the
* HandleHelp and HandlePath methods for testing their functionality.
*/
class FindMock : public hdfs::tools::Find {
public:
/**
* {@inheritdoc}
*/
FindMock(const int argc, char **argv) : Find(argc, argv) {}

// Abiding to the Rule of 5
FindMock(const FindMock &) = delete;
FindMock(FindMock &&) = delete;
FindMock &operator=(const FindMock &) = delete;
FindMock &operator=(FindMock &&) = delete;
~FindMock() override;

/**
* Defines the methods and the corresponding arguments that are expected
* to be called on this instance of {@link HdfsTool} for the given test case.
*
* @param test_case An {@link std::function} object that points to the
* function defining the test case
* @param args The arguments that are passed to this test case
*/
void SetExpectations(std::function<std::unique_ptr<FindMock>()> test_case,
const std::vector<std::string> &args = {}) const;

MOCK_METHOD(bool, HandleHelp, (), (const, override));

MOCK_METHOD(bool, HandlePath,
(const std::string &, const std::string &, uint32_t),
(const, override));
};
} // namespace hdfs::tools::test

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "hdfs-df-mock.h"
#include "hdfs-disallow-snapshot-mock.h"
#include "hdfs-du-mock.h"
#include "hdfs-find-mock.h"
#include "hdfs-get-mock.h"
#include "hdfs-mkdir-mock.h"
#include "hdfs-move-to-local-mock.h"
Expand Down Expand Up @@ -140,6 +141,14 @@ INSTANTIATE_TEST_SUITE_P(
PassAPath<hdfs::tools::test::RmMock>,
PassRecursivePath<hdfs::tools::test::RmMock>));

INSTANTIATE_TEST_SUITE_P(
HdfsFind, HdfsToolBasicTest,
testing::Values(CallHelp<hdfs::tools::test::FindMock>,
PassAPath<hdfs::tools::test::FindMock>,
PassNStrMNumAndAPath<hdfs::tools::test::FindMock>,
PassMOptPermissionsAndAPath<hdfs::tools::test::FindMock>,
PassNOptAndAPath<hdfs::tools::test::FindMock>));

// Negative tests
INSTANTIATE_TEST_SUITE_P(
HdfsAllowSnapshot, HdfsToolNegativeTestThrows,
Expand Down Expand Up @@ -210,6 +219,17 @@ INSTANTIATE_TEST_SUITE_P(
PassRecursiveOwnerAndAPath<hdfs::tools::test::RmMock>,
PassMOpt<hdfs::tools::test::RmMock>));

INSTANTIATE_TEST_SUITE_P(
HdfsFind, HdfsToolNegativeTestThrows,
testing::Values(Pass2Paths<hdfs::tools::test::FindMock>,
Pass3Paths<hdfs::tools::test::FindMock>,
PassRecursiveOwnerAndAPath<hdfs::tools::test::FindMock>,
PassRecursive<hdfs::tools::test::FindMock>,
PassRecursivePath<hdfs::tools::test::FindMock>,
PassMPOptsPermissionsAndAPath<hdfs::tools::test::FindMock>,
PassMOpt<hdfs::tools::test::FindMock>,
PassNOpt<hdfs::tools::test::FindMock>));

INSTANTIATE_TEST_SUITE_P(
HdfsRm, HdfsToolNegativeTestNoThrow,
testing::Values(PassRecursive<hdfs::tools::test::RmMock>));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ template <class T> std::unique_ptr<T> PassNOptAndAPath() {
static char *argv[] = {exe.data(), arg1.data(), arg2.data(), arg3.data()};

auto hdfs_tool = std::make_unique<T>(argc, argv);
hdfs_tool->SetExpectations(PassNOptAndAPath<T>, {arg2, arg3});
hdfs_tool->SetExpectations(PassNOptAndAPath<T>, {arg1, arg2, arg3});
return hdfs_tool;
}

Expand Down Expand Up @@ -271,4 +271,34 @@ template <class T> std::unique_ptr<T> PassMPOptsPermissionsAndAPath() {
return hdfs_tool;
}

template <class T> std::unique_ptr<T> PassNStrMNumAndAPath() {
constexpr auto argc = 6;
static std::string exe("hdfs_tool_name");
static std::string arg1("-n");
static std::string arg2("some_str");
static std::string arg3("-m");
static std::string arg4("757");
static std::string arg5("some/path");

static char *argv[] = {exe.data(), arg1.data(), arg2.data(),
arg3.data(), arg4.data(), arg5.data()};

auto hdfs_tool = std::make_unique<T>(argc, argv);
hdfs_tool->SetExpectations(PassNStrMNumAndAPath<T>,
{arg1, arg2, arg3, arg4, arg5});
return hdfs_tool;
}

template <class T> std::unique_ptr<T> PassNOpt() {
constexpr auto argc = 2;
static std::string exe("hdfs_tool_name");
static std::string arg1("-n");

static char *argv[] = {exe.data(), arg1.data()};

auto hdfs_tool = std::make_unique<T>(argc, argv);
hdfs_tool->SetExpectations(PassNOpt<T>, {arg1});
return hdfs_tool;
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ add_subdirectory(hdfs-chown)

add_subdirectory(hdfs-chmod)

add_executable(hdfs_find hdfs_find.cc)
target_link_libraries(hdfs_find tools_common hdfspp_static)
add_subdirectory(hdfs-find)

add_subdirectory(hdfs-mkdir)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

add_library(hdfs_find_lib STATIC $<TARGET_OBJECTS:hdfs_tool_obj> hdfs-find.cc)
target_include_directories(hdfs_find_lib PRIVATE ../../tools ${Boost_INCLUDE_DIRS})
target_link_libraries(hdfs_find_lib PRIVATE Boost::boost Boost::program_options tools_common hdfspp_static)

add_executable(hdfs_find main.cc)
target_include_directories(hdfs_find PRIVATE ../../tools)
target_link_libraries(hdfs_find PRIVATE hdfs_find_lib)

install(TARGETS hdfs_find RUNTIME DESTINATION bin)
Loading