Skip to content

Commit

Permalink
tool: basic integration test
Browse files Browse the repository at this point in the history
So far all it does is spot check some help pages, but in the future we
should augment it to test functionality too. For now that's not a big deal
because every tool function is covered in either master_migration-itest or
master_failover-itest.

Change-Id: Ib386882c1874e987d5824cfe742cc86627cd9eaa
Reviewed-on: http://gerrit.cloudera.org:8080/4058
Tested-by: Kudu Jenkins
Reviewed-by: Todd Lipcon <[email protected]>
  • Loading branch information
adembo authored and toddlipcon committed Aug 25, 2016
1 parent a1e6b88 commit 7321b38
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 3 deletions.
13 changes: 11 additions & 2 deletions build-support/dist_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ def ldd_deps(exe):
If the provided 'exe' is not a binary executable, returns
an empty list.
"""
if exe.endswith(".sh"):
if (exe.endswith(".pl") or
exe.endswith(".py") or
exe.endswith(".sh") or
exe.endswith(".txt")):
return []
p = subprocess.Popen(["ldd", exe], stdout=subprocess.PIPE)
out, err = p.communicate()
Expand Down Expand Up @@ -243,7 +246,13 @@ def create_archive_input(staging, argv,
if os.path.isdir(d):
d += "/"
deps.append(d)
for d in deps:
# DEPS_FOR_ALL may include binaries whose dependencies are not dependencies
# of the test executable. We must include those dependencies in the archive
# for the binaries to be usable.
deps.extend(ldd_deps(d))

# Deduplicate dependencies included via DEPS_FOR_ALL.
for d in set(deps):
# System libraries will end up being relative paths out
# of the build tree. We need to copy those into the build
# tree somewhere.
Expand Down
3 changes: 3 additions & 0 deletions src/kudu/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ ADD_KUDU_TEST(ksck_remote-test)
ADD_KUDU_TEST(kudu-admin-test)
ADD_KUDU_TEST_DEPENDENCIES(kudu-admin-test
kudu-admin)
ADD_KUDU_TEST(kudu-tool-test)
ADD_KUDU_TEST_DEPENDENCIES(kudu-tool-test
kudu)
ADD_KUDU_TEST(kudu-ts-cli-test)
ADD_KUDU_TEST_DEPENDENCIES(kudu-ts-cli-test
kudu-ts-cli)
Expand Down
154 changes: 154 additions & 0 deletions src/kudu/tools/kudu-tool-test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// 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 <string>
#include <vector>

#include <gtest/gtest.h>
#include <glog/stl_logging.h>

#include "kudu/gutil/strings/split.h"
#include "kudu/gutil/strings/substitute.h"
#include "kudu/util/env.h"
#include "kudu/util/path_util.h"
#include "kudu/util/subprocess.h"
#include "kudu/util/test_macros.h"
#include "kudu/util/test_util.h"

namespace kudu {
namespace tools {

using std::string;
using std::vector;

class ToolTest : public KuduTest {
public:
ToolTest() {
string exe;
CHECK_OK(env_->GetExecutablePath(&exe));
string bin_root = DirName(exe);
string tool_path = JoinPathSegments(bin_root, "kudu");
CHECK(env_->FileExists(tool_path)) << "kudu tool not found at " << tool_path;
tool_path_ = tool_path;
}

Status RunTool(const string& arg_str,
vector<string>* stdout_lines,
vector<string>* stderr_lines) const {
vector<string> args = { tool_path_ };
vector<string> more_args = strings::Split(arg_str, " ",
strings::SkipEmpty());
args.insert(args.end(), more_args.begin(), more_args.end());

string stdout;
string stderr;
Status s = Subprocess::Call(args, &stdout, &stderr);
StripWhiteSpace(&stdout);
StripWhiteSpace(&stderr);
*stdout_lines = strings::Split(stdout, "\n", strings::SkipEmpty());
*stderr_lines = strings::Split(stderr, "\n", strings::SkipEmpty());
return s;

}

void RunTestHelp(const string& arg_str,
const vector<string>& regexes,
const Status& expected_status = Status::OK()) const {
vector<string> stdout;
vector<string> stderr;
Status s = RunTool(arg_str, &stdout, &stderr);
SCOPED_TRACE(stdout);
SCOPED_TRACE(stderr);

// These are always true for showing help.
ASSERT_TRUE(s.IsRuntimeError());
ASSERT_TRUE(stdout.empty());
ASSERT_FALSE(stderr.empty());

// If it was an invalid command, the usage string is on the second line.
int usage_idx = 0;
if (!expected_status.ok()) {
ASSERT_EQ(expected_status.ToString(), stderr[0]);
usage_idx = 1;
}
ASSERT_EQ(0, stderr[usage_idx].find("Usage: "));

// Strip away everything up to the usage string to test for regexes.
vector<string> remaining_lines;
for (int i = usage_idx + 1; i < stderr.size(); i++) {
remaining_lines.push_back(stderr[i]);
}
for (const auto& r : regexes) {
ASSERT_STRINGS_ANY_MATCH(remaining_lines, r);
}
}

private:
string tool_path_;
};

TEST_F(ToolTest, TestTopLevelHelp) {
const vector<string> kTopLevelRegexes = {
"fs.*Kudu filesystem",
"pbc.*protobuf container",
"tablet.*Kudu replica"
};
NO_FATALS(RunTestHelp("", kTopLevelRegexes));
NO_FATALS(RunTestHelp("--help", kTopLevelRegexes));
NO_FATALS(RunTestHelp("not_a_mode", kTopLevelRegexes,
Status::InvalidArgument("unknown command 'not_a_mode'")));
}

TEST_F(ToolTest, TestModeHelp) {
{
const vector<string> kFsModeRegexes = {
"format.*new Kudu filesystem",
"print_uuid.*UUID of a Kudu filesystem"
};
NO_FATALS(RunTestHelp("fs", kFsModeRegexes));
NO_FATALS(RunTestHelp("fs not_a_mode", kFsModeRegexes,
Status::InvalidArgument("unknown command 'not_a_mode'")));
}
{
const vector<string> kTabletModeRegexes = {
"cmeta.*consensus metadata file",
"copy.*Copy a replica"
};
NO_FATALS(RunTestHelp("tablet", kTabletModeRegexes));
}
{
const vector<string> kCmetaModeRegexes = {
"print_replica_uuids.*Print all replica UUIDs",
"rewrite_raft_config.*Rewrite a replica"
};
NO_FATALS(RunTestHelp("tablet cmeta", kCmetaModeRegexes));
}
}

TEST_F(ToolTest, TestActionHelp) {
const vector<string> kFormatActionRegexes = {
"-fs_wal_dir \\(Directory",
"-fs_data_dirs \\(Comma-separated list",
"-uuid \\(The uuid"
};
NO_FATALS(RunTestHelp("fs format --help", kFormatActionRegexes));
NO_FATALS(RunTestHelp("fs format extra_arg", kFormatActionRegexes,
Status::InvalidArgument("too many arguments: 'extra_arg'")));
}

} // namespace tools
} // namespace kudu
21 changes: 20 additions & 1 deletion src/kudu/util/test_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
ASSERT_THAT(str, testing::ContainsRegex(pattern))

// Batched substring regular expressions in extended regex (POSIX) syntax.
#define ASSERT_STRINGS_MATCH(strings, pattern) do { \
//
// All strings must match the pattern.
#define ASSERT_STRINGS_ALL_MATCH(strings, pattern) do { \
const auto& _strings = (strings); \
const auto& _pattern = (pattern); \
int _str_idx = 0; \
Expand All @@ -75,6 +77,23 @@
} \
} while (0)

// Batched substring regular expressions in extended regex (POSIX) syntax.
//
// At least one string must match the pattern.
#define ASSERT_STRINGS_ANY_MATCH(strings, pattern) do { \
const auto& _strings = (strings); \
const auto& _pattern = (pattern); \
bool matched = false; \
for (const auto& str : _strings) { \
if (testing::internal::RE::PartialMatch(str, testing::internal::RE(_pattern))) { \
matched = true; \
break; \
} \
} \
ASSERT_TRUE(matched) \
<< "not one string matched pattern " << _pattern; \
} while (0)

#define ASSERT_FILE_EXISTS(env, path) do { \
std::string _s = path; \
ASSERT_TRUE(env->FileExists(_s)) \
Expand Down

0 comments on commit 7321b38

Please sign in to comment.