File: router_test_helpers.h

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (297 lines) | stat: -rw-r--r-- 12,411 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
  Copyright (c) 2015, 2025, Oracle and/or its affiliates.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License, version 2.0,
  as published by the Free Software Foundation.

  This program is designed to work with certain software (including
  but not limited to OpenSSL) that is licensed under separate terms,
  as designated in a particular file or component or in included license
  documentation.  The authors of MySQL hereby grant you an additional
  permission to link the program and your derivative works with the
  separately licensed software that they have either included with
  the program or referenced in the documentation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef ROUTER_TESTS_TEST_HELPERS_INCLUDED
#define ROUTER_TESTS_TEST_HELPERS_INCLUDED

#include <chrono>
#include <functional>
#include <map>
#include <stdexcept>
#include <typeinfo>

#include "mysql/harness/stdx/attribute.h"

#define SKIP_GIT_TESTS(COND)                                       \
  if (COND) {                                                      \
    std::cout << "[  SKIPPED ] Tests using Git repository skipped" \
              << std::endl;                                        \
    return;                                                        \
  }

#define HARNESS_TEST_THROW_LIKE_(statement, expected_exception,                \
                                 expected_message, fail)                       \
  GTEST_AMBIGUOUS_ELSE_BLOCKER_                                                \
  if (::testing::internal::AlwaysTrue()) {                                     \
    try {                                                                      \
      GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement);               \
      fail() << "Expected exception of type " #expected_exception              \
             << " but got none\n";                                             \
    } catch (const expected_exception &e) {                                    \
      if (std::string(e.what()).find(expected_message) == std::string::npos) { \
        fail() << "Expected exception of type " #expected_exception            \
                  " with message: "                                            \
               << expected_message << "\nbut got message: " << e.what()        \
               << "\n";                                                        \
      }                                                                        \
    } catch (...) {                                                            \
      std::exception_ptr eptr = std::current_exception();                      \
      try {                                                                    \
        std::rethrow_exception(eptr);                                          \
      } catch (const std::exception &e) {                                      \
        fail() << "Expected exception of type " #expected_message "\nbut got " \
               << typeid(e).name() << ": " << e.what() << "\n";                \
      } catch (...) {                                                          \
        fail() << "Expected exception of type " #expected_message "\nbut got " \
               << "non-std-exception\n";                                       \
      }                                                                        \
    }                                                                          \
  } else                                                                       \
    do {                                                                       \
    } while (0)

#define ASSERT_THROW_LIKE(statement, expected_exception, expected_message)  \
  HARNESS_TEST_THROW_LIKE_(statement, expected_exception, expected_message, \
                           FAIL)

#define EXPECT_THROW_LIKE(statement, expected_exception, expected_message)  \
  HARNESS_TEST_THROW_LIKE_(statement, expected_exception, expected_message, \
                           ADD_FAILURE)

#include "mysql/harness/filesystem.h"

constexpr auto kDefaultPortReadyTimeout = std::chrono::milliseconds(5000);

/** @brief Returns the CMake source root folder
 *
 * @return mysql_harness::Path
 */
mysql_harness::Path get_cmake_source_dir();

/** @brief Gets environment variable as path
 *
 * Gets environment envvar and returns it as Path. When the environment
 * variable is not set, the alternative is tried.
 *
 * Throws runtime_error when the folder is not available.
 *
 * @param envvar Name of the environment variable
 * @param alternative Alternative Path when environment variable is not
 * available
 * @return mysql_harness::Path
 */
mysql_harness::Path get_envvar_path(const std::string &envvar,
                                    mysql_harness::Path alternative);

/** @brief Checks if the given regex pattern can be found in the input string
 *
 *
 * @param s       input string to check
 * @param pattern regex pattern to look for
 *
 * @return true if the given pattern could be found, false otherwise
 */
bool pattern_found(const std::string &s, const std::string &pattern);

/** @brief Initializes Windows sockets (no-op on other OSes)
 *
 * Exits program with error upon failure.
 */
void init_windows_sockets();

/** @brief Probes if the selected TCP port is accepting the connections.
 *
 * @param port      TCP port number to check
 * @param timeout   maximum timeout to wait for the port
 * @param hostname  name/IP address of the network host to check
 *
 * @returns true if the selected port accepts connections, false otherwise
 */
[[nodiscard]] bool wait_for_port_ready(
    uint16_t port, std::chrono::milliseconds timeout = kDefaultPortReadyTimeout,
    const std::string &hostname = "127.0.0.1");

/** @brief Probes if the selected unix socket is accepting the connections.
 *
 * @param socket    name of the socket to check
 * @param timeout   maximum timeout to wait for the socket
 *
 * @returns true if the selected socket accepts connections, false otherwise
 */
[[nodiscard]] bool wait_for_socket_ready(
    const std::string &socket,
    std::chrono::milliseconds timeout = kDefaultPortReadyTimeout);

