1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "test/core/end2end/end2end_tests.h"
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "src/core/lib/surface/channel.h"
25 #include "src/core/lib/surface/server.h"
26
27 #include <grpc/byte_buffer.h>
28 #include <grpc/grpc.h>
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
31 #include <grpc/support/time.h>
32 #include "src/core/lib/channel/channelz_registry.h"
33 #include "src/core/lib/gpr/string.h"
34 #include "test/core/end2end/cq_verifier.h"
35
tag(intptr_t t)36 static void* tag(intptr_t t) { return (void*)t; }
37
begin_test(grpc_end2end_test_config config,const char * test_name,grpc_channel_args * client_args,grpc_channel_args * server_args)38 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
39 const char* test_name,
40 grpc_channel_args* client_args,
41 grpc_channel_args* server_args) {
42 grpc_end2end_test_fixture f;
43 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
44 f = config.create_fixture(client_args, server_args);
45 config.init_server(&f, server_args);
46 config.init_client(&f, client_args);
47 return f;
48 }
49
n_seconds_from_now(int n)50 static gpr_timespec n_seconds_from_now(int n) {
51 return grpc_timeout_seconds_to_deadline(n);
52 }
53
five_seconds_from_now(void)54 static gpr_timespec five_seconds_from_now(void) {
55 return n_seconds_from_now(5);
56 }
57
drain_cq(grpc_completion_queue * cq)58 static void drain_cq(grpc_completion_queue* cq) {
59 grpc_event ev;
60 do {
61 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
62 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
63 }
64
shutdown_server(grpc_end2end_test_fixture * f)65 static void shutdown_server(grpc_end2end_test_fixture* f) {
66 if (!f->server) return;
67 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
68 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
69 grpc_timeout_seconds_to_deadline(5),
70 nullptr)
71 .type == GRPC_OP_COMPLETE);
72 grpc_server_destroy(f->server);
73 f->server = nullptr;
74 }
75
shutdown_client(grpc_end2end_test_fixture * f)76 static void shutdown_client(grpc_end2end_test_fixture* f) {
77 if (!f->client) return;
78 grpc_channel_destroy(f->client);
79 f->client = nullptr;
80 }
81
end_test(grpc_end2end_test_fixture * f)82 static void end_test(grpc_end2end_test_fixture* f) {
83 shutdown_server(f);
84 shutdown_client(f);
85
86 grpc_completion_queue_shutdown(f->cq);
87 drain_cq(f->cq);
88 grpc_completion_queue_destroy(f->cq);
89 grpc_completion_queue_destroy(f->shutdown_cq);
90 }
91
run_one_request(grpc_end2end_test_config,grpc_end2end_test_fixture f,bool request_is_success)92 static void run_one_request(grpc_end2end_test_config /*config*/,
93 grpc_end2end_test_fixture f,
94 bool request_is_success) {
95 grpc_call* c;
96 grpc_call* s;
97 cq_verifier* cqv = cq_verifier_create(f.cq);
98 grpc_op ops[6];
99 grpc_op* op;
100 grpc_metadata_array initial_metadata_recv;
101 grpc_metadata_array trailing_metadata_recv;
102 grpc_metadata_array request_metadata_recv;
103 grpc_call_details call_details;
104 grpc_status_code status;
105 grpc_call_error error;
106 grpc_slice details;
107 int was_cancelled = 2;
108
109 gpr_timespec deadline = five_seconds_from_now();
110 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
111 grpc_slice_from_static_string("/foo"), nullptr,
112 deadline, nullptr);
113 GPR_ASSERT(c);
114
115 grpc_metadata_array_init(&initial_metadata_recv);
116 grpc_metadata_array_init(&trailing_metadata_recv);
117 grpc_metadata_array_init(&request_metadata_recv);
118 grpc_call_details_init(&call_details);
119
120 memset(ops, 0, sizeof(ops));
121 op = ops;
122 op->op = GRPC_OP_SEND_INITIAL_METADATA;
123 op->data.send_initial_metadata.count = 0;
124 op->flags = 0;
125 op->reserved = nullptr;
126 op++;
127 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
128 op->flags = 0;
129 op->reserved = nullptr;
130 op++;
131 op->op = GRPC_OP_RECV_INITIAL_METADATA;
132 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
133 op->flags = 0;
134 op->reserved = nullptr;
135 op++;
136 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
137 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
138 op->data.recv_status_on_client.status = &status;
139 op->data.recv_status_on_client.status_details = &details;
140 op->data.recv_status_on_client.error_string = nullptr;
141 op->flags = 0;
142 op->reserved = nullptr;
143 op++;
144 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
145 nullptr);
146 GPR_ASSERT(GRPC_CALL_OK == error);
147
148 error =
149 grpc_server_request_call(f.server, &s, &call_details,
150 &request_metadata_recv, f.cq, f.cq, tag(101));
151 GPR_ASSERT(GRPC_CALL_OK == error);
152 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
153 cq_verify(cqv);
154
155 memset(ops, 0, sizeof(ops));
156 op = ops;
157 op->op = GRPC_OP_SEND_INITIAL_METADATA;
158 op->data.send_initial_metadata.count = 0;
159 op->flags = 0;
160 op->reserved = nullptr;
161 op++;
162 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
163 op->data.send_status_from_server.trailing_metadata_count = 0;
164 op->data.send_status_from_server.status =
165 request_is_success ? GRPC_STATUS_OK : GRPC_STATUS_UNIMPLEMENTED;
166 grpc_slice status_details = grpc_slice_from_static_string("xyz");
167 op->data.send_status_from_server.status_details = &status_details;
168 op->flags = 0;
169 op->reserved = nullptr;
170 op++;
171 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
172 op->data.recv_close_on_server.cancelled = &was_cancelled;
173 op->flags = 0;
174 op->reserved = nullptr;
175 op++;
176 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
177 nullptr);
178 GPR_ASSERT(GRPC_CALL_OK == error);
179
180 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
181 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
182 cq_verify(cqv);
183
184 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
185 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
186 GPR_ASSERT(0 == call_details.flags);
187
188 grpc_slice_unref(details);
189 grpc_metadata_array_destroy(&initial_metadata_recv);
190 grpc_metadata_array_destroy(&trailing_metadata_recv);
191 grpc_metadata_array_destroy(&request_metadata_recv);
192 grpc_call_details_destroy(&call_details);
193
194 grpc_call_unref(c);
195 grpc_call_unref(s);
196
197 cq_verifier_destroy(cqv);
198 }
199
test_channelz(grpc_end2end_test_config config)200 static void test_channelz(grpc_end2end_test_config config) {
201 grpc_end2end_test_fixture f;
202
203 grpc_arg arg[] = {
204 grpc_channel_arg_integer_create(
205 const_cast<char*>(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE),
206 0),
207 grpc_channel_arg_integer_create(
208 const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ), true)};
209 grpc_channel_args args = {GPR_ARRAY_SIZE(arg), arg};
210
211 f = begin_test(config, "test_channelz", &args, &args);
212 grpc_core::channelz::ChannelNode* channelz_channel =
213 grpc_channel_get_channelz_node(f.client);
214 GPR_ASSERT(channelz_channel != nullptr);
215
216 grpc_core::channelz::ServerNode* channelz_server =
217 grpc_server_get_channelz_node(f.server);
218 GPR_ASSERT(channelz_server != nullptr);
219
220 std::string json = channelz_channel->RenderJsonString();
221 // nothing is present yet
222 GPR_ASSERT(json.find("\"callsStarted\"") == json.npos);
223 GPR_ASSERT(json.find("\"callsFailed\"") == json.npos);
224 GPR_ASSERT(json.find("\"callsSucceeded\"") == json.npos);
225
226 // one successful request
227 run_one_request(config, f, true);
228
229 json = channelz_channel->RenderJsonString();
230 GPR_ASSERT(json.find("\"callsStarted\":\"1\"") != json.npos);
231 GPR_ASSERT(json.find("\"callsSucceeded\":\"1\"") != json.npos);
232
233 // one failed request
234 run_one_request(config, f, false);
235
236 json = channelz_channel->RenderJsonString();
237 GPR_ASSERT(json.find("\"callsStarted\":\"2\"") != json.npos);
238 GPR_ASSERT(json.find("\"callsFailed\":\"1\"") != json.npos);
239 GPR_ASSERT(json.find("\"callsSucceeded\":\"1\"") != json.npos);
240 // channel tracing is not enabled, so these should not be preset.
241 GPR_ASSERT(json.find("\"trace\"") == json.npos);
242 GPR_ASSERT(json.find("\"description\":\"Channel created\"") == json.npos);
243 GPR_ASSERT(json.find("\"severity\":\"CT_INFO\"") == json.npos);
244
245 json = channelz_server->RenderJsonString();
246 GPR_ASSERT(json.find("\"callsStarted\":\"2\"") != json.npos);
247 GPR_ASSERT(json.find("\"callsFailed\":\"1\"") != json.npos);
248 GPR_ASSERT(json.find("\"callsSucceeded\":\"1\"") != json.npos);
249 // channel tracing is not enabled, so these should not be preset.
250 GPR_ASSERT(json.find("\"trace\"") == json.npos);
251 GPR_ASSERT(json.find("\"description\":\"Channel created\"") == json.npos);
252 GPR_ASSERT(json.find("\"severity\":\"CT_INFO\"") == json.npos);
253
254 json = channelz_server->RenderServerSockets(0, 100);
255 GPR_ASSERT(json.find("\"end\":true") != json.npos);
256
257 end_test(&f);
258 config.tear_down_data(&f);
259 }
260
test_channelz_with_channel_trace(grpc_end2end_test_config config)261 static void test_channelz_with_channel_trace(grpc_end2end_test_config config) {
262 grpc_end2end_test_fixture f;
263
264 grpc_arg arg[] = {
265 grpc_channel_arg_integer_create(
266 const_cast<char*>(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE),
267 1024 * 1024),
268 grpc_channel_arg_integer_create(
269 const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ), true)};
270 grpc_channel_args args = {GPR_ARRAY_SIZE(arg), arg};
271
272 f = begin_test(config, "test_channelz_with_channel_trace", &args, &args);
273 grpc_core::channelz::ChannelNode* channelz_channel =
274 grpc_channel_get_channelz_node(f.client);
275 GPR_ASSERT(channelz_channel != nullptr);
276
277 grpc_core::channelz::ServerNode* channelz_server =
278 grpc_server_get_channelz_node(f.server);
279 GPR_ASSERT(channelz_server != nullptr);
280
281 run_one_request(config, f, true);
282
283 std::string json = channelz_channel->RenderJsonString();
284 GPR_ASSERT(json.find("\"trace\"") != json.npos);
285 GPR_ASSERT(json.find("\"description\":\"Channel created\"") != json.npos);
286 GPR_ASSERT(json.find("\"severity\":\"CT_INFO\"") != json.npos);
287
288 json = channelz_server->RenderJsonString();
289 GPR_ASSERT(json.find("\"trace\"") != json.npos);
290 GPR_ASSERT(json.find("\"description\":\"Server created\"") != json.npos);
291 GPR_ASSERT(json.find("\"severity\":\"CT_INFO\"") != json.npos);
292
293 end_test(&f);
294 config.tear_down_data(&f);
295 }
296
test_channelz_disabled(grpc_end2end_test_config config)297 static void test_channelz_disabled(grpc_end2end_test_config config) {
298 grpc_end2end_test_fixture f;
299
300 grpc_arg arg[] = {
301 grpc_channel_arg_integer_create(
302 const_cast<char*>(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE),
303 0),
304 grpc_channel_arg_integer_create(
305 const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ), false)};
306 grpc_channel_args args = {GPR_ARRAY_SIZE(arg), arg};
307
308 f = begin_test(config, "test_channelz_disabled", &args, &args);
309 grpc_core::channelz::ChannelNode* channelz_channel =
310 grpc_channel_get_channelz_node(f.client);
311 GPR_ASSERT(channelz_channel == nullptr);
312 // one successful request
313 run_one_request(config, f, true);
314 GPR_ASSERT(channelz_channel == nullptr);
315 end_test(&f);
316 config.tear_down_data(&f);
317 }
318
channelz(grpc_end2end_test_config config)319 void channelz(grpc_end2end_test_config config) {
320 test_channelz(config);
321 test_channelz_with_channel_trace(config);
322 test_channelz_disabled(config);
323 }
324
channelz_pre_init(void)325 void channelz_pre_init(void) {}
326