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