• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/gpr/string.h"
33 #include "test/core/end2end/cq_verifier.h"
34 
tag(intptr_t t)35 static void* tag(intptr_t t) { return (void*)t; }
36 
begin_test(grpc_end2end_test_config config,const char * test_name,grpc_channel_args * client_args,grpc_channel_args * server_args)37 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
38                                             const char* test_name,
39                                             grpc_channel_args* client_args,
40                                             grpc_channel_args* server_args) {
41   grpc_end2end_test_fixture f;
42   gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
43   f = config.create_fixture(client_args, server_args);
44   config.init_server(&f, server_args);
45   config.init_client(&f, client_args);
46   return f;
47 }
48 
n_seconds_from_now(int n)49 static gpr_timespec n_seconds_from_now(int n) {
50   return grpc_timeout_seconds_to_deadline(n);
51 }
52 
five_seconds_from_now(void)53 static gpr_timespec five_seconds_from_now(void) {
54   return n_seconds_from_now(5);
55 }
56 
drain_cq(grpc_completion_queue * cq)57 static void drain_cq(grpc_completion_queue* cq) {
58   grpc_event ev;
59   do {
60     ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
61   } while (ev.type != GRPC_QUEUE_SHUTDOWN);
62 }
63 
shutdown_server(grpc_end2end_test_fixture * f)64 static void shutdown_server(grpc_end2end_test_fixture* f) {
65   if (!f->server) return;
66   grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
67   GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
68                                          grpc_timeout_seconds_to_deadline(5),
69                                          nullptr)
70                  .type == GRPC_OP_COMPLETE);
71   grpc_server_destroy(f->server);
72   f->server = nullptr;
73 }
74 
shutdown_client(grpc_end2end_test_fixture * f)75 static void shutdown_client(grpc_end2end_test_fixture* f) {
76   if (!f->client) return;
77   grpc_channel_destroy(f->client);
78   f->client = nullptr;
79 }
80 
end_test(grpc_end2end_test_fixture * f)81 static void end_test(grpc_end2end_test_fixture* f) {
82   shutdown_server(f);
83   shutdown_client(f);
84 
85   grpc_completion_queue_shutdown(f->cq);
86   drain_cq(f->cq);
87   grpc_completion_queue_destroy(f->cq);
88   grpc_completion_queue_destroy(f->shutdown_cq);
89 }
90 
run_one_request(grpc_end2end_test_config config,grpc_end2end_test_fixture f,bool request_is_success)91 static void run_one_request(grpc_end2end_test_config config,
92                             grpc_end2end_test_fixture f,
93                             bool request_is_success) {
94   grpc_call* c;
95   grpc_call* s;
96   cq_verifier* cqv = cq_verifier_create(f.cq);
97   grpc_op ops[6];
98   grpc_op* op;
99   grpc_metadata_array initial_metadata_recv;
100   grpc_metadata_array trailing_metadata_recv;
101   grpc_metadata_array request_metadata_recv;
102   grpc_call_details call_details;
103   grpc_status_code status;
104   grpc_call_error error;
105   grpc_slice details;
106   int was_cancelled = 2;
107 
108   gpr_timespec deadline = five_seconds_from_now();
109   c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
110                                grpc_slice_from_static_string("/foo"), nullptr,
111                                deadline, nullptr);
112   GPR_ASSERT(c);
113 
114   grpc_metadata_array_init(&initial_metadata_recv);
115   grpc_metadata_array_init(&trailing_metadata_recv);
116   grpc_metadata_array_init(&request_metadata_recv);
117   grpc_call_details_init(&call_details);
118 
119   memset(ops, 0, sizeof(ops));
120   op = ops;
121   op->op = GRPC_OP_SEND_INITIAL_METADATA;
122   op->data.send_initial_metadata.count = 0;
123   op->flags = 0;
124   op->reserved = nullptr;
125   op++;
126   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
127   op->flags = 0;
128   op->reserved = nullptr;
129   op++;
130   op->op = GRPC_OP_RECV_INITIAL_METADATA;
131   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
132   op->flags = 0;
133   op->reserved = nullptr;
134   op++;
135   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
136   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
137   op->data.recv_status_on_client.status = &status;
138   op->data.recv_status_on_client.status_details = &details;
139   op->data.recv_status_on_client.error_string = nullptr;
140   op->flags = 0;
141   op->reserved = nullptr;
142   op++;
143   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
144                                 nullptr);
145   GPR_ASSERT(GRPC_CALL_OK == error);
146 
147   error =
148       grpc_server_request_call(f.server, &s, &call_details,
149                                &request_metadata_recv, f.cq, f.cq, tag(101));
150   GPR_ASSERT(GRPC_CALL_OK == error);
151   CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
152   cq_verify(cqv);
153 
154   memset(ops, 0, sizeof(ops));
155   op = ops;
156   op->op = GRPC_OP_SEND_INITIAL_METADATA;
157   op->data.send_initial_metadata.count = 0;
158   op->flags = 0;
159   op->reserved = nullptr;
160   op++;
161   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
162   op->data.send_status_from_server.trailing_metadata_count = 0;
163   op->data.send_status_from_server.status =
164       request_is_success ? GRPC_STATUS_OK : GRPC_STATUS_UNIMPLEMENTED;
165   grpc_slice status_details = grpc_slice_from_static_string("xyz");
166   op->data.send_status_from_server.status_details = &status_details;
167   op->flags = 0;
168   op->reserved = nullptr;
169   op++;
170   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
171   op->data.recv_close_on_server.cancelled = &was_cancelled;
172   op->flags = 0;
173   op->reserved = nullptr;
174   op++;
175   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
176                                 nullptr);
177   GPR_ASSERT(GRPC_CALL_OK == error);
178 
179   CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
180   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
181   cq_verify(cqv);
182 
183   GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
184   GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
185   GPR_ASSERT(0 == call_details.flags);
186 
187   grpc_slice_unref(details);
188   grpc_metadata_array_destroy(&initial_metadata_recv);
189   grpc_metadata_array_destroy(&trailing_metadata_recv);
190   grpc_metadata_array_destroy(&request_metadata_recv);
191   grpc_call_details_destroy(&call_details);
192 
193   grpc_call_unref(c);
194   grpc_call_unref(s);
195 
196   cq_verifier_destroy(cqv);
197 }
198 
test_channelz(grpc_end2end_test_config config)199 static void test_channelz(grpc_end2end_test_config config) {
200   grpc_end2end_test_fixture f;
201 
202   grpc_arg arg;
203   arg.type = GRPC_ARG_INTEGER;
204   arg.key = const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ);
205   arg.value.integer = true;
206   grpc_channel_args args = {1, &arg};
207 
208   f = begin_test(config, "test_channelz", &args, &args);
209   grpc_core::channelz::ChannelNode* channelz_channel =
210       grpc_channel_get_channelz_node(f.client);
211   GPR_ASSERT(channelz_channel != nullptr);
212 
213   grpc_core::channelz::ServerNode* channelz_server =
214       grpc_server_get_channelz_node(f.server);
215   GPR_ASSERT(channelz_server != nullptr);
216 
217   char* json = channelz_channel->RenderJsonString();
218   GPR_ASSERT(json != nullptr);
219   // nothing is present yet
220   GPR_ASSERT(nullptr == strstr(json, "\"callsStarted\""));
221   GPR_ASSERT(nullptr == strstr(json, "\"callsFailed\""));
222   GPR_ASSERT(nullptr == strstr(json, "\"callsSucceeded\""));
223   gpr_free(json);
224 
225   // one successful request
226   run_one_request(config, f, true);
227 
228   json = channelz_channel->RenderJsonString();
229   GPR_ASSERT(json != nullptr);
230   GPR_ASSERT(nullptr != strstr(json, "\"callsStarted\":\"1\""));
231   GPR_ASSERT(nullptr != strstr(json, "\"callsSucceeded\":\"1\""));
232   gpr_free(json);
233 
234   // one failed request
235   run_one_request(config, f, false);
236 
237   json = channelz_channel->RenderJsonString();
238   GPR_ASSERT(json != nullptr);
239   gpr_log(GPR_INFO, "%s", json);
240   GPR_ASSERT(nullptr != strstr(json, "\"callsStarted\":\"2\""));
241   GPR_ASSERT(nullptr != strstr(json, "\"callsFailed\":\"1\""));
242   GPR_ASSERT(nullptr != strstr(json, "\"callsSucceeded\":\"1\""));
243   // channel tracing is not enabled, so these should not be preset.
244   GPR_ASSERT(nullptr == strstr(json, "\"trace\""));
245   GPR_ASSERT(nullptr == strstr(json, "\"description\":\"Channel created\""));
246   GPR_ASSERT(nullptr == strstr(json, "\"severity\":\"CT_INFO\""));
247   gpr_free(json);
248 
249   json = channelz_server->RenderJsonString();
250   GPR_ASSERT(json != nullptr);
251   gpr_log(GPR_INFO, "%s", json);
252   GPR_ASSERT(nullptr != strstr(json, "\"callsStarted\":\"2\""));
253   GPR_ASSERT(nullptr != strstr(json, "\"callsFailed\":\"1\""));
254   GPR_ASSERT(nullptr != strstr(json, "\"callsSucceeded\":\"1\""));
255   // channel tracing is not enabled, so these should not be preset.
256   GPR_ASSERT(nullptr == strstr(json, "\"trace\""));
257   GPR_ASSERT(nullptr == strstr(json, "\"description\":\"Channel created\""));
258   GPR_ASSERT(nullptr == strstr(json, "\"severity\":\"CT_INFO\""));
259   gpr_free(json);
260 
261   end_test(&f);
262   config.tear_down_data(&f);
263 }
264 
test_channelz_with_channel_trace(grpc_end2end_test_config config)265 static void test_channelz_with_channel_trace(grpc_end2end_test_config config) {
266   grpc_end2end_test_fixture f;
267 
268   grpc_arg arg[2];
269   arg[0].type = GRPC_ARG_INTEGER;
270   arg[0].key = const_cast<char*>(GRPC_ARG_MAX_CHANNEL_TRACE_EVENTS_PER_NODE);
271   arg[0].value.integer = 5;
272   arg[1].type = GRPC_ARG_INTEGER;
273   arg[1].key = const_cast<char*>(GRPC_ARG_ENABLE_CHANNELZ);
274   arg[1].value.integer = true;
275   grpc_channel_args args = {GPR_ARRAY_SIZE(arg), arg};
276 
277   f = begin_test(config, "test_channelz_with_channel_trace", &args, &args);
278   grpc_core::channelz::ChannelNode* channelz_channel =
279       grpc_channel_get_channelz_node(f.client);
280   GPR_ASSERT(channelz_channel != nullptr);
281 
282   grpc_core::channelz::ServerNode* channelz_server =
283       grpc_server_get_channelz_node(f.server);
284   GPR_ASSERT(channelz_server != nullptr);
285 
286   char* json = channelz_channel->RenderJsonString();
287   GPR_ASSERT(json != nullptr);
288   gpr_log(GPR_INFO, "%s", json);
289   GPR_ASSERT(nullptr != strstr(json, "\"trace\""));
290   GPR_ASSERT(nullptr != strstr(json, "\"description\":\"Channel created\""));
291   GPR_ASSERT(nullptr != strstr(json, "\"severity\":\"CT_INFO\""));
292   gpr_free(json);
293 
294   json = channelz_server->RenderJsonString();
295   GPR_ASSERT(json != nullptr);
296   gpr_log(GPR_INFO, "%s", json);
297   GPR_ASSERT(nullptr != strstr(json, "\"trace\""));
298   GPR_ASSERT(nullptr != strstr(json, "\"description\":\"Server created\""));
299   GPR_ASSERT(nullptr != strstr(json, "\"severity\":\"CT_INFO\""));
300   gpr_free(json);
301 
302   end_test(&f);
303   config.tear_down_data(&f);
304 }
305 
test_channelz_disabled(grpc_end2end_test_config config)306 static void test_channelz_disabled(grpc_end2end_test_config config) {
307   grpc_end2end_test_fixture f;
308 
309   f = begin_test(config, "test_channelz_disabled", nullptr, nullptr);
310   grpc_core::channelz::ChannelNode* channelz_channel =
311       grpc_channel_get_channelz_node(f.client);
312   GPR_ASSERT(channelz_channel == nullptr);
313   // one successful request
314   run_one_request(config, f, true);
315   GPR_ASSERT(channelz_channel == nullptr);
316   end_test(&f);
317   config.tear_down_data(&f);
318 }
319 
channelz(grpc_end2end_test_config config)320 void channelz(grpc_end2end_test_config config) {
321   test_channelz(config);
322   test_channelz_with_channel_trace(config);
323   test_channelz_disabled(config);
324 }
325 
channelz_pre_init(void)326 void channelz_pre_init(void) {}
327