Skip to content
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 @@ -40,6 +40,7 @@ add_executable(hdfs_tool_tests
hdfs-ls-mock.cc
hdfs-setrep-mock.cc
hdfs-stat-mock.cc
hdfs-tail-mock.cc
main.cc)
target_include_directories(hdfs_tool_tests PRIVATE
../tools
Expand All @@ -64,6 +65,7 @@ target_include_directories(hdfs_tool_tests PRIVATE
../../tools/hdfs-ls
../../tools/hdfs-setrep
../../tools/hdfs-stat
../../tools/hdfs-tail
../../tools/hdfs-cat)
target_link_libraries(hdfs_tool_tests PRIVATE
gmock_main
Expand All @@ -87,5 +89,6 @@ target_link_libraries(hdfs_tool_tests PRIVATE
hdfs_ls_lib
hdfs_setrep_lib
hdfs_stat_lib
hdfs_tail_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,64 @@
/**
* 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-tail-mock.h"
#include "hdfs-tool-tests.h"

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

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

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

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

if (*test_case_func == &PassFOptAndAPath<TailMock>) {
const auto f_opt = args[0];
const auto path = args[1];
ASSERT_EQ(f_opt, "-f");
EXPECT_CALL(*this, HandlePath(path, true))
.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_TAIL_MOCK
#define LIBHDFSPP_TOOLS_HDFS_TAIL_MOCK

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

#include <gmock/gmock.h>

#include "hdfs-tail.h"

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

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

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

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

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "hdfs-rm-mock.h"
#include "hdfs-setrep-mock.h"
#include "hdfs-stat-mock.h"
#include "hdfs-tail-mock.h"
#include "hdfs-tool-test-fixtures.h"
#include "hdfs-tool-tests.h"

Expand Down Expand Up @@ -168,6 +169,12 @@ INSTANTIATE_TEST_SUITE_P(
testing::Values(CallHelp<hdfs::tools::test::StatMock>,
PassAPath<hdfs::tools::test::StatMock>));

INSTANTIATE_TEST_SUITE_P(
HdfsTail, HdfsToolBasicTest,
testing::Values(PassAPath<hdfs::tools::test::TailMock>,
CallHelp<hdfs::tools::test::TailMock>,
PassFOptAndAPath<hdfs::tools::test::TailMock>));

// Negative tests
INSTANTIATE_TEST_SUITE_P(
HdfsAllowSnapshot, HdfsToolNegativeTestThrows,
Expand Down Expand Up @@ -282,6 +289,20 @@ INSTANTIATE_TEST_SUITE_P(
PassMOpt<hdfs::tools::test::StatMock>,
PassNOpt<hdfs::tools::test::StatMock>));

INSTANTIATE_TEST_SUITE_P(
HdfsTail, HdfsToolNegativeTestThrows,
testing::Values(Pass2Paths<hdfs::tools::test::TailMock>,
Pass3Paths<hdfs::tools::test::TailMock>,
PassNOptAndAPath<hdfs::tools::test::TailMock>,
PassRecursiveOwnerAndAPath<hdfs::tools::test::TailMock>,
PassMOpt<hdfs::tools::test::TailMock>,
PassRecursive<hdfs::tools::test::TailMock>,
PassRecursivePath<hdfs::tools::test::TailMock>,
PassNOpt<hdfs::tools::test::TailMock>,
PassOwnerAndAPath<hdfs::tools::test::TailMock>,
PassMPOptsPermissionsAndAPath<hdfs::tools::test::TailMock>,
PassPermissionsAndAPath<hdfs::tools::test::TailMock>));

INSTANTIATE_TEST_SUITE_P(
HdfsRm, HdfsToolNegativeTestNoThrow,
testing::Values(PassRecursive<hdfs::tools::test::RmMock>));
Expand Down Expand Up @@ -341,3 +362,7 @@ INSTANTIATE_TEST_SUITE_P(
INSTANTIATE_TEST_SUITE_P(
HdfsSetrep, HdfsToolNegativeTestNoThrow,
testing::Values(PassAPath<hdfs::tools::test::SetrepMock>));

INSTANTIATE_TEST_SUITE_P(
HdfsTail, HdfsToolNegativeTestNoThrow,
testing::Values(PassFOpt<hdfs::tools::test::TailMock>));
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ template <class T> std::unique_ptr<T> PassRecursivePath() {
return hdfs_tool;
}

template <class T> std::unique_ptr<T> PassFOptAndAPath() {
constexpr auto argc = 3;
static std::string exe("hdfs_tool_name");
static std::string arg1("-f");
static std::string arg2("a/b/c");

static char *argv[] = {exe.data(), arg1.data(), arg2.data()};

auto hdfs_tool = std::make_unique<T>(argc, argv);
hdfs_tool->SetExpectations(PassFOptAndAPath<T>, {arg1, arg2});
return hdfs_tool;
}

template <class T> std::unique_ptr<T> CallHelp() {
constexpr auto argc = 2;
static std::string exe("hdfs_tool_name");
Expand Down Expand Up @@ -226,6 +239,18 @@ template <class T> std::unique_ptr<T> PassMOpt() {
return hdfs_tool;
}

template <class T> std::unique_ptr<T> PassFOpt() {
constexpr auto argc = 2;
static std::string exe("hdfs_tool_name");
static std::string arg1("-f");

static char *argv[] = {exe.data(), arg1.data()};

auto hdfs_tool = std::make_unique<T>(argc, argv);
hdfs_tool->SetExpectations(PassFOpt<T>, {arg1});
return hdfs_tool;
}

template <class T> std::unique_ptr<T> PassPOptAndPath() {
constexpr auto argc = 3;
static std::string exe("hdfs_tool_name");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,46 +34,24 @@ add_library(hdfs_tool_obj OBJECT hdfs-tool.cc)
target_include_directories(hdfs_tool_obj PRIVATE ../tools)

add_subdirectory(hdfs-cat)

add_subdirectory(hdfs-chgrp)

add_subdirectory(hdfs-chown)

add_subdirectory(hdfs-chmod)

add_subdirectory(hdfs-find)

add_subdirectory(hdfs-mkdir)

add_subdirectory(hdfs-rm)

add_subdirectory(hdfs-ls)

add_subdirectory(hdfs-stat)

add_subdirectory(hdfs-count)

add_subdirectory(hdfs-df)

add_subdirectory(hdfs-du)

add_subdirectory(hdfs-get)

add_subdirectory(hdfs-copy-to-local)

add_subdirectory(hdfs-move-to-local)

add_subdirectory(hdfs-setrep)

add_subdirectory(hdfs-allow-snapshot)

add_subdirectory(hdfs-disallow-snapshot)

add_subdirectory(hdfs-create-snapshot)

add_subdirectory(hdfs-rename-snapshot)

add_subdirectory(hdfs-delete-snapshot)

add_executable(hdfs_tail hdfs_tail.cc)
target_link_libraries(hdfs_tail tools_common hdfspp_static)
add_subdirectory(hdfs-tail)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# 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.
#

add_library(hdfs_tail_lib STATIC $<TARGET_OBJECTS:hdfs_tool_obj> hdfs-tail.cc)
target_include_directories(hdfs_tail_lib PRIVATE ../../tools ${Boost_INCLUDE_DIRS})
target_link_libraries(hdfs_tail_lib PRIVATE Boost::boost Boost::program_options tools_common hdfspp_static)

add_executable(hdfs_tail main.cc)
target_include_directories(hdfs_tail PRIVATE ../../tools)
target_link_libraries(hdfs_tail PRIVATE hdfs_tail_lib)

install(TARGETS hdfs_tail RUNTIME DESTINATION bin)
Loading