Skip to content

Commit

Permalink
HDFS-16472. Make HDFS setrep tool cross platform (#4130)
Browse files Browse the repository at this point in the history
* The source files for hdfs_setrep
   uses getopt for parsing the
   command line arguments.
* getopt is available only on Linux
   and thus, isn't cross platform.
* We need to replace getopt
  with boost::program_options
  to make this tool cross platform.
  • Loading branch information
GauthamBanasandra authored Apr 5, 2022
1 parent 34b3275 commit 4ef1d3e
Show file tree
Hide file tree
Showing 11 changed files with 617 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ add_executable(hdfs_tool_tests
hdfs-get-mock.cc
hdfs-find-mock.cc
hdfs-ls-mock.cc
hdfs-setrep-mock.cc
main.cc)
target_include_directories(hdfs_tool_tests PRIVATE
../tools
Expand All @@ -60,6 +61,7 @@ target_include_directories(hdfs_tool_tests PRIVATE
../../tools/hdfs-get
../../tools/hdfs-find
../../tools/hdfs-ls
../../tools/hdfs-setrep
../../tools/hdfs-cat)
target_link_libraries(hdfs_tool_tests PRIVATE
gmock_main
Expand All @@ -81,5 +83,6 @@ target_link_libraries(hdfs_tool_tests PRIVATE
hdfs_get_lib
hdfs_find_lib
hdfs_ls_lib
hdfs_setrep_lib
hdfs_cat_lib)
add_test(hdfs_tool_tests hdfs_tool_tests)
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* 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-setrep-mock.h"
#include "hdfs-tool-tests.h"

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

void SetrepMock::SetExpectations(
std::function<std::unique_ptr<SetrepMock>()> 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<SetrepMock> (*)()>();
ASSERT_NE(test_case_func, nullptr);

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

if (*test_case_func == &PassPermissionsAndAPath<SetrepMock>) {
const auto number = args[0];
const auto path = args[1];
EXPECT_CALL(*this, HandlePath(path, number))
.Times(1)
.WillOnce(testing::Return(true));
}
}
} // namespace hdfs::tools::test
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* 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_SETREP_MOCK
#define LIBHDFSPP_TOOLS_HDFS_SETREP_MOCK

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

#include <gmock/gmock.h>

#include "hdfs-setrep.h"

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

// Abiding to the Rule of 5
SetrepMock(const SetrepMock &) = delete;
SetrepMock(SetrepMock &&) = delete;
SetrepMock &operator=(const SetrepMock &) = delete;
SetrepMock &operator=(SetrepMock &&) = delete;
~SetrepMock() 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<SetrepMock>()> 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 &),
(const, override));
};
} // namespace hdfs::tools::test

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "hdfs-move-to-local-mock.h"
#include "hdfs-rename-snapshot-mock.h"
#include "hdfs-rm-mock.h"
#include "hdfs-setrep-mock.h"
#include "hdfs-tool-test-fixtures.h"
#include "hdfs-tool-tests.h"

Expand Down Expand Up @@ -156,6 +157,11 @@ INSTANTIATE_TEST_SUITE_P(
PassMOptPermissionsAndAPath<hdfs::tools::test::FindMock>,
PassNOptAndAPath<hdfs::tools::test::FindMock>));

INSTANTIATE_TEST_SUITE_P(
HdfsSetrep, HdfsToolBasicTest,
testing::Values(CallHelp<hdfs::tools::test::SetrepMock>,
PassPermissionsAndAPath<hdfs::tools::test::SetrepMock>));

// Negative tests
INSTANTIATE_TEST_SUITE_P(
HdfsAllowSnapshot, HdfsToolNegativeTestThrows,
Expand Down Expand Up @@ -245,6 +251,20 @@ INSTANTIATE_TEST_SUITE_P(
PassMOpt<hdfs::tools::test::FindMock>,
PassNOpt<hdfs::tools::test::FindMock>));

INSTANTIATE_TEST_SUITE_P(
HdfsChgrp, HdfsToolNegativeTestThrows,
testing::Values(PassNOptAndAPath<hdfs::tools::test::ChgrpMock>));

INSTANTIATE_TEST_SUITE_P(
HdfsSetrep, HdfsToolNegativeTestThrows,
testing::Values(
Pass3Paths<hdfs::tools::test::SetrepMock>,
PassRecursiveOwnerAndAPath<hdfs::tools::test::SetrepMock>,
PassRecursive<hdfs::tools::test::SetrepMock>,
PassMPOptsPermissionsAndAPath<hdfs::tools::test::SetrepMock>,
PassMOpt<hdfs::tools::test::SetrepMock>,
PassNOpt<hdfs::tools::test::SetrepMock>));

INSTANTIATE_TEST_SUITE_P(
HdfsRm, HdfsToolNegativeTestNoThrow,
testing::Values(PassRecursive<hdfs::tools::test::RmMock>));
Expand Down Expand Up @@ -302,5 +322,5 @@ INSTANTIATE_TEST_SUITE_P(
testing::Values(PassAPath<hdfs::tools::test::ChgrpMock>));

INSTANTIATE_TEST_SUITE_P(
HdfsChgrp, HdfsToolNegativeTestThrows,
testing::Values(PassNOptAndAPath<hdfs::tools::test::ChgrpMock>));
HdfsSetrep, HdfsToolNegativeTestNoThrow,
testing::Values(PassAPath<hdfs::tools::test::SetrepMock>));
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ add_subdirectory(hdfs-copy-to-local)

add_subdirectory(hdfs-move-to-local)

add_executable(hdfs_setrep hdfs_setrep.cc)
target_link_libraries(hdfs_setrep tools_common hdfspp_static)
add_subdirectory(hdfs-setrep)

add_subdirectory(hdfs-allow-snapshot)

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_setrep_lib STATIC $<TARGET_OBJECTS:hdfs_tool_obj> hdfs-setrep.cc)
target_include_directories(hdfs_setrep_lib PRIVATE ../../tools ${Boost_INCLUDE_DIRS})
target_link_libraries(hdfs_setrep_lib PRIVATE Boost::boost Boost::program_options tools_common hdfspp_static)

add_executable(hdfs_setrep main.cc)
target_include_directories(hdfs_setrep PRIVATE ../../tools)
target_link_libraries(hdfs_setrep PRIVATE hdfs_setrep_lib)

install(TARGETS hdfs_setrep RUNTIME DESTINATION bin)
Loading

0 comments on commit 4ef1d3e

Please sign in to comment.