/** @brief Probes if the selected file exists or not.
 *
 * @param file    name of the file to check
 * @param timeout maximum timeout to wait for the file to exist or not
 * @param exists  determines if we expect the file to exist or not
 *
 * @returns true if the file exists, false otherwise
 */
[[nodiscard]] bool wait_file_exists(
    const std::string &file, const bool exists = true,
    std::chrono::milliseconds timeout = std::chrono::seconds(5));

/** @brief Check if a given port is open / not used by any application.
 *
 * @param port TCP port that will be checked
 *
 * @returns true if the selected port is not used, false otherwise
 */
[[nodiscard]] bool is_port_unused(const uint16_t port);

/** @brief Check if a given port can be bind to.
 *
 * @param port TCP port that will be checked
 *
 * @returns true if the selected port can be bind to, false otherwise
 */
[[nodiscard]] bool is_port_bindable(const uint16_t port);

/** @brief Check if a given unix socket can be bind to.
 *
 * @param socket unix socket that will be checked
 *
 * @returns true if the selected socket can be bind to, false otherwise
 */
[[nodiscard]] bool is_socket_bindable(const std::string &socket);

/**
 * Wait until the port is not available (is used by any application).
 *
 * @param port      TCP port number to check
 * @param timeout   maximum timeout to wait for the port
 *
 * @return false if the port is still available after the timeout expiry,
 *         true otherwise.
 */
[[nodiscard]] bool wait_for_port_used(
    const uint16_t port,
    std::chrono::milliseconds timeout = std::chrono::seconds(10));

/**
 * Wait until the port is available (is not used by any application).
 *
 * @param port      TCP port number to check
 * @param timeout   maximum timeout to wait for the port
 *
 * @return false if the port is still not available after the timeout expiry,
 *         true otherwise.
 */
[[nodiscard]] bool wait_for_port_unused(
    const uint16_t port,
    std::chrono::milliseconds timeout = std::chrono::seconds(10));

/** @brief Initializes keyring and adds keyring-related config items to
 * [DEFAULT] section
 *
 * @param default_section [DEFAULT] section
 * @param keyring_dir directory inside of which keyring files will be created
 * @param user Router user
 * @param password Router user password
 */
void init_keyring(std::map<std::string, std::string> &default_section,
                  const std::string &keyring_dir,
                  const std::string &user = "mysql_router1_user",
                  const std::string &password = "root");

/** @brief returns true if the selected file contains a string
 *          that is true for a given predicate
 *
 * @param file_path path to the file we want to search
 * @param predicate predicate to test the file
 * @param sleep_time max time to wait for the entry in the file
 * @deprecated use wait_log_contains() or get_file_output() with
 * "EXPECT_THAT(..., Contains())"
 */
[[deprecated]] bool find_in_file(
    const std::string &file_path,
    const std::function<bool(const std::string &)> &predicate,
    std::chrono::milliseconds sleep_time = std::chrono::milliseconds(5000));

/** @brief returns the content of selected file as a string
 *
 * @param file_name name of the file
 * @param file_path path to the file
 * @param throw_on_error if true, throws std::runtime_error on failure
 *                       if false, returns error message instead of actual file
 * contents
 */
std::string get_file_output(const std::string &file_name,
                            const std::string &file_path,
                            bool throw_on_error = false);

/** @brief returns the content of selected file as a string
 *
 * @param file_name full path and name of the file
 * @param throw_on_error if true, throws std::runtime_error on failure
 *                       if false, returns error message instead of actual file
 * contents
 */
std::string get_file_output(const std::string &file_name,
                            bool throw_on_error = false);

// need to return void to be able to use ASSERT_ macros
void connect_client_and_query_port(unsigned router_port, std::string &out_port,
                                   bool should_fail = false);

/**
 * Add a "<key>=<value>" line in a configuration file in a given config section.
 *
 * @param config_path configuration file path
 * @param section_name configuration section name
 * @param key part of configuration to be added
 * @param value part of configuration to be added
 *
 * @retval true config line inserted successfully
 * @retval false config line not inserted
 */
bool add_line_to_config_file(const std::string &config_path,
                             const std::string &section_name,
                             const std::string &key, const std::string &value);

/**
 * Wait for the nth occurrence of the log_regex in the log_file with timeout
 * If it's found returns the timepoint from the matched line prefix
 * If timed out or failed to convert the timestamp returns unexpected
 *
 * @param log_file path to file containing router log
 * @param log_regex value that is going to be searched for in the log
 * @param occurence number denoting which occurrence of a log_regex is expected
 * @param timeout number of milliseconds we are going to wait for the log_regex
 * to occur at expected position
 *
 * @returns if log_regex is found at expected position return the timestamp of
 * this log
 */
std::optional<std::chrono::time_point<std::chrono::system_clock>>
get_log_timestamp(
    const std::string &log_file, const std::string &log_regex,
    const unsigned occurence = 1,
    const std::chrono::milliseconds timeout = std::chrono::seconds(1));

#endif  // ROUTER_TESTS_TEST_HELPERS_INCLUDED