Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/test/mocks/grpc/mocks.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include <cstdint>
4
#include <memory>
5
#include <string>
6
7
#include "envoy/config/core/v3/grpc_service.pb.h"
8
#include "envoy/grpc/async_client.h"
9
#include "envoy/grpc/async_client_manager.h"
10
#include "envoy/stats/scope.h"
11
12
#include "source/common/grpc/typed_async_client.h"
13
14
#include "test/test_common/utility.h"
15
16
#include "gmock/gmock.h"
17
18
namespace Envoy {
19
namespace Grpc {
20
21
class MockAsyncRequest : public AsyncRequest {
22
public:
23
  MockAsyncRequest();
24
  ~MockAsyncRequest() override;
25
26
  MOCK_METHOD(void, cancel, ());
27
};
28
29
class MockAsyncStream : public RawAsyncStream {
30
public:
31
  MockAsyncStream();
32
  ~MockAsyncStream() override;
33
34
0
  void sendMessageRaw(Buffer::InstancePtr&& request, bool end_stream) override {
35
0
    sendMessageRaw_(request, end_stream);
36
0
  }
37
  MOCK_METHOD(void, sendMessageRaw_, (Buffer::InstancePtr & request, bool end_stream));
38
  MOCK_METHOD(void, closeStream, ());
39
  MOCK_METHOD(void, resetStream, ());
40
  MOCK_METHOD(bool, isAboveWriteBufferHighWatermark, (), (const));
41
  MOCK_METHOD(const StreamInfo::StreamInfo&, streamInfo, (), (const));
42
};
43
44
template <class ResponseType> using ResponseTypePtr = std::unique_ptr<ResponseType>;
45
46
template <class ResponseType>
47
class MockAsyncRequestCallbacks : public AsyncRequestCallbacks<ResponseType> {
48
public:
49
  void onSuccess(ResponseTypePtr<ResponseType>&& response, Tracing::Span& span) override {
50
    onSuccess_(*response, span);
51
  }
52
53
  MOCK_METHOD(void, onCreateInitialMetadata, (Http::RequestHeaderMap & metadata));
54
  MOCK_METHOD(void, onSuccess_, (const ResponseType& response, Tracing::Span& span));
55
  MOCK_METHOD(void, onFailure,
56
              (Status::GrpcStatus status, const std::string& message, Tracing::Span& span));
57
};
58
59
template <class ResponseType>
60
class MockAsyncStreamCallbacks : public AsyncStreamCallbacks<ResponseType> {
61
public:
62
  void onReceiveInitialMetadata(Http::ResponseHeaderMapPtr&& metadata) override {
63
    onReceiveInitialMetadata_(*metadata);
64
  }
65
66
  void onReceiveMessage(ResponseTypePtr<ResponseType>&& message) override {
67
    onReceiveMessage_(*message);
68
  }
69
70
  void onReceiveTrailingMetadata(Http::ResponseTrailerMapPtr&& metadata) override {
71
    onReceiveTrailingMetadata_(*metadata);
72
  }
73
74
  MOCK_METHOD(void, onCreateInitialMetadata, (Http::RequestHeaderMap & metadata));
75
  MOCK_METHOD(void, onReceiveInitialMetadata_, (const Http::ResponseHeaderMap& metadata));
76
  MOCK_METHOD(void, onReceiveMessage_, (const ResponseType& message));
77
  MOCK_METHOD(void, onReceiveTrailingMetadata_, (const Http::ResponseTrailerMap& metadata));
78
  MOCK_METHOD(void, onRemoteClose, (Status::GrpcStatus status, const std::string& message));
79
};
80
81
class MockAsyncClient : public RawAsyncClient {
82
public:
83
  MockAsyncClient();
84
  ~MockAsyncClient() override;
85
86
  MOCK_METHOD(AsyncRequest*, sendRaw,
87
              (absl::string_view service_full_name, absl::string_view method_name,
88
               Buffer::InstancePtr&& request, RawAsyncRequestCallbacks& callbacks,
89
               Tracing::Span& parent_span, const Http::AsyncClient::RequestOptions& options));
90
  MOCK_METHOD(RawAsyncStream*, startRaw,
91
              (absl::string_view service_full_name, absl::string_view method_name,
92
               RawAsyncStreamCallbacks& callbacks,
93
               const Http::AsyncClient::StreamOptions& options));
94
  MOCK_METHOD(absl::string_view, destination, ());
95
96
  std::unique_ptr<testing::NiceMock<Grpc::MockAsyncRequest>> async_request_;
97
  // Keep track of the number of requests to detect potential race condition.
98
  int send_count_{};
99
};
100
101
class MockAsyncClientFactory : public AsyncClientFactory {
102
public:
103
  MockAsyncClientFactory();
104
  ~MockAsyncClientFactory() override;
105
106
  MOCK_METHOD(RawAsyncClientPtr, createUncachedRawAsyncClient, ());
107
};
108
109
class MockAsyncClientManager : public AsyncClientManager {
110
public:
111
  MockAsyncClientManager();
112
  ~MockAsyncClientManager() override;
113
114
  MOCK_METHOD(AsyncClientFactoryPtr, factoryForGrpcService,
115
              (const envoy::config::core::v3::GrpcService& grpc_service, Stats::Scope& scope,
116
               bool skip_cluster_check));
117
118
  MOCK_METHOD(RawAsyncClientSharedPtr, getOrCreateRawAsyncClient,
119
              (const envoy::config::core::v3::GrpcService& grpc_service, Stats::Scope& scope,
120
               bool skip_cluster_check));
121
122
  MOCK_METHOD(RawAsyncClientSharedPtr, getOrCreateRawAsyncClientWithHashKey,
123
              (const GrpcServiceConfigWithHashKey& config_with_hash_key, Stats::Scope& scope,
124
               bool skip_cluster_check));
125
};
126
127
#if defined(ENVOY_ENABLE_FULL_PROTOS)
128
MATCHER_P(ProtoBufferEq, expected, "") {
129
  typename std::remove_const<decltype(expected)>::type proto;
130
  if (!proto.ParseFromString(arg->toString())) {
131
    *result_listener << "\nParse of buffer failed\n";
132
    return false;
133
  }
134
  auto equal = ::Envoy::TestUtility::protoEqual(proto, expected);
135
  if (!equal) {
136
    *result_listener << "\n"
137
                     << "=======================Expected proto:===========================\n"
138
                     << expected.DebugString()
139
                     << "------------------is not equal to actual proto:------------------\n"
140
                     << proto.DebugString()
141
                     << "=================================================================\n";
142
  }
143
  return equal;
144
}
145
146
MATCHER_P2(ProtoBufferEqIgnoringField, expected, ignored_field, "") {
147
  typename std::remove_const<decltype(expected)>::type proto;
148
  if (!proto.ParseFromArray(static_cast<char*>(arg->linearize(arg->length())), arg->length())) {
149
    *result_listener << "\nParse of buffer failed\n";
150
    return false;
151
  }
152
  const bool equal = ::Envoy::TestUtility::protoEqualIgnoringField(proto, expected, ignored_field);
153
  if (!equal) {
154
    std::string but_ignoring = absl::StrCat("(but ignoring ", ignored_field, ")");
155
    *result_listener << "\n"
156
                     << ::Envoy::TestUtility::addLeftAndRightPadding("Expected proto:") << "\n"
157
                     << ::Envoy::TestUtility::addLeftAndRightPadding(but_ignoring) << "\n"
158
                     << expected.DebugString()
159
                     << ::Envoy::TestUtility::addLeftAndRightPadding(
160
                            "is not equal to actual proto:")
161
                     << "\n"
162
                     << proto.DebugString()
163
                     << ::Envoy::TestUtility::addLeftAndRightPadding("") // line full of padding
164
                     << "\n";
165
  }
166
  return equal;
167
}
168
169
MATCHER_P(ProtoBufferEqIgnoreRepeatedFieldOrdering, expected, "") {
170
  typename std::remove_const<decltype(expected)>::type proto;
171
  if (!proto.ParseFromArray(static_cast<char*>(arg->linearize(arg->length())), arg->length())) {
172
    *result_listener << "\nParse of buffer failed\n";
173
    return false;
174
  }
175
  const bool equal =
176
      ::Envoy::TestUtility::protoEqual(proto, expected, /*ignore_repeated_field_ordering=*/true);
177
  if (!equal) {
178
    *result_listener << "\n"
179
                     << "=======================Expected proto:===========================\n"
180
                     << expected.DebugString()
181
                     << "------------------is not equal to actual proto:------------------\n"
182
                     << proto.DebugString()
183
                     << "=================================================================\n";
184
  }
185
  return equal;
186
}
187
#endif
188
189
} // namespace Grpc
190
} // namespace Envoy