Coverage Report

Created: 2024-09-19 09:45

/proc/self/cwd/test/mocks/upstream/cluster_info.cc
Line
Count
Source (jump to first uncovered line)
1
#include "test/mocks/upstream/cluster_info.h"
2
3
#include <limits>
4
5
#include "envoy/common/optref.h"
6
#include "envoy/config/cluster/v3/cluster.pb.h"
7
#include "envoy/upstream/host_description.h"
8
#include "envoy/upstream/upstream.h"
9
10
#include "source/common/config/metadata.h"
11
#include "source/common/http/utility.h"
12
#include "source/common/network/raw_buffer_socket.h"
13
#include "source/common/router/upstream_codec_filter.h"
14
#include "source/common/upstream/upstream_impl.h"
15
16
using testing::_;
17
using testing::Invoke;
18
using testing::Return;
19
using testing::ReturnPointee;
20
using testing::ReturnRef;
21
22
namespace Envoy {
23
namespace Upstream {
24
25
0
MockIdleTimeEnabledClusterInfo::MockIdleTimeEnabledClusterInfo() {
26
0
  ON_CALL(*this, idleTimeout()).WillByDefault(Return(std::chrono::milliseconds(1000)));
27
0
}
28
29
0
MockIdleTimeEnabledClusterInfo::~MockIdleTimeEnabledClusterInfo() = default;
30
31
MockUpstreamLocalAddressSelector::MockUpstreamLocalAddressSelector(
32
    Network::Address::InstanceConstSharedPtr& address)
33
401k
    : address_(address) {
34
401k
  ON_CALL(*this, getUpstreamLocalAddressImpl(_))
35
401k
      .WillByDefault(
36
401k
          Invoke([&](const Network::Address::InstanceConstSharedPtr&) -> UpstreamLocalAddress {
37
22.4k
            UpstreamLocalAddress ret;
38
22.4k
            ret.address_ = address_;
39
22.4k
            ret.socket_options_ = nullptr;
40
22.4k
            return ret;
41
22.4k
          }));
42
401k
}
43
44
MockClusterInfo::MockClusterInfo()
45
    : http2_options_(::Envoy::Http2::Utility::initializeAndValidateOptions(
46
                         envoy::config::core::v3::Http2ProtocolOptions())
47
                         .value()),
48
      traffic_stat_names_(stats_store_.symbolTable()),
49
      config_update_stats_names_(stats_store_.symbolTable()),
50
      lb_stat_names_(stats_store_.symbolTable()), endpoint_stat_names_(stats_store_.symbolTable()),
51
      cluster_load_report_stat_names_(stats_store_.symbolTable()),
52
      cluster_circuit_breakers_stat_names_(stats_store_.symbolTable()),
53
      cluster_request_response_size_stat_names_(stats_store_.symbolTable()),
54
      cluster_timeout_budget_stat_names_(stats_store_.symbolTable()),
55
      traffic_stats_(
56
          ClusterInfoImpl::generateStats(stats_store_.rootScope(), traffic_stat_names_, false)),
57
      config_update_stats_(config_update_stats_names_, *stats_store_.rootScope()),
58
      lb_stats_(lb_stat_names_, *stats_store_.rootScope()),
59
      endpoint_stats_(endpoint_stat_names_, *stats_store_.rootScope()),
60
      transport_socket_matcher_(new NiceMock<Upstream::MockTransportSocketMatcher>()),
61
      load_report_stats_(ClusterInfoImpl::generateLoadReportStats(
62
          *load_report_stats_store_.rootScope(), cluster_load_report_stat_names_)),
63
      request_response_size_stats_(std::make_unique<ClusterRequestResponseSizeStats>(
64
          ClusterInfoImpl::generateRequestResponseSizeStats(
65
              *request_response_size_stats_store_.rootScope(),
66
              cluster_request_response_size_stat_names_))),
67
      timeout_budget_stats_(
68
          std::make_unique<ClusterTimeoutBudgetStats>(ClusterInfoImpl::generateTimeoutBudgetStats(
69
              *timeout_budget_stats_store_.rootScope(), cluster_timeout_budget_stat_names_))),
70
      circuit_breakers_stats_(ClusterInfoImpl::generateCircuitBreakersStats(
71
          *stats_store_.rootScope(), cluster_circuit_breakers_stat_names_.default_, true,
72
          cluster_circuit_breakers_stat_names_)),
73
      resource_manager_(new Upstream::ResourceManagerImpl(
74
          runtime_, "fake_key", 1, 1024, 1024, 1, std::numeric_limits<uint64_t>::max(),
75
          std::numeric_limits<uint64_t>::max(), circuit_breakers_stats_, absl::nullopt,
76
          absl::nullopt)),
77
      upstream_local_address_selector_(
78
          std::make_shared<NiceMock<MockUpstreamLocalAddressSelector>>(source_address_)),
79
401k
      stats_scope_(stats_store_.createScope("test_scope")) {
80
401k
  ON_CALL(*this, connectTimeout()).WillByDefault(Return(std::chrono::milliseconds(5001)));
81
401k
  ON_CALL(*this, idleTimeout()).WillByDefault(Return(absl::optional<std::chrono::milliseconds>()));
82
401k
  ON_CALL(*this, perUpstreamPreconnectRatio()).WillByDefault(Return(1.0));
83
401k
  ON_CALL(*this, name()).WillByDefault(ReturnRef(name_));
84
401k
  ON_CALL(*this, observabilityName()).WillByDefault(ReturnRef(observability_name_));
85
401k
  ON_CALL(*this, edsServiceName()).WillByDefault(Invoke([this]() -> const std::string& {
86
0
    return eds_service_name_.has_value() ? eds_service_name_.value() : Envoy::EMPTY_STRING;
87
0
  }));
88
401k
  ON_CALL(*this, loadBalancerFactory()).WillByDefault(Invoke([this]() -> TypedLoadBalancerFactory& {
89
0
    return *lb_factory_;
90
0
  }));
91
401k
  ON_CALL(*this, http1Settings()).WillByDefault(ReturnRef(http1_settings_));
92
401k
  ON_CALL(*this, http2Options()).WillByDefault(ReturnRef(http2_options_));
93
401k
  ON_CALL(*this, http3Options()).WillByDefault(ReturnRef(http3_options_));
94
401k
  ON_CALL(*this, commonHttpProtocolOptions())
95
401k
      .WillByDefault(ReturnRef(common_http_protocol_options_));
96
401k
  ON_CALL(*this, extensionProtocolOptions(_)).WillByDefault(Return(extension_protocol_options_));
97
401k
  ON_CALL(*this, maxResponseHeadersCount())
98
401k
      .WillByDefault(ReturnPointee(&max_response_headers_count_));
99
401k
  ON_CALL(*this, maxRequestsPerConnection())
100
401k
      .WillByDefault(ReturnPointee(&max_requests_per_connection_));
101
401k
  ON_CALL(*this, trafficStats()).WillByDefault(ReturnRef(traffic_stats_));
102
401k
  ON_CALL(*this, lbStats()).WillByDefault(ReturnRef(lb_stats_));
103
401k
  ON_CALL(*this, configUpdateStats()).WillByDefault(ReturnRef(config_update_stats_));
104
401k
  ON_CALL(*this, endpointStats()).WillByDefault(ReturnRef(endpoint_stats_));
105
401k
  ON_CALL(*this, statsScope()).WillByDefault(ReturnRef(*stats_store_.rootScope()));
106
  // TODO(incfly): The following is a hack because it's not possible to directly embed
107
  // a mock transport socket factory matcher due to circular dependencies. Fix this up in a follow
108
  // up.
109
401k
  ON_CALL(*this, transportSocketMatcher())
110
401k
      .WillByDefault(
111
401k
          Invoke([this]() -> TransportSocketMatcher& { return *transport_socket_matcher_; }));
112
401k
  ON_CALL(*this, loadReportStats()).WillByDefault(ReturnRef(load_report_stats_));
113
401k
  ON_CALL(*this, requestResponseSizeStats())
114
401k
      .WillByDefault(Return(
115
401k
          std::reference_wrapper<ClusterRequestResponseSizeStats>(*request_response_size_stats_)));
116
401k
  ON_CALL(*this, timeoutBudgetStats())
117
401k
      .WillByDefault(
118
401k
          Return(std::reference_wrapper<ClusterTimeoutBudgetStats>(*timeout_budget_stats_)));
119
401k
  ON_CALL(*this, getUpstreamLocalAddressSelector())
120
401k
      .WillByDefault(Return(upstream_local_address_selector_));
121
401k
  ON_CALL(*this, resourceManager(_))
122
401k
      .WillByDefault(Invoke(
123
401k
          [this](ResourcePriority) -> Upstream::ResourceManager& { return *resource_manager_; }));
124
401k
  ON_CALL(*this, upstreamConfig())
125
401k
      .WillByDefault(
126
401k
          Invoke([this]() -> OptRef<const envoy::config::core::v3::TypedExtensionConfig> {
127
5
            return makeOptRefFromPtr<const envoy::config::core::v3::TypedExtensionConfig>(
128
5
                upstream_config_.get());
129
5
          }));
130
131
401k
  ON_CALL(*this, lbConfig()).WillByDefault(ReturnRef(lb_config_));
132
401k
  ON_CALL(*this, metadata()).WillByDefault(ReturnRef(metadata_));
133
401k
  ON_CALL(*this, upstreamHttpProtocolOptions())
134
401k
      .WillByDefault(ReturnRef(upstream_http_protocol_options_));
135
401k
  ON_CALL(*this, alternateProtocolsCacheOptions())
136
401k
      .WillByDefault(ReturnRef(alternate_protocols_cache_options_));
137
  // Delayed construction of typed_metadata_, to allow for injection of metadata
138
401k
  ON_CALL(*this, typedMetadata())
139
401k
      .WillByDefault(Invoke([this]() -> const Envoy::Config::TypedMetadata& {
140
0
        if (typed_metadata_ == nullptr) {
141
0
          typed_metadata_ =
142
0
              std::make_unique<Config::TypedMetadataImpl<ClusterTypedMetadataFactory>>(metadata_);
143
0
        }
144
0
        return *typed_metadata_;
145
0
      }));
146
401k
  ON_CALL(*this, clusterType())
147
401k
      .WillByDefault(
148
401k
          Invoke([this]() -> OptRef<const envoy::config::cluster::v3::Cluster::CustomClusterType> {
149
0
            return makeOptRefFromPtr<const envoy::config::cluster::v3::Cluster::CustomClusterType>(
150
0
                cluster_type_.get());
151
0
          }));
152
401k
  ON_CALL(*this, upstreamHttpProtocol(_))
153
401k
      .WillByDefault(Return(std::vector<Http::Protocol>{Http::Protocol::Http11}));
154
401k
  ON_CALL(*this, createFilterChain(_, _, _))
155
401k
      .WillByDefault(Invoke([&](Http::FilterChainManager& manager, bool only_create_if_configured,
156
401k
                                const Http::FilterChainOptions&) -> bool {
157
10
        if (only_create_if_configured) {
158
5
          return false;
159
5
        }
160
5
        Http::FilterFactoryCb factory_cb =
161
5
            [](Http::FilterChainFactoryCallbacks& callbacks) -> void {
162
5
          callbacks.addStreamDecoderFilter(std::make_shared<Router::UpstreamCodecFilter>());
163
5
        };
164
5
        manager.applyFilterFactoryCb({}, factory_cb);
165
5
        return true;
166
10
      }));
167
401k
  ON_CALL(*this, loadBalancerConfig())
168
401k
      .WillByDefault(Return(makeOptRefFromPtr<const LoadBalancerConfig>(nullptr)));
169
401k
  ON_CALL(*this, makeHeaderValidator(_)).WillByDefault(Invoke([&](Http::Protocol protocol) {
170
23.2k
    return header_validator_factory_ ? header_validator_factory_->createClientHeaderValidator(
171
0
                                           protocol, codecStats(protocol))
172
23.2k
                                     : nullptr;
173
23.2k
  }));
174
401k
  ON_CALL(*this, lrsReportMetricNames())
175
401k
      .WillByDefault(Invoke([this]() -> OptRef<const Envoy::Orca::LrsReportMetricNames> {
176
0
        return makeOptRefFromPtr<const Envoy::Orca::LrsReportMetricNames>(
177
0
            lrs_report_metric_names_.get());
178
0
      }));
179
401k
}
180
181
401k
MockClusterInfo::~MockClusterInfo() = default;
182
183
0
::Envoy::Http::HeaderValidatorStats& MockClusterInfo::codecStats(Http::Protocol protocol) const {
184
0
  switch (protocol) {
185
0
  case ::Envoy::Http::Protocol::Http10:
186
0
  case ::Envoy::Http::Protocol::Http11:
187
0
    return http1CodecStats();
188
0
  case ::Envoy::Http::Protocol::Http2:
189
0
    return http2CodecStats();
190
0
  case ::Envoy::Http::Protocol::Http3:
191
0
    return http3CodecStats();
192
0
  }
193
0
  PANIC_DUE_TO_CORRUPT_ENUM;
194
0
}
195
196
0
Http::Http1::CodecStats& MockClusterInfo::http1CodecStats() const {
197
0
  return Http::Http1::CodecStats::atomicGet(http1_codec_stats_, *stats_scope_);
198
0
}
199
200
815
Http::Http2::CodecStats& MockClusterInfo::http2CodecStats() const {
201
815
  return Http::Http2::CodecStats::atomicGet(http2_codec_stats_, *stats_scope_);
202
815
}
203
204
0
Http::Http3::CodecStats& MockClusterInfo::http3CodecStats() const {
205
0
  return Http::Http3::CodecStats::atomicGet(http3_codec_stats_, *stats_scope_);
206
0
}
207
208
} // namespace Upstream
209
} // namespace Envoy