forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_metadata_fetcher_integration_test.cc
152 lines (124 loc) · 5.59 KB
/
aws_metadata_fetcher_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
#include "common/common/fmt.h"
#include "extensions/filters/http/common/aws/utility.h"
#include "test/integration/integration.h"
#include "test/integration/utility.h"
#include "test/server/utility.h"
#include "test/test_common/utility.h"
namespace Envoy {
using Envoy::Extensions::HttpFilters::Common::Aws::Utility;
class AwsMetadataIntegrationTestBase : public ::testing::Test, public BaseIntegrationTest {
public:
AwsMetadataIntegrationTestBase(int status_code, int delay_s)
: BaseIntegrationTest(Network::Address::IpVersion::v4, renderConfig(status_code, delay_s)) {}
static std::string renderConfig(int status_code, int delay_s) {
return fmt::format(ConfigHelper::BASE_CONFIG + R"EOF(
filter_chains:
filters:
name: envoy.http_connection_manager
config:
stat_prefix: metadata_test
http_filters:
- name: envoy.fault
config:
delay:
fixed_delay:
seconds: {}
nanos: {}
percentage:
numerator: 100
denominator: HUNDRED
- name: envoy.router
codec_type: HTTP1
route_config:
virtual_hosts:
name: metadata_endpoint
routes:
- name: auth_route
direct_response:
status: {}
body:
inline_string: METADATA_VALUE_WITH_AUTH
match:
prefix: "/"
headers:
- name: Authorization
exact_match: AUTH_TOKEN
- name: no_auth_route
direct_response:
status: {}
body:
inline_string: METADATA_VALUE
match:
prefix: "/"
domains: "*"
name: route_config_0
)EOF",
delay_s, delay_s > 0 ? 0 : 1000, status_code, status_code);
}
void SetUp() override { BaseIntegrationTest::initialize(); }
void TearDown() override {
test_server_.reset();
fake_upstreams_.clear();
}
};
class AwsMetadataIntegrationTestSuccess : public AwsMetadataIntegrationTestBase {
public:
AwsMetadataIntegrationTestSuccess() : AwsMetadataIntegrationTestBase(200, 0) {}
};
TEST_F(AwsMetadataIntegrationTestSuccess, Success) {
const auto endpoint = fmt::format("{}:{}", Network::Test::getLoopbackAddressUrlString(version_),
lookupPort("listener_0"));
const auto response = Utility::metadataFetcher(endpoint, "", "");
ASSERT_TRUE(response.has_value());
EXPECT_EQ("METADATA_VALUE", *response);
ASSERT_NE(nullptr, test_server_->counter("http.metadata_test.downstream_rq_completed"));
EXPECT_EQ(1, test_server_->counter("http.metadata_test.downstream_rq_completed")->value());
}
TEST_F(AwsMetadataIntegrationTestSuccess, AuthToken) {
const auto endpoint = fmt::format("{}:{}", Network::Test::getLoopbackAddressUrlString(version_),
lookupPort("listener_0"));
const auto response = Utility::metadataFetcher(endpoint, "", "AUTH_TOKEN");
ASSERT_TRUE(response.has_value());
EXPECT_EQ("METADATA_VALUE_WITH_AUTH", *response);
ASSERT_NE(nullptr, test_server_->counter("http.metadata_test.downstream_rq_completed"));
EXPECT_EQ(1, test_server_->counter("http.metadata_test.downstream_rq_completed")->value());
}
class AwsMetadataIntegrationTestFailure : public AwsMetadataIntegrationTestBase {
public:
AwsMetadataIntegrationTestFailure() : AwsMetadataIntegrationTestBase(503, 0) {}
};
TEST_F(AwsMetadataIntegrationTestFailure, Failure) {
const auto endpoint = fmt::format("{}:{}", Network::Test::getLoopbackAddressUrlString(version_),
lookupPort("listener_0"));
const auto start_time = timeSystem().monotonicTime();
const auto response = Utility::metadataFetcher(endpoint, "", "");
const auto end_time = timeSystem().monotonicTime();
EXPECT_FALSE(response.has_value());
// Verify correct number of retries
ASSERT_NE(nullptr, test_server_->counter("http.metadata_test.downstream_rq_completed"));
EXPECT_EQ(4, test_server_->counter("http.metadata_test.downstream_rq_completed")->value());
// Verify correct sleep time between retries: 4 * 1000 = 4000
EXPECT_LE(4000,
std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count());
}
class AwsMetadataIntegrationTestTimeout : public AwsMetadataIntegrationTestBase {
public:
AwsMetadataIntegrationTestTimeout() : AwsMetadataIntegrationTestBase(200, 10) {}
};
TEST_F(AwsMetadataIntegrationTestTimeout, Timeout) {
const auto endpoint = fmt::format("{}:{}", Network::Test::getLoopbackAddressUrlString(version_),
lookupPort("listener_0"));
const auto start_time = timeSystem().monotonicTime();
const auto response = Utility::metadataFetcher(endpoint, "", "");
const auto end_time = timeSystem().monotonicTime();
EXPECT_FALSE(response.has_value());
// We do now check http.metadata_test.downstream_rq_completed value here because it's
// behavior is different between Linux and Mac when Curl disconnects on timeout. On Mac it is
// incremented, while on Linux it is not.
// Verify correct sleep time between retries: 4 * 5000 = 20000
EXPECT_LE(20000,
std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count());
EXPECT_GT(40000,
std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count());
}
} // namespace Envoy