Skip to content

Commit d9de11d

Browse files
authored
Merge branch 'apache:trunk' into HDFS-16410
2 parents 2436f49 + c3006be commit d9de11d

File tree

12 files changed

+628
-178
lines changed

12 files changed

+628
-178
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
@@ -29,11 +29,13 @@ add_executable(hdfs_tool_tests
2929
hdfs-tool-test-fixtures.cc
3030
hdfs-tool-tests.cc
3131
hdfs-df-mock.cc
32+
hdfs-du-mock.cc
3233
main.cc)
3334
target_include_directories(hdfs_tool_tests PRIVATE
3435
../tools
3536
../../tools
3637
../../tools/hdfs-df
38+
../../tools/hdfs-du
3739
../../tools/hdfs-allow-snapshot
3840
../../tools/hdfs-disallow-snapshot
3941
../../tools/hdfs-delete-snapshot
@@ -46,6 +48,7 @@ target_include_directories(hdfs_tool_tests PRIVATE
4648
target_link_libraries(hdfs_tool_tests PRIVATE
4749
gmock_main
4850
hdfs_df_lib
51+
hdfs_du_lib
4952
hdfs_allowSnapshot_lib
5053
hdfs_disallowSnapshot_lib
5154
hdfs_deleteSnapshot_lib
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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-du-mock.h"
28+
#include "hdfs-tool-tests.h"
29+
30+
namespace hdfs::tools::test {
31+
DuMock::~DuMock() = default;
32+
33+
void DuMock::SetExpectations(std::function<std::unique_ptr<DuMock>()> test_case,
34+
const std::vector<std::string> &args) const {
35+
// Get the pointer to the function that defines the test case
36+
const auto test_case_func = test_case.target<std::unique_ptr<DuMock> (*)()>();
37+
ASSERT_NE(test_case_func, nullptr);
38+
39+
// Set the expected method calls and their corresponding arguments for each
40+
// test case
41+
if (*test_case_func == &CallHelp<DuMock>) {
42+
EXPECT_CALL(*this, HandleHelp()).Times(1).WillOnce(testing::Return(true));
43+
return;
44+
}
45+
46+
if (*test_case_func == &PassAPath<DuMock>) {
47+
const auto arg1 = args[0];
48+
EXPECT_CALL(*this, HandlePath(arg1, false))
49+
.Times(1)
50+
.WillOnce(testing::Return(true));
51+
}
52+
53+
if (*test_case_func == &PassRecursivePath<DuMock>) {
54+
const auto arg1 = args[0];
55+
const auto arg2 = args[1];
56+
ASSERT_EQ(arg1, "-R");
57+
EXPECT_CALL(*this, HandlePath(arg2, true))
58+
.Times(1)
59+
.WillOnce(testing::Return(true));
60+
}
61+
62+
if (*test_case_func == &PassRecursive<DuMock>) {
63+
const auto arg1 = args[0];
64+
ASSERT_EQ(arg1, "-R");
65+
}
66+
}
67+
} // 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_DU_MOCK
20+
#define LIBHDFSPP_TOOLS_HDFS_DU_MOCK
21+
22+
#include <functional>
23+
#include <memory>
24+
#include <string>
25+
#include <vector>
26+
27+
#include <gmock/gmock.h>
28+
29+
#include "hdfs-du.h"
30+
31+
namespace hdfs::tools::test {
32+
/**
33+
* {@class DuMock} is an {@class Du} whereby it mocks the
34+
* HandleHelp and HandlePath methods for testing their functionality.
35+
*/
36+
class DuMock : public hdfs::tools::Du {
37+
public:
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
DuMock(const int argc, char **argv) : Du(argc, argv) {}
42+
43+
// Abiding to the Rule of 5
44+
DuMock(const DuMock &) = delete;
45+
DuMock(DuMock &&) = delete;
46+
DuMock &operator=(const DuMock &) = delete;
47+
DuMock &operator=(DuMock &&) = delete;
48+
~DuMock() 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<DuMock>()> 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 bool),
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "hdfs-delete-snapshot-mock.h"
2929
#include "hdfs-df-mock.h"
3030
#include "hdfs-disallow-snapshot-mock.h"
31+
#include "hdfs-du-mock.h"
3132
#include "hdfs-rename-snapshot-mock.h"
3233
#include "hdfs-tool-test-fixtures.h"
3334
#include "hdfs-tool-tests.h"
@@ -67,6 +68,12 @@ INSTANTIATE_TEST_SUITE_P(HdfsDf, HdfsToolBasicTest,
6768
testing::Values(PassAPath<hdfs::tools::test::DfMock>,
6869
CallHelp<hdfs::tools::test::DfMock>));
6970

71+
INSTANTIATE_TEST_SUITE_P(
72+
HdfsDu, HdfsToolBasicTest,
73+
testing::Values(PassAPath<hdfs::tools::test::DuMock>,
74+
CallHelp<hdfs::tools::test::DuMock>,
75+
PassRecursivePath<hdfs::tools::test::DuMock>));
76+
7077
INSTANTIATE_TEST_SUITE_P(
7178
HdfsDeleteSnapshot, HdfsToolBasicTest,
7279
testing::Values(CallHelp<hdfs::tools::test::DeleteSnapshotMock>,
@@ -114,6 +121,14 @@ INSTANTIATE_TEST_SUITE_P(
114121
HdfsDf, HdfsToolNegativeTestThrows,
115122
testing::Values(Pass2Paths<hdfs::tools::test::DfMock>));
116123

124+
INSTANTIATE_TEST_SUITE_P(
125+
HdfsDu, HdfsToolNegativeTestThrows,
126+
testing::Values(Pass2Paths<hdfs::tools::test::DuMock>,
127+
Pass3Paths<hdfs::tools::test::DuMock>,
128+
PassNOptAndAPath<hdfs::tools::test::DuMock>,
129+
PassOwnerAndAPath<hdfs::tools::test::DuMock>,
130+
PassPermissionsAndAPath<hdfs::tools::test::DuMock>));
131+
117132
INSTANTIATE_TEST_SUITE_P(
118133
HdfsCat, HdfsToolNegativeTestThrows,
119134
testing::Values(Pass2Paths<hdfs::tools::test::CatMock>));
@@ -122,6 +137,10 @@ INSTANTIATE_TEST_SUITE_P(
122137
HdfsDeleteSnapshot, HdfsToolNegativeTestNoThrow,
123138
testing::Values(PassAPath<hdfs::tools::test::DeleteSnapshotMock>));
124139

140+
INSTANTIATE_TEST_SUITE_P(
141+
HdfsDu, HdfsToolNegativeTestNoThrow,
142+
testing::Values(PassRecursive<hdfs::tools::test::DuMock>));
143+
125144
INSTANTIATE_TEST_SUITE_P(
126145
HdfsChown, HdfsToolNegativeTestNoThrow,
127146
testing::Values(PassAPath<hdfs::tools::test::ChownMock>));

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,31 @@ template <class T> std::unique_ptr<T> PassAPath() {
4444
return hdfs_tool;
4545
}
4646

47+
template <class T> std::unique_ptr<T> PassRecursive() {
48+
constexpr auto argc = 2;
49+
static std::string exe("hdfs_tool_name");
50+
static std::string arg1("-R");
51+
52+
static char *argv[] = {exe.data(), arg1.data()};
53+
54+
auto hdfs_tool = std::make_unique<T>(argc, argv);
55+
hdfs_tool->SetExpectations(PassRecursive<T>, {arg1});
56+
return hdfs_tool;
57+
}
58+
59+
template <class T> std::unique_ptr<T> PassRecursivePath() {
60+
constexpr auto argc = 3;
61+
static std::string exe("hdfs_tool_name");
62+
static std::string arg1("-R");
63+
static std::string arg2("a/b/c");
64+
65+
static char *argv[] = {exe.data(), arg1.data(), arg2.data()};
66+
67+
auto hdfs_tool = std::make_unique<T>(argc, argv);
68+
hdfs_tool->SetExpectations(PassRecursivePath<T>, {arg1, arg2});
69+
return hdfs_tool;
70+
}
71+
4772
template <class T> std::unique_ptr<T> CallHelp() {
4873
constexpr auto argc = 2;
4974
static std::string exe("hdfs_tool_name");

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
@@ -61,8 +61,7 @@ target_link_libraries(hdfs_count tools_common hdfspp_static)
6161

6262
add_subdirectory(hdfs-df)
6363

64-
add_executable(hdfs_du hdfs_du.cc)
65-
target_link_libraries(hdfs_du tools_common hdfspp_static)
64+
add_subdirectory(hdfs-du)
6665

6766
add_executable(hdfs_get hdfs_get.cc)
6867
target_link_libraries(hdfs_get tools_common hdfspp_static)
Lines changed: 27 additions & 0 deletions
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_du_lib STATIC $<TARGET_OBJECTS:hdfs_tool_obj> hdfs-du.cc)
20+
target_include_directories(hdfs_du_lib PRIVATE ../../tools ${Boost_INCLUDE_DIRS})
21+
target_link_libraries(hdfs_du_lib PRIVATE Boost::boost Boost::program_options tools_common hdfspp_static)
22+
23+
add_executable(hdfs_du main.cc)
24+
target_include_directories(hdfs_du PRIVATE ../../tools)
25+
target_link_libraries(hdfs_du PRIVATE hdfs_du_lib)
26+
27+
install(TARGETS hdfs_du RUNTIME DESTINATION bin)

0 commit comments

Comments
 (0)