forked from bharathv/kudu
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
a1e6b88
commit 7321b38
Showing
4 changed files
with
188 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters