Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HDFS-16445. Make HDFS count, mkdir, rm cross platform #3945

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ add_executable(hdfs_tool_tests
hdfs-du-mock.cc
hdfs-copy-to-local-mock.cc
hdfs-move-to-local-mock.cc
hdfs-count-mock.cc
hdfs-mkdir-mock.cc
hdfs-rm-mock.cc
main.cc)
target_include_directories(hdfs_tool_tests PRIVATE
../tools
Expand All @@ -48,6 +51,9 @@ target_include_directories(hdfs_tool_tests PRIVATE
../../tools/hdfs-chmod
../../tools/hdfs-copy-to-local
../../tools/hdfs-move-to-local
../../tools/hdfs-count
../../tools/hdfs-mkdir
../../tools/hdfs-rm
../../tools/hdfs-cat)
target_link_libraries(hdfs_tool_tests PRIVATE
gmock_main
Expand All @@ -63,5 +69,8 @@ target_link_libraries(hdfs_tool_tests PRIVATE
hdfs_chmod_lib
hdfs_copyToLocal_lib
hdfs_moveToLocal_lib
hdfs_count_lib
hdfs_mkdir_lib
hdfs_rm_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,63 @@
/**
* 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-count-mock.h"
#include "hdfs-tool-tests.h"

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

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

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

if (*test_case_func == &PassAPath<CountMock>) {
const auto arg1 = args[0];
EXPECT_CALL(*this, HandlePath(false, arg1))
.Times(1)
.WillOnce(testing::Return(true));
}

if (*test_case_func == &PassQOptAndPath<CountMock>) {
const auto arg1 = args[0];
const auto arg2 = args[1];
EXPECT_CALL(*this, HandlePath(true, arg2))
.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_COUNT_MOCK
#define LIBHDFSPP_TOOLS_HDFS_COUNT_MOCK

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

#include <gmock/gmock.h>

#include "hdfs-count.h"

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

// Abiding to the Rule of 5
CountMock(const CountMock &) = delete;
CountMock(CountMock &&) = delete;
CountMock &operator=(const CountMock &) = delete;
CountMock &operator=(CountMock &&) = delete;
~CountMock() 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<CountMock>()> test_case,
const std::vector<std::string> &args = {}) const;

MOCK_METHOD(bool, HandleHelp, (), (const, override));

MOCK_METHOD(bool, HandlePath, (const bool, const std::string &),
(const, override));
};
} // namespace hdfs::tools::test

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* 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 <optional>
#include <string>
#include <vector>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "hdfs-mkdir-mock.h"
#include "hdfs-tool-tests.h"

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

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

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

if (*test_case_func == &PassAPath<MkdirMock>) {
const auto arg1 = args[0];
const std::optional<std::string> permissions = std::nullopt;
EXPECT_CALL(*this, HandlePath(false, permissions, arg1))
.Times(1)
.WillOnce(testing::Return(true));
}

if (*test_case_func == &PassPOptAndPath<MkdirMock>) {
const auto arg1 = args[1];
const std::optional<std::string> permissions = std::nullopt;
EXPECT_CALL(*this, HandlePath(true, permissions, arg1))
.Times(1)
.WillOnce(testing::Return(true));
}

if (*test_case_func == &PassMOptPermissionsAndAPath<MkdirMock>) {
const auto arg1 = args[1];
const auto arg2 = args[2];
const auto permissions = std::optional(arg1);
EXPECT_CALL(*this, HandlePath(false, permissions, arg2))
.Times(1)
.WillOnce(testing::Return(true));
}

if (*test_case_func == &PassMPOptsPermissionsAndAPath<MkdirMock>) {
const auto arg1 = args[1];
const auto arg2 = args[3];
const auto permissions = std::optional(arg1);
EXPECT_CALL(*this, HandlePath(true, permissions, arg2))
.Times(1)
.WillOnce(testing::Return(true));
}
}
} // namespace hdfs::tools::test
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* 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_MKDIR_MOCK
#define LIBHDFSPP_TOOLS_HDFS_MKDIR_MOCK

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

#include <gmock/gmock.h>

#include "hdfs-mkdir.h"

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

// Abiding to the Rule of 5
MkdirMock(const MkdirMock &) = delete;
MkdirMock(MkdirMock &&) = delete;
MkdirMock &operator=(const MkdirMock &) = delete;
MkdirMock &operator=(MkdirMock &&) = delete;
~MkdirMock() 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<MkdirMock>()> test_case,
const std::vector<std::string> &args = {}) const;

MOCK_METHOD(bool, HandleHelp, (), (const, override));

MOCK_METHOD(bool, HandlePath,
(bool, const std::optional<std::string> &, const std::string &),
(const, override));
};
} // namespace hdfs::tools::test

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* 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-rm-mock.h"
#include "hdfs-tool-tests.h"

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

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

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

if (*test_case_func == &PassAPath<RmMock>) {
const auto arg1 = args[0];
EXPECT_CALL(*this, HandlePath(false, arg1))
.Times(1)
.WillOnce(testing::Return(true));
}

if (*test_case_func == &PassRecursivePath<RmMock>) {
const auto arg1 = args[1];
EXPECT_CALL(*this, HandlePath(true, arg1))
.Times(1)
.WillOnce(testing::Return(true));
}
}
} // namespace hdfs::tools::test
Loading