diff --git a/tests/aws-cpp-sdk-s3-crt-integration-tests/BucketAndObjectOperationTest.cpp b/tests/aws-cpp-sdk-s3-crt-integration-tests/BucketAndObjectOperationTest.cpp index 9f1c56490ec..3230fe83840 100644 --- a/tests/aws-cpp-sdk-s3-crt-integration-tests/BucketAndObjectOperationTest.cpp +++ b/tests/aws-cpp-sdk-s3-crt-integration-tests/BucketAndObjectOperationTest.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -44,6 +45,7 @@ #include #include #include + #include #ifdef _WIN32 @@ -88,9 +90,6 @@ namespace //to get around this, this string is url encoded version of "TestUnicode中国Key". At test time, we'll convert it to the unicode string static const char* URLENCODED_UNICODE_KEY = "TestUnicode%E4%B8%AD%E5%9B%BDKey"; static const char* URIESCAPE_KEY = "Esc a=pe+Me$"; - static const char* TEST_BUCKET_TAG = "IntegrationTestResource"; - - static const int TIMEOUT_MAX = 20; void AppendUUID(std::string& bucketName) { @@ -139,10 +138,8 @@ namespace public: static std::shared_ptr Client; static std::shared_ptr oregonClient; - static std::shared_ptr m_HttpClient; protected: - static void SetUpTestCase() { EnsureUniqueBucketNames(); @@ -150,24 +147,23 @@ namespace static void TearDownTestCase() { - DeleteBucket(CalculateBucketName(BASE_CREATE_BUCKET_TEST_NAME.c_str())); - DeleteBucket(CalculateBucketName(BASE_DNS_UNFRIENDLY_TEST_NAME.c_str())); - DeleteBucket(CalculateBucketName(BASE_LOCATION_BUCKET_TEST_NAME.c_str())); - DeleteBucket(CalculateBucketName(BASE_OBJECTS_BUCKET_NAME.c_str())); - DeleteBucket(CalculateBucketName(BASE_OBJECTS_DEFAULT_CTOR_BUCKET_NAME.c_str())); - DeleteBucket(CalculateBucketName(BASE_PUT_OBJECTS_BUCKET_NAME.c_str())); - DeleteBucket(CalculateBucketName(BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str())); - DeleteBucket(CalculateBucketName(BASE_PUT_MULTIPART_BUCKET_NAME.c_str())); - DeleteBucket(CalculateBucketName(BASE_ERRORS_TESTING_BUCKET.c_str())); - DeleteBucket(CalculateBucketName(BASE_PUT_WEIRD_CHARSETS_OBJECTS_BUCKET_NAME.c_str())); - DeleteBucket(CalculateBucketName(BASE_EVENT_STREAM_TEST_BUCKET_NAME.c_str())); - DeleteBucket(CalculateBucketName(BASE_EVENT_STREAM_LARGE_FILE_TEST_BUCKET_NAME.c_str())); - DeleteBucket(CalculateBucketName(BASE_EVENT_STREAM_ERRORS_IN_EVENT_TEST_BUCKET_NAME.c_str())); - DeleteBucket(CalculateBucketName(BASE_CHECKSUMS_BUCKET_NAME.c_str())); + Testing::S3TestUtils::DeleteBucket(Client, CalculateBucketName(BASE_CREATE_BUCKET_TEST_NAME.c_str())); + Testing::S3TestUtils::DeleteBucket(Client, CalculateBucketName(BASE_DNS_UNFRIENDLY_TEST_NAME.c_str())); + Testing::S3TestUtils::DeleteBucket(Client, CalculateBucketName(BASE_LOCATION_BUCKET_TEST_NAME.c_str())); + Testing::S3TestUtils::DeleteBucket(Client, CalculateBucketName(BASE_OBJECTS_BUCKET_NAME.c_str())); + Testing::S3TestUtils::DeleteBucket(Client, CalculateBucketName(BASE_OBJECTS_DEFAULT_CTOR_BUCKET_NAME.c_str())); + Testing::S3TestUtils::DeleteBucket(Client, CalculateBucketName(BASE_PUT_OBJECTS_BUCKET_NAME.c_str())); + Testing::S3TestUtils::DeleteBucket(Client, CalculateBucketName(BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str())); + Testing::S3TestUtils::DeleteBucket(Client, CalculateBucketName(BASE_PUT_MULTIPART_BUCKET_NAME.c_str())); + Testing::S3TestUtils::DeleteBucket(Client, CalculateBucketName(BASE_ERRORS_TESTING_BUCKET.c_str())); + Testing::S3TestUtils::DeleteBucket(Client, CalculateBucketName(BASE_PUT_WEIRD_CHARSETS_OBJECTS_BUCKET_NAME.c_str())); + Testing::S3TestUtils::DeleteBucket(Client, CalculateBucketName(BASE_EVENT_STREAM_TEST_BUCKET_NAME.c_str())); + Testing::S3TestUtils::DeleteBucket(Client, CalculateBucketName(BASE_EVENT_STREAM_LARGE_FILE_TEST_BUCKET_NAME.c_str())); + Testing::S3TestUtils::DeleteBucket(Client, CalculateBucketName(BASE_EVENT_STREAM_ERRORS_IN_EVENT_TEST_BUCKET_NAME.c_str())); + Testing::S3TestUtils::DeleteBucket(Client, CalculateBucketName(BASE_CHECKSUMS_BUCKET_NAME.c_str())); Client = nullptr; oregonClient = nullptr; - m_HttpClient = nullptr; } // Create new S3 CRT Client for each test case, as multiple bucket is not supported yet. @@ -190,7 +186,6 @@ namespace oregonClient = Aws::MakeShared(ALLOCATION_TAG, Aws::MakeShared(ALLOCATION_TAG), s3ClientConfig, AWSAuthV4Signer::PayloadSigningPolicy::Never /*signPayloads*/, true /*useVirtualAddressing*/); - m_HttpClient = Aws::Http::CreateHttpClient(s3ClientConfig); } static std::shared_ptr CreateStreamForUploadPart(uint32_t sizeInMb, const char* partTag) @@ -247,196 +242,14 @@ namespace ASSERT_STREQ(ss.str().c_str(), outcome.GetResult().GetETag().c_str()); } - static void TagTestBucket(const Aws::String& bucketName, const std::shared_ptr& client) { - ASSERT_TRUE(!bucketName.empty()); - ASSERT_TRUE(client); - - PutBucketTaggingRequest taggingRequest; - taggingRequest.SetBucket(bucketName); - Tag tag; - tag.SetKey(TEST_BUCKET_TAG); - tag.SetValue(TEST_BUCKET_TAG); - Tagging tagging; - tagging.AddTagSet(tag); - taggingRequest.SetTagging(tagging); - - auto taggingOutcome = CallOperationWithUnconditionalRetry(client.get(), &Aws::S3Crt::S3CrtClient::PutBucketTagging, taggingRequest); - AWS_ASSERT_SUCCESS(taggingOutcome); - } - - static bool WaitForBucketToPropagate(const Aws::String& bucketName, const std::shared_ptr& client = Client) - { - unsigned timeoutCount = 0; - while (timeoutCount++ < TIMEOUT_MAX) - { - ListObjectsRequest listObjectsRequest; - listObjectsRequest.SetBucket(bucketName); - ListObjectsOutcome listObjectsOutcome = client->ListObjects(listObjectsRequest); - if (listObjectsOutcome.IsSuccess()) - { - return true; - } - - std::this_thread::sleep_for(std::chrono::seconds(10)); - } - - return false; - } - - static bool WaitForObjectToPropagate(const Aws::String& bucketName, const char* objectKey) - { - unsigned timeoutCount = 0; - while (timeoutCount++ < TIMEOUT_MAX) - { - GetObjectRequest getObjectRequest; - getObjectRequest.SetBucket(bucketName); - getObjectRequest.SetKey(objectKey); - GetObjectOutcome getObjectOutcome = Client->GetObject(getObjectRequest); - if (getObjectOutcome.IsSuccess()) - { - return true; - } - - std::this_thread::sleep_for(std::chrono::seconds(5)); - } - - return false; - } - - static void EmptyBucket(const Aws::String& bucketName) - { - ListObjectsRequest listObjectsRequest; - listObjectsRequest.SetBucket(bucketName); - - ListObjectsOutcome listObjectsOutcome = Client->ListObjects(listObjectsRequest); - - if (!listObjectsOutcome.IsSuccess()) - return; - - for (const auto& object : listObjectsOutcome.GetResult().GetContents()) - { - DeleteObjectRequest deleteObjectRequest; - deleteObjectRequest.SetBucket(bucketName); - deleteObjectRequest.SetKey(object.GetKey()); - Client->DeleteObject(deleteObjectRequest); - } - } - - static void WaitForBucketToEmpty(const Aws::String& bucketName) - { - ListObjectsRequest listObjectsRequest; - listObjectsRequest.SetBucket(bucketName); - - unsigned checkForObjectsCount = 0; - while (checkForObjectsCount++ < TIMEOUT_MAX) - { - ListObjectsOutcome listObjectsOutcome = Client->ListObjects(listObjectsRequest); - AWS_ASSERT_SUCCESS(listObjectsOutcome); - - if (listObjectsOutcome.GetResult().GetContents().size() > 0) - { - std::this_thread::sleep_for(std::chrono::seconds(5)); - } - else - { - break; - } - } - } - - static void DeleteBucket(const Aws::String& bucketName) - { - HeadBucketRequest headBucketRequest; - headBucketRequest.SetBucket(bucketName); - HeadBucketOutcome bucketOutcome = Client->HeadBucket(headBucketRequest); - - if (bucketOutcome.IsSuccess()) - { - EmptyBucket(bucketName); - WaitForBucketToEmpty(bucketName); - - DeleteBucketRequest deleteBucketRequest; - deleteBucketRequest.SetBucket(bucketName); - - auto deleteBucketOutcome = CallOperationWithUnconditionalRetry(Client.get(), &Aws::S3Crt::S3CrtClient::DeleteBucket, deleteBucketRequest); - AWS_ASSERT_SUCCESS(deleteBucketOutcome); - } - } - static Aws::String CalculateBucketName(const Aws::String& bucketPrefix) { - return Aws::Testing::GetAwsResourcePrefix() + bucketPrefix; + return Testing::S3TestUtils::CalculateBucketName(bucketPrefix); } - - static Aws::String PreparePresignedUrlTest() - { - Aws::String fullBucketName = CalculateBucketName(BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); - SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); - CreateBucketRequest createBucketRequest; - createBucketRequest.SetBucket(fullBucketName); - createBucketRequest.SetACL(BucketCannedACL::private_); - CreateBucketOutcome createBucketOutcome = Client->CreateBucket(createBucketRequest); - AWS_EXPECT_SUCCESS(createBucketOutcome); - const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); - EXPECT_TRUE(!createBucketResult.GetLocation().empty()); - EXPECT_TRUE(WaitForBucketToPropagate(fullBucketName)); - TagTestBucket(fullBucketName, Client); - return fullBucketName; - } - - static void DoPresignedUrlTest(const Aws::String& bucketName, std::shared_ptr& putRequest) - { - std::shared_ptr objectStream = Aws::MakeShared("BucketAndObjectOperationTest"); - *objectStream << "Test Object"; - objectStream->flush(); - - putRequest->AddContentBody(objectStream); - Aws::StringStream intConverter; - intConverter << objectStream->tellp(); - putRequest->SetContentLength(intConverter.str()); - putRequest->SetContentType("text/plain"); - std::shared_ptr putResponse = m_HttpClient->MakeRequest(putRequest); - - ASSERT_EQ(HttpResponseCode::OK, putResponse->GetResponseCode()); - - ASSERT_TRUE(WaitForObjectToPropagate(bucketName, TEST_OBJ_KEY)); - - // GetObject with presigned url - Aws::String presignedUrlGet = Client->GeneratePresignedUrl(bucketName, TEST_OBJ_KEY, HttpMethod::HTTP_GET); - std::shared_ptr getRequest = CreateHttpRequest(presignedUrlGet, HttpMethod::HTTP_GET, Aws::Utils::Stream::DefaultResponseStreamFactoryMethod); - std::shared_ptr getResponse = m_HttpClient->MakeRequest(getRequest); - - ASSERT_EQ(HttpResponseCode::OK, getResponse->GetResponseCode()); - Aws::StringStream ss; - ss << getResponse->GetResponseBody().rdbuf(); - ASSERT_STREQ("Test Object", ss.str().c_str()); - - Aws::S3Crt::Model::GetObjectRequest getObjectRequest; - getObjectRequest.WithBucket(bucketName).WithKey(TEST_OBJ_KEY); - auto outcome = Client->GetObject(getObjectRequest); - AWS_ASSERT_SUCCESS(outcome); - if (putRequest->HasHeader(Aws::S3Crt::SSEHeaders::SERVER_SIDE_ENCRYPTION)) - { - ASSERT_STREQ(Aws::S3Crt::Model::ServerSideEncryptionMapper::GetNameForServerSideEncryption(outcome.GetResult().GetServerSideEncryption()).c_str(), putRequest->GetHeaderValue(Aws::S3Crt::SSEHeaders::SERVER_SIDE_ENCRYPTION).c_str()); - } - } - - static void CleanUpPresignedUrlTest() - { - Aws::String fullBucketName = CalculateBucketName(BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); - SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); - Aws::String presignedUrlDelete = Client->GeneratePresignedUrl(fullBucketName, TEST_OBJ_KEY, HttpMethod::HTTP_DELETE); - std::shared_ptr deleteRequest = CreateHttpRequest(presignedUrlDelete, HttpMethod::HTTP_DELETE, Aws::Utils::Stream::DefaultResponseStreamFactoryMethod); - std::shared_ptr deleteResponse = m_HttpClient->MakeRequest(deleteRequest); - ASSERT_EQ(HttpResponseCode::NO_CONTENT, deleteResponse->GetResponseCode()); - WaitForBucketToEmpty(fullBucketName); - } - }; std::shared_ptr BucketAndObjectOperationTest::Client(nullptr); std::shared_ptr BucketAndObjectOperationTest::oregonClient(nullptr); - std::shared_ptr BucketAndObjectOperationTest::m_HttpClient(nullptr); TEST_F(BucketAndObjectOperationTest, TestBucketOperationsErrorWithMissingRequiredFields) { @@ -457,6 +270,7 @@ namespace { const Aws::String fullBucketName = CalculateBucketName(BASE_CREATE_BUCKET_TEST_NAME.c_str()); SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); + CreateBucketRequest createBucketRequest; createBucketRequest.SetBucket(fullBucketName); createBucketRequest.SetACL(BucketCannedACL::private_); @@ -465,8 +279,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_FALSE(createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); ListBucketsOutcome listBucketsOutcome = Client->ListBuckets(); AWS_ASSERT_SUCCESS(listBucketsOutcome); @@ -505,8 +319,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_FALSE(createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, oregonClient)); - TagTestBucket(fullBucketName, oregonClient); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(oregonClient, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(oregonClient, fullBucketName); GetBucketLocationRequest locationRequest; locationRequest.SetBucket(fullBucketName); @@ -533,8 +347,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_TRUE(!createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -557,8 +371,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_TRUE(!createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -574,7 +388,7 @@ namespace PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest); AWS_ASSERT_SUCCESS(putObjectOutcome); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, TEST_OBJ_KEY)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, TEST_OBJ_KEY)); GetObjectRequest getObjectRequest; getObjectRequest.SetBucket(fullBucketName); @@ -604,7 +418,7 @@ namespace DeleteObjectOutcome deleteObjectOutcome = Client->DeleteObject(deleteObjectRequest); AWS_ASSERT_SUCCESS(deleteObjectOutcome); - WaitForBucketToEmpty(fullBucketName); + Testing::S3TestUtils::WaitForBucketToEmpty(Client, fullBucketName); headObjectOutcome = Client->HeadObject(headObjectRequest); ASSERT_FALSE(headObjectOutcome.IsSuccess()); @@ -632,8 +446,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_TRUE(!createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -648,7 +462,7 @@ namespace PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest); AWS_ASSERT_SUCCESS(putObjectOutcome); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, TEST_OBJ_KEY)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, TEST_OBJ_KEY)); GetObjectRequest getObjectRequest; getObjectRequest.SetBucket(fullBucketName); @@ -677,7 +491,7 @@ namespace DeleteObjectOutcome deleteObjectOutcome = Client->DeleteObject(deleteObjectRequest); AWS_ASSERT_SUCCESS(deleteObjectOutcome); - WaitForBucketToEmpty(fullBucketName); + Testing::S3TestUtils::WaitForBucketToEmpty(Client, fullBucketName); headObjectOutcome = Client->HeadObject(headObjectRequest); ASSERT_FALSE(headObjectOutcome.IsSuccess()); @@ -695,8 +509,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_TRUE(!createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); // S3 CRT Client doesn't support this edge case right now. //test unicode @@ -715,7 +529,7 @@ namespace PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest); AWS_ASSERT_SUCCESS(putObjectOutcome); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, unicodekey.c_str())); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, unicodekey.c_str())); DeleteObjectRequest deleteObjectRequest; deleteObjectRequest.SetBucket(fullBucketName); @@ -738,7 +552,7 @@ namespace PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest); AWS_ASSERT_SUCCESS(putObjectOutcome); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, URIESCAPE_KEY)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, URIESCAPE_KEY)); DeleteObjectRequest deleteObjectRequest; deleteObjectRequest.SetBucket(fullBucketName); @@ -746,22 +560,22 @@ namespace DeleteObjectOutcome deleteObjectOutcome = Client->DeleteObject(deleteObjectRequest); AWS_ASSERT_SUCCESS(deleteObjectOutcome); } - WaitForBucketToEmpty(fullBucketName); + Testing::S3TestUtils::WaitForBucketToEmpty(Client, fullBucketName); } TEST_F(BucketAndObjectOperationTest, TestObjectOperationsWithPresignedUrls) { - Aws::String fullBucketName = PreparePresignedUrlTest(); + Aws::String fullBucketName = Testing::S3TestUtils::PreparePresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); Aws::String presignedUrlPut = Client->GeneratePresignedUrl(fullBucketName, TEST_OBJ_KEY, HttpMethod::HTTP_PUT); std::shared_ptr putRequest = CreateHttpRequest(presignedUrlPut, HttpMethod::HTTP_PUT, Aws::Utils::Stream::DefaultResponseStreamFactoryMethod); - DoPresignedUrlTest(fullBucketName, putRequest); - CleanUpPresignedUrlTest(); + Testing::S3TestUtils::DoPresignedUrlTest(&ServerSideEncryptionMapper::GetNameForServerSideEncryption, Client, fullBucketName, putRequest); + Testing::S3TestUtils::CleanUpPresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); } TEST_F(BucketAndObjectOperationTest, TestObjectOperationsWithPresignedUrlsAndCustomizedHeaders) { - Aws::String fullBucketName = PreparePresignedUrlTest(); + Aws::String fullBucketName = Testing::S3TestUtils::PreparePresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); Aws::Http::HeaderValueCollection collections; collections.emplace("TestKey1", "TestVal1"); @@ -773,25 +587,25 @@ namespace ASSERT_NE(presignedUrlPut.find("testkey2"), Aws::String::npos); putRequest->SetHeaderValue("TestKey1", "TestVal1"); putRequest->SetHeaderValue("TestKey2", "TestVal2"); - DoPresignedUrlTest(fullBucketName, putRequest); - CleanUpPresignedUrlTest(); + Testing::S3TestUtils::DoPresignedUrlTest(&ServerSideEncryptionMapper::GetNameForServerSideEncryption, Client, fullBucketName, putRequest); + Testing::S3TestUtils::CleanUpPresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); } TEST_F(BucketAndObjectOperationTest, TestObjectOperationsWithPresignedUrlsWithSSES3) { - Aws::String fullBucketName = PreparePresignedUrlTest(); + Aws::String fullBucketName = Testing::S3TestUtils::PreparePresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); Aws::String presignedUrlPut = Client->GeneratePresignedUrlWithSSES3(fullBucketName, TEST_OBJ_KEY, HttpMethod::HTTP_PUT); std::shared_ptr putRequest = CreateHttpRequest(presignedUrlPut, HttpMethod::HTTP_PUT, Aws::Utils::Stream::DefaultResponseStreamFactoryMethod); putRequest->SetHeaderValue(Aws::S3Crt::SSEHeaders::SERVER_SIDE_ENCRYPTION, Aws::S3Crt::Model::ServerSideEncryptionMapper::GetNameForServerSideEncryption(Aws::S3Crt::Model::ServerSideEncryption::AES256)); - DoPresignedUrlTest(fullBucketName, putRequest); - CleanUpPresignedUrlTest(); + Testing::S3TestUtils::DoPresignedUrlTest(&ServerSideEncryptionMapper::GetNameForServerSideEncryption, Client, fullBucketName, putRequest); + Testing::S3TestUtils::CleanUpPresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); } TEST_F(BucketAndObjectOperationTest, TestObjectOperationsWithPresignedUrlsWithSSES3AndCustomizedHeaders) { - Aws::String fullBucketName = PreparePresignedUrlTest(); + Aws::String fullBucketName = Testing::S3TestUtils::PreparePresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); Aws::Http::HeaderValueCollection collections; collections.emplace("TestKey1", "TestVal1"); @@ -805,26 +619,26 @@ namespace ASSERT_NE(presignedUrlPut.find("testkey2"), Aws::String::npos); putRequest->SetHeaderValue("TestKey1", "TestVal1"); putRequest->SetHeaderValue("TestKey2", "TestVal2"); - DoPresignedUrlTest(fullBucketName, putRequest); - CleanUpPresignedUrlTest(); + Testing::S3TestUtils::DoPresignedUrlTest(&ServerSideEncryptionMapper::GetNameForServerSideEncryption, Client, fullBucketName, putRequest); + Testing::S3TestUtils::CleanUpPresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); } TEST_F(BucketAndObjectOperationTest, TestObjectOperationsWithPresignedUrlsWithSSEKMS) { - Aws::String fullBucketName = PreparePresignedUrlTest(); + Aws::String fullBucketName = Testing::S3TestUtils::PreparePresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); Aws::String presignedUrlPut = Client->GeneratePresignedUrlWithSSEKMS(fullBucketName, TEST_OBJ_KEY, HttpMethod::HTTP_PUT); //Using default KMS key in this AWS account std::shared_ptr putRequest = CreateHttpRequest(presignedUrlPut, HttpMethod::HTTP_PUT, Aws::Utils::Stream::DefaultResponseStreamFactoryMethod); putRequest->SetHeaderValue(Aws::S3Crt::SSEHeaders::SERVER_SIDE_ENCRYPTION, Aws::S3Crt::Model::ServerSideEncryptionMapper::GetNameForServerSideEncryption(Aws::S3Crt::Model::ServerSideEncryption::aws_kms)); - DoPresignedUrlTest(fullBucketName, putRequest); - CleanUpPresignedUrlTest(); + Testing::S3TestUtils::DoPresignedUrlTest(&ServerSideEncryptionMapper::GetNameForServerSideEncryption, Client, fullBucketName, putRequest); + Testing::S3TestUtils::CleanUpPresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); } TEST_F(BucketAndObjectOperationTest, TestObjectOperationsWithPresignedUrlsWithSSEKMSAndCustomizedHeaders) { - Aws::String fullBucketName = PreparePresignedUrlTest(); + Aws::String fullBucketName = Testing::S3TestUtils::PreparePresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); Aws::Http::HeaderValueCollection collections; collections.emplace("TestKey1", "TestVal1"); @@ -838,8 +652,8 @@ namespace ASSERT_NE(presignedUrlPut.find("testkey2"), Aws::String::npos); putRequest->SetHeaderValue("TestKey1", "TestVal1"); putRequest->SetHeaderValue("TestKey2", "TestVal2"); - DoPresignedUrlTest(fullBucketName, putRequest); - CleanUpPresignedUrlTest(); + Testing::S3TestUtils::DoPresignedUrlTest(&ServerSideEncryptionMapper::GetNameForServerSideEncryption, Client, fullBucketName, putRequest); + Testing::S3TestUtils::CleanUpPresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); } TEST_F(BucketAndObjectOperationTest, TestThatErrorsParse) @@ -863,8 +677,8 @@ namespace CreateBucketOutcome createBucketOutcome = Client->CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); GetObjectRequest getObjectRequest; getObjectRequest.SetBucket(fullBucketName); @@ -882,8 +696,8 @@ namespace createBucketRequest.SetACL(BucketCannedACL::private_); CreateBucketOutcome createBucketOutcome = Client->CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -918,8 +732,8 @@ namespace createBucketRequest.SetACL(BucketCannedACL::private_); CreateBucketOutcome createBucketOutcome = Client->CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -952,8 +766,8 @@ namespace createBucketRequest.SetACL(BucketCannedACL::private_); CreateBucketOutcome createBucketOutcome = Client->CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); auto objectStream = Aws::MakeShared("BucketAndObjectOperationTest"); *objectStream << "Test Japanese & Chinese Unicode keys"; @@ -987,8 +801,8 @@ namespace createBucketRequest.SetACL(BucketCannedACL::private_); CreateBucketOutcome createBucketOutcome = Client->CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -1007,7 +821,7 @@ namespace PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest); AWS_ASSERT_SUCCESS(putObjectOutcome); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, TEST_EVENT_STREAM_OBJ_KEY)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, TEST_EVENT_STREAM_OBJ_KEY)); SelectObjectContentRequest selectObjectContentRequest; selectObjectContentRequest.SetBucket(fullBucketName); @@ -1067,8 +881,8 @@ namespace createBucketRequest.SetACL(BucketCannedACL::private_); CreateBucketOutcome createBucketOutcome = Client->CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName)); - TagTestBucket(fullBucketName); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -1087,7 +901,7 @@ namespace PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest); AWS_ASSERT_SUCCESS(putObjectOutcome); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, TEST_EVENT_STREAM_OBJ_KEY)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, TEST_EVENT_STREAM_OBJ_KEY)); SelectObjectContentRequest selectObjectContentRequest; selectObjectContentRequest.SetBucket(fullBucketName); @@ -1143,8 +957,8 @@ namespace createBucketRequest.SetACL(BucketCannedACL::private_); CreateBucketOutcome createBucketOutcome = Client->CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -1166,7 +980,7 @@ namespace PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest); AWS_ASSERT_SUCCESS(putObjectOutcome); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, TEST_EVENT_STREAM_OBJ_KEY)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, TEST_EVENT_STREAM_OBJ_KEY)); SelectObjectContentRequest selectObjectContentRequest; selectObjectContentRequest.SetBucket(fullBucketName); @@ -1249,8 +1063,8 @@ namespace createBucketRequest.SetACL(BucketCannedACL::private_); CreateBucketOutcome createBucketOutcome = Client->CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -1273,7 +1087,7 @@ namespace PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest); AWS_ASSERT_SUCCESS(putObjectOutcome); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, TEST_EVENT_STREAM_OBJ_KEY)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, TEST_EVENT_STREAM_OBJ_KEY)); SelectObjectContentRequest selectObjectContentRequest; selectObjectContentRequest.SetBucket(fullBucketName); @@ -1345,8 +1159,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_TRUE(!createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -1642,8 +1456,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_TRUE(!createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); diff --git a/tests/aws-cpp-sdk-s3-crt-integration-tests/CancelCrtRequestTest.cpp b/tests/aws-cpp-sdk-s3-crt-integration-tests/CancelCrtRequestTest.cpp index df80374d3f4..be2913d51cb 100644 --- a/tests/aws-cpp-sdk-s3-crt-integration-tests/CancelCrtRequestTest.cpp +++ b/tests/aws-cpp-sdk-s3-crt-integration-tests/CancelCrtRequestTest.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -46,7 +47,7 @@ class CancelCrtRequestTest : public ::testing::Test { configuration.region = "us-east-1"; m_client = Aws::MakeShared(ALLOCATION_TAG, configuration); - m_bucketName = CalculateBucketName(BUCKET_NAME); + m_bucketName = Testing::S3TestUtils::CalculateBucketName(BUCKET_NAME); SCOPED_TRACE(Aws::String("FullBucketName ") + m_bucketName); CreateBucketRequest createBucketRequest; createBucketRequest.SetBucket(m_bucketName); @@ -54,107 +55,15 @@ class CancelCrtRequestTest : public ::testing::Test { CreateBucketOutcome createBucketOutcome = m_client->CreateBucket(createBucketRequest); AWS_EXPECT_SUCCESS(createBucketOutcome); - EXPECT_TRUE(WaitForBucketToPropagate(m_bucketName)); - TagTestBucket(m_bucketName); + EXPECT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(m_client, m_bucketName)); + Testing::S3TestUtils::TagTestBucket(m_client, m_bucketName); } void TearDown() override { - DeleteBucket(m_bucketName); + Testing::S3TestUtils::DeleteBucket(m_client, m_bucketName); m_client.reset(); } - void EmptyBucket(const Aws::String& bucketName) { - ListObjectsRequest listObjectsRequest; - listObjectsRequest.SetBucket(bucketName); - - ListObjectsOutcome listObjectsOutcome = m_client->ListObjects(listObjectsRequest); - - if (!listObjectsOutcome.IsSuccess()) return; - - for (const auto& object : listObjectsOutcome.GetResult().GetContents()) { - DeleteObjectRequest deleteObjectRequest; - deleteObjectRequest.SetBucket(bucketName); - deleteObjectRequest.SetKey(object.GetKey()); - auto deleteOutcome = m_client->DeleteObject(deleteObjectRequest); - AWS_UNREFERENCED_PARAM(deleteOutcome); - } - } - - void WaitForBucketToEmpty(const Aws::String& bucketName) { - ListObjectsRequest listObjectsRequest; - listObjectsRequest.SetBucket(bucketName); - - unsigned checkForObjectsCount = 0; - static const int TIMEOUT_MAX = 20; - while (checkForObjectsCount++ < TIMEOUT_MAX) { - ListObjectsOutcome listObjectsOutcome = m_client->ListObjects(listObjectsRequest); - AWS_ASSERT_SUCCESS(listObjectsOutcome); - - if (!listObjectsOutcome.GetResult().GetContents().empty()) { - std::this_thread::sleep_for(std::chrono::seconds(5)); - } else { - break; - } - } - } - - void DeleteBucket(const Aws::String& bucketName) { - HeadBucketRequest headBucketRequest; - headBucketRequest.SetBucket(bucketName); - HeadBucketOutcome bucketOutcome = m_client->HeadBucket(headBucketRequest); - - if (bucketOutcome.IsSuccess()) { - EmptyBucket(bucketName); - WaitForBucketToEmpty(bucketName); - - DeleteBucketRequest deleteBucketRequest; - deleteBucketRequest.SetBucket(bucketName); - - auto deleteBucketOutcome = - CallOperationWithUnconditionalRetry(m_client.get(), &Aws::S3Crt::S3CrtClient::DeleteBucket, deleteBucketRequest); - AWS_ASSERT_SUCCESS(deleteBucketOutcome); - } - } - - bool WaitForBucketToPropagate(const Aws::String& bucketName) { - unsigned timeoutCount = 0; - static const int TIMEOUT_MAX = 20; - while (timeoutCount++ < TIMEOUT_MAX) { - ListObjectsRequest listObjectsRequest; - listObjectsRequest.SetBucket(bucketName); - ListObjectsOutcome listObjectsOutcome = m_client->ListObjects(listObjectsRequest); - if (listObjectsOutcome.IsSuccess()) { - return true; - } - - std::this_thread::sleep_for(std::chrono::seconds(10)); - } - - return false; - } - - void TagTestBucket(const Aws::String& bucketName) { - ASSERT_TRUE(!bucketName.empty()); - ASSERT_TRUE(m_client); - - PutBucketTaggingRequest taggingRequest; - taggingRequest.SetBucket(bucketName); - Tag tag; - static const char* TEST_BUCKET_TAG = "IntegrationTestResource"; - tag.SetKey(TEST_BUCKET_TAG); - tag.SetValue(TEST_BUCKET_TAG); - Tagging tagging; - tagging.AddTagSet(tag); - taggingRequest.SetTagging(tagging); - - auto taggingOutcome = CallOperationWithUnconditionalRetry(m_client.get(), &Aws::S3Crt::S3CrtClient::PutBucketTagging, taggingRequest); - AWS_ASSERT_SUCCESS(taggingOutcome); - } - - static Aws::String CalculateBucketName(const Aws::String& bucketPrefix) { return Aws::Testing::GetAwsResourcePrefix() + bucketPrefix; } - - static Aws::String randomString() { return StringUtils::ToLower(Aws::String(UUID::RandomUUID()).c_str()); } - std::shared_ptr m_client; Aws::String m_bucketName; }; diff --git a/tests/aws-cpp-sdk-s3-integration-tests/BucketAndObjectOperationTest.cpp b/tests/aws-cpp-sdk-s3-integration-tests/BucketAndObjectOperationTest.cpp index c4675b441f3..46ae26f8d78 100644 --- a/tests/aws-cpp-sdk-s3-integration-tests/BucketAndObjectOperationTest.cpp +++ b/tests/aws-cpp-sdk-s3-integration-tests/BucketAndObjectOperationTest.cpp @@ -5,13 +5,13 @@ #include #include +#include + #include #include #include #include #include -#include -#include #include #include #include @@ -102,7 +102,6 @@ namespace static const char* URLENCODED_UNICODE_KEY = "TestUnicode%E4%B8%AD%E5%9B%BDKey"; static const char* URIESCAPE_KEY = "Esc a=pe+Me$"; static const char* CUSTOM_ENDPOINT_OVERRIDE = "beta.example.com"; - static const char* TEST_BUCKET_TAG = "IntegrationTestResource"; static const int TIMEOUT_MAX = 20; @@ -157,7 +156,6 @@ namespace std::shared_ptr globalClient; std::shared_ptr oregonClient; std::shared_ptr retryClient; - std::shared_ptr ClientFactory; std::shared_ptr m_HttpClient; std::shared_ptr Limiter; Aws::UnorderedSet m_bucketsToDelete; @@ -215,7 +213,7 @@ namespace TestingMonitoringManager::CleanupTestingMonitoring(); for (const auto& bucketName : m_bucketsToDelete) { - DeleteBucket(bucketName); + Testing::S3TestUtils::DeleteBucket(Client, bucketName); } m_bucketsToDelete.clear(); Limiter = nullptr; @@ -280,67 +278,6 @@ namespace ASSERT_STREQ(ss.str().c_str(), outcome.GetResult().GetETag().c_str()); } - static void TagTestBucket(const Aws::String& bucketName, const std::shared_ptr& client) { - ASSERT_TRUE(!bucketName.empty()); - ASSERT_TRUE(client); - - PutBucketTaggingRequest taggingRequest; - taggingRequest.SetBucket(bucketName); - Tag tag; - tag.SetKey(TEST_BUCKET_TAG); - tag.SetValue(TEST_BUCKET_TAG); - Tagging tagging; - tagging.AddTagSet(tag); - taggingRequest.SetTagging(tagging); - - auto taggingOutcome = CallOperationWithUnconditionalRetry(client.get(), &Aws::S3::S3Client::PutBucketTagging, taggingRequest); - AWS_ASSERT_SUCCESS(taggingOutcome); - } - - bool WaitForBucketToPropagate(const Aws::String& bucketName, std::shared_ptr& client) - { - if (!client) - { - client = Client; - } - - unsigned timeoutCount = 0; - while (timeoutCount++ < TIMEOUT_MAX) - { - ListObjectsRequest listObjectsRequest; - listObjectsRequest.SetBucket(bucketName); - ListObjectsOutcome listObjectsOutcome = client->ListObjects(listObjectsRequest); - if (listObjectsOutcome.IsSuccess()) - { - return true; - } - - std::this_thread::sleep_for(std::chrono::seconds(10)); - } - - return false; - } - - bool WaitForObjectToPropagate(const Aws::String& bucketName, const char* objectKey) - { - unsigned timeoutCount = 0; - while (timeoutCount++ < TIMEOUT_MAX) - { - GetObjectRequest getObjectRequest; - getObjectRequest.SetBucket(bucketName); - getObjectRequest.SetKey(objectKey); - GetObjectOutcome getObjectOutcome = Client->GetObject(getObjectRequest); - if (getObjectOutcome.IsSuccess()) - { - return true; - } - - std::this_thread::sleep_for(std::chrono::seconds(5)); - } - - return false; - } - bool WaitForObjectWithSSECToPropagate(const Aws::String& bucketName, const char* objectKey, const ByteBuffer& sseKey) { unsigned timeoutCount = 0; @@ -377,147 +314,14 @@ namespace return createBucketOutcome; } - void EmptyBucket(const Aws::String& bucketName) - { - ListObjectsRequest listObjectsRequest; - listObjectsRequest.SetBucket(bucketName); - - ListObjectsOutcome listObjectsOutcome = Client->ListObjects(listObjectsRequest); - - if (!listObjectsOutcome.IsSuccess()) - return; - - for (const auto& object : listObjectsOutcome.GetResult().GetContents()) - { - DeleteObjectRequest deleteObjectRequest; - deleteObjectRequest.SetBucket(bucketName); - deleteObjectRequest.SetKey(object.GetKey()); - auto deleteObjectOutcome = Client->DeleteObject(deleteObjectRequest); - AWS_ASSERT_SUCCESS(deleteObjectOutcome); - } - } - - void WaitForBucketToEmpty(const Aws::String& bucketName) - { - ListObjectsRequest listObjectsRequest; - listObjectsRequest.SetBucket(bucketName); - - unsigned checkForObjectsCount = 0; - while (checkForObjectsCount++ < TIMEOUT_MAX) - { - ListObjectsOutcome listObjectsOutcome = Client->ListObjects(listObjectsRequest); - AWS_EXPECT_SUCCESS(listObjectsOutcome); - - if (listObjectsOutcome.GetResult().GetContents().size() > 0) - { - std::this_thread::sleep_for(std::chrono::seconds(5)); - } - else - { - break; - } - } - } - - void DeleteBucket(const Aws::String& bucketName) - { - HeadBucketRequest headBucketRequest; - headBucketRequest.SetBucket(bucketName); - HeadBucketOutcome bucketOutcome = Client->HeadBucket(headBucketRequest); - - if (bucketOutcome.IsSuccess()) - { - EmptyBucket(bucketName); - WaitForBucketToEmpty(bucketName); - - DeleteBucketRequest deleteBucketRequest; - deleteBucketRequest.SetBucket(bucketName); - - auto deleteBucketOutcome = CallOperationWithUnconditionalRetry(Client.get(), &Aws::S3::S3Client::DeleteBucket, deleteBucketRequest); - AWS_EXPECT_SUCCESS(deleteBucketOutcome); - } - } - static Aws::String CalculateBucketName(const Aws::String& bucketPrefix) { return Aws::Testing::GetAwsResourcePrefix() + bucketPrefix; } - Aws::String PreparePresignedUrlTest() - { - Aws::String fullBucketName = CalculateBucketName(BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); - CreateBucketRequest createBucketRequest; - createBucketRequest.SetBucket(fullBucketName); - createBucketRequest.SetACL(BucketCannedACL::private_); - CreateBucketOutcome createBucketOutcome = CreateBucket(createBucketRequest); - AWS_EXPECT_SUCCESS(createBucketOutcome); - const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); - EXPECT_TRUE(!createBucketResult.GetLocation().empty()); - EXPECT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); - return fullBucketName; - } - - void DoPresignedUrlTest(const Aws::String& bucketName, std::shared_ptr& putRequest) - { - std::shared_ptr objectStream = Aws::MakeShared("BucketAndObjectOperationTest"); - *objectStream << "Test Object"; - objectStream->flush(); - - putRequest->AddContentBody(objectStream); - Aws::StringStream intConverter; - intConverter << objectStream->tellp(); - putRequest->SetContentLength(intConverter.str()); - putRequest->SetContentType("text/plain"); - std::shared_ptr putResponse = m_HttpClient->MakeRequest(putRequest); - - ASSERT_EQ(HttpResponseCode::OK, putResponse->GetResponseCode()); - - ASSERT_TRUE(WaitForObjectToPropagate(bucketName, TEST_OBJ_KEY)); - - // GetObject with presigned url - Aws::String presignedUrlGet = Client->GeneratePresignedUrl(bucketName, TEST_OBJ_KEY, HttpMethod::HTTP_GET); - std::shared_ptr getRequest = CreateHttpRequest(presignedUrlGet, HttpMethod::HTTP_GET, Aws::Utils::Stream::DefaultResponseStreamFactoryMethod); - std::shared_ptr getResponse = m_HttpClient->MakeRequest(getRequest); - - ASSERT_EQ(HttpResponseCode::OK, getResponse->GetResponseCode()); - Aws::StringStream ss; - ss << getResponse->GetResponseBody().rdbuf(); - ASSERT_STREQ("Test Object", ss.str().c_str()); - - Aws::S3::Model::GetObjectRequest getObjectRequest; - getObjectRequest.WithBucket(bucketName).WithKey(TEST_OBJ_KEY); - auto outcome = Client->GetObject(getObjectRequest); - AWS_ASSERT_SUCCESS(outcome); - if (putRequest->HasHeader(Aws::S3::SSEHeaders::SERVER_SIDE_ENCRYPTION)) - { - ASSERT_STREQ(Aws::S3::Model::ServerSideEncryptionMapper::GetNameForServerSideEncryption(outcome.GetResult().GetServerSideEncryption()).c_str(), putRequest->GetHeaderValue(Aws::S3::SSEHeaders::SERVER_SIDE_ENCRYPTION).c_str()); - } - } - - void CleanUpPresignedUrlTest() - { - Aws::String fullBucketName = CalculateBucketName(BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); - Aws::String presignedUrlDelete = Client->GeneratePresignedUrl(fullBucketName, TEST_OBJ_KEY, HttpMethod::HTTP_DELETE); - std::shared_ptr deleteRequest = CreateHttpRequest(presignedUrlDelete, HttpMethod::HTTP_DELETE, Aws::Utils::Stream::DefaultResponseStreamFactoryMethod); - static const size_t RETRIES = 5; - size_t deleteAttempt = 0; - Aws::Http::HttpResponseCode deleteResponseCode = Aws::Http::HttpResponseCode::REQUEST_NOT_MADE; - while(deleteAttempt < RETRIES && deleteResponseCode == Aws::Http::HttpResponseCode::REQUEST_NOT_MADE) - { - deleteResponseCode = m_HttpClient->MakeRequest(deleteRequest)->GetResponseCode(); - deleteAttempt++; - if(deleteResponseCode == Aws::Http::HttpResponseCode::REQUEST_NOT_MADE) { - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - } - } - ASSERT_EQ(HttpResponseCode::NO_CONTENT, deleteResponseCode); - WaitForBucketToEmpty(fullBucketName); - } - void DoTestObjectOperationsWithPresignedUrlsWithSSEC(bool withCustomizedHeaders) { - const Aws::String fullBucketName = PreparePresignedUrlTest(); + const Aws::String fullBucketName = Testing::S3TestUtils::PreparePresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); std::shared_ptr objectStream = Aws::MakeShared("BucketAndObjectOperationTest"); *objectStream << "Test Object"; @@ -597,7 +401,7 @@ namespace ss << outcome.GetResult().GetBody().rdbuf(); ASSERT_STREQ("Test Object", ss.str().c_str()); ASSERT_EQ(outcome.GetResult().GetSSECustomerAlgorithm(), Aws::S3::Model::ServerSideEncryptionMapper::GetNameForServerSideEncryption(Aws::S3::Model::ServerSideEncryption::AES256)); - CleanUpPresignedUrlTest(); + Testing::S3TestUtils::CleanUpPresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); } void DoTestGetObjectWithBucketARN(const ClientConfiguration& config, const Aws::String& bucketArn, const Aws::String& expectedEndpoint, @@ -678,8 +482,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_TRUE(!createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -701,7 +505,7 @@ namespace ss << "\"" << HashingUtils::HexEncode(HashingUtils::CalculateMD5(*putObjectRequest.GetBody())) << "\""; ASSERT_STREQ(ss.str().c_str(), putObjectOutcome.GetResult().GetETag().c_str()); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, TEST_OBJ_KEY)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, TEST_OBJ_KEY)); GetObjectRequest getObjectRequest; getObjectRequest.SetBucket(fullBucketName); @@ -748,8 +552,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_FALSE(createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); ListBucketsOutcome listBucketsOutcome = Client->ListBuckets(); AWS_ASSERT_SUCCESS(listBucketsOutcome); @@ -788,8 +592,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_FALSE(createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, oregonClient)); - TagTestBucket(fullBucketName, oregonClient); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(oregonClient, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(oregonClient, fullBucketName); GetBucketLocationRequest locationRequest; locationRequest.SetBucket(fullBucketName); @@ -816,8 +620,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_TRUE(!createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -842,8 +646,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_TRUE(!createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); Aws::Vector objectKeysWithNewlineCharacter; objectKeysWithNewlineCharacter.push_back(Aws::String(TEST_NEWLINE_KEY) + "-\n-LF"); @@ -911,8 +715,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_TRUE(!createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -935,7 +739,7 @@ namespace ss << "\"" << HashingUtils::HexEncode(HashingUtils::CalculateMD5(*putObjectRequest.GetBody())) << "\""; ASSERT_STREQ(ss.str().c_str(), putObjectOutcome.GetResult().GetETag().c_str()); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, TEST_OBJ_KEY)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, TEST_OBJ_KEY)); GetObjectRequest getObjectAsyncRequest; getObjectAsyncRequest.WithBucket(fullBucketName).WithKey(TEST_OBJ_KEY); Aws::Utils::Threading::Semaphore sem(0, 1); @@ -981,7 +785,7 @@ namespace DeleteObjectOutcome deleteObjectOutcome = Client->DeleteObject(deleteObjectRequest); AWS_ASSERT_SUCCESS(deleteObjectOutcome); - WaitForBucketToEmpty(fullBucketName); + Testing::S3TestUtils::WaitForBucketToEmpty(Client, fullBucketName); headObjectOutcome = Client->HeadObject(headObjectRequest); ASSERT_FALSE(headObjectOutcome.IsSuccess()); @@ -999,8 +803,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_TRUE(!createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); //test unicode { @@ -1018,7 +822,7 @@ namespace PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest); AWS_ASSERT_SUCCESS(putObjectOutcome); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, unicodekey.c_str())); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, unicodekey.c_str())); DeleteObjectRequest deleteObjectRequest; deleteObjectRequest.SetBucket(fullBucketName); @@ -1041,7 +845,7 @@ namespace PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest); AWS_ASSERT_SUCCESS(putObjectOutcome); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, URIESCAPE_KEY)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, URIESCAPE_KEY)); DeleteObjectRequest deleteObjectRequest; deleteObjectRequest.SetBucket(fullBucketName); @@ -1050,22 +854,22 @@ namespace AWS_ASSERT_SUCCESS(deleteObjectOutcome); } - WaitForBucketToEmpty(fullBucketName); + Testing::S3TestUtils::WaitForBucketToEmpty(Client, fullBucketName); } TEST_F(BucketAndObjectOperationTest, TestObjectOperationsWithPresignedUrls) { - const Aws::String fullBucketName = PreparePresignedUrlTest(); + const Aws::String fullBucketName = Testing::S3TestUtils::PreparePresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); Aws::String presignedUrlPut = Client->GeneratePresignedUrl(fullBucketName, TEST_OBJ_KEY, HttpMethod::HTTP_PUT); std::shared_ptr putRequest = CreateHttpRequest(presignedUrlPut, HttpMethod::HTTP_PUT, Aws::Utils::Stream::DefaultResponseStreamFactoryMethod); - DoPresignedUrlTest(fullBucketName, putRequest); - CleanUpPresignedUrlTest(); + Testing::S3TestUtils::DoPresignedUrlTest(&ServerSideEncryptionMapper::GetNameForServerSideEncryption, Client, fullBucketName, putRequest); + Testing::S3TestUtils::CleanUpPresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); } TEST_F(BucketAndObjectOperationTest, TestObjectOperationsWithPresignedUrlsAndCustomizedHeaders) { - const Aws::String fullBucketName = PreparePresignedUrlTest(); + const Aws::String fullBucketName = Testing::S3TestUtils::PreparePresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); Aws::Http::HeaderValueCollection collections; collections.emplace("TestKey1", "TestVal1"); @@ -1077,25 +881,25 @@ namespace ASSERT_NE(presignedUrlPut.find("testkey2"), std::string::npos); putRequest->SetHeaderValue("TestKey1", "TestVal1"); putRequest->SetHeaderValue("TestKey2", "TestVal2"); - DoPresignedUrlTest(fullBucketName, putRequest); - CleanUpPresignedUrlTest(); + Testing::S3TestUtils::DoPresignedUrlTest(&ServerSideEncryptionMapper::GetNameForServerSideEncryption, Client, fullBucketName, putRequest); + Testing::S3TestUtils::CleanUpPresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); } TEST_F(BucketAndObjectOperationTest, TestObjectOperationsWithPresignedUrlsWithSSES3) { - const Aws::String fullBucketName = PreparePresignedUrlTest(); + const Aws::String fullBucketName = Testing::S3TestUtils::PreparePresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); Aws::String presignedUrlPut = Client->GeneratePresignedUrlWithSSES3(fullBucketName, TEST_OBJ_KEY, HttpMethod::HTTP_PUT); std::shared_ptr putRequest = CreateHttpRequest(presignedUrlPut, HttpMethod::HTTP_PUT, Aws::Utils::Stream::DefaultResponseStreamFactoryMethod); putRequest->SetHeaderValue(Aws::S3::SSEHeaders::SERVER_SIDE_ENCRYPTION, Aws::S3::Model::ServerSideEncryptionMapper::GetNameForServerSideEncryption(Aws::S3::Model::ServerSideEncryption::AES256)); - DoPresignedUrlTest(fullBucketName, putRequest); - CleanUpPresignedUrlTest(); + Testing::S3TestUtils::DoPresignedUrlTest(&ServerSideEncryptionMapper::GetNameForServerSideEncryption, Client, fullBucketName, putRequest); + Testing::S3TestUtils::CleanUpPresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); } TEST_F(BucketAndObjectOperationTest, TestObjectOperationsWithPresignedUrlsWithSSES3AndCustomizedHeaders) { - const Aws::String fullBucketName = PreparePresignedUrlTest(); + const Aws::String fullBucketName = Testing::S3TestUtils::PreparePresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); Aws::Http::HeaderValueCollection collections; collections.emplace("TestKey1", "TestVal1"); @@ -1109,26 +913,26 @@ namespace ASSERT_NE(presignedUrlPut.find("testkey2"), std::string::npos); putRequest->SetHeaderValue("TestKey1", "TestVal1"); putRequest->SetHeaderValue("TestKey2", "TestVal2"); - DoPresignedUrlTest(fullBucketName, putRequest); - CleanUpPresignedUrlTest(); + Testing::S3TestUtils::DoPresignedUrlTest(&ServerSideEncryptionMapper::GetNameForServerSideEncryption, Client, fullBucketName, putRequest); + Testing::S3TestUtils::CleanUpPresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); } TEST_F(BucketAndObjectOperationTest, TestObjectOperationsWithPresignedUrlsWithSSEKMS) { - const Aws::String fullBucketName = PreparePresignedUrlTest(); + const Aws::String fullBucketName = Testing::S3TestUtils::PreparePresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); Aws::String presignedUrlPut = Client->GeneratePresignedUrlWithSSEKMS(fullBucketName, TEST_OBJ_KEY, HttpMethod::HTTP_PUT); //Using default KMS key in this AWS account std::shared_ptr putRequest = CreateHttpRequest(presignedUrlPut, HttpMethod::HTTP_PUT, Aws::Utils::Stream::DefaultResponseStreamFactoryMethod); putRequest->SetHeaderValue(Aws::S3::SSEHeaders::SERVER_SIDE_ENCRYPTION, Aws::S3::Model::ServerSideEncryptionMapper::GetNameForServerSideEncryption(Aws::S3::Model::ServerSideEncryption::aws_kms)); - DoPresignedUrlTest(fullBucketName, putRequest); - CleanUpPresignedUrlTest(); + Testing::S3TestUtils::DoPresignedUrlTest(&ServerSideEncryptionMapper::GetNameForServerSideEncryption, Client, fullBucketName, putRequest); + Testing::S3TestUtils::CleanUpPresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); } TEST_F(BucketAndObjectOperationTest, TestObjectOperationsWithPresignedUrlsWithSSEKMSAndCustomizedHeaders) { - const Aws::String fullBucketName = PreparePresignedUrlTest(); + const Aws::String fullBucketName = Testing::S3TestUtils::PreparePresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); Aws::Http::HeaderValueCollection collections; collections.emplace("TestKey1", "TestVal1"); @@ -1142,8 +946,8 @@ namespace ASSERT_NE(presignedUrlPut.find("testkey2"), std::string::npos); putRequest->SetHeaderValue("TestKey1", "TestVal1"); putRequest->SetHeaderValue("TestKey2", "TestVal2"); - DoPresignedUrlTest(fullBucketName, putRequest); - CleanUpPresignedUrlTest(); + Testing::S3TestUtils::DoPresignedUrlTest(&ServerSideEncryptionMapper::GetNameForServerSideEncryption, Client, fullBucketName, putRequest); + Testing::S3TestUtils::CleanUpPresignedUrlTest(Client, BASE_PUT_OBJECTS_PRESIGNED_URLS_BUCKET_NAME.c_str()); } @@ -1171,8 +975,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_TRUE(!createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); CreateMultipartUploadRequest createMultipartUploadRequest; createMultipartUploadRequest.SetBucket(fullBucketName); @@ -1240,7 +1044,7 @@ namespace completeMultipartUploadRequest); AWS_ASSERT_SUCCESS(completeMultipartUploadOutcome); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, multipartKeyName)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, multipartKeyName)); GetObjectRequest getObjectRequest; getObjectRequest.SetBucket(fullBucketName); @@ -1406,8 +1210,8 @@ namespace CreateBucketOutcome createBucketOutcome = CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); GetObjectRequest getObjectRequest; getObjectRequest.SetBucket(fullBucketName); @@ -1430,8 +1234,8 @@ namespace createBucketRequest.SetACL(BucketCannedACL::private_); CreateBucketOutcome createBucketOutcome = CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -1466,8 +1270,8 @@ namespace createBucketRequest.SetACL(BucketCannedACL::private_); CreateBucketOutcome createBucketOutcome = CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -1496,8 +1300,8 @@ namespace createBucketRequest.SetACL(BucketCannedACL::private_); CreateBucketOutcome createBucketOutcome = CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); auto objectStream = Aws::MakeShared("BucketAndObjectOperationTest"); *objectStream << "Test Japanese & Chinese Unicode keys"; @@ -1532,8 +1336,8 @@ namespace createBucketRequest.SetObjectLockEnabledForBucket(true); CreateBucketOutcome createBucketOutcome = CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -1578,8 +1382,8 @@ namespace createBucketRequest.SetACL(BucketCannedACL::private_); CreateBucketOutcome createBucketOutcome = CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -1598,7 +1402,7 @@ namespace PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest); AWS_ASSERT_SUCCESS(putObjectOutcome); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, TEST_EVENT_STREAM_OBJ_KEY)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, TEST_EVENT_STREAM_OBJ_KEY)); SelectObjectContentRequest selectObjectContentRequest; selectObjectContentRequest.SetBucket(fullBucketName); @@ -1656,8 +1460,8 @@ namespace createBucketRequest.SetACL(BucketCannedACL::private_); CreateBucketOutcome createBucketOutcome = CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -1676,7 +1480,7 @@ namespace PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest); AWS_ASSERT_SUCCESS(putObjectOutcome); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, TEST_EVENT_STREAM_OBJ_KEY)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, TEST_EVENT_STREAM_OBJ_KEY)); SelectObjectContentRequest selectObjectContentRequest; selectObjectContentRequest.SetBucket(fullBucketName); @@ -1731,8 +1535,8 @@ namespace createBucketRequest.SetACL(BucketCannedACL::private_); CreateBucketOutcome createBucketOutcome = CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -1754,7 +1558,7 @@ namespace PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest); AWS_ASSERT_SUCCESS(putObjectOutcome); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, TEST_EVENT_STREAM_OBJ_KEY)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, TEST_EVENT_STREAM_OBJ_KEY)); SelectObjectContentRequest selectObjectContentRequest; selectObjectContentRequest.SetBucket(fullBucketName); @@ -1837,8 +1641,8 @@ namespace createBucketRequest.SetACL(BucketCannedACL::private_); CreateBucketOutcome createBucketOutcome = CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); @@ -1861,7 +1665,7 @@ namespace PutObjectOutcome putObjectOutcome = Client->PutObject(putObjectRequest); AWS_ASSERT_SUCCESS(putObjectOutcome); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, TEST_EVENT_STREAM_OBJ_KEY)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, TEST_EVENT_STREAM_OBJ_KEY)); SelectObjectContentRequest selectObjectContentRequest; selectObjectContentRequest.SetBucket(fullBucketName); @@ -1909,8 +1713,8 @@ namespace CreateBucketOutcome createBucketOutcome = CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - WaitForBucketToPropagate(fullBucketName, Client); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); // Checksums in request body using aws-chunked trailer std::shared_ptr objectStream = Aws::MakeShared(ALLOCATION_TAG); @@ -1992,8 +1796,8 @@ namespace CreateBucketOutcome createBucketOutcome = CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); CreateMultipartUploadRequest createMultipartUploadRequest; createMultipartUploadRequest.SetBucket(fullBucketName); @@ -2053,7 +1857,7 @@ namespace CompleteMultipartUploadOutcome completeMultipartUploadOutcome = Client->CompleteMultipartUpload(completeMultipartUploadRequest); AWS_ASSERT_SUCCESS(completeMultipartUploadOutcome); - ASSERT_TRUE(WaitForObjectToPropagate(fullBucketName, multipartKeyName)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(Client, fullBucketName, multipartKeyName)); GetObjectRequest getObjectRequest; getObjectRequest.SetBucket(fullBucketName); @@ -2080,8 +1884,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_TRUE(!createBucketResult.GetLocation().empty()); - WaitForBucketToPropagate(fullBucketName, oregonClient); - TagTestBucket(fullBucketName, oregonClient); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(oregonClient, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(oregonClient, fullBucketName); ListObjectsRequest listObjectsRequest; listObjectsRequest.SetBucket(fullBucketName); @@ -2421,8 +2225,8 @@ namespace AWS_ASSERT_SUCCESS(createBucketOutcome); const CreateBucketResult& createBucketResult = createBucketOutcome.GetResult(); ASSERT_TRUE(!createBucketResult.GetLocation().empty()); - ASSERT_TRUE(WaitForBucketToPropagate(fullBucketName, Client)); - TagTestBucket(fullBucketName, Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(Client, fullBucketName)); + Testing::S3TestUtils::TagTestBucket(Client, fullBucketName); PutObjectRequest putObjectRequest; putObjectRequest.SetBucket(fullBucketName); diff --git a/tests/aws-cpp-sdk-s3control-integration-tests/S3ControlTest.cpp b/tests/aws-cpp-sdk-s3control-integration-tests/S3ControlTest.cpp index e55715f1edd..196987435bf 100644 --- a/tests/aws-cpp-sdk-s3control-integration-tests/S3ControlTest.cpp +++ b/tests/aws-cpp-sdk-s3control-integration-tests/S3ControlTest.cpp @@ -6,6 +6,8 @@ #include #include +#include + #include #include #include @@ -64,7 +66,6 @@ namespace static const char PRESIGNED_URLS_BUCKET_NAME[] = "presignedaccesspointbucket"; static const char PRESIGNED_URLS_ACCESS_POINT[] = "presignedaccesspoint"; static const char TEST_OBJECT_KEY[] = "TestObjectKey"; - static const char TEST_BUCKET_TAG[] = "IntegrationTestResource"; static const int TIMEOUT_MAX = 20; class S3ControlTest : public ::testing::Test @@ -144,61 +145,6 @@ namespace return {configuration}; } - static void TagTestBucket(const Aws::String& bucketName, const S3::S3Client& client) { - ASSERT_TRUE(!bucketName.empty()); - - Aws::S3::Model::PutBucketTaggingRequest taggingRequest; - taggingRequest.SetBucket(bucketName); - Aws::S3::Model::Tag tag; - tag.SetKey(TEST_BUCKET_TAG); - tag.SetValue(TEST_BUCKET_TAG); - Aws::S3::Model::Tagging tagging; - tagging.AddTagSet(tag); - taggingRequest.SetTagging(tagging); - - auto taggingOutcome = CallOperationWithUnconditionalRetry(&client, &Aws::S3::S3Client::PutBucketTagging, taggingRequest); - AWS_ASSERT_SUCCESS(taggingOutcome); - } - - static bool WaitForBucketToPropagate(const Aws::String& bucketName, const S3::S3Client& client) - { - unsigned timeoutCount = 0; - while (timeoutCount++ < TIMEOUT_MAX) - { - S3::Model::ListObjectsRequest ListObjectsRequest; - ListObjectsRequest.SetBucket(bucketName); - auto listObjectsOutcome = client.ListObjects(ListObjectsRequest); - if (listObjectsOutcome.IsSuccess()) - { - return true; - } - - std::this_thread::sleep_for(std::chrono::seconds(10)); - } - - return false; - } - - static bool WaitForObjectToPropagate(const Aws::String& accessPointArn, const char* objectKey, const S3::S3Client& client) - { - unsigned timeoutCount = 0; - while (timeoutCount++ < TIMEOUT_MAX) - { - S3::Model::GetObjectRequest getObjectRequest; - getObjectRequest.SetBucket(accessPointArn); - getObjectRequest.SetKey(objectKey); - auto getObjectOutcome = client.GetObject(getObjectRequest); - if (getObjectOutcome.IsSuccess()) - { - return true; - } - - std::this_thread::sleep_for(std::chrono::seconds(5)); - } - - return false; - } - /** * Wait for S3-MRAP server-side asynchronous Operation completion * @@ -264,8 +210,8 @@ namespace createBucketRequest.SetCreateBucketConfiguration(bucketConfiguration); auto createBucketOutcome = m_s3Client.CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(bucketName, m_s3Client)); - TagTestBucket(bucketName, m_s3Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(m_s3Client, bucketName)); + Testing::S3TestUtils::TagTestBucket(m_s3Client, bucketName); CreateAccessPointRequest createAccessPointRequest; createAccessPointRequest.SetName(accessPointName); @@ -323,8 +269,8 @@ namespace createBucketRequest.SetCreateBucketConfiguration(bucketConfiguration); auto createBucketOutcome = s3Client.CreateBucket(createBucketRequest); AWS_ASSERT_SUCCESS(createBucketOutcome); - ASSERT_TRUE(WaitForBucketToPropagate(regionalBucketName, s3Client)); - TagTestBucket(regionalBucketName, s3Client); + ASSERT_TRUE(Testing::S3TestUtils::WaitForBucketToPropagate(s3Client, regionalBucketName)); + Testing::S3TestUtils::TagTestBucket(m_s3Client, regionalBucketName); S3Control::Model::Region regionalBucket; regionalBucket.SetBucket(regionalBucketName); @@ -397,7 +343,7 @@ namespace ASSERT_EQ(HttpResponseCode::OK, putResponse->GetResponseCode()); - ASSERT_TRUE(WaitForObjectToPropagate(accessPointArn, TEST_OBJECT_KEY, m_s3Client)); + ASSERT_TRUE(Testing::S3TestUtils::WaitForObjectToPropagate(m_s3Client, accessPointArn, TEST_OBJECT_KEY)); // GetObject with presigned url Aws::String presignedUrlGet = m_s3Client.GeneratePresignedUrl(accessPointArn, TEST_OBJECT_KEY, HttpMethod::HTTP_GET); diff --git a/tests/aws-cpp-sdk-transfer-tests/TransferTests.cpp b/tests/aws-cpp-sdk-transfer-tests/TransferTests.cpp index b16b330f632..487d3a2c47e 100644 --- a/tests/aws-cpp-sdk-transfer-tests/TransferTests.cpp +++ b/tests/aws-cpp-sdk-transfer-tests/TransferTests.cpp @@ -6,6 +6,8 @@ #include #include #include +#include + #include #include #include @@ -684,21 +686,7 @@ class TransferTests : public ::testing::TestWithParam static bool WaitForBucketToPropagate(const Aws::String& bucketName) { - unsigned timeoutCount = 0; - while (timeoutCount++ < WAIT_MAX_RETRIES) - { - ListObjectsRequest listObjectsRequest; - listObjectsRequest.SetBucket(bucketName); - ListObjectsOutcome listObjectsOutcome = m_s3Clients[TestType::Https]->ListObjects(listObjectsRequest); - if (listObjectsOutcome.IsSuccess()) - { - return true; - } - - std::this_thread::sleep_for(std::chrono::seconds(10)); - } - - return false; + return Aws::Testing::S3TestUtils::WaitForBucketToPropagate(m_s3Clients[TestType::Https], bucketName); } static bool WaitForObjectToPropagate(const Aws::String& bucketName, @@ -776,23 +764,7 @@ class TransferTests : public ::testing::TestWithParam static void WaitForBucketToEmpty(const Aws::String& bucketName) { - ListObjectsRequest listObjectsRequest; - listObjectsRequest.SetBucket(bucketName); - - unsigned checkForObjectsCount = 0; - while (checkForObjectsCount++ < WAIT_MAX_RETRIES) - { - ListObjectsOutcome listObjectsOutcome = m_s3Clients[TestType::Https]->ListObjects(listObjectsRequest); - - if (!listObjectsOutcome.IsSuccess() || listObjectsOutcome.GetResult().GetContents().size() > 0) - { - std::this_thread::sleep_for(std::chrono::seconds(5)); - } - else - { - break; - } - } + return Aws::Testing::S3TestUtils::WaitForBucketToEmpty(m_s3Clients[TestType::Https], bucketName); } static void DeleteBucket(const Aws::String& bucketName) diff --git a/tests/testing-resources/CMakeLists.txt b/tests/testing-resources/CMakeLists.txt index 0711dd14a8d..15ebac846d2 100644 --- a/tests/testing-resources/CMakeLists.txt +++ b/tests/testing-resources/CMakeLists.txt @@ -19,6 +19,7 @@ file(GLOB AWS_TESTING_HTTP_MOCKS_HEADERS "include/aws/testing/mocks/http/*.h") file(GLOB AWS_TESTING_CLIENT_MOCKS_HEADERS "include/aws/testing/mocks/aws/client/*.h") file(GLOB AWS_TESTING_MONITORING_MOCKS_HEADERS "include/aws/testing/mocks/monitoring/*.h") file(GLOB AWS_TESTING_PLATFORM_HEADERS "include/aws/testing/platform/*.h") +file(GLOB AWS_TESTING_S3_TEST_UTILS_HEADERS "include/aws/testing/s3-test-utils/*.h") file(GLOB AWS_TESTING_SOURCE "source/*.cpp") file(GLOB AWS_TESTING_EXTERNAL_SOURCE "source/external/*.cc") @@ -36,6 +37,7 @@ file(GLOB TestingResources_SHARED_SRC ${AWS_TESTING_CLIENT_MOCKS_HEADERS} ${AWS_TESTING_MONITORING_MOCKS_HEADERS} ${AWS_TESTING_PLATFORM_HEADERS} + ${AWS_TESTING_S3_TEST_UTILS_HEADERS} ${AWS_TESTING_SOURCE} ${AWS_TESTING_EXTERNAL_SOURCE} ${AWS_TESTING_GTEST_SOURCE} @@ -59,6 +61,7 @@ if(PLATFORM_WINDOWS) if(MSVC) source_group("Header Files\\aws\\testing" FILES ${AWS_TESTING_HEADERS}) source_group("Header Files\\aws\\testing\\platform" FILES ${AWS_TESTING_PLATFORM_HEADERS}) + source_group("Header Files\\aws\\testing\\s3-test-utils\\platform" FILES ${AWS_TESTING_S3_TEST_UTILS_HEADERS}) source_group("Header Files\\aws\\external" FILES ${AWS_TESTING_EXTERNAL_HEADERS}) source_group("Header Files\\aws\\external\\gtest" FILES ${AWS_TESTING_GTEST_HEADERS}) source_group("Header Files\\aws\\external\\gtest\\internal" FILES ${AWS_TESTING_GTEST_HEADERS_INTERNAL}) @@ -148,6 +151,7 @@ install (FILES ${AWS_TESTING_GTEST_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/aws install (FILES ${AWS_TESTING_GTEST_HEADERS_INTERNAL} DESTINATION ${INCLUDE_DIRECTORY}/aws/external/gtest/internal) install (FILES ${AWS_TESTING_GTEST_HEADERS_INTERNAL_CUSTOM} DESTINATION ${INCLUDE_DIRECTORY}/aws/external/gtest/internal/custom) install (FILES ${AWS_TESTING_PLATFORM_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/aws/testing/platform) +install (FILES ${AWS_TESTING_S3_TEST_UTILS_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/aws/testing/s3-test-utils) install (FILES ${AWS_TESTING_AUTH_MOCKS_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/aws/testing/mocks/aws/auth) install (FILES ${AWS_TESTING_CLIENT_MOCKS_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/aws/testing/mocks/aws/client) install (FILES ${AWS_TESTING_EVENT_MOCKS_HEADERS} DESTINATION ${INCLUDE_DIRECTORY}/aws/testing/mocks/event) diff --git a/tests/testing-resources/include/aws/testing/s3-test-utils/S3TestUtils.h b/tests/testing-resources/include/aws/testing/s3-test-utils/S3TestUtils.h new file mode 100644 index 00000000000..95f1a8995bd --- /dev/null +++ b/tests/testing-resources/include/aws/testing/s3-test-utils/S3TestUtils.h @@ -0,0 +1,286 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace Aws { +namespace Testing { +/** + * A helper template to get the Request and Outcome types given the generated Operation method. + * i.e. given Aws::S3::S3Client, will extract PutBucketTaggingRequest and PutBucketTagginOutcome types if used as + * GetOpTypes + * @tparam OperationFuncT a method of the generated Client + * + * Mainly needed only to allow to include this header at any arbitrary point in .cpp, not only after S3 model headers included + */ +template +struct GetOpTypes : GetOpTypes {}; + +template +struct GetOpTypes { + using RequestType = typename std::remove_const::type>::type; + using ReturnType = typename std::remove_const::type>::type; +}; + +/** + * A set of common test utilities for generated S3 Clients (legacy and CRT) used across different tests + */ +class S3TestUtils { + public: + template + static void TagTestBucket(const S3ClientT& client, const Aws::String& bucketName) { + ASSERT_TRUE(!bucketName.empty()); + using PutBucketTaggingRequest = typename GetOpTypes::RequestType; + PutBucketTaggingRequest taggingRequest; + taggingRequest.SetBucket(bucketName); + + using Tagging = typename std::remove_const::type>::type; + Tagging tagging; + using Tag = typename std::remove_const::type>::type; + Tag tag; + static const char* TEST_BUCKET_TAG = "IntegrationTestResource"; + tag.SetKey(TEST_BUCKET_TAG); + tag.SetValue(TEST_BUCKET_TAG); + + tagging.AddTagSet(tag); + taggingRequest.SetTagging(tagging); + + auto taggingOutcome = CallOperationWithUnconditionalRetry(&client, &S3ClientT::PutBucketTagging, taggingRequest); + AWS_ASSERT_SUCCESS(taggingOutcome); + } + + template + static void TagTestBucket(const std::shared_ptr& client, const Aws::String& bucketName) { + ASSERT_TRUE(client); + return TagTestBucket(*client.get(), bucketName); + } + + template + static bool WaitForBucketToPropagate(const S3ClientT& client, const Aws::String& bucketName) { + EXPECT_TRUE(!bucketName.empty()); + + static const int TIMEOUT_MAX = 20; + unsigned timeoutCount = 0; + using ListObjectsRequest = typename GetOpTypes::RequestType; + using ListObjectsOutcome = typename GetOpTypes::ReturnType; + while (timeoutCount++ < TIMEOUT_MAX) { + ListObjectsRequest listObjectsRequest; + listObjectsRequest.SetBucket(bucketName); + ListObjectsOutcome listObjectsOutcome = client.ListObjects(listObjectsRequest); + if (listObjectsOutcome.IsSuccess()) { + return true; + } + + std::this_thread::sleep_for(std::chrono::seconds(10)); + } + + return false; + } + + template + static bool WaitForBucketToPropagate(const std::shared_ptr& client, const Aws::String& bucketName) { + EXPECT_TRUE(client); + return WaitForBucketToPropagate(*client, bucketName); + } + + template + static bool WaitForObjectToPropagate(const S3ClientT& client, const Aws::String& bucketName, const char* objectKey) { + EXPECT_TRUE(!bucketName.empty()); + EXPECT_TRUE(objectKey); + + static const int TIMEOUT_MAX = 20; + unsigned timeoutCount = 0; + using GetObjectRequest = typename GetOpTypes::RequestType; + using GetObjectOutcome = typename GetOpTypes::ReturnType; + while (timeoutCount++ < TIMEOUT_MAX) { + GetObjectRequest getObjectRequest; + getObjectRequest.SetBucket(bucketName); + getObjectRequest.SetKey(objectKey); + GetObjectOutcome getObjectOutcome = client.GetObject(getObjectRequest); + if (getObjectOutcome.IsSuccess()) { + return true; + } + + std::this_thread::sleep_for(std::chrono::seconds(5)); + } + + return false; + } + + template + static bool WaitForObjectToPropagate(const std::shared_ptr& client, const Aws::String& bucketName, const char* objectKey) { + EXPECT_TRUE(client); + return WaitForObjectToPropagate(*client, bucketName, objectKey); + } + + template + static void EmptyBucket(const std::shared_ptr& client, const Aws::String& bucketName) { + using ListObjectsRequest = typename GetOpTypes::RequestType; + using ListObjectsOutcome = typename GetOpTypes::ReturnType; + ListObjectsRequest listObjectsRequest; + listObjectsRequest.SetBucket(bucketName); + + ListObjectsOutcome listObjectsOutcome = client->ListObjects(listObjectsRequest); + + if (!listObjectsOutcome.IsSuccess()) return; + + using DeleteObjectRequest = typename GetOpTypes::RequestType; + for (const auto& object : listObjectsOutcome.GetResult().GetContents()) { + DeleteObjectRequest deleteObjectRequest; + deleteObjectRequest.SetBucket(bucketName); + deleteObjectRequest.SetKey(object.GetKey()); + auto res = client->DeleteObject(deleteObjectRequest); + AWS_UNREFERENCED_PARAM(res); + } + } + + template + static void WaitForBucketToEmpty(const std::shared_ptr& client, const Aws::String& bucketName) { + using ListObjectsRequest = typename GetOpTypes::RequestType; + using ListObjectsOutcome = typename GetOpTypes::ReturnType; + ListObjectsRequest listObjectsRequest; + listObjectsRequest.SetBucket(bucketName); + + unsigned checkForObjectsCount = 0; + static const int TIMEOUT_MAX = 20; + while (checkForObjectsCount++ < TIMEOUT_MAX) { + ListObjectsOutcome listObjectsOutcome = client->ListObjects(listObjectsRequest); + AWS_ASSERT_SUCCESS(listObjectsOutcome); + + if (!listObjectsOutcome.GetResult().GetContents().empty()) { + std::this_thread::sleep_for(std::chrono::seconds(5)); + } else { + break; + } + } + } + + template + static void DeleteBucket(const std::shared_ptr& client, const Aws::String& bucketName) { + using HeadBucketRequest = typename GetOpTypes::RequestType; + HeadBucketRequest headBucketRequest; + headBucketRequest.SetBucket(bucketName); + auto bucketOutcome = client->HeadBucket(headBucketRequest); + + if (bucketOutcome.IsSuccess()) { + EmptyBucket(client, bucketName); + WaitForBucketToEmpty(client, bucketName); + + using DeleteBucketRequest = typename GetOpTypes::RequestType; + DeleteBucketRequest deleteBucketRequest; + deleteBucketRequest.SetBucket(bucketName); + + auto deleteBucketOutcome = CallOperationWithUnconditionalRetry(client.get(), &S3ClientT::DeleteBucket, deleteBucketRequest); + AWS_ASSERT_SUCCESS(deleteBucketOutcome); + } + } + + static Aws::String CalculateBucketName(const Aws::String& bucketPrefix) { return Aws::Testing::GetAwsResourcePrefix() + bucketPrefix; } + + template + static Aws::String PreparePresignedUrlTest(const std::shared_ptr& client, const Aws::String& bucketName) { + const Aws::String fullBucketName = CalculateBucketName(bucketName); + SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); + using CreateBucketRequest = typename GetOpTypes::RequestType; + CreateBucketRequest createBucketRequest; + createBucketRequest.SetBucket(fullBucketName); + // enum class BucketCannedACL; + using BucketCannedACL = + typename std::remove_const::type>::type; + createBucketRequest.SetACL(BucketCannedACL::private_); + auto createBucketOutcome = client->CreateBucket(createBucketRequest); + AWS_EXPECT_SUCCESS(createBucketOutcome); + const auto& createBucketResult = createBucketOutcome.GetResult(); + EXPECT_TRUE(!createBucketResult.GetLocation().empty()); + EXPECT_TRUE(WaitForBucketToPropagate(client, fullBucketName)); + TagTestBucket(client, fullBucketName); + return fullBucketName; + } + + template + static void DoPresignedUrlTest(GetNameForServerSideEncryptionFunc&& GetNameForServerSideEncryption, + const std::shared_ptr& client, const Aws::String& bucketName, + std::shared_ptr& putRequest) { + using namespace Aws::Http; + static const char* TEST_OBJ_KEY = "TestObjectKey"; + const std::shared_ptr httpClient = []() { + Aws::Client::ClientConfiguration clientConfig; + clientConfig.scheme = Scheme::HTTPS; + clientConfig.executor = Aws::MakeShared("DoPresignedUrlTest", 4); + clientConfig.region = Region::US_WEST_2; + clientConfig.useDualStack = true; + return Aws::Http::CreateHttpClient(clientConfig); + }(); /*IIFE*/ + + std::shared_ptr objectStream = Aws::MakeShared("BucketAndObjectOperationTest"); + *objectStream << "Test Object"; + objectStream->flush(); + + putRequest->AddContentBody(objectStream); + Aws::StringStream intConverter; + intConverter << objectStream->tellp(); + putRequest->SetContentLength(intConverter.str()); + putRequest->SetContentType("text/plain"); + std::shared_ptr putResponse = httpClient->MakeRequest(putRequest); + + ASSERT_EQ(Http::HttpResponseCode::OK, putResponse->GetResponseCode()); + + ASSERT_TRUE(WaitForObjectToPropagate(client, bucketName, TEST_OBJ_KEY)); + + // GetObject with presigned url + Aws::String presignedUrlGet = client->GeneratePresignedUrl(bucketName, TEST_OBJ_KEY, HttpMethod::HTTP_GET); + std::shared_ptr getRequest = + CreateHttpRequest(presignedUrlGet, HttpMethod::HTTP_GET, Aws::Utils::Stream::DefaultResponseStreamFactoryMethod); + std::shared_ptr getResponse = httpClient->MakeRequest(getRequest); + + ASSERT_EQ(Http::HttpResponseCode::OK, getResponse->GetResponseCode()); + Aws::StringStream ss; + ss << getResponse->GetResponseBody().rdbuf(); + ASSERT_STREQ("Test Object", ss.str().c_str()); + + using GetObjectRequest = typename GetOpTypes::RequestType; + GetObjectRequest getObjectRequest; + getObjectRequest.WithBucket(bucketName).WithKey(TEST_OBJ_KEY); + auto outcome = client->GetObject(getObjectRequest); + AWS_ASSERT_SUCCESS(outcome); + static const char SERVER_SIDE_ENCRYPTION[] = "x-amz-server-side-encryption"; + if (putRequest->HasHeader(SERVER_SIDE_ENCRYPTION)) { + ASSERT_STREQ(GetNameForServerSideEncryption(outcome.GetResult().GetServerSideEncryption()).c_str(), + putRequest->GetHeaderValue(SERVER_SIDE_ENCRYPTION).c_str()); + } + } + + template + static void CleanUpPresignedUrlTest(const std::shared_ptr& client, const Aws::String& bucketName) { + using namespace Aws::Http; + static const char* TEST_OBJ_KEY = "TestObjectKey"; + const std::shared_ptr httpClient = []() { + Aws::Client::ClientConfiguration clientConfig; + clientConfig.scheme = Scheme::HTTPS; + clientConfig.executor = Aws::MakeShared("DoPresignedUrlTest", 4); + clientConfig.region = Region::US_WEST_2; + clientConfig.useDualStack = true; + return Aws::Http::CreateHttpClient(clientConfig); + }(); /*IIFE*/ + + Aws::String fullBucketName = CalculateBucketName(bucketName); + SCOPED_TRACE(Aws::String("FullBucketName ") + fullBucketName); + Aws::String presignedUrlDelete = client->GeneratePresignedUrl(fullBucketName, TEST_OBJ_KEY, HttpMethod::HTTP_DELETE); + std::shared_ptr deleteRequest = + CreateHttpRequest(presignedUrlDelete, HttpMethod::HTTP_DELETE, Aws::Utils::Stream::DefaultResponseStreamFactoryMethod); + std::shared_ptr deleteResponse = httpClient->MakeRequest(deleteRequest); + ASSERT_EQ(HttpResponseCode::NO_CONTENT, deleteResponse->GetResponseCode()); + WaitForBucketToEmpty(client, fullBucketName); + } +}; +} // namespace Testing +} // namespace Aws