Skip to content

Commit 4ef1d3e

Browse files
HDFS-16472. Make HDFS setrep tool cross platform (#4130)
* 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.
1 parent 34b3275 commit 4ef1d3e

File tree

11 files changed

+617
-176
lines changed

11 files changed

+617
-176
lines changed

hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ add_executable(hdfs_tool_tests
3838
hdfs-get-mock.cc
3939
hdfs-find-mock.cc
4040
hdfs-ls-mock.cc
41+
hdfs-setrep-mock.cc
4142
main.cc)
4243
target_include_directories(hdfs_tool_tests PRIVATE
4344
../tools
@@ -60,6 +61,7 @@ target_include_directories(hdfs_tool_tests PRIVATE
6061
../../tools/hdfs-get
6162
../../tools/hdfs-find
6263
../../tools/hdfs-ls
64+
../../tools/hdfs-setrep
6365
../../tools/hdfs-cat)
6466
target_link_libraries(hdfs_tool_tests PRIVATE
6567
gmock_main
@@ -81,5 +83,6 @@ target_link_libraries(hdfs_tool_tests PRIVATE
8183
hdfs_get_lib
8284
hdfs_find_lib
8385
hdfs_ls_lib
86+
hdfs_setrep_lib
8487
hdfs_cat_lib)
8588
add_test(hdfs_tool_tests hdfs_tool_tests)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include <functional>
20+
#include <memory>
21+
#include <string>
22+
#include <vector>
23+
24+
#include <gmock/gmock.h>
25+
#include <gtest/gtest.h>
26+
27+
#include "hdfs-setrep-mock.h"
28+
#include "hdfs-tool-tests.h"
29+
30+
namespace hdfs::tools::test {
31+
SetrepMock::~SetrepMock() = default;
32+
33+
void SetrepMock::SetExpectations(
34+
std::function<std::unique_ptr<SetrepMock>()> test_case,
35+
const std::vector<std::string> &args) const {
36+
// Get the pointer to the function that defines the test case
37+
const auto test_case_func =
38+
test_case.target<std::unique_ptr<SetrepMock> (*)()>();
39+
ASSERT_NE(test_case_func, nullptr);
40+
41+
// Set the expected method calls and their corresponding arguments for each
42+
// test case
43+
if (*test_case_func == &CallHelp<SetrepMock>) {
44+
EXPECT_CALL(*this, HandleHelp()).Times(1).WillOnce(testing::Return(true));
45+
return;
46+
}
47+
48+
if (*test_case_func == &PassPermissionsAndAPath<SetrepMock>) {
49+
const auto number = args[0];
50+
const auto path = args[1];
51+
EXPECT_CALL(*this, HandlePath(path, number))
52+
.Times(1)
53+
.WillOnce(testing::Return(true));
54+
}
55+
}
56+
} // namespace hdfs::tools::test
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#ifndef LIBHDFSPP_TOOLS_HDFS_SETREP_MOCK
20+
#define LIBHDFSPP_TOOLS_HDFS_SETREP_MOCK
21+
22+
#include <functional>
23+
#include <memory>
24+
#include <string>
25+
#include <vector>
26+
27+
#include <gmock/gmock.h>
28+
29+
#include "hdfs-setrep.h"
30+
31+
namespace hdfs::tools::test {
32+
/**
33+
* {@class SetrepMock} is an {@class Setrep} whereby it mocks the
34+
* HandleHelp and HandlePath methods for testing their functionality.
35+
*/
36+
class SetrepMock : public hdfs::tools::Setrep {
37+
public:
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
SetrepMock(const int argc, char **argv) : Setrep(argc, argv) {}
42+
43+
// Abiding to the Rule of 5
44+
SetrepMock(const SetrepMock &) = delete;
45+
SetrepMock(SetrepMock &&) = delete;
46+
SetrepMock &operator=(const SetrepMock &) = delete;
47+
SetrepMock &operator=(SetrepMock &&) = delete;
48+
~SetrepMock() override;
49+
50+
/**
51+
* Defines the methods and the corresponding arguments that are expected
52+
* to be called on this instance of {@link HdfsTool} for the given test case.
53+
*
54+
* @param test_case An {@link std::function} object that points to the
55+
* function defining the test case
56+
* @param args The arguments that are passed to this test case
57+
*/
58+
void SetExpectations(std::function<std::unique_ptr<SetrepMock>()> test_case,
59+
const std::vector<std::string> &args = {}) const;
60+
61+
MOCK_METHOD(bool, HandleHelp, (), (const, override));
62+
63+
MOCK_METHOD(bool, HandlePath, (const std::string &, const std::string &),
64+
(const, override));
65+
};
66+
} // namespace hdfs::tools::test
67+
68+
#endif

hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tests/tools/hdfs-tool-tests.cc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "hdfs-move-to-local-mock.h"
3939
#include "hdfs-rename-snapshot-mock.h"
4040
#include "hdfs-rm-mock.h"
41+
#include "hdfs-setrep-mock.h"
4142
#include "hdfs-tool-test-fixtures.h"
4243
#include "hdfs-tool-tests.h"
4344

@@ -156,6 +157,11 @@ INSTANTIATE_TEST_SUITE_P(
156157
PassMOptPermissionsAndAPath<hdfs::tools::test::FindMock>,
157158
PassNOptAndAPath<hdfs::tools::test::FindMock>));
158159

160+
INSTANTIATE_TEST_SUITE_P(
161+
HdfsSetrep, HdfsToolBasicTest,
162+
testing::Values(CallHelp<hdfs::tools::test::SetrepMock>,
163+
PassPermissionsAndAPath<hdfs::tools::test::SetrepMock>));
164+
159165
// Negative tests
160166
INSTANTIATE_TEST_SUITE_P(
161167
HdfsAllowSnapshot, HdfsToolNegativeTestThrows,
@@ -245,6 +251,20 @@ INSTANTIATE_TEST_SUITE_P(
245251
PassMOpt<hdfs::tools::test::FindMock>,
246252
PassNOpt<hdfs::tools::test::FindMock>));
247253

254+
INSTANTIATE_TEST_SUITE_P(
255+
HdfsChgrp, HdfsToolNegativeTestThrows,
256+
testing::Values(PassNOptAndAPath<hdfs::tools::test::ChgrpMock>));
257+
258+
INSTANTIATE_TEST_SUITE_P(
259+
HdfsSetrep, HdfsToolNegativeTestThrows,
260+
testing::Values(
261+
Pass3Paths<hdfs::tools::test::SetrepMock>,
262+
PassRecursiveOwnerAndAPath<hdfs::tools::test::SetrepMock>,
263+
PassRecursive<hdfs::tools::test::SetrepMock>,
264+
PassMPOptsPermissionsAndAPath<hdfs::tools::test::SetrepMock>,
265+
PassMOpt<hdfs::tools::test::SetrepMock>,
266+
PassNOpt<hdfs::tools::test::SetrepMock>));
267+
248268
INSTANTIATE_TEST_SUITE_P(
249269
HdfsRm, HdfsToolNegativeTestNoThrow,
250270
testing::Values(PassRecursive<hdfs::tools::test::RmMock>));
@@ -302,5 +322,5 @@ INSTANTIATE_TEST_SUITE_P(
302322
testing::Values(PassAPath<hdfs::tools::test::ChgrpMock>));
303323

304324
INSTANTIATE_TEST_SUITE_P(
305-
HdfsChgrp, HdfsToolNegativeTestThrows,
306-
testing::Values(PassNOptAndAPath<hdfs::tools::test::ChgrpMock>));
325+
HdfsSetrep, HdfsToolNegativeTestNoThrow,
326+
testing::Values(PassAPath<hdfs::tools::test::SetrepMock>));

hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/tools/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ add_subdirectory(hdfs-copy-to-local)
6464

6565
add_subdirectory(hdfs-move-to-local)
6666

67-
add_executable(hdfs_setrep hdfs_setrep.cc)
68-
target_link_libraries(hdfs_setrep tools_common hdfspp_static)
67+
add_subdirectory(hdfs-setrep)
6968

7069
add_subdirectory(hdfs-allow-snapshot)
7170

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
add_library(hdfs_setrep_lib STATIC $<TARGET_OBJECTS:hdfs_tool_obj> hdfs-setrep.cc)
20+
target_include_directories(hdfs_setrep_lib PRIVATE ../../tools ${Boost_INCLUDE_DIRS})
21+
target_link_libraries(hdfs_setrep_lib PRIVATE Boost::boost Boost::program_options tools_common hdfspp_static)
22+
23+
add_executable(hdfs_setrep main.cc)
24+
target_include_directories(hdfs_setrep PRIVATE ../../tools)
25+
target_link_libraries(hdfs_setrep PRIVATE hdfs_setrep_lib)
26+
27+
install(TARGETS hdfs_setrep RUNTIME DESTINATION bin)

0 commit comments

Comments
 (0)