Skip to content

Commit

Permalink
aws: fix test flake when no IMDS can be found (#35633)
Browse files Browse the repository at this point in the history
Commit Message: aws: fix test flake when no IMDS can be found
Additional Description: Lack of IMDS (169.254.169.254 address) can cause
a race condition and crash during testing due to the shutdown of cluster
manager. This scenario should not occur normally, as cluster manager
will still exist and the lack of IMDS is handled gracefully.

Signed-off-by: Nigel Brittain <[email protected]>
  • Loading branch information
nbaws authored Aug 9, 2024
1 parent 422e511 commit a298895
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions source/extensions/common/aws/metadata_fetcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class MetadataFetcherImpl : public MetadataFetcher,
ASSERT(!request_);
complete_ = false;
receiver_ = makeOptRef(receiver);

// Stop processing if we are shutting down
if (cm_.isShutdown()) {
return;
}

const auto thread_local_cluster = cm_.getThreadLocalCluster(cluster_name_);
if (thread_local_cluster == nullptr) {
ENVOY_LOG(error, "{} AWS Metadata failed: [cluster = {}] not found", __func__, cluster_name_);
Expand Down
26 changes: 26 additions & 0 deletions test/extensions/common/aws/metadata_fetcher_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ MATCHER_P(OptionsHasRetryPolicy, policyMatcher, "") {
class MetadataFetcherTest : public testing::Test {
public:
void setupFetcher() {

mock_factory_ctx_.server_factory_context_.cluster_manager_.initializeThreadLocalClusters(
{"cluster_name"});
fetcher_ = MetadataFetcher::create(mock_factory_ctx_.server_factory_context_.cluster_manager_,
"cluster_name");
EXPECT_TRUE(fetcher_ != nullptr);
}

void setupFetcherShutDown() {
ON_CALL(mock_factory_ctx_.server_factory_context_.cluster_manager_, getThreadLocalCluster(_))
.WillByDefault(Return(nullptr));
ON_CALL(mock_factory_ctx_.server_factory_context_.cluster_manager_, isShutdown())
.WillByDefault(Return(true));

mock_factory_ctx_.server_factory_context_.cluster_manager_.initializeThreadLocalClusters(
{"cluster_name"});
fetcher_ = MetadataFetcher::create(mock_factory_ctx_.server_factory_context_.cluster_manager_,
Expand All @@ -103,6 +117,18 @@ TEST_F(MetadataFetcherTest, TestGetSuccess) {
fetcher_->fetch(message, parent_span_, receiver);
}

TEST_F(MetadataFetcherTest, TestClusterShutdown) {
// Setup
setupFetcherShutDown();
Http::RequestMessageImpl message;
MockMetadataReceiver receiver;
EXPECT_CALL(receiver, onMetadataSuccess(_)).Times(0);
EXPECT_CALL(receiver, onMetadataError(_)).Times(0);

// Act
fetcher_->fetch(message, parent_span_, receiver);
}

TEST_F(MetadataFetcherTest, TestRequestMatchAndSpanPassedDown) {
// Setup
setupFetcher();
Expand Down

0 comments on commit a298895

Please sign in to comment.