s3_facade_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 #include <cstdio>
16 #include <iostream>
17 #include <fstream>
18 #include <stdlib.h>
19 #include <string>
20 
21 #include <gtest/gtest.h>
22 #include <gmock/gmock.h>
23 
24 #include <aws/core/Aws.h>
25 #include <aws/s3/S3Client.h>
26 #include <aws/s3/model/PutObjectRequest.h>
27 #include <s3_common/s3_facade.h>
28 #include <s3_common/utils.h>
29 
30 using namespace Aws::S3;
31 
32 using ::testing::Return;
33 using ::testing::_;
34 
35 bool FileExists(const std::string& name)
36 {
37  std::ifstream ifile(name);
38  return ifile.good();
39 }
40 
41 class MockS3Client : public S3Client
42 {
43 public:
44  MockS3Client() = default;
45  MOCK_CONST_METHOD1(PutObject, Model::PutObjectOutcome(const Model::PutObjectRequest &));
46 };
47 
48 class S3FacadeTest : public ::testing::Test
49 {
50 protected:
51  bool enable_encryption = false;
52  std::unique_ptr<MockS3Client> client;
53  std::string upload_file;
54 
55  void SetUp() override {
56  client = std::make_unique<MockS3Client>();
57  char upload_file_path[] = "/tmp/S3FileUploadTestXXXXXX";
58  int fd = mkstemp(upload_file_path);
59  close(fd);
60  upload_file = std::string(upload_file_path);
61  }
62  void TearDown() override {
63  if (FileExists(upload_file)) {
64  remove(upload_file.c_str());
65  }
66  }
67 };
68 
69 TEST_F(S3FacadeTest, TestClientConfigConstructor)
70 {
71  Aws::Client::ClientConfiguration config;
72  S3Facade s3_facade(enable_encryption, config);
73  // No credentials configured, exepect upload to fail
74  auto outcome = s3_facade.PutObject(upload_file, "bucket", "key");
75  EXPECT_FALSE(outcome.IsSuccess());
76 }
77 
78 TEST_F(S3FacadeTest, TestPutObjectSuccess)
79 {
80  // Successful outcome
81  Model::PutObjectResult result;
82  Model::PutObjectOutcome outcome(result);
83  EXPECT_CALL(*client, PutObject(_))
84  .WillOnce(Return(outcome));
85  auto s3_facade = std::make_shared<S3Facade>(enable_encryption, std::move(client));
86  auto facade_outcome = s3_facade->PutObject(upload_file, "bucket", "key");
87 
88  EXPECT_TRUE(facade_outcome.IsSuccess());
89 }
90 
91 TEST_F(S3FacadeTest, TestPutObjectFileDoesntExist)
92 {
93  // Delete the file so that it doens't exist when trying to read.
94  // Note that there is some chance that another file with the generated name
95  // is created between deleting this file and trying to open it in PutObject.
96  remove(upload_file.c_str());
97  auto s3_facade = std::make_shared<S3Facade>(enable_encryption, std::move(client));
98 
99  auto outcome = s3_facade->PutObject(upload_file, "bucket", "key");
100  EXPECT_FALSE(outcome.IsSuccess());
101  EXPECT_EQ(S3Errors::INVALID_PARAMETER_VALUE, outcome.GetError().GetErrorType());
102 }
103 
104 TEST_F(S3FacadeTest, TestPutObjectFailureFromSDK)
105 {
106  Aws::Client::AWSError<S3Errors> error(S3Errors::INTERNAL_FAILURE, false);
107  Model::PutObjectOutcome outcome(error);
108  EXPECT_CALL(*client, PutObject(_))
109  .WillOnce(Return(outcome));
110  auto s3_facade = std::make_shared<S3Facade>(enable_encryption, std::move(client));
111 
112  auto facade_outcome = s3_facade->PutObject(upload_file, "bucket", "key");
113  EXPECT_FALSE(outcome.IsSuccess());
114  EXPECT_EQ(S3Errors::INTERNAL_FAILURE, facade_outcome.GetError().GetErrorType());
115 }
virtual Model::PutObjectOutcome PutObject(const std::string &file_path, const std::string &bucket, const std::string &key)
Call s3 PutObject api to upload file to s3.
Definition: s3_facade.cpp:47
void SetUp() override
std::string upload_file
std::unique_ptr< MockS3Client > client
bool FileExists(const std::string &name)
void TearDown() override
TEST_F(S3FacadeTest, TestClientConfigConstructor)


s3_common
Author(s): AWS RoboMaker
autogenerated on Tue Jun 1 2021 02:51:29