Skip to content

Commit 8465e8f

Browse files
Sergey RyabininSergeyRyabinin
authored andcommitted
Integration test giving an example how to call dualstack endpoint
1 parent 34d0358 commit 8465e8f

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
6+
#include <aws/external/gtest.h>
7+
#include <aws/testing/mocks/http/MockHttpClient.h>
8+
9+
#include <aws/core/auth/AWSCredentials.h>
10+
#include <aws/core/http/standard/StandardHttpResponse.h>
11+
#include <aws/core/http/HttpClientFactory.h>
12+
13+
#include <aws/ec2/EC2Client.h>
14+
#include <aws/ec2/model/DescribeHostsRequest.h>
15+
16+
static const char* ALLOCATION_TAG = "AWSEC2DualStackTests";
17+
18+
/*
19+
* A test suite that shows how to use a dualstack endpoint with EC2
20+
*/
21+
class EC2DualStackTests : public ::testing::Test
22+
{
23+
};
24+
25+
26+
TEST_F(EC2DualStackTests, TestDualStackMocked)
27+
{
28+
std::shared_ptr<MockHttpClient> mockHttpClient;
29+
std::shared_ptr<MockHttpClientFactory> mockHttpClientFactory;
30+
mockHttpClient = Aws::MakeShared<MockHttpClient>(ALLOCATION_TAG);
31+
mockHttpClientFactory = Aws::MakeShared<MockHttpClientFactory>(ALLOCATION_TAG);
32+
mockHttpClientFactory->SetClient(mockHttpClient);
33+
SetHttpClientFactory(mockHttpClientFactory);
34+
35+
// mock service reply
36+
const Aws::String goodReply = R"(<DescribeHostsResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">)"
37+
R"(<requestId>baadf00d-1111-2222-3333-444444444444</requestId><hostSet/></DescribeHostsResponse>)";
38+
std::shared_ptr<Aws::Http::HttpRequest> requestTmp = CreateHttpRequest(Aws::Http::URI("dummy"), Aws::Http::HttpMethod::HTTP_GET, Aws::Utils::Stream::DefaultResponseStreamFactoryMethod);
39+
std::shared_ptr<Aws::Http::Standard::StandardHttpResponse> goodResponse =
40+
Aws::MakeShared<Aws::Http::Standard::StandardHttpResponse>(ALLOCATION_TAG, requestTmp);
41+
goodResponse->SetResponseCode(Aws::Http::HttpResponseCode::OK);
42+
goodResponse->GetResponseBody() << goodReply;
43+
mockHttpClient->AddResponseToReturn(goodResponse);
44+
45+
Aws::Client::ClientConfiguration clientConfig;
46+
clientConfig.region = "us-east-1";
47+
// Although SDK does not properly compute dualstack endpoints, let's keep this flag for future's backward compatibility
48+
clientConfig.useDualStack = true;
49+
// TODO: remove next with proper endpoint generation support
50+
// Use dualstack endpoint posted by EC2
51+
clientConfig.endpointOverride = "api.ec2.us-east-1.aws";
52+
Aws::Auth::AWSCredentials mockCreds("accessKey", "secretKey", "sessionToken");
53+
Aws::EC2::EC2Client ec2Client(mockCreds, clientConfig);
54+
55+
Aws::EC2::Model::DescribeHostsRequest request;
56+
Aws::EC2::Model::DescribeHostsOutcome response = ec2Client.DescribeHosts(request);
57+
ASSERT_TRUE(response.IsSuccess());
58+
59+
auto requestsMade = mockHttpClient->GetAllRequestsMade();
60+
ASSERT_EQ(1u, requestsMade.size());
61+
const Aws::Http::Standard::StandardHttpRequest requestMade = requestsMade[0];
62+
ASSERT_EQ("https://api.ec2.us-east-1.aws", requestMade.GetURIString());
63+
ASSERT_EQ("api.ec2.us-east-1.aws", requestMade.GetHeaderValue("host"));
64+
65+
mockHttpClient->Reset();
66+
mockHttpClient = nullptr;
67+
mockHttpClientFactory = nullptr;
68+
Aws::Http::CleanupHttp();
69+
Aws::Http::InitHttp();
70+
}
71+
72+
TEST_F(EC2DualStackTests, TestDualStackEndpoint)
73+
{
74+
Aws::Client::ClientConfiguration clientConfig;
75+
clientConfig.region = "us-east-1";
76+
// Although SDK does not properly compute dualstack endpoints, let's keep this flag for future's backward compatibility
77+
clientConfig.useDualStack = true;
78+
// TODO: remove next with proper endpoint generation support
79+
// Use dualstack endpoint posted by EC2
80+
clientConfig.endpointOverride = "api.ec2.us-east-1.aws";
81+
82+
Aws::EC2::EC2Client ec2Client(clientConfig);
83+
84+
Aws::EC2::Model::DescribeHostsRequest request;
85+
Aws::EC2::Model::DescribeHostsOutcome response = ec2Client.DescribeHosts(request);
86+
87+
ASSERT_TRUE(response.IsSuccess());
88+
}

0 commit comments

Comments
 (0)