• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2016 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 <grpc/grpc.h>
20 #include <grpcpp/impl/codegen/config.h>
21 #include <gtest/gtest.h>
22 
23 #include "src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h"
24 #include "src/core/lib/iomgr/sockaddr.h"
25 #include "src/core/lib/iomgr/sockaddr_utils.h"
26 #include "src/proto/grpc/lb/v1/load_balancer.pb.h"  // C++ version
27 
28 namespace grpc {
29 namespace {
30 
31 using grpc::lb::v1::LoadBalanceRequest;
32 using grpc::lb::v1::LoadBalanceResponse;
33 
34 class GrpclbTest : public ::testing::Test {};
35 
Ip4ToPackedString(const char * ip_str)36 grpc::string Ip4ToPackedString(const char* ip_str) {
37   struct in_addr ip4;
38   GPR_ASSERT(inet_pton(AF_INET, ip_str, &ip4) == 1);
39   return grpc::string(reinterpret_cast<const char*>(&ip4), sizeof(ip4));
40 }
41 
PackedStringToIp(const grpc_grpclb_ip_address & pb_ip)42 grpc::string PackedStringToIp(const grpc_grpclb_ip_address& pb_ip) {
43   char ip_str[46] = {0};
44   int af = -1;
45   if (pb_ip.size == 4) {
46     af = AF_INET;
47   } else if (pb_ip.size == 16) {
48     af = AF_INET6;
49   } else {
50     abort();
51   }
52   GPR_ASSERT(inet_ntop(af, (void*)pb_ip.bytes, ip_str, 46) != nullptr);
53   return ip_str;
54 }
55 
TEST_F(GrpclbTest,CreateRequest)56 TEST_F(GrpclbTest, CreateRequest) {
57   const grpc::string service_name = "AServiceName";
58   LoadBalanceRequest request;
59   grpc_grpclb_request* c_req = grpc_grpclb_request_create(service_name.c_str());
60   grpc_slice slice = grpc_grpclb_request_encode(c_req);
61   const int num_bytes_written = GRPC_SLICE_LENGTH(slice);
62   EXPECT_GT(num_bytes_written, 0);
63   request.ParseFromArray(GRPC_SLICE_START_PTR(slice), num_bytes_written);
64   EXPECT_EQ(request.initial_request().name(), service_name);
65   grpc_slice_unref(slice);
66   grpc_grpclb_request_destroy(c_req);
67 }
68 
TEST_F(GrpclbTest,ParseInitialResponse)69 TEST_F(GrpclbTest, ParseInitialResponse) {
70   LoadBalanceResponse response;
71   auto* initial_response = response.mutable_initial_response();
72   auto* client_stats_report_interval =
73       initial_response->mutable_client_stats_report_interval();
74   client_stats_report_interval->set_seconds(123);
75   client_stats_report_interval->set_nanos(456);
76   const grpc::string encoded_response = response.SerializeAsString();
77   grpc_slice encoded_slice =
78       grpc_slice_from_copied_string(encoded_response.c_str());
79 
80   grpc_grpclb_initial_response* c_initial_response =
81       grpc_grpclb_initial_response_parse(encoded_slice);
82   EXPECT_FALSE(c_initial_response->has_load_balancer_delegate);
83   EXPECT_EQ(c_initial_response->client_stats_report_interval.seconds, 123);
84   EXPECT_EQ(c_initial_response->client_stats_report_interval.nanos, 456);
85   grpc_slice_unref(encoded_slice);
86   grpc_grpclb_initial_response_destroy(c_initial_response);
87 }
88 
TEST_F(GrpclbTest,ParseResponseServerList)89 TEST_F(GrpclbTest, ParseResponseServerList) {
90   LoadBalanceResponse response;
91   auto* serverlist = response.mutable_server_list();
92   auto* server = serverlist->add_servers();
93   server->set_ip_address(Ip4ToPackedString("127.0.0.1"));
94   server->set_port(12345);
95   server->set_load_balance_token("rate_limting");
96   server->set_drop(true);
97   server = response.mutable_server_list()->add_servers();
98   server->set_ip_address(Ip4ToPackedString("10.0.0.1"));
99   server->set_port(54321);
100   server->set_load_balance_token("load_balancing");
101   server->set_drop(true);
102 
103   const grpc::string encoded_response = response.SerializeAsString();
104   const grpc_slice encoded_slice = grpc_slice_from_copied_buffer(
105       encoded_response.data(), encoded_response.size());
106   grpc_grpclb_serverlist* c_serverlist =
107       grpc_grpclb_response_parse_serverlist(encoded_slice);
108   ASSERT_EQ(c_serverlist->num_servers, 2ul);
109   EXPECT_TRUE(c_serverlist->servers[0]->has_ip_address);
110   EXPECT_EQ(PackedStringToIp(c_serverlist->servers[0]->ip_address),
111             "127.0.0.1");
112   EXPECT_EQ(c_serverlist->servers[0]->port, 12345);
113   EXPECT_STREQ(c_serverlist->servers[0]->load_balance_token, "rate_limting");
114   EXPECT_TRUE(c_serverlist->servers[0]->drop);
115   EXPECT_TRUE(c_serverlist->servers[1]->has_ip_address);
116 
117   EXPECT_EQ(PackedStringToIp(c_serverlist->servers[1]->ip_address), "10.0.0.1");
118   EXPECT_EQ(c_serverlist->servers[1]->port, 54321);
119   EXPECT_STREQ(c_serverlist->servers[1]->load_balance_token, "load_balancing");
120   EXPECT_TRUE(c_serverlist->servers[1]->drop);
121 
122   grpc_slice_unref(encoded_slice);
123   grpc_grpclb_destroy_serverlist(c_serverlist);
124 }
125 
126 }  // namespace
127 }  // namespace grpc
128 
main(int argc,char ** argv)129 int main(int argc, char** argv) {
130   ::testing::InitGoogleTest(&argc, argv);
131   grpc_init();
132   int ret = RUN_ALL_TESTS();
133   grpc_shutdown();
134   return ret;
135 }
136