Skip to content

Commit 11a63dd

Browse files
authored
CPP: tests updates (#5625)
* adding mocks
1 parent 5e037ba commit 11a63dd

File tree

59 files changed

+1281
-47
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1281
-47
lines changed

cpp/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ RUN useradd -ms /bin/bash tests && \
5858

5959
USER tests
6060

61-
CMD ["python3", "/src/cpp/run_automated_tests.py "]
61+
CMD ["python3", "/src/cpp/run_automated_tests.py", "-23"]

cpp/README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ for more information.
6161
Build steps
6262
-----------
6363
To build the docker image, run the following command from the shell. This command must be run in
64-
the "aws-doc-sdk-examples" directory, the parent directory of "cpp", in order to access the resources folder.
64+
the "aws-doc-sdk-examples/cpp" directory.
6565

6666
.. code-block:: bash
6767
68-
docker build -f cpp/Dockerfile -t <container_tag> .
68+
docker build -t <container_tag> .
6969

7070
The following command will run the docker image, copying your AWS credentials.
7171

cpp/example_code/aurora/tests/aurora_gtests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
Aws::SDKOptions AwsDocTest::Aurora_GTests::s_options;
1212
std::unique_ptr<Aws::Client::ClientConfiguration> AwsDocTest::Aurora_GTests::s_clientConfig;
13-
static const char ALLOCATION_TAG[] = "RDS_GTEST";
13+
static const char ALLOCATION_TAG[] = "AURORA_GTEST";
1414

1515
void AwsDocTest::Aurora_GTests::SetUpTestSuite() {
1616
InitAPI(s_options);

cpp/example_code/autoscaling/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ target_compile_definitions(
123123
${CURRENT_TARGET}
124124
PUBLIC
125125
TESTING_BUILD
126+
SRC_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
126127
)
127128

128129
target_link_libraries(

cpp/example_code/autoscaling/tests/autoscaling_gtests.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
#include "autoscaling_gtests.h"
77
#include <fstream>
88
#include <aws/core/client/ClientConfiguration.h>
9+
#include <aws/testing/mocks/http/MockHttpClient.h>
10+
static const char ALLOCATION_TAG[] = "AUTOSCALING_GTEST";
911

1012
Aws::SDKOptions AwsDocTest::AutoScaling_GTests::s_options;
1113
std::unique_ptr<Aws::Client::ClientConfiguration> AwsDocTest::AutoScaling_GTests::s_clientConfig;
1214

1315
void AwsDocTest::AutoScaling_GTests::SetUpTestSuite() {
16+
s_options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Debug;
1417
InitAPI(s_options);
1518

1619
// s_clientConfig must be a pointer because the client config must be initialized
@@ -79,3 +82,39 @@ int AwsDocTest::MyStringBuffer::underflow() {
7982
return result;
8083
}
8184

85+
AwsDocTest::MockHTTP::MockHTTP() {
86+
mockHttpClient = Aws::MakeShared<MockHttpClient>(ALLOCATION_TAG);
87+
mockHttpClientFactory = Aws::MakeShared<MockHttpClientFactory>(ALLOCATION_TAG);
88+
mockHttpClientFactory->SetClient(mockHttpClient);
89+
SetHttpClientFactory(mockHttpClientFactory);
90+
requestTmp = CreateHttpRequest(Aws::Http::URI("https://test.com/"),
91+
Aws::Http::HttpMethod::HTTP_GET,
92+
Aws::Utils::Stream::DefaultResponseStreamFactoryMethod);
93+
}
94+
95+
AwsDocTest::MockHTTP::~MockHTTP() {
96+
Aws::Http::CleanupHttp();
97+
Aws::Http::InitHttp();
98+
}
99+
100+
bool AwsDocTest::MockHTTP::addResponseWithBody(const std::string &fileName,
101+
Aws::Http::HttpResponseCode httpResponseCode) {
102+
103+
std::string fullPath = std::string(SRC_DIR) + "/" + fileName;
104+
std::ifstream inStream(fullPath);
105+
if (inStream) {
106+
std::shared_ptr<Aws::Http::Standard::StandardHttpResponse> goodResponse = Aws::MakeShared<Aws::Http::Standard::StandardHttpResponse>(
107+
ALLOCATION_TAG, requestTmp);
108+
goodResponse->AddHeader("Content-Type", "text/xml");
109+
goodResponse->SetResponseCode(httpResponseCode);
110+
goodResponse->GetResponseBody() << inStream.rdbuf();
111+
mockHttpClient->AddResponseToReturn(goodResponse);
112+
return true;
113+
}
114+
115+
std::cerr << "MockHTTP::addResponseWithBody open file error '" << fullPath << "'."
116+
<< std::endl;
117+
118+
return false;
119+
}
120+

cpp/example_code/autoscaling/tests/autoscaling_gtests.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <memory>
1212
#include <gtest/gtest.h>
1313

14+
class MockHttpClient;
15+
16+
class MockHttpClientFactory;
17+
1418
namespace AwsDocTest {
1519

1620
class MyStringBuffer : public std::stringbuf {
@@ -38,7 +42,7 @@ namespace AwsDocTest {
3842

3943
private:
4044

41-
bool suppressStdOut();
45+
static bool suppressStdOut();
4246

4347
static Aws::SDKOptions s_options;
4448

@@ -48,6 +52,23 @@ namespace AwsDocTest {
4852
MyStringBuffer m_cinBuffer;
4953
std::streambuf *m_savedInBuffer = nullptr;
5054
};
55+
56+
class MockHTTP {
57+
public:
58+
MockHTTP();
59+
60+
virtual ~MockHTTP();
61+
62+
bool addResponseWithBody(const std::string &fileName,
63+
Aws::Http::HttpResponseCode httpResponseCode = Aws::Http::HttpResponseCode::OK);
64+
65+
private:
66+
67+
std::shared_ptr<MockHttpClient> mockHttpClient;
68+
std::shared_ptr<MockHttpClientFactory> mockHttpClientFactory;
69+
std::shared_ptr<Aws::Http::HttpRequest> requestTmp;
70+
}; // MockHTTP
71+
5172
} // AwsDocTest
5273

5374
#endif // AUTOSCALING_EXAMPLES_AUTOSCALING_GTESTS_H

cpp/example_code/autoscaling/tests/gtest_groups_and_instances_scenario.cpp

Lines changed: 167 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,172 @@ SPDX-License-Identifier: Apache-2.0
1717
#include "autoscaling_samples.h"
1818

1919
namespace AwsDocTest {
20+
extern const std::vector<std::string> RESPONSES;
21+
bool addHttpResponses(MockHTTP &mockHttp);
22+
23+
#if 0
24+
// Only run the un-mocked test in special cases because of its long execution time.
25+
// NOLINTNEXTLINE (readability-named-parameter)
26+
TEST_F(AutoScaling_GTests, groups_and_instances_scenario_2L_) {
27+
AddCommandLineResponses(RESPONSES);
28+
29+
bool result = AwsDoc::AutoScaling::groupsAndInstancesScenario(*s_clientConfig);
30+
ASSERT_TRUE(result);
31+
}
32+
#endif
2033
// NOLINTNEXTLINE (readability-named-parameter)
21-
TEST_F(AutoScaling_GTests, groups_and_instances_scenario_2_
22-
) {
23-
AddCommandLineResponses( {
24-
"n", // "Would you like to use an existing EC2 launch template (y/n)?"
25-
"integration_tests_template", // "Enter the name for a new EC2 launch template: "
26-
"integration_tests_group", // "Enter a name for the Amazon EC2 Auto Scaling group: "
27-
"1", // "Choose an Availability Zone: "
28-
"y", // "Do you want to collect metrics about the Amazon EC2 Auto Scaling during this demo (y/n)?"
29-
"", // "Press enter to continue:"
30-
"", // "Press enter to continue:"
31-
"1", // "Which EC2 instance do you want to stop?"
32-
"", // "Press enter to continue:"
33-
"1", // "Which metric would you like to view? "
34-
"n", // "Would you like to view another metric (y/n)? "
35-
"", // "Press enter to continue:"
36-
"y", // "Delete the EC2 Auto Scaling group 'integration_tests_group' (y/n)?"
37-
"y" // "Delete the EC2 launch template 'integration_tests_template' (y/n)?"
38-
}
39-
);
40-
41-
bool result = AwsDoc::AutoScaling::groupsAndInstancesScenario(*s_clientConfig);
42-
ASSERT_TRUE(result);
43-
}
34+
TEST_F(AutoScaling_GTests, groups_and_instances_scenario_3_) {
35+
AddCommandLineResponses(RESPONSES);
36+
MockHTTP mockHttp;
37+
bool result = addHttpResponses(mockHttp);
38+
ASSERT_TRUE(result) << preconditionError();
39+
40+
result = AwsDoc::AutoScaling::groupsAndInstancesScenario(*s_clientConfig);
41+
ASSERT_TRUE(result);
42+
}
43+
44+
const std::vector<std::string> RESPONSES = {
45+
"n", // "Would you like to use an existing EC2 launch template (y/n)?"
46+
"integration_tests_template", // "Enter the name for a new EC2 launch template: "
47+
"integration_tests_group", // "Enter a name for the Amazon EC2 Auto Scaling group: "
48+
"1", // "Choose an Availability Zone: "
49+
"y", // "Do you want to collect metrics about the Amazon EC2 Auto Scaling during this demo (y/n)?"
50+
"", // "Press enter to continue:"
51+
"", // "Press enter to continue:"
52+
"1", // "Which EC2 instance do you want to stop?"
53+
"", // "Press enter to continue:"
54+
"1", // "Which metric would you like to view? "
55+
"n", // "Would you like to view another metric (y/n)? "
56+
"", // "Press enter to continue:"
57+
"y", // "Delete the EC2 Auto Scaling group 'integration_tests_group' (y/n)?"
58+
"y" // "Delete the EC2 launch template 'integration_tests_template' (y/n)?"
59+
};
60+
61+
bool addHttpResponses(MockHTTP &mockHttp) {
62+
if (!mockHttp.addResponseWithBody(
63+
"mock_input/1-CreateLaunchTemplate.xml")) {
64+
return false;
65+
}
66+
if (!mockHttp.addResponseWithBody(
67+
"mock_input/2-DescribeAvailabilityZones.xml")) {
68+
return false;
69+
}
70+
if (!mockHttp.addResponseWithBody(
71+
"mock_input/3-CreateAutoScalingGroup.xml")) {
72+
return false;
73+
}
74+
if (!mockHttp.addResponseWithBody(
75+
"mock_input/4-DescribeAutoScalingGroups.xml")) {
76+
return false;
77+
}
78+
if (!mockHttp.addResponseWithBody(
79+
"mock_input/5-DescribeAutoScalingGroups.xml")) {
80+
return false;
81+
}
82+
if (!mockHttp.addResponseWithBody(
83+
"mock_input/6-DescribeAutoScalingInstances.xml")) {
84+
return false;
85+
}
86+
if (!mockHttp.addResponseWithBody(
87+
"mock_input/7-DescribeAutoScalingGroups.xml")) {
88+
return false;
89+
}
90+
if (!mockHttp.addResponseWithBody("mock_input/8-EnableMetricsCollection.xml")) {
91+
return false;
92+
}
93+
if (!mockHttp.addResponseWithBody(
94+
"mock_input/9-UpdateAutoScalingGroup.xml")) {
95+
return false;
96+
}
97+
if (!mockHttp.addResponseWithBody(
98+
"mock_input/10-DescribeAutoScalingGroups.xml")) {
99+
return false;
100+
}
101+
if (!mockHttp.addResponseWithBody(
102+
"mock_input/11-SetDesiredCapacity.xml")) {
103+
return false;
104+
}
105+
if (!mockHttp.addResponseWithBody(
106+
"mock_input/12-DescribeAutoScalingGroups.xml")) {
107+
return false;
108+
}
109+
if (!mockHttp.addResponseWithBody(
110+
"mock_input/13-DescribeAutoScalingGroups.xml")) {
111+
return false;
112+
}
113+
if (!mockHttp.addResponseWithBody(
114+
"mock_input/14-DescribeAutoScalingInstances.xml")) {
115+
return false;
116+
}
117+
if (!mockHttp.addResponseWithBody("mock_input/15-DescribeAutoScalingGroups.xml")) {
118+
return false;
119+
}
120+
if (!mockHttp.addResponseWithBody("mock_input/16-TerminateInstanceInAutoScalingGroup.xml")) {
121+
return false;
122+
}
123+
if (!mockHttp.addResponseWithBody("mock_input/17-DescribeAutoScalingGroups.xml")) {
124+
return false;
125+
}
126+
if (!mockHttp.addResponseWithBody(
127+
"mock_input/72-DescribeAutoScalingInstances.xml")) {
128+
return false;
129+
}
130+
if (!mockHttp.addResponseWithBody(
131+
"mock_input/73-DescribeAutoScalingGroups.xml")) {
132+
return false;
133+
}
134+
if (!mockHttp.addResponseWithBody(
135+
"mock_input/74-DescribeScalingActivities.xml")) {
136+
return false;
137+
}
138+
if (!mockHttp.addResponseWithBody("mock_input/75-ListMetrics.xml")) {
139+
return false;
140+
}
141+
if (!mockHttp.addResponseWithBody("mock_input/76-GetMetricStatistics.xml")) {
142+
return false;
143+
}
144+
if (!mockHttp.addResponseWithBody("mock_input/77-DisableMetricsCollection.xml")) {
145+
return false;
146+
}
147+
if (!mockHttp.addResponseWithBody("mock_input/78-UpdateAutoScalingGroup.xml")) {
148+
return false;
149+
}
150+
if (!mockHttp.addResponseWithBody("mock_input/79-DescribeAutoScalingGroups.xml")) {
151+
return false;
152+
}
153+
if (!mockHttp.addResponseWithBody("mock_input/80-TerminateInstanceInAutoScalingGroup.xml")) {
154+
return false;
155+
}
156+
if (!mockHttp.addResponseWithBody(
157+
"mock_input/81-TerminateInstanceInAutoScalingGroup.xml")) {
158+
return false;
159+
}
160+
if (!mockHttp.addResponseWithBody(
161+
"mock_input/82-DescribeAutoScalingGroups.xml")) {
162+
return false;
163+
}
164+
if (!mockHttp.addResponseWithBody(
165+
"mock_input/133-DescribeAutoScalingInstances.xml")) {
166+
return false;
167+
}
168+
if (!mockHttp.addResponseWithBody(
169+
"mock_input/134-DescribeAutoScalingGroups.xml")) {
170+
return false;
171+
}
172+
if (!mockHttp.addResponseWithBody(
173+
"mock_input/135-DescribeAutoScalingGroups.xml")) {
174+
return false;
175+
}
176+
if (!mockHttp.addResponseWithBody(
177+
"mock_input/136-DeleteAutoScalingGroup.xml")) {
178+
return false;
179+
}
180+
if (!mockHttp.addResponseWithBody(
181+
"mock_input/137-DeleteLaunchTemplate.xml")) {
182+
return false;
183+
}
184+
185+
return true;
186+
}
187+
44188
} // AwsDocTest
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Response>
3+
<Errors>
4+
<Error>
5+
<Code>InvalidLaunchTemplateName.AlreadyExistsException</Code>
6+
<Message>Launch template name already in use.</Message>
7+
</Error>
8+
</Errors>
9+
<RequestID>b8545979-2a83-4c70-bf83-1052a2ed4ecc</RequestID>
10+
</Response>
11+

0 commit comments

Comments
 (0)