|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.imds; |
| 17 | + |
| 18 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.get; |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.put; |
| 22 | +import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor; |
| 23 | +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
| 24 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; |
| 25 | +import static com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED; |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 28 | + |
| 29 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 30 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Rule; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.mockito.junit.MockitoJUnitRunner; |
| 36 | +import software.amazon.awssdk.core.SdkSystemSetting; |
| 37 | +import software.amazon.awssdk.core.exception.SdkClientException; |
| 38 | +import software.amazon.awssdk.http.SdkHttpClient; |
| 39 | +import software.amazon.awssdk.http.apache.ApacheHttpClient; |
| 40 | + |
| 41 | +/** |
| 42 | + * Unit Tests to test the Ec2Metadata Client functionality with Apache HttpClient. |
| 43 | + */ |
| 44 | +@RunWith(MockitoJUnitRunner.class) |
| 45 | +public class Ec2MetadataWithApacheClientTest { |
| 46 | + |
| 47 | + private static final String TOKEN_RESOURCE_PATH = "/latest/api/token"; |
| 48 | + |
| 49 | + private static final String EC2_METADATA_TOKEN_TTL_HEADER = "x-aws-ec2-metadata-token-ttl-seconds"; |
| 50 | + |
| 51 | + private static final String EC2_METADATA_ROOT = "/latest/meta-data"; |
| 52 | + |
| 53 | + private static final String AMI_ID_RESOURCE = EC2_METADATA_ROOT + "/ami-id"; |
| 54 | + |
| 55 | + private SdkHttpClient httpClient; |
| 56 | + |
| 57 | + @Rule |
| 58 | + public WireMockRule mockMetadataEndpoint = new WireMockRule(); |
| 59 | + |
| 60 | + @Before |
| 61 | + public void methodSetup() { |
| 62 | + System.setProperty(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT.property(), "http://localhost:" + mockMetadataEndpoint.port()); |
| 63 | + httpClient = ApacheHttpClient.create(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void get_failedThriceWith401() { |
| 68 | + |
| 69 | + stubFor(put(urlPathEqualTo(TOKEN_RESOURCE_PATH)).willReturn(aResponse().withBody("some-token"))); |
| 70 | + stubFor(get(urlPathEqualTo(AMI_ID_RESOURCE)).willReturn(aResponse().withBody("{}").withStatus(401))); |
| 71 | + |
| 72 | + assertThatThrownBy(() -> { |
| 73 | + Ec2Metadata ec2Metadata = |
| 74 | + Ec2Metadata.builder().httpClient(httpClient).build(); |
| 75 | + MetadataResponse metadataResponse = ec2Metadata.get("/latest/meta-data/ami-id"); |
| 76 | + }).hasMessageContaining("Exceeded maximum number of retries.") |
| 77 | + .isInstanceOf(SdkClientException.class); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void get_failedOnceWith401_shouldSucceedOnSecondAttempt() { |
| 82 | + |
| 83 | + stubFor(put(urlPathEqualTo(TOKEN_RESOURCE_PATH)).willReturn(aResponse().withBody("some-token"))); |
| 84 | + |
| 85 | + stubFor(get(urlPathEqualTo(AMI_ID_RESOURCE)).inScenario("Retry Scenario") |
| 86 | + .whenScenarioStateIs(STARTED) |
| 87 | + .willReturn(aResponse().withStatus(401)) |
| 88 | + .willSetStateTo("Cause Success")); |
| 89 | + |
| 90 | + stubFor(get(urlPathEqualTo(AMI_ID_RESOURCE)).inScenario("Retry Scenario") |
| 91 | + .whenScenarioStateIs("Cause Success") |
| 92 | + .willReturn(aResponse().withBody("{}"))); |
| 93 | + |
| 94 | + |
| 95 | + Ec2Metadata ec2Metadata = Ec2Metadata.builder().httpClient(httpClient).build(); |
| 96 | + MetadataResponse metadataResponse = ec2Metadata.get("/latest/meta-data/ami-id"); |
| 97 | + assertThat(metadataResponse.asString()).isEqualTo("{}"); |
| 98 | + |
| 99 | + WireMock.verify(putRequestedFor(urlPathEqualTo(TOKEN_RESOURCE_PATH)).withHeader(EC2_METADATA_TOKEN_TTL_HEADER, equalTo("21600"))); |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments