Skip to content

Commit 9aef3db

Browse files
committed
Fix flaky IMDS client test
This test should be less flaky since the mock client should never trigger the retry logic in the IMDS client by sometimes returning a 500.
1 parent c4f94c9 commit 9aef3db

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

core/imds/src/test/java/software/amazon/awssdk/imds/internal/CachedTokenClientTest.java

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,31 @@
2828
import static com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED;
2929
import static org.assertj.core.api.Assertions.assertThat;
3030
import static org.assertj.core.api.Assertions.assertThatThrownBy;
31+
import static org.mockito.ArgumentMatchers.any;
32+
import static org.mockito.Mockito.mock;
33+
import static org.mockito.Mockito.when;
3134
import static software.amazon.awssdk.imds.TestConstants.EC2_METADATA_TOKEN_TTL_HEADER;
3235

3336
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
3437
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
38+
import java.io.ByteArrayInputStream;
39+
import java.io.IOException;
3540
import java.net.URI;
41+
import java.nio.charset.StandardCharsets;
3642
import java.time.Duration;
3743
import org.junit.jupiter.api.AfterEach;
3844
import org.junit.jupiter.api.BeforeEach;
3945
import org.junit.jupiter.api.Test;
46+
import org.mockito.ArgumentCaptor;
47+
import org.mockito.Mockito;
4048
import software.amazon.awssdk.core.exception.SdkClientException;
49+
import software.amazon.awssdk.http.AbortableInputStream;
50+
import software.amazon.awssdk.http.ExecutableHttpRequest;
51+
import software.amazon.awssdk.http.HttpExecuteRequest;
52+
import software.amazon.awssdk.http.HttpExecuteResponse;
53+
import software.amazon.awssdk.http.SdkHttpClient;
54+
import software.amazon.awssdk.http.SdkHttpFullResponse;
55+
import software.amazon.awssdk.http.SdkHttpResponse;
4156
import software.amazon.awssdk.imds.Ec2MetadataClient;
4257
import software.amazon.awssdk.imds.Ec2MetadataResponse;
4358

@@ -58,13 +73,32 @@ void tearDown(WireMockRuntimeInfo wmRuntimeInfo) {
5873
}
5974

6075
@Test
61-
void get_tokenFailsError4xx_shouldNotRetry() {
62-
stubFor(put(urlPathEqualTo("/latest/api/token")).willReturn(aResponse().withStatus(400).withBody("ERROR 400")));
63-
stubFor(get(urlPathEqualTo("/latest/meta-data/ami-id")).willReturn(aResponse().withBody("{}")));
64-
65-
assertThatThrownBy(() -> clientBuilder.build().get("/latest/meta-data/ami-id")).isInstanceOf(SdkClientException.class);
66-
verify(exactly(1), putRequestedFor(urlPathEqualTo("/latest/api/token"))
67-
.withHeader("x-aws-ec2-metadata-token-ttl-seconds", equalTo("21600")));
76+
void get_tokenFailsError4xx_shouldNotRetry() throws IOException {
77+
SdkHttpClient mockClient = mock(SdkHttpClient.class);
78+
ExecutableHttpRequest mockRequest = mock(ExecutableHttpRequest.class);
79+
when(mockClient.prepareRequest(any(HttpExecuteRequest.class))).thenReturn(mockRequest);
80+
81+
AbortableInputStream content =
82+
AbortableInputStream.create(new ByteArrayInputStream("ERROR 400".getBytes(StandardCharsets.UTF_8)));
83+
SdkHttpResponse httpResponse = SdkHttpFullResponse.builder()
84+
.statusCode(400)
85+
.build();
86+
HttpExecuteResponse executeResponse = HttpExecuteResponse.builder()
87+
.response(httpResponse)
88+
.responseBody(content)
89+
.build();
90+
when(mockRequest.call()).thenReturn(executeResponse);
91+
92+
Ec2MetadataClient imdsClient = Ec2MetadataClient.builder().httpClient(mockClient).build();
93+
94+
assertThatThrownBy(() ->imdsClient.get("/latest/meta-data/ami-id")).isInstanceOf(SdkClientException.class);
95+
96+
ArgumentCaptor<HttpExecuteRequest> requestCaptor = ArgumentCaptor.forClass(HttpExecuteRequest.class);
97+
Mockito.verify(mockClient).prepareRequest(requestCaptor.capture());
98+
HttpExecuteRequest executeRequest = requestCaptor.getValue();
99+
assertThat(executeRequest.httpRequest().encodedPath()).isEqualTo("/latest/api/token");
100+
assertThat(executeRequest.httpRequest().firstMatchingHeader("x-aws-ec2-metadata-token-ttl-seconds").get())
101+
.isEqualTo("21600");
68102
}
69103

70104
@Test

0 commit comments

Comments
 (0)