/Users/deen/code/yugabyte-db/src/yb/tools/tools_test_utils.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/tools_test_utils.h" |
15 | | |
16 | | #include "yb/util/jsonreader.h" |
17 | | #include "yb/util/net/net_util.h" |
18 | | #include "yb/util/path_util.h" |
19 | | #include "yb/util/random_util.h" |
20 | | #include "yb/util/status_log.h" |
21 | | #include "yb/util/subprocess.h" |
22 | | #include "yb/util/test_util.h" |
23 | | |
24 | | DEFINE_bool(verbose_yb_backup, false, "Add --verbose flag to yb_backup.py."); |
25 | | |
26 | | namespace yb { |
27 | | namespace tools { |
28 | | |
29 | | Status RunBackupCommand( |
30 | | const HostPort& pg_hp, const std::string& master_addresses, |
31 | | const std::string& tserver_http_addresses, const std::string& tmp_dir, |
32 | 0 | const std::vector<std::string>& extra_args) { |
33 | 0 | std::vector <std::string> args = { |
34 | 0 | "python3", GetToolPath("../../../managed/devops/bin", "yb_backup.py"), |
35 | 0 | "--masters", master_addresses, |
36 | 0 | "--ts_web_hosts_ports", tserver_http_addresses, |
37 | 0 | "--remote_yb_admin_binary", GetToolPath("yb-admin"), |
38 | 0 | "--remote_ysql_dump_binary", GetPgToolPath("ysql_dump"), |
39 | 0 | "--remote_ysql_shell_binary", GetPgToolPath("ysqlsh"), |
40 | 0 | "--ysql_host", pg_hp.host(), |
41 | 0 | "--ysql_port", AsString(pg_hp.port()), |
42 | 0 | "--storage_type", "nfs", |
43 | 0 | "--nfs_storage_path", tmp_dir, |
44 | 0 | "--no_ssh", |
45 | 0 | "--no_auto_name", |
46 | 0 | }; |
47 | 0 | #if defined(__APPLE__) |
48 | 0 | args.push_back("--mac"); |
49 | 0 | #endif // defined(__APPLE__) |
50 | |
|
51 | 0 | std::string backup_cmd; |
52 | 0 | for (const auto& a : extra_args) { |
53 | 0 | args.push_back(a); |
54 | 0 | if (a == "create" || a == "restore") { |
55 | 0 | backup_cmd = a; |
56 | 0 | } |
57 | 0 | } |
58 | |
|
59 | 0 | if (FLAGS_verbose_yb_backup) { |
60 | 0 | args.push_back("--verbose"); |
61 | 0 | } |
62 | |
|
63 | 0 | LOG(INFO) << "Run tool: " << AsString(args); |
64 | 0 | string output; |
65 | 0 | RETURN_NOT_OK(Subprocess::Call(args, &output)); |
66 | 0 | LOG(INFO) << "Tool output: " << output; |
67 | |
|
68 | 0 | JsonReader r(output); |
69 | 0 | RETURN_NOT_OK(r.Init()); |
70 | 0 | string error; |
71 | 0 | Status s = r.ExtractString(r.root(), "error", &error); |
72 | 0 | if (s.ok()) { |
73 | 0 | LOG(ERROR) << "yb_backup.py error: " << error; |
74 | 0 | return STATUS(RuntimeError, "yb_backup.py error", error); |
75 | 0 | } |
76 | | |
77 | 0 | if (backup_cmd == "create") { |
78 | 0 | string url; |
79 | 0 | RETURN_NOT_OK(r.ExtractString(r.root(), "snapshot_url", &url)); |
80 | 0 | LOG(INFO) << "Backup-create operation result - snapshot url: " << url; |
81 | 0 | } else if (backup_cmd == "restore") { |
82 | 0 | bool result_ok = false; |
83 | 0 | RETURN_NOT_OK(r.ExtractBool(r.root(), "success", &result_ok)); |
84 | 0 | LOG(INFO) << "Backup-restore operation result: " << result_ok; |
85 | 0 | if (!result_ok) { |
86 | 0 | return STATUS(RuntimeError, "Failed backup restore operation"); |
87 | 0 | } |
88 | 0 | } else { |
89 | 0 | return STATUS(InvalidArgument, "Unknown backup command", ToString(args)); |
90 | 0 | } |
91 | | |
92 | 0 | return Status::OK(); |
93 | 0 | } |
94 | | |
95 | 0 | TmpDirProvider::~TmpDirProvider() { |
96 | 0 | if (!dir_.empty()) { |
97 | 0 | LOG(INFO) << "Deleting temporary folder: " << dir_; |
98 | 0 | CHECK_OK(Env::Default()->DeleteRecursively(dir_)); |
99 | 0 | } |
100 | 0 | } |
101 | | |
102 | 0 | std::string TmpDirProvider::operator/(const std::string& subdir) { |
103 | 0 | return JoinPathSegments(**this, subdir); |
104 | 0 | } |
105 | | |
106 | 0 | std::string TmpDirProvider::operator*() { |
107 | 0 | if (dir_.empty()) { |
108 | 0 | std::string temp; |
109 | 0 | CHECK_OK(Env::Default()->GetTestDirectory(&temp)); |
110 | 0 | dir_ = JoinPathSegments( |
111 | 0 | temp, std::string(CURRENT_TEST_CASE_NAME()) + '_' + RandomHumanReadableString(8)); |
112 | 0 | } |
113 | | // Create the directory if it doesn't exist. |
114 | 0 | if (!Env::Default()->DirExists(dir_)) { |
115 | 0 | EXPECT_OK(Env::Default()->CreateDir(dir_)); |
116 | 0 | } |
117 | 0 | return dir_; |
118 | 0 | } |
119 | | |
120 | | } // namespace tools |
121 | | } // namespace yb |