channel_trace_proto_helper.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. *
  3. * Copyright 2018 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. #include <grpc/support/port_platform.h>
  19. #include "test/cpp/util/channel_trace_proto_helper.h"
  20. #include <gtest/gtest.h>
  21. #include <grpc/grpc.h>
  22. #include <grpc/support/log.h>
  23. #include <grpcpp/impl/codegen/config.h>
  24. #include <grpcpp/impl/codegen/config_protobuf.h>
  25. #include "src/core/lib/iomgr/error.h"
  26. #include "src/core/lib/json/json.h"
  27. #include "src/proto/grpc/channelz/channelz.pb.h"
  28. namespace grpc {
  29. namespace {
  30. // Generic helper that takes in a json string, converts it to a proto, and
  31. // then back to json. This ensures that the json string was correctly formatted
  32. // according to https://developers.google.com/protocol-buffers/docs/proto3#json
  33. template <typename Message>
  34. void VaidateProtoJsonTranslation(const std::string& json_str) {
  35. Message msg;
  36. grpc::protobuf::json::JsonParseOptions parse_options;
  37. // If the following line is failing, then uncomment the last line of the
  38. // comment, and uncomment the lines that print the two strings. You can
  39. // then compare the output, and determine what fields are missing.
  40. //
  41. // parse_options.ignore_unknown_fields = true;
  42. grpc::protobuf::util::Status s =
  43. grpc::protobuf::json::JsonStringToMessage(json_str, &msg, parse_options);
  44. EXPECT_TRUE(s.ok());
  45. std::string proto_json_str;
  46. grpc::protobuf::json::JsonPrintOptions print_options;
  47. // We usually do not want this to be true, however it can be helpful to
  48. // uncomment and see the output produced then all fields are printed.
  49. // print_options.always_print_primitive_fields = true;
  50. s = grpc::protobuf::json::MessageToJsonString(msg, &proto_json_str);
  51. EXPECT_TRUE(s.ok());
  52. // Parse JSON and re-dump to string, to make sure formatting is the
  53. // same as what would be generated by our JSON library.
  54. grpc_error_handle error = GRPC_ERROR_NONE;
  55. grpc_core::Json parsed_json =
  56. grpc_core::Json::Parse(proto_json_str.c_str(), &error);
  57. ASSERT_EQ(error, GRPC_ERROR_NONE) << grpc_error_std_string(error);
  58. ASSERT_EQ(parsed_json.type(), grpc_core::Json::Type::OBJECT);
  59. proto_json_str = parsed_json.Dump();
  60. // uncomment these to compare the json strings.
  61. // gpr_log(GPR_ERROR, "tracer json: %s", json_str.c_str());
  62. // gpr_log(GPR_ERROR, "proto json: %s", proto_json_str.c_str());
  63. EXPECT_EQ(json_str, proto_json_str);
  64. }
  65. } // namespace
  66. namespace testing {
  67. void ValidateChannelTraceProtoJsonTranslation(const char* json_c_str) {
  68. VaidateProtoJsonTranslation<grpc::channelz::v1::ChannelTrace>(json_c_str);
  69. }
  70. void ValidateChannelProtoJsonTranslation(const char* json_c_str) {
  71. VaidateProtoJsonTranslation<grpc::channelz::v1::Channel>(json_c_str);
  72. }
  73. void ValidateGetTopChannelsResponseProtoJsonTranslation(
  74. const char* json_c_str) {
  75. VaidateProtoJsonTranslation<grpc::channelz::v1::GetTopChannelsResponse>(
  76. json_c_str);
  77. }
  78. void ValidateGetChannelResponseProtoJsonTranslation(const char* json_c_str) {
  79. VaidateProtoJsonTranslation<grpc::channelz::v1::GetChannelResponse>(
  80. json_c_str);
  81. }
  82. void ValidateGetServerResponseProtoJsonTranslation(const char* json_c_str) {
  83. VaidateProtoJsonTranslation<grpc::channelz::v1::GetServerResponse>(
  84. json_c_str);
  85. }
  86. void ValidateSubchannelProtoJsonTranslation(const char* json_c_str) {
  87. VaidateProtoJsonTranslation<grpc::channelz::v1::Subchannel>(json_c_str);
  88. }
  89. void ValidateServerProtoJsonTranslation(const char* json_c_str) {
  90. VaidateProtoJsonTranslation<grpc::channelz::v1::Server>(json_c_str);
  91. }
  92. void ValidateGetServersResponseProtoJsonTranslation(const char* json_c_str) {
  93. VaidateProtoJsonTranslation<grpc::channelz::v1::GetServersResponse>(
  94. json_c_str);
  95. }
  96. } // namespace testing
  97. } // namespace grpc