Skip to content

Commit 244ffb2

Browse files
GauthamBanasandraHarshitGupta11
authored andcommitted
HDFS-16278. Make HDFS snapshot tools cross platform (apache#3563)
1 parent 8e54b9b commit 244ffb2

25 files changed

+1383
-284
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919
add_executable(hdfs_tool_tests
2020
hdfs-allow-snapshot-mock.cc
21+
hdfs-disallow-snapshot-mock.cc
22+
hdfs-rename-snapshot-mock.cc
2123
hdfs-delete-snapshot-mock.cc
24+
hdfs-create-snapshot-mock.cc
2225
hdfs-cat-mock.cc
2326
hdfs-tool-test-fixtures.cc
2427
hdfs-tool-tests.cc
@@ -29,12 +32,18 @@ target_include_directories(hdfs_tool_tests PRIVATE
2932
../../tools
3033
../../tools/hdfs-df
3134
../../tools/hdfs-allow-snapshot
35+
../../tools/hdfs-disallow-snapshot
3236
../../tools/hdfs-delete-snapshot
37+
../../tools/hdfs-create-snapshot
38+
../../tools/hdfs-rename-snapshot
3339
../../tools/hdfs-cat)
3440
target_link_libraries(hdfs_tool_tests PRIVATE
3541
gmock_main
3642
hdfs_df_lib
3743
hdfs_allowSnapshot_lib
44+
hdfs_disallowSnapshot_lib
3845
hdfs_deleteSnapshot_lib
46+
hdfs_createSnapshot_lib
47+
hdfs_renameSnapshot_lib
3948
hdfs_cat_lib)
4049
add_test(hdfs_tool_tests hdfs_tool_tests)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
19+
20+
#include <functional>
21+
#include <memory>
22+
#include <optional>
23+
#include <string>
24+
#include <vector>
25+
26+
#include <gmock/gmock.h>
27+
#include <gtest/gtest.h>
28+
29+
#include "hdfs-create-snapshot-mock.h"
30+
#include "hdfs-tool-tests.h"
31+
32+
namespace hdfs::tools::test {
33+
CreateSnapshotMock::~CreateSnapshotMock() = default;
34+
35+
void CreateSnapshotMock::SetExpectations(
36+
std::function<std::unique_ptr<CreateSnapshotMock>()> test_case,
37+
const std::vector<std::string> &args) const {
38+
// Get the pointer to the function that defines the test case
39+
const auto test_case_func =
40+
test_case.target<std::unique_ptr<CreateSnapshotMock> (*)()>();
41+
ASSERT_NE(test_case_func, nullptr);
42+
43+
// Set the expected method calls and their corresponding arguments for each
44+
// test case
45+
if (*test_case_func == &CallHelp<CreateSnapshotMock>) {
46+
EXPECT_CALL(*this, HandleHelp()).Times(1).WillOnce(testing::Return(true));
47+
return;
48+
}
49+
50+
if (*test_case_func == &PassNOptAndAPath<CreateSnapshotMock>) {
51+
const auto arg1 = args[1];
52+
const auto arg2 = std::optional{args[0]};
53+
EXPECT_CALL(*this, HandleSnapshot(arg1, arg2))
54+
.Times(1)
55+
.WillOnce(testing::Return(true));
56+
}
57+
}
58+
} // namespace hdfs::tools::test
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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_CREATE_SNAPSHOT_MOCK
20+
#define LIBHDFSPP_TOOLS_HDFS_CREATE_SNAPSHOT_MOCK
21+
22+
#include <functional>
23+
#include <memory>
24+
#include <optional>
25+
#include <string>
26+
#include <vector>
27+
28+
#include <gmock/gmock.h>
29+
30+
#include "hdfs-create-snapshot.h"
31+
32+
namespace hdfs::tools::test {
33+
/**
34+
* {@class CreateSnapshotMock} is an {@class CreateSnapshot} whereby it mocks
35+
* the HandleHelp and HandleSnapshot methods for testing their functionality.
36+
*/
37+
class CreateSnapshotMock : public hdfs::tools::CreateSnapshot {
38+
public:
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
CreateSnapshotMock(const int argc, char **argv)
43+
: CreateSnapshot(argc, argv) {}
44+
45+
// Abiding to the Rule of 5
46+
CreateSnapshotMock(const CreateSnapshotMock &) = delete;
47+
CreateSnapshotMock(CreateSnapshotMock &&) = delete;
48+
CreateSnapshotMock &operator=(const CreateSnapshotMock &) = delete;
49+
CreateSnapshotMock &operator=(CreateSnapshotMock &&) = delete;
50+
~CreateSnapshotMock() override;
51+
52+
/**
53+
* Defines the methods and the corresponding arguments that are expected
54+
* to be called on this instance of {@link HdfsTool} for the given test case.
55+
*
56+
* @param test_case An {@link std::function} object that points to the
57+
* function defining the test case
58+
* @param args The arguments that are passed to this test case
59+
*/
60+
void SetExpectations(
61+
std::function<std::unique_ptr<CreateSnapshotMock>()> test_case,
62+
const std::vector<std::string> &args = {}) const;
63+
64+
MOCK_METHOD(bool, HandleHelp, (), (const, override));
65+
66+
MOCK_METHOD(bool, HandleSnapshot,
67+
(const std::string &, const std::optional<std::string> &),
68+
(const, override));
69+
};
70+
} // namespace hdfs::tools::test
71+
72+
#endif
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,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
19+
20+
#include <functional>
21+
#include <memory>
22+
#include <string>
23+
#include <vector>
24+
25+
#include <gmock/gmock.h>
26+
#include <gtest/gtest.h>
27+
28+
#include "hdfs-disallow-snapshot-mock.h"
29+
#include "hdfs-tool-tests.h"
30+
31+
namespace hdfs::tools::test {
32+
DisallowSnapshotMock::~DisallowSnapshotMock() = default;
33+
34+
void DisallowSnapshotMock::SetExpectations(
35+
std::function<std::unique_ptr<DisallowSnapshotMock>()> 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<DisallowSnapshotMock> (*)()>();
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<DisallowSnapshotMock>) {
45+
EXPECT_CALL(*this, HandleHelp()).Times(1).WillOnce(testing::Return(true));
46+
return;
47+
}
48+
49+
if (*test_case_func == &PassAPath<DisallowSnapshotMock>) {
50+
const auto arg1 = args[0];
51+
EXPECT_CALL(*this, HandleSnapshot(arg1))
52+
.Times(1)
53+
.WillOnce(testing::Return(true));
54+
}
55+
}
56+
} // namespace hdfs::tools::test
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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_DISALLOW_SNAPSHOT_MOCK
20+
#define LIBHDFSPP_TOOLS_HDFS_DISALLOW_SNAPSHOT_MOCK
21+
22+
#include <functional>
23+
#include <memory>
24+
#include <string>
25+
#include <vector>
26+
27+
#include <gmock/gmock.h>
28+
29+
#include "hdfs-disallow-snapshot.h"
30+
31+
namespace hdfs::tools::test {
32+
/**
33+
* {@class DisallowSnapshotMock} is an {@class DisallowSnapshot} whereby it
34+
* mocks the HandleHelp and HandleSnapshot methods for testing their
35+
* functionality.
36+
*/
37+
class DisallowSnapshotMock : public hdfs::tools::DisallowSnapshot {
38+
public:
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
DisallowSnapshotMock(const int argc, char **argv)
43+
: DisallowSnapshot(argc, argv) {}
44+
45+
// Abiding to the Rule of 5
46+
DisallowSnapshotMock(const DisallowSnapshotMock &) = delete;
47+
DisallowSnapshotMock(DisallowSnapshotMock &&) = delete;
48+
DisallowSnapshotMock &operator=(const DisallowSnapshotMock &) = delete;
49+
DisallowSnapshotMock &operator=(DisallowSnapshotMock &&) = delete;
50+
~DisallowSnapshotMock() override;
51+
52+
/**
53+
* Defines the methods and the corresponding arguments that are expected
54+
* to be called on this instance of {@link HdfsTool} for the given test case.
55+
*
56+
* @param test_case An {@link std::function} object that points to the
57+
* function defining the test case
58+
* @param args The arguments that are passed to this test case
59+
*/
60+
void SetExpectations(
61+
std::function<std::unique_ptr<DisallowSnapshotMock>()> test_case,
62+
const std::vector<std::string> &args = {}) const;
63+
64+
MOCK_METHOD(bool, HandleHelp, (), (const, override));
65+
66+
MOCK_METHOD(bool, HandleSnapshot, (const std::string &), (const, override));
67+
};
68+
} // namespace hdfs::tools::test
69+
70+
#endif
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
19+
20+
#include <functional>
21+
#include <memory>
22+
#include <string>
23+
#include <vector>
24+
25+
#include <gmock/gmock.h>
26+
#include <gtest/gtest.h>
27+
28+
#include "hdfs-rename-snapshot-mock.h"
29+
#include "hdfs-tool-tests.h"
30+
31+
namespace hdfs::tools::test {
32+
RenameSnapshotMock::~RenameSnapshotMock() = default;
33+
34+
void RenameSnapshotMock::SetExpectations(
35+
std::function<std::unique_ptr<RenameSnapshotMock>()> 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<RenameSnapshotMock> (*)()>();
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<RenameSnapshotMock>) {
45+
EXPECT_CALL(*this, HandleHelp()).Times(1).WillOnce(testing::Return(true));
46+
return;
47+
}
48+
49+
if (*test_case_func == &Pass3Paths<RenameSnapshotMock>) {
50+
const auto arg1 = args[0];
51+
const auto arg2 = args[1];
52+
const auto arg3 = args[2];
53+
EXPECT_CALL(*this, HandleSnapshot(arg1, arg2, arg3))
54+
.Times(1)
55+
.WillOnce(testing::Return(true));
56+
}
57+
}
58+
} // namespace hdfs::tools::test

0 commit comments

Comments
 (0)