Skip to content

Commit bbed73b

Browse files
Add unit tests for hdfs-chgrp
1 parent afc4c7c commit bbed73b

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
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
@@ -24,6 +24,7 @@ add_executable(hdfs_tool_tests
2424
hdfs-create-snapshot-mock.cc
2525
hdfs-cat-mock.cc
2626
hdfs-chown-mock.cc
27+
hdfs-chgrp-mock.cc
2728
hdfs-tool-test-fixtures.cc
2829
hdfs-tool-tests.cc
2930
hdfs-df-mock.cc
@@ -38,6 +39,7 @@ target_include_directories(hdfs_tool_tests PRIVATE
3839
../../tools/hdfs-create-snapshot
3940
../../tools/hdfs-rename-snapshot
4041
../../tools/hdfs-chown
42+
../../tools/hdfs-chgrp
4143
../../tools/hdfs-cat)
4244
target_link_libraries(hdfs_tool_tests PRIVATE
4345
gmock_main
@@ -48,5 +50,6 @@ target_link_libraries(hdfs_tool_tests PRIVATE
4850
hdfs_createSnapshot_lib
4951
hdfs_renameSnapshot_lib
5052
hdfs_chown_lib
53+
hdfs_chgrp_lib
5154
hdfs_cat_lib)
5255
add_test(hdfs_tool_tests hdfs_tool_tests)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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-chgrp-mock.h"
28+
#include "hdfs-chgrp.h"
29+
#include "hdfs-tool-tests.h"
30+
31+
namespace hdfs::tools::test {
32+
ChgrpMock::~ChgrpMock() = default;
33+
34+
void ChgrpMock::SetExpectations(
35+
std::function<std::unique_ptr<ChgrpMock>()> test_case,
36+
const std::vector<std::string> &args) const {
37+
// Get the pointer to the function that defines the test case
38+
const auto test_case_func =
39+
test_case.target<std::unique_ptr<ChgrpMock> (*)()>();
40+
ASSERT_NE(test_case_func, nullptr);
41+
42+
// Set the expected method calls and their corresponding arguments for each
43+
// test case
44+
if (*test_case_func == &CallHelp<ChgrpMock>) {
45+
EXPECT_CALL(*this, HandleHelp()).Times(1).WillOnce(testing::Return(true));
46+
return;
47+
}
48+
49+
if (*test_case_func == &PassOwnerAndAPath<ChgrpMock>) {
50+
const auto arg1 = args[0];
51+
const auto arg2 = args[1];
52+
EXPECT_CALL(*this, HandlePath(arg1, false, arg2))
53+
.Times(1)
54+
.WillOnce(testing::Return(true));
55+
}
56+
57+
if (*test_case_func == &PassRecursiveOwnerAndAPath<ChgrpMock>) {
58+
const auto arg1 = args[1];
59+
const auto arg2 = args[2];
60+
const Ownership ownership(arg1);
61+
EXPECT_CALL(*this, HandlePath(arg1, true, arg2))
62+
.Times(1)
63+
.WillOnce(testing::Return(true));
64+
}
65+
}
66+
} // namespace hdfs::tools::test
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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_CHGRP_MOCK
20+
#define LIBHDFSPP_TOOLS_HDFS_CHGRP_MOCK
21+
22+
#include <functional>
23+
#include <memory>
24+
#include <string>
25+
#include <vector>
26+
27+
#include <gmock/gmock.h>
28+
29+
#include "hdfs-chgrp.h"
30+
31+
namespace hdfs::tools::test {
32+
/**
33+
* {@class ChgrpMock} is an {@class Chgrp} whereby it mocks the
34+
* HandleHelp and HandlePath methods for testing their functionality.
35+
*/
36+
class ChgrpMock : public hdfs::tools::Chgrp {
37+
public:
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
ChgrpMock(const int argc, char **argv) : Chgrp(argc, argv) {}
42+
43+
// Abiding to the Rule of 5
44+
ChgrpMock(const ChgrpMock &) = delete;
45+
ChgrpMock(ChgrpMock &&) = delete;
46+
ChgrpMock &operator=(const ChgrpMock &) = delete;
47+
ChgrpMock &operator=(ChgrpMock &&) = delete;
48+
~ChgrpMock() 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<ChgrpMock>()> test_case,
59+
const std::vector<std::string> &args = {}) const;
60+
61+
MOCK_METHOD(bool, HandleHelp, (), (const, override));
62+
63+
MOCK_METHOD(bool, HandlePath,
64+
(const std::string &, bool, const std::string &),
65+
(const, override));
66+
};
67+
} // namespace hdfs::tools::test
68+
69+
#endif

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "hdfs-allow-snapshot-mock.h"
2323
#include "hdfs-cat-mock.h"
24+
#include "hdfs-chgrp-mock.h"
2425
#include "hdfs-chown-mock.h"
2526
#include "hdfs-create-snapshot-mock.h"
2627
#include "hdfs-delete-snapshot-mock.h"
@@ -76,6 +77,12 @@ INSTANTIATE_TEST_SUITE_P(
7677
PassOwnerAndAPath<hdfs::tools::test::ChownMock>,
7778
PassRecursiveOwnerAndAPath<hdfs::tools::test::ChownMock>));
7879

80+
INSTANTIATE_TEST_SUITE_P(
81+
HdfsChgrp, HdfsToolBasicTest,
82+
testing::Values(CallHelp<hdfs::tools::test::ChgrpMock>,
83+
PassOwnerAndAPath<hdfs::tools::test::ChgrpMock>,
84+
PassRecursiveOwnerAndAPath<hdfs::tools::test::ChgrpMock>));
85+
7986
// Negative tests
8087
INSTANTIATE_TEST_SUITE_P(
8188
HdfsAllowSnapshot, HdfsToolNegativeTestThrows,
@@ -114,3 +121,11 @@ INSTANTIATE_TEST_SUITE_P(
114121
INSTANTIATE_TEST_SUITE_P(
115122
HdfsChown, HdfsToolNegativeTestThrows,
116123
testing::Values(PassNOptAndAPath<hdfs::tools::test::ChownMock>));
124+
125+
INSTANTIATE_TEST_SUITE_P(
126+
HdfsChgrp, HdfsToolNegativeTestNoThrow,
127+
testing::Values(PassAPath<hdfs::tools::test::ChgrpMock>));
128+
129+
INSTANTIATE_TEST_SUITE_P(
130+
HdfsChgrp, HdfsToolNegativeTestThrows,
131+
testing::Values(PassNOptAndAPath<hdfs::tools::test::ChgrpMock>));

0 commit comments

Comments
 (0)