File: mock_server_testutils.cc

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 (205 lines) | stat: -rw-r--r-- 7,966 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

/*
  Copyright (c) 2019, 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
*/

#include "mock_server_testutils.h"

#include <array>
#include <chrono>
#include <thread>

#include <gmock/gmock.h>
#ifdef RAPIDJSON_NO_SIZETYPEDEFINE
#include "my_rapidjson_size_t.h"
#endif

#include <rapidjson/error/en.h>

#include "config_builder.h"
#include "mysql/harness/stdx/ranges.h"  // enumerate
#include "mysqlrouter/mock_server_rest_client.h"
#include "mysqlrouter/rest_client.h"
#include "rest_api_testutils.h"

std::string json_to_string(const JsonValue &json_doc) {
  JsonStringBuffer out_buffer;

  rapidjson::Writer<JsonStringBuffer> out_writer{out_buffer};
  json_doc.Accept(out_writer);
  return out_buffer.GetString();
}

std::vector<GRNode> classic_ports_to_gr_nodes(
    const std::vector<uint16_t> &classic_ports) {
  std::vector<GRNode> result;

  for (const auto [id, port] : stdx::views::enumerate(classic_ports)) {
    result.emplace_back(port, "uuid-" + std::to_string(id + 1));
  }

  return result;
}

std::vector<ClusterNode> classic_ports_to_cluster_nodes(
    const std::vector<uint16_t> &classic_ports) {
  std::vector<ClusterNode> result;
  for (const auto [id, port] : stdx::views::enumerate(classic_ports)) {
    result.emplace_back(port, "uuid-" + std::to_string(id + 1));
  }

  return result;
}

JsonValue mock_GR_metadata_as_json(
    const std::string &gr_id, const std::vector<GRNode> &gr_nodes,
    unsigned gr_pos, const std::vector<ClusterNode> &cluster_nodes,
    unsigned primary_id, uint64_t view_id, bool error_on_md_query,
    const std::string &gr_node_host) {
  JsonValue json_doc(rapidjson::kObjectType);
  JsonAllocator allocator;
  json_doc.AddMember(
      "gr_id", JsonValue(gr_id.c_str(), gr_id.length(), allocator), allocator);

  JsonValue gr_nodes_json(rapidjson::kArrayType);
  JsonValue cluster_nodes_json(rapidjson::kArrayType);
  for (auto &gr_node : gr_nodes) {
    JsonValue node(rapidjson::kArrayType);
    node.PushBack(JsonValue(gr_node.server_uuid.c_str(),
                            gr_node.server_uuid.length(), allocator),
                  allocator);
    node.PushBack(static_cast<int>(gr_node.classic_port), allocator);
    node.PushBack(JsonValue(gr_node.member_status.c_str(),
                            gr_node.member_status.length(), allocator),
                  allocator);

    gr_nodes_json.PushBack(node, allocator);
  }

  for (auto &cluster_node : cluster_nodes) {
    JsonValue node(rapidjson::kArrayType);
    node.PushBack(JsonValue(cluster_node.server_uuid.c_str(),
                            cluster_node.server_uuid.length(), allocator),
                  allocator);
    node.PushBack(static_cast<int>(cluster_node.classic_port), allocator);
    node.PushBack(static_cast<int>(cluster_node.x_port), allocator);
    node.PushBack(JsonValue(cluster_node.attributes.c_str(),
                            cluster_node.attributes.length(), allocator),
                  allocator);

    cluster_nodes_json.PushBack(node, allocator);
  }

  json_doc.AddMember("gr_nodes", gr_nodes_json, allocator);
  json_doc.AddMember("gr_pos", gr_pos, allocator);
  json_doc.AddMember("cluster_nodes", cluster_nodes_json, allocator);
  json_doc.AddMember("primary_id", static_cast<int>(primary_id), allocator);
  if (view_id > 0) {
    json_doc.AddMember("view_id", view_id, allocator);
  }
  json_doc.AddMember("error_on_md_query", error_on_md_query ? 1 : 0, allocator);
  json_doc.AddMember(
      "gr_node_host",
      JsonValue(gr_node_host.c_str(), gr_node_host.length(), allocator),
      allocator);

  return json_doc;
}

void set_mock_metadata(uint16_t http_port, const std::string &gr_id,
                       const std::vector<GRNode> &gr_nodes, unsigned gr_pos,
                       const std::vector<ClusterNode> &cluster_nodes,
                       unsigned primary_id, uint64_t view_id,
                       bool error_on_md_query,
                       const std::string &gr_node_host) {
  const auto json_doc = mock_GR_metadata_as_json(
      gr_id, gr_nodes, gr_pos, cluster_nodes, primary_id, view_id,
      error_on_md_query, gr_node_host);

  const auto json_str = json_to_string(json_doc);

  ASSERT_NO_THROW(MockServerRestClient(http_port).set_globals(json_str));
}

void set_mock_bootstrap_data(
    uint16_t http_port, const std::string &cluster_name,
    const std::vector<std::pair<std::string, unsigned>> &gr_members_ports,
    const mysqlrouter::MetadataSchemaVersion &metadata_version,
    const std::string &cluster_specific_id) {
  JsonValue json_doc(rapidjson::kObjectType);
  JsonAllocator allocator;
  json_doc.AddMember(
      "cluster_name",
      JsonValue(cluster_name.c_str(), cluster_name.length(), allocator),
      allocator);

  JsonValue gr_members_json(rapidjson::kArrayType);
  for (const auto [id, gr_member] : stdx::views::enumerate(gr_members_ports)) {
    JsonValue member(rapidjson::kArrayType);
    const std::string uuid = "uuid-" + std::to_string(id + 1);
    member.PushBack(JsonValue(uuid.c_str(), uuid.length(), allocator),
                    allocator);
    member.PushBack(
        JsonValue(gr_member.first.c_str(), gr_member.first.length(), allocator),
        allocator);
    member.PushBack(static_cast<int>(gr_member.second), allocator);
    gr_members_json.PushBack(member, allocator);
  }
  JsonValue cluster_instances_json{gr_members_json, allocator};
  json_doc.AddMember("gr_members", gr_members_json, allocator);
  json_doc.AddMember("innodb_cluster_instances", cluster_instances_json,
                     allocator);

  JsonValue md_version(rapidjson::kArrayType);
  md_version.PushBack(static_cast<int>(metadata_version.major), allocator);
  md_version.PushBack(static_cast<int>(metadata_version.minor), allocator);
  md_version.PushBack(static_cast<int>(metadata_version.patch), allocator);
  json_doc.AddMember("metadata_version", md_version, allocator);

  json_doc.AddMember("gr_id",
                     JsonValue(cluster_specific_id.c_str(),
                               cluster_specific_id.length(), allocator),
                     allocator);

  const auto json_str = json_to_string(json_doc);

  EXPECT_NO_THROW(MockServerRestClient(http_port).set_globals(json_str));
}

void set_mock_server_version(uint16_t http_port, const std::string &version) {
  std::string server_globals =
      MockServerRestClient(http_port).get_globals_as_json_string();
  JsonDocument globals;
  if (globals.Parse<0>(server_globals.c_str()).HasParseError()) {
    FAIL() << "Failed parsing mock server globals";
  }

  JsonAllocator allocator;
  globals.RemoveMember("server_version");
  globals.AddMember("server_version",
                    JsonValue(version.c_str(), version.length(), allocator),
                    allocator);
  server_globals = json_to_string(globals);
  MockServerRestClient(http_port).set_globals(server_globals);
}