forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alpn_integration_test.cc
189 lines (163 loc) · 8.07 KB
/
alpn_integration_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "test/integration/autonomous_upstream.h"
#include "test/integration/http_integration.h"
#include "gtest/gtest.h"
namespace Envoy {
namespace {
class AlpnIntegrationTest : public testing::TestWithParam<Network::Address::IpVersion>,
public HttpIntegrationTest {
public:
AlpnIntegrationTest() : HttpIntegrationTest(Http::CodecType::HTTP2, GetParam()) {}
void SetUp() override {
autonomous_upstream_ = true;
setUpstreamCount(2);
setDownstreamProtocol(Http::CodecType::HTTP2);
upstream_tls_ = true;
}
void initialize() override {
config_helper_.configureUpstreamTls(true);
// If the test is configured to have multiple upstreams, make sure the Envoy
// config also has 2 upstreams.
if (fake_upstreams_count_ == 2) {
config_helper_.addConfigModifier([&](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
auto* static_resources = bootstrap.mutable_static_resources();
auto* cluster = static_resources->mutable_clusters(0);
auto* load_assignment = cluster->mutable_load_assignment();
load_assignment->set_cluster_name(cluster->name());
auto* locality = load_assignment->add_endpoints();
locality->set_priority(0);
locality->mutable_locality()->set_region("region");
locality->add_lb_endpoints()->mutable_endpoint()->MergeFrom(
ConfigHelper::buildEndpoint(Network::Test::getLoopbackAddressString(version_)));
});
}
HttpIntegrationTest::initialize();
}
void createUpstreams() override {
for (uint32_t i = 0; i < fake_upstreams_count_; ++i) {
auto endpoint = upstream_address_fn_(i);
auto config = upstreamConfig();
config.upstream_protocol_ = protocols_[i];
createUpstream(endpoint, config);
}
}
std::vector<Http::CodecType> protocols_;
};
INSTANTIATE_TEST_SUITE_P(IpVersions, AlpnIntegrationTest,
testing::ValuesIn(TestEnvironment::getIpVersionsForTest()),
TestUtility::ipTestParamsToString);
TEST_P(AlpnIntegrationTest, Http2New) {
setUpstreamProtocol(Http::CodecType::HTTP2);
protocols_ = {Http::CodecType::HTTP2, Http::CodecType::HTTP2};
initialize();
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
auto response = codec_client_->makeHeaderOnlyRequest(default_request_headers_);
auto response2 = codec_client_->makeHeaderOnlyRequest(default_request_headers_);
ASSERT_TRUE(response->waitForEndStream());
ASSERT_TRUE(response->complete());
EXPECT_EQ("200", response->headers().Status()->value().getStringView());
ASSERT_TRUE(response2->waitForEndStream());
ASSERT_TRUE(response2->complete());
EXPECT_EQ("200", response2->headers().Status()->value().getStringView());
}
TEST_P(AlpnIntegrationTest, Http1New) {
setUpstreamProtocol(Http::CodecType::HTTP1);
protocols_ = {Http::CodecType::HTTP1, Http::CodecType::HTTP1};
initialize();
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
IntegrationCodecClientPtr codec_client2 = makeHttpConnection(lookupPort("http"));
auto response = codec_client_->makeHeaderOnlyRequest(default_request_headers_);
auto response2 = codec_client2->makeHeaderOnlyRequest(default_request_headers_);
ASSERT_TRUE(response->waitForEndStream());
ASSERT_TRUE(response->complete());
EXPECT_EQ("200", response->headers().Status()->value().getStringView());
ASSERT_TRUE(response->waitForEndStream());
ASSERT_TRUE(response->complete());
EXPECT_EQ("200", response->headers().Status()->value().getStringView());
ASSERT_TRUE(response2->waitForEndStream());
ASSERT_TRUE(response2->complete());
EXPECT_EQ("200", response2->headers().Status()->value().getStringView());
codec_client2->close();
}
TEST_P(AlpnIntegrationTest, Http1RememberLimits) {
setUpstreamProtocol(Http::CodecType::HTTP1);
protocols_ = {Http::CodecType::HTTP1, Http::CodecType::HTTP1};
initialize();
// Send a request and response, then close the connection.
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
default_request_headers_.addCopy(AutonomousStream::CLOSE_AFTER_RESPONSE, "yes");
auto response = codec_client_->makeHeaderOnlyRequest(default_request_headers_);
ASSERT_TRUE(response->waitForEndStream());
test_server_->waitForCounterGe("cluster.cluster_0.upstream_cx_destroy", 1);
test_server_->waitForCounterGe("cluster.cluster_0.upstream_cx_total", 1);
fake_upstreams_.clear();
{
IntegrationCodecClientPtr codec_client1 = makeHttpConnection(lookupPort("http"));
auto response1 = codec_client1->makeHeaderOnlyRequest(default_request_headers_);
IntegrationCodecClientPtr codec_client2 = makeHttpConnection(lookupPort("http"));
auto response2 = codec_client2->makeHeaderOnlyRequest(default_request_headers_);
// Envoy should attempt to establish 2 new connections, one for each stream.
test_server_->waitForCounterGe("cluster.cluster_0.upstream_cx_total", 3);
codec_client1->close();
codec_client2->close();
}
}
TEST_P(AlpnIntegrationTest, Http2RememberSettings) {
setUpstreamCount(1);
setUpstreamProtocol(Http::CodecType::HTTP2);
autonomous_upstream_ = false;
protocols_ = {Http::CodecType::HTTP2, Http::CodecType::HTTP2};
initialize();
// Send a request and response, get the settings update then close the connection.
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
auto response =
sendRequestAndWaitForResponse(default_request_headers_, 0, default_response_headers_, 0);
// Adjust the max concurrent streams of one connection created above to 2.
auto bytes_read = test_server_->counter("cluster.cluster_0.upstream_cx_rx_bytes_total");
const Http2Frame settings_frame = Http2Frame::makeSettingsFrame(
Http2Frame::SettingsFlags::None, {{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 1}});
std::string settings_data(settings_frame);
Buffer::OwnedImpl settings_buffer(settings_data);
upstream_request_->postToConnectionThread([this, &settings_buffer]() {
fake_upstream_connection_->connection().write(settings_buffer, false);
});
test_server_->waitForCounterGe("cluster.cluster_0.upstream_cx_rx_bytes_total",
bytes_read + settings_data.size());
// Close the connection.
ASSERT_TRUE(fake_upstream_connection_->close());
test_server_->waitForCounterGe("cluster.cluster_0.upstream_cx_destroy", 1);
test_server_->waitForCounterGe("cluster.cluster_0.upstream_cx_total", 1);
{
absl::MutexLock l(&fake_upstreams_[0]->lock());
IntegrationCodecClientPtr codec_client1 = makeHttpConnection(lookupPort("http"));
auto response1 = codec_client1->makeHeaderOnlyRequest(default_request_headers_);
IntegrationCodecClientPtr codec_client2 = makeHttpConnection(lookupPort("http"));
auto response2 = codec_client2->makeHeaderOnlyRequest(default_request_headers_);
// Envoy should attempt to establish 2 new connections, one for each stream.
test_server_->waitForCounterGe("cluster.cluster_0.upstream_cx_total", 3);
codec_client1->close();
codec_client2->close();
}
}
TEST_P(AlpnIntegrationTest, Mixed) {
protocols_ = {Http::CodecType::HTTP1, Http::CodecType::HTTP2};
initialize();
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
// Kick off two simultaneous requests, to ensure two upstream connections are
// created.
auto encoder_decoder1 = codec_client_->startRequest(default_request_headers_);
auto& encoder1 = encoder_decoder1.first;
auto& response1 = encoder_decoder1.second;
auto encoder_decoder2 = codec_client_->startRequest(default_request_headers_);
auto& encoder2 = encoder_decoder2.first;
auto& response2 = encoder_decoder2.second;
// Finish both streams to ensure both responses come through.
Buffer::OwnedImpl data("");
encoder1.encodeData(data, true);
encoder2.encodeData(data, true);
ASSERT_TRUE(response1->waitForEndStream());
ASSERT_TRUE(response2->waitForEndStream());
EXPECT_EQ("200", response1->headers().Status()->value().getStringView());
EXPECT_EQ("200", response2->headers().Status()->value().getStringView());
}
} // namespace
} // namespace Envoy