Skip to content

HDFS-16473. Make HDFS stat tool cross platform #4145

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

Merged
merged 5 commits into from
Apr 8, 2022
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 @@ -39,6 +39,7 @@ add_executable(hdfs_tool_tests
hdfs-find-mock.cc
hdfs-ls-mock.cc
hdfs-setrep-mock.cc
hdfs-stat-mock.cc
main.cc)
target_include_directories(hdfs_tool_tests PRIVATE
../tools
Expand All @@ -62,6 +63,7 @@ target_include_directories(hdfs_tool_tests PRIVATE
../../tools/hdfs-find
../../tools/hdfs-ls
../../tools/hdfs-setrep
../../tools/hdfs-stat
../../tools/hdfs-cat)
target_link_libraries(hdfs_tool_tests PRIVATE
gmock_main
Expand All @@ -84,5 +86,6 @@ target_link_libraries(hdfs_tool_tests PRIVATE
hdfs_find_lib
hdfs_ls_lib
hdfs_setrep_lib
hdfs_stat_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,55 @@
/**
* 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-stat-mock.h"
#include "hdfs-tool-tests.h"

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

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

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

if (*test_case_func == &PassAPath<StatMock>) {
const auto path = args[0];
EXPECT_CALL(*this, HandlePath(path))
.Times(1)
.WillOnce(testing::Return(true));
}
}
} // namespace hdfs::tools::test
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* 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_STAT_MOCK
#define LIBHDFSPP_TOOLS_HDFS_STAT_MOCK

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

#include <gmock/gmock.h>

#include "hdfs-stat.h"

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

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

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

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

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

Expand Down Expand Up @@ -162,6 +163,11 @@ INSTANTIATE_TEST_SUITE_P(
testing::Values(CallHelp<hdfs::tools::test::SetrepMock>,
PassPermissionsAndAPath<hdfs::tools::test::SetrepMock>));

INSTANTIATE_TEST_SUITE_P(
HdfsStat, HdfsToolBasicTest,
testing::Values(CallHelp<hdfs::tools::test::StatMock>,
PassAPath<hdfs::tools::test::StatMock>));

// Negative tests
INSTANTIATE_TEST_SUITE_P(
HdfsAllowSnapshot, HdfsToolNegativeTestThrows,
Expand Down Expand Up @@ -265,6 +271,17 @@ INSTANTIATE_TEST_SUITE_P(
PassMOpt<hdfs::tools::test::SetrepMock>,
PassNOpt<hdfs::tools::test::SetrepMock>));

INSTANTIATE_TEST_SUITE_P(
HdfsStat, HdfsToolNegativeTestThrows,
testing::Values(Pass2Paths<hdfs::tools::test::StatMock>,
Pass3Paths<hdfs::tools::test::StatMock>,
PassRecursiveOwnerAndAPath<hdfs::tools::test::StatMock>,
PassRecursive<hdfs::tools::test::StatMock>,
PassRecursivePath<hdfs::tools::test::StatMock>,
PassMPOptsPermissionsAndAPath<hdfs::tools::test::StatMock>,
PassMOpt<hdfs::tools::test::StatMock>,
PassNOpt<hdfs::tools::test::StatMock>));

INSTANTIATE_TEST_SUITE_P(
HdfsRm, HdfsToolNegativeTestNoThrow,
testing::Values(PassRecursive<hdfs::tools::test::RmMock>));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ add_subdirectory(hdfs-rm)

add_subdirectory(hdfs-ls)

add_executable(hdfs_stat hdfs_stat.cc)
target_link_libraries(hdfs_stat tools_common hdfspp_static)
add_subdirectory(hdfs-stat)

add_subdirectory(hdfs-count)

Expand Down
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_stat_lib STATIC $<TARGET_OBJECTS:hdfs_tool_obj> hdfs-stat.cc)
target_include_directories(hdfs_stat_lib PRIVATE ../../tools ${Boost_INCLUDE_DIRS})
target_link_libraries(hdfs_stat_lib PRIVATE Boost::boost Boost::program_options tools_common hdfspp_static)

add_executable(hdfs_stat main.cc)
target_include_directories(hdfs_stat PRIVATE ../../tools)
target_link_libraries(hdfs_stat PRIVATE hdfs_stat_lib)

install(TARGETS hdfs_stat RUNTIME DESTINATION bin)
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* 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 <future>
#include <iostream>
#include <memory>
#include <ostream>
#include <sstream>
#include <string>

#include "hdfs-stat.h"
#include "tools_common.h"

namespace hdfs::tools {
Stat::Stat(const int argc, char **argv) : HdfsTool(argc, argv) {}

bool Stat::Initialize() {
auto add_options = opt_desc_.add_options();
add_options("help,h", "Displays the stat information for the given path. The "
"path can be a file or a directory.");
add_options("path", po::value<std::string>(),
"The path in the filesystem for which to display the "
"stat information.");

// We allow only one positional argument to be passed to this tool. An
// exception is thrown if multiple arguments are passed.
pos_opt_desc_.add("path", 1);

po::store(po::command_line_parser(argc_, argv_)
.options(opt_desc_)
.positional(pos_opt_desc_)
.run(),
opt_val_);
po::notify(opt_val_);
return true;
}

std::string Stat::GetDescription() const {
std::stringstream desc;
desc << "Usage: hdfs_stat PATH" << std::endl
<< std::endl
<< "Displays the stat information for the given path." << std::endl
<< "The path can be a file or a directory." << std::endl
<< "Examples:" << std::endl
<< "hdfs_stat hdfs://localhost.localdomain:8020/dir/file" << std::endl;
return desc.str();
}

bool Stat::Do() {
if (!Initialize()) {
std::cerr << "Unable to initialize HDFS stat tool" << std::endl;
return false;
}

if (!ValidateConstraints()) {
std::cout << GetDescription();
return false;
}

if (opt_val_.count("help") > 0) {
return HandleHelp();
}

if (opt_val_.count("path") > 0) {
const auto path = opt_val_["path"].as<std::string>();
return HandlePath(path);
}

return false;
}

bool Stat::HandleHelp() const {
std::cout << GetDescription();
return true;
}

bool Stat::HandlePath(const std::string &path) const {
// Building a URI object from the given uri_path
auto uri = hdfs::parse_path_or_exit(path);

const auto fs = hdfs::doConnect(uri, false);
if (!fs) {
std::cerr << "Could not connect the file system. " << std::endl;
return false;
}

hdfs::StatInfo stat_info;
const auto status = fs->GetFileInfo(uri.get_path(), stat_info);
if (!status.ok()) {
std::cerr << "Error: " << status.ToString() << std::endl;
return false;
}
std::cout << stat_info.str() << std::endl;
return true;
}
} // namespace hdfs::tools
Loading