Skip to content

Commit

Permalink
CPP: tests updates (awsdocs#5625)
Browse files Browse the repository at this point in the history
* adding mocks
  • Loading branch information
meyertst-aws authored and max-webster committed Mar 15, 2024
1 parent ecf7c61 commit 0b20d17
Show file tree
Hide file tree
Showing 59 changed files with 1,281 additions and 47 deletions.
2 changes: 1 addition & 1 deletion cpp/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ RUN useradd -ms /bin/bash tests && \

USER tests

CMD ["python3", "/src/cpp/run_automated_tests.py "]
CMD ["python3", "/src/cpp/run_automated_tests.py", "-23"]
4 changes: 2 additions & 2 deletions cpp/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ for more information.
Build steps
-----------
To build the docker image, run the following command from the shell. This command must be run in
the "aws-doc-sdk-examples" directory, the parent directory of "cpp", in order to access the resources folder.
the "aws-doc-sdk-examples/cpp" directory.

.. code-block:: bash
docker build -f cpp/Dockerfile -t <container_tag> .
docker build -t <container_tag> .

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

Expand Down
2 changes: 1 addition & 1 deletion cpp/example_code/aurora/tests/aurora_gtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

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

void AwsDocTest::Aurora_GTests::SetUpTestSuite() {
InitAPI(s_options);
Expand Down
1 change: 1 addition & 0 deletions cpp/example_code/autoscaling/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ target_compile_definitions(
${CURRENT_TARGET}
PUBLIC
TESTING_BUILD
SRC_DIR="${CMAKE_CURRENT_SOURCE_DIR}"
)

target_link_libraries(
Expand Down
39 changes: 39 additions & 0 deletions cpp/example_code/autoscaling/tests/autoscaling_gtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
#include "autoscaling_gtests.h"
#include <fstream>
#include <aws/core/client/ClientConfiguration.h>
#include <aws/testing/mocks/http/MockHttpClient.h>
static const char ALLOCATION_TAG[] = "AUTOSCALING_GTEST";

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

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

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

AwsDocTest::MockHTTP::MockHTTP() {
mockHttpClient = Aws::MakeShared<MockHttpClient>(ALLOCATION_TAG);
mockHttpClientFactory = Aws::MakeShared<MockHttpClientFactory>(ALLOCATION_TAG);
mockHttpClientFactory->SetClient(mockHttpClient);
SetHttpClientFactory(mockHttpClientFactory);
requestTmp = CreateHttpRequest(Aws::Http::URI("https://test.com/"),
Aws::Http::HttpMethod::HTTP_GET,
Aws::Utils::Stream::DefaultResponseStreamFactoryMethod);
}

AwsDocTest::MockHTTP::~MockHTTP() {
Aws::Http::CleanupHttp();
Aws::Http::InitHttp();
}

bool AwsDocTest::MockHTTP::addResponseWithBody(const std::string &fileName,
Aws::Http::HttpResponseCode httpResponseCode) {

std::string fullPath = std::string(SRC_DIR) + "/" + fileName;
std::ifstream inStream(fullPath);
if (inStream) {
std::shared_ptr<Aws::Http::Standard::StandardHttpResponse> goodResponse = Aws::MakeShared<Aws::Http::Standard::StandardHttpResponse>(
ALLOCATION_TAG, requestTmp);
goodResponse->AddHeader("Content-Type", "text/xml");
goodResponse->SetResponseCode(httpResponseCode);
goodResponse->GetResponseBody() << inStream.rdbuf();
mockHttpClient->AddResponseToReturn(goodResponse);
return true;
}

std::cerr << "MockHTTP::addResponseWithBody open file error '" << fullPath << "'."
<< std::endl;

return false;
}

23 changes: 22 additions & 1 deletion cpp/example_code/autoscaling/tests/autoscaling_gtests.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include <memory>
#include <gtest/gtest.h>

class MockHttpClient;

class MockHttpClientFactory;

namespace AwsDocTest {

class MyStringBuffer : public std::stringbuf {
Expand Down Expand Up @@ -38,7 +42,7 @@ namespace AwsDocTest {

private:

bool suppressStdOut();
static bool suppressStdOut();

static Aws::SDKOptions s_options;

Expand All @@ -48,6 +52,23 @@ namespace AwsDocTest {
MyStringBuffer m_cinBuffer;
std::streambuf *m_savedInBuffer = nullptr;
};

class MockHTTP {
public:
MockHTTP();

virtual ~MockHTTP();

bool addResponseWithBody(const std::string &fileName,
Aws::Http::HttpResponseCode httpResponseCode = Aws::Http::HttpResponseCode::OK);

private:

std::shared_ptr<MockHttpClient> mockHttpClient;
std::shared_ptr<MockHttpClientFactory> mockHttpClientFactory;
std::shared_ptr<Aws::Http::HttpRequest> requestTmp;
}; // MockHTTP

} // AwsDocTest

#endif // AUTOSCALING_EXAMPLES_AUTOSCALING_GTESTS_H
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,172 @@ SPDX-License-Identifier: Apache-2.0
#include "autoscaling_samples.h"

namespace AwsDocTest {
extern const std::vector<std::string> RESPONSES;
bool addHttpResponses(MockHTTP &mockHttp);

#if 0
// Only run the un-mocked test in special cases because of its long execution time.
// NOLINTNEXTLINE (readability-named-parameter)
TEST_F(AutoScaling_GTests, groups_and_instances_scenario_2L_) {
AddCommandLineResponses(RESPONSES);

bool result = AwsDoc::AutoScaling::groupsAndInstancesScenario(*s_clientConfig);
ASSERT_TRUE(result);
}
#endif
// NOLINTNEXTLINE (readability-named-parameter)
TEST_F(AutoScaling_GTests, groups_and_instances_scenario_2_
) {
AddCommandLineResponses( {
"n", // "Would you like to use an existing EC2 launch template (y/n)?"
"integration_tests_template", // "Enter the name for a new EC2 launch template: "
"integration_tests_group", // "Enter a name for the Amazon EC2 Auto Scaling group: "
"1", // "Choose an Availability Zone: "
"y", // "Do you want to collect metrics about the Amazon EC2 Auto Scaling during this demo (y/n)?"
"", // "Press enter to continue:"
"", // "Press enter to continue:"
"1", // "Which EC2 instance do you want to stop?"
"", // "Press enter to continue:"
"1", // "Which metric would you like to view? "
"n", // "Would you like to view another metric (y/n)? "
"", // "Press enter to continue:"
"y", // "Delete the EC2 Auto Scaling group 'integration_tests_group' (y/n)?"
"y" // "Delete the EC2 launch template 'integration_tests_template' (y/n)?"
}
);

bool result = AwsDoc::AutoScaling::groupsAndInstancesScenario(*s_clientConfig);
ASSERT_TRUE(result);
}
TEST_F(AutoScaling_GTests, groups_and_instances_scenario_3_) {
AddCommandLineResponses(RESPONSES);
MockHTTP mockHttp;
bool result = addHttpResponses(mockHttp);
ASSERT_TRUE(result) << preconditionError();

result = AwsDoc::AutoScaling::groupsAndInstancesScenario(*s_clientConfig);
ASSERT_TRUE(result);
}

const std::vector<std::string> RESPONSES = {
"n", // "Would you like to use an existing EC2 launch template (y/n)?"
"integration_tests_template", // "Enter the name for a new EC2 launch template: "
"integration_tests_group", // "Enter a name for the Amazon EC2 Auto Scaling group: "
"1", // "Choose an Availability Zone: "
"y", // "Do you want to collect metrics about the Amazon EC2 Auto Scaling during this demo (y/n)?"
"", // "Press enter to continue:"
"", // "Press enter to continue:"
"1", // "Which EC2 instance do you want to stop?"
"", // "Press enter to continue:"
"1", // "Which metric would you like to view? "
"n", // "Would you like to view another metric (y/n)? "
"", // "Press enter to continue:"
"y", // "Delete the EC2 Auto Scaling group 'integration_tests_group' (y/n)?"
"y" // "Delete the EC2 launch template 'integration_tests_template' (y/n)?"
};

bool addHttpResponses(MockHTTP &mockHttp) {
if (!mockHttp.addResponseWithBody(
"mock_input/1-CreateLaunchTemplate.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/2-DescribeAvailabilityZones.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/3-CreateAutoScalingGroup.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/4-DescribeAutoScalingGroups.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/5-DescribeAutoScalingGroups.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/6-DescribeAutoScalingInstances.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/7-DescribeAutoScalingGroups.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody("mock_input/8-EnableMetricsCollection.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/9-UpdateAutoScalingGroup.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/10-DescribeAutoScalingGroups.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/11-SetDesiredCapacity.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/12-DescribeAutoScalingGroups.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/13-DescribeAutoScalingGroups.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/14-DescribeAutoScalingInstances.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody("mock_input/15-DescribeAutoScalingGroups.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody("mock_input/16-TerminateInstanceInAutoScalingGroup.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody("mock_input/17-DescribeAutoScalingGroups.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/72-DescribeAutoScalingInstances.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/73-DescribeAutoScalingGroups.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/74-DescribeScalingActivities.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody("mock_input/75-ListMetrics.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody("mock_input/76-GetMetricStatistics.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody("mock_input/77-DisableMetricsCollection.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody("mock_input/78-UpdateAutoScalingGroup.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody("mock_input/79-DescribeAutoScalingGroups.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody("mock_input/80-TerminateInstanceInAutoScalingGroup.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/81-TerminateInstanceInAutoScalingGroup.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/82-DescribeAutoScalingGroups.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/133-DescribeAutoScalingInstances.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/134-DescribeAutoScalingGroups.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/135-DescribeAutoScalingGroups.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/136-DeleteAutoScalingGroup.xml")) {
return false;
}
if (!mockHttp.addResponseWithBody(
"mock_input/137-DeleteLaunchTemplate.xml")) {
return false;
}

return true;
}

} // AwsDocTest
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Errors>
<Error>
<Code>InvalidLaunchTemplateName.AlreadyExistsException</Code>
<Message>Launch template name already in use.</Message>
</Error>
</Errors>
<RequestID>b8545979-2a83-4c70-bf83-1052a2ed4ecc</RequestID>
</Response>

Loading

0 comments on commit 0b20d17

Please sign in to comment.