YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
89
std::string AdminTestBase::GetAdminToolPath() const {
34
89
  return GetToolPath(kAdminToolName);
35
89
}
36
37
87
std::string AdminTestBase::GetMasterAddresses() const {
38
87
  std::string result;
39
205
  for (const auto* master : cluster_->master_daemons()) {
40
205
    if (!result.empty()) {
41
118
      result += ",";
42
118
    }
43
205
    result += AsString(master->bound_rpc_addr());
44
205
  }
45
87
  return result;
46
87
}
47
48
93
Result<std::string> AdminTestBase::CallAdminVec(const std::vector<std::string>& args) {
49
93
  std::string result;
50
93
  LOG(INFO) << "Execute: " << AsString(args);
51
93
  auto status = Subprocess::Call(args, &result, StdFdTypes{StdFdType::kOut, StdFdType::kErr});
52
93
  if (!status.ok()) {
53
40
    return status.CloneAndAppend(result);
54
40
  }
55
53
  return result;
56
93
}
57
58
25
Result<rapidjson::Document> AdminTestBase::ParseJson(const std::string& raw) {
59
25
  rapidjson::Document result;
60
25
  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
25
  return result;
65
25
}
66
67
14
Result<CassandraSession> AdminTestBase::CqlConnect(const std::string& db_name) {
68
14
  if (!cql_driver_) {
69
14
    std::vector<std::string> hosts;
70
56
    for (size_t i = 0; i < cluster_->num_tablet_servers(); 
++i42
) {
71
42
      hosts.push_back(cluster_->tablet_server(i)->bind_host());
72
42
    }
73
14
    LOG(INFO) << "CQL hosts: " << AsString(hosts);
74
14
    cql_driver_ = std::make_unique<CppCassandraDriver>(
75
14
        hosts, cluster_->tablet_server(0)->cql_rpc_port(), UsePartitionAwareRouting::kTrue);
76
14
  }
77
14
  auto result = VERIFY_RESULT(cql_driver_->CreateSession());
78
14
  if (!db_name.empty()) {
79
0
    RETURN_NOT_OK(result.ExecuteQuery(Format("USE $0", db_name)));
80
0
  }
81
14
  return result;
82
14
}
83
84
48
Result<const rapidjson::Value&> Get(const rapidjson::Value& value, const char* name) {
85
48
  auto it = value.FindMember(name);
86
48
  if (it == value.MemberEnd()) {
87
0
    return STATUS_FORMAT(InvalidArgument, "Missing $0 field", name);
88
0
  }
89
48
  return it->value;
90
48
}
91
92
22
Result<rapidjson::Value&> Get(rapidjson::Value* value, const char* name) {
93
22
  auto it = value->FindMember(name);
94
22
  if (it == value->MemberEnd()) {
95
0
    return STATUS_FORMAT(InvalidArgument, "Missing $0 field", name);
96
0
  }
97
22
  return it->value;
98
22
}
99
100
}  // namespace tools
101
}  // namespace yb