/Users/deen/code/yugabyte-db/src/yb/tools/admin-test-base.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include "yb/tools/admin-test-base.h" |
15 | | |
16 | | #include "yb/integration-tests/cql_test_util.h" |
17 | | #include "yb/integration-tests/external_mini_cluster.h" |
18 | | |
19 | | #include "yb/util/format.h" |
20 | | #include "yb/util/status_format.h" |
21 | | #include "yb/util/subprocess.h" |
22 | | |
23 | | namespace yb { |
24 | | namespace tools { |
25 | | |
26 | | namespace { |
27 | | |
28 | | const char* const kAdminToolName = "yb-admin"; |
29 | | |
30 | | } |
31 | | |
32 | | // Figure out where the admin tool is. |
33 | 0 | std::string AdminTestBase::GetAdminToolPath() const { |
34 | 0 | return GetToolPath(kAdminToolName); |
35 | 0 | } |
36 | | |
37 | 0 | std::string AdminTestBase::GetMasterAddresses() const { |
38 | 0 | std::string result; |
39 | 0 | for (const auto* master : cluster_->master_daemons()) { |
40 | 0 | if (!result.empty()) { |
41 | 0 | result += ","; |
42 | 0 | } |
43 | 0 | result += AsString(master->bound_rpc_addr()); |
44 | 0 | } |
45 | 0 | return result; |
46 | 0 | } |
47 | | |
48 | 0 | Result<std::string> AdminTestBase::CallAdminVec(const std::vector<std::string>& args) { |
49 | 0 | std::string result; |
50 | 0 | LOG(INFO) << "Execute: " << AsString(args); |
51 | 0 | auto status = Subprocess::Call(args, &result, StdFdTypes{StdFdType::kOut, StdFdType::kErr}); |
52 | 0 | if (!status.ok()) { |
53 | 0 | return status.CloneAndAppend(result); |
54 | 0 | } |
55 | 0 | return result; |
56 | 0 | } |
57 | | |
58 | 0 | Result<rapidjson::Document> AdminTestBase::ParseJson(const std::string& raw) { |
59 | 0 | rapidjson::Document result; |
60 | 0 | if (result.Parse(raw.c_str(), raw.length()).HasParseError()) { |
61 | 0 | return STATUS_FORMAT( |
62 | 0 | InvalidArgument, "Failed to parse json output $0: $1", result.GetParseError(), raw); |
63 | 0 | } |
64 | 0 | return result; |
65 | 0 | } |
66 | | |
67 | 0 | Result<CassandraSession> AdminTestBase::CqlConnect(const std::string& db_name) { |
68 | 0 | if (!cql_driver_) { |
69 | 0 | std::vector<std::string> hosts; |
70 | 0 | for (size_t i = 0; i < cluster_->num_tablet_servers(); ++i) { |
71 | 0 | hosts.push_back(cluster_->tablet_server(i)->bind_host()); |
72 | 0 | } |
73 | 0 | LOG(INFO) << "CQL hosts: " << AsString(hosts); |
74 | 0 | cql_driver_ = std::make_unique<CppCassandraDriver>( |
75 | 0 | hosts, cluster_->tablet_server(0)->cql_rpc_port(), UsePartitionAwareRouting::kTrue); |
76 | 0 | } |
77 | 0 | auto result = VERIFY_RESULT(cql_driver_->CreateSession()); |
78 | 0 | if (!db_name.empty()) { |
79 | 0 | RETURN_NOT_OK(result.ExecuteQuery(Format("USE $0", db_name))); |
80 | 0 | } |
81 | 0 | return result; |
82 | 0 | } |
83 | | |
84 | 0 | Result<const rapidjson::Value&> Get(const rapidjson::Value& value, const char* name) { |
85 | 0 | auto it = value.FindMember(name); |
86 | 0 | if (it == value.MemberEnd()) { |
87 | 0 | return STATUS_FORMAT(InvalidArgument, "Missing $0 field", name); |
88 | 0 | } |
89 | 0 | return it->value; |
90 | 0 | } |
91 | | |
92 | 0 | Result<rapidjson::Value&> Get(rapidjson::Value* value, const char* name) { |
93 | 0 | auto it = value->FindMember(name); |
94 | 0 | if (it == value->MemberEnd()) { |
95 | 0 | return STATUS_FORMAT(InvalidArgument, "Missing $0 field", name); |
96 | 0 | } |
97 | 0 | return it->value; |
98 | 0 | } |
99 | | |
100 | | } // namespace tools |
101 | | } // namespace yb |