/Users/deen/code/yugabyte-db/src/yb/yql/pgwrapper/pg_wrapper_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/yql/pgwrapper/pg_wrapper_test_base.h" |
15 | | |
16 | | #include "yb/util/env_util.h" |
17 | | #include "yb/util/path_util.h" |
18 | | #include "yb/util/size_literals.h" |
19 | | #include "yb/util/string_trim.h" |
20 | | #include "yb/util/tostring.h" |
21 | | |
22 | | #include "yb/yql/pgwrapper/pg_wrapper.h" |
23 | | |
24 | | using std::unique_ptr; |
25 | | |
26 | | using yb::util::TrimStr; |
27 | | using yb::util::TrimTrailingWhitespaceFromEveryLine; |
28 | | using yb::util::LeftShiftTextBlock; |
29 | | |
30 | | namespace yb { |
31 | | namespace pgwrapper { |
32 | | |
33 | 0 | void PgWrapperTestBase::SetUp() { |
34 | 0 | YBMiniClusterTestBase::SetUp(); |
35 | |
|
36 | 0 | ExternalMiniClusterOptions opts; |
37 | 0 | opts.enable_ysql = true; |
38 | | |
39 | | // With ysql_num_shards_per_tserver=1 and 3 tservers we'll be creating 3 tablets per table, which |
40 | | // is enough for most tests. |
41 | 0 | opts.extra_tserver_flags.emplace_back("--ysql_num_shards_per_tserver=1"); |
42 | | |
43 | | // Collect old records very aggressively to catch bugs with old readpoints. |
44 | 0 | opts.extra_tserver_flags.emplace_back("--timestamp_history_retention_interval_sec=0"); |
45 | |
|
46 | 0 | opts.extra_master_flags.emplace_back("--hide_pg_catalog_table_creation_logs"); |
47 | |
|
48 | 0 | opts.num_masters = GetNumMasters(); |
49 | |
|
50 | 0 | opts.num_tablet_servers = GetNumTabletServers(); |
51 | |
|
52 | 0 | opts.extra_master_flags.emplace_back("--client_read_write_timeout_ms=120000"); |
53 | 0 | opts.extra_master_flags.emplace_back(Format("--memory_limit_hard_bytes=$0", 2_GB)); |
54 | |
|
55 | 0 | UpdateMiniClusterOptions(&opts); |
56 | |
|
57 | 0 | cluster_.reset(new ExternalMiniCluster(opts)); |
58 | 0 | ASSERT_OK(cluster_->Start()); |
59 | |
|
60 | 0 | if (cluster_->num_tablet_servers() > 0) { |
61 | 0 | pg_ts = cluster_->tablet_server(0); |
62 | 0 | } |
63 | | |
64 | | // TODO: fix cluster verification for PostgreSQL tables. |
65 | 0 | DontVerifyClusterBeforeNextTearDown(); |
66 | 0 | } |
67 | | |
68 | | namespace { |
69 | | |
70 | 0 | string TrimSqlOutput(string output) { |
71 | 0 | return TrimStr(TrimTrailingWhitespaceFromEveryLine(LeftShiftTextBlock(output))); |
72 | 0 | } |
73 | | |
74 | 0 | string CertsDir() { |
75 | 0 | const auto sub_dir = JoinPathSegments("ent", "test_certs"); |
76 | 0 | return JoinPathSegments(env_util::GetRootDir(sub_dir), sub_dir); |
77 | 0 | } |
78 | | |
79 | | } // namespace |
80 | | |
81 | 0 | void PgCommandTestBase::RunPsqlCommand(const string &statement, const string &expected_output) { |
82 | 0 | string tmp_dir; |
83 | 0 | ASSERT_OK(Env::Default()->GetTestDirectory(&tmp_dir)); |
84 | |
|
85 | 0 | unique_ptr<WritableFile> tmp_file; |
86 | 0 | string tmp_file_name; |
87 | 0 | ASSERT_OK( |
88 | 0 | Env::Default()->NewTempWritableFile( |
89 | 0 | WritableFileOptions(), |
90 | 0 | tmp_dir + "/psql_statementXXXXXX", |
91 | 0 | &tmp_file_name, |
92 | 0 | &tmp_file)); |
93 | 0 | ASSERT_OK(tmp_file->Append(statement)); |
94 | 0 | ASSERT_OK(tmp_file->Close()); |
95 | |
|
96 | 0 | vector<string> argv{ |
97 | 0 | GetPostgresInstallRoot() + "/bin/ysqlsh", |
98 | 0 | "-h", pg_ts->bind_host(), |
99 | 0 | "-p", std::to_string(pg_ts->pgsql_rpc_port()), |
100 | 0 | "-U", "yugabyte", |
101 | 0 | "-f", tmp_file_name |
102 | 0 | }; |
103 | |
|
104 | 0 | if (!db_name_.empty()) { |
105 | 0 | argv.push_back("-d"); |
106 | 0 | argv.push_back(db_name_); |
107 | 0 | } |
108 | |
|
109 | 0 | if (encrypt_connection_) { |
110 | 0 | argv.push_back(Format( |
111 | 0 | "sslmode=require sslcert=$0/ysql.crt sslrootcert=$0/ca.crt sslkey=$0/ysql.key", |
112 | 0 | CertsDir())); |
113 | 0 | } |
114 | |
|
115 | 0 | LOG(INFO) << "Run tool: " << yb::ToString(argv); |
116 | 0 | Subprocess proc(argv.front(), argv); |
117 | 0 | if (use_auth_) { |
118 | 0 | proc.SetEnv("PGPASSWORD", "yugabyte"); |
119 | 0 | } |
120 | |
|
121 | 0 | string psql_stdout; |
122 | 0 | LOG(INFO) << "Executing statement: " << statement; |
123 | 0 | ASSERT_OK(proc.Call(&psql_stdout)); |
124 | 0 | LOG(INFO) << "Output from statement {{ " << statement << " }}:\n" |
125 | 0 | << psql_stdout; |
126 | 0 | ASSERT_EQ(TrimSqlOutput(expected_output), TrimSqlOutput(psql_stdout)); |
127 | 0 | } |
128 | | |
129 | 0 | void PgCommandTestBase::UpdateMiniClusterOptions(ExternalMiniClusterOptions* options) { |
130 | 0 | PgWrapperTestBase::UpdateMiniClusterOptions(options); |
131 | 0 | if (encrypt_connection_) { |
132 | 0 | const vector<string> common_flags{"--use_node_to_node_encryption=true", |
133 | 0 | "--certs_dir=" + CertsDir()}; |
134 | 0 | for (auto flags : {&options->extra_master_flags, &options->extra_tserver_flags}) { |
135 | 0 | flags->insert(flags->begin(), common_flags.begin(), common_flags.end()); |
136 | 0 | } |
137 | 0 | options->extra_tserver_flags.push_back("--use_client_to_server_encryption=true"); |
138 | 0 | options->extra_tserver_flags.push_back("--allow_insecure_connections=false"); |
139 | 0 | options->use_even_ips = true; |
140 | 0 | } |
141 | |
|
142 | 0 | if (use_auth_) { |
143 | 0 | options->extra_tserver_flags.push_back("--ysql_enable_auth"); |
144 | 0 | } |
145 | 0 | } |
146 | | |
147 | | } // namespace pgwrapper |
148 | | } // namespace yb |