Skip to content

Commit 8e917c5

Browse files
authored
weighted cluster: add header mutation integrations tests for weighted clusters (#40857)
Commit Message: Additional Description: Risk Level: low Testing: added integration tests Docs Changes: Release Notes: --------- Signed-off-by: Boteng Yao <[email protected]>
1 parent 55a2ea5 commit 8e917c5

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

source/extensions/filters/http/header_mutation/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ envoy_cc_extension(
2828
name = "config",
2929
srcs = ["config.cc"],
3030
hdrs = ["config.h"],
31+
extra_visibility = [
32+
"//test/integration:__subpackages__",
33+
],
3134
deps = [
3235
":header_mutation_lib",
3336
"//envoy/registry",

test/integration/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2736,9 +2736,11 @@ envoy_cc_test(
27362736
deps = [
27372737
":http_integration_lib",
27382738
":integration_lib",
2739+
"//source/extensions/filters/http/header_mutation:config",
27392740
"//test/integration/filters:repick_cluster_filter_lib",
27402741
"@envoy_api//envoy/config/bootstrap/v3:pkg_cc_proto",
27412742
"@envoy_api//envoy/config/route/v3:pkg_cc_proto",
2743+
"@envoy_api//envoy/extensions/filters/http/header_mutation/v3:pkg_cc_proto",
27422744
"@envoy_api//envoy/extensions/filters/network/http_connection_manager/v3:pkg_cc_proto",
27432745
],
27442746
)

test/integration/weighted_cluster_integration_test.cc

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
66
#include "envoy/config/route/v3/route_components.pb.h"
7+
#include "envoy/extensions/filters/http/header_mutation/v3/header_mutation.pb.h"
78
#include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h"
89

910
#include "test/integration/filters/repick_cluster_filter.h"
@@ -28,7 +29,7 @@ class WeightedClusterIntegrationTest : public testing::TestWithParam<Network::Ad
2829
}
2930
}
3031

31-
void initializeConfig(const std::vector<uint64_t>& weights) {
32+
void initializeConfig(const std::vector<uint64_t>& weights, bool test_header_mutation = false) {
3233
// Set the cluster configuration for `cluster_1`
3334
config_helper_.addConfigModifier([](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
3435
auto* cluster = bootstrap.mutable_static_resources()->add_clusters();
@@ -39,10 +40,13 @@ class WeightedClusterIntegrationTest : public testing::TestWithParam<Network::Ad
3940

4041
// Add the custom filter.
4142
config_helper_.addFilter("name: repick-cluster-filter");
43+
if (test_header_mutation) {
44+
config_helper_.addFilter("name: envoy.filters.http.header_mutation");
45+
}
4246

4347
// Modify route with weighted cluster configuration.
4448
config_helper_.addConfigModifier(
45-
[&weights](
49+
[&weights, &test_header_mutation](
4650
envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
4751
hcm) {
4852
auto* weighted_clusters = hcm.mutable_route_config()
@@ -56,6 +60,28 @@ class WeightedClusterIntegrationTest : public testing::TestWithParam<Network::Ad
5660
cluster->set_name("cluster_0");
5761
cluster->mutable_weight()->set_value(weights[0]);
5862

63+
if (test_header_mutation) {
64+
envoy::config::core::v3::HeaderValueOption* header_value_option =
65+
cluster->mutable_request_headers_to_add()->Add();
66+
auto* mutable_header = header_value_option->mutable_header();
67+
mutable_header->set_key("x-cluster-name-test");
68+
mutable_header->set_value("cluster_0");
69+
70+
envoy::extensions::filters::http::header_mutation::v3::HeaderMutationPerRoute
71+
header_mutation;
72+
std::string header_mutation_config = R"EOF(
73+
mutations:
74+
response_mutations:
75+
- append:
76+
header:
77+
key: "x-cluster-header-mutation-test"
78+
value: "cluster-0"
79+
)EOF";
80+
TestUtility::loadFromYaml(header_mutation_config, header_mutation);
81+
(*cluster->mutable_typed_per_filter_config())["envoy.filters.http.header_mutation"]
82+
.PackFrom(header_mutation);
83+
}
84+
5985
// Add a cluster with `cluster_header` specified.
6086
cluster = weighted_clusters->add_clusters();
6187
cluster->set_cluster_header(std::string(Envoy::RepickClusterFilter::ClusterHeaderName));
@@ -67,7 +93,8 @@ class WeightedClusterIntegrationTest : public testing::TestWithParam<Network::Ad
6793

6894
const std::vector<uint64_t>& getDefaultWeights() { return default_weights_; }
6995

70-
void sendRequestAndValidateResponse(const std::vector<uint64_t>& upstream_indices) {
96+
void sendRequestAndValidateResponse(const std::vector<uint64_t>& upstream_indices,
97+
bool check_header_mutation = false) {
7198
// Create a client aimed at Envoy’s default HTTP port.
7299
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
73100

@@ -86,6 +113,14 @@ class WeightedClusterIntegrationTest : public testing::TestWithParam<Network::Ad
86113
// Verify the proxied request was received upstream, as expected.
87114
EXPECT_TRUE(upstream_request_->complete());
88115
EXPECT_EQ(0U, upstream_request_->bodyLength());
116+
if (check_header_mutation) {
117+
EXPECT_EQ(
118+
upstream_request_->headers().get(Http::LowerCaseString("x-cluster-name-test")).size(), 1);
119+
EXPECT_EQ(result.response->headers()
120+
.get(Http::LowerCaseString("x-cluster-header-mutation-test"))
121+
.size(),
122+
1);
123+
}
89124
// Verify the proxied response was received downstream, as expected.
90125
EXPECT_TRUE(result.response->complete());
91126
EXPECT_EQ("200", result.response->headers().getStatusValue());
@@ -121,6 +156,19 @@ TEST_P(WeightedClusterIntegrationTest, SteerTrafficToOneClusterWithName) {
121156
EXPECT_EQ(test_server_->counter("cluster.cluster_0.upstream_cx_total")->value(), 1);
122157
}
123158

159+
// Test the header mutations in weighted clusters.
160+
TEST_P(WeightedClusterIntegrationTest, SteerTrafficToOneClusterWithHeaderMutation) {
161+
setDeterministicValue();
162+
initializeConfig(getDefaultWeights(), true);
163+
164+
// The expected destination cluster upstream is index 0 since the selected
165+
// value is set to 0 indirectly via `setDeterministicValue()` above to set the weight to 0.
166+
sendRequestAndValidateResponse({0}, true);
167+
168+
// Check that the expected upstream cluster has incoming request.
169+
EXPECT_EQ(test_server_->counter("cluster.cluster_0.upstream_cx_total")->value(), 1);
170+
}
171+
124172
// Steer the traffic (i.e. send the request) to the weighted cluster with `cluster_header`
125173
// specified.
126174
TEST_P(WeightedClusterIntegrationTest, SteerTrafficToOneClusterWithHeader) {

0 commit comments

Comments
 (0)