s3_file_uploader_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 
17 
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 
23 #include <aws/core/Aws.h>
24 #include <aws/s3/S3Client.h>
25 #include <ros/ros.h>
26 #include <ros/console.h>
27 #include <ros/callback_queue.h>
28 
30 #include <file_uploader_msgs/UploadFilesAction.h>
33 #include <s3_common/utils.h>
34 
35 using namespace Aws::S3;
36 
38 
39 using ::testing::Return;
40 using ::testing::_;
41 using ::testing::ContainerEq;
42 using ::testing::Invoke;
43 
44 class S3UploaderTest : public ::testing::Test
45 {
46 protected:
47  file_uploader_msgs::UploadFilesGoal goal;
51  std::unique_ptr<MockS3UploadManager> upload_manager;
53  Model::PutObjectOutcome successful_outcome;
54 
55  void TearDown() override
56  {
57  executor.stop();
58  }
59 
60 public:
62  executor(0), nh("~"), action_client(nh, "UploadFiles"),
63  upload_manager(std::make_unique<MockS3UploadManager>()),
64  successful_outcome(Model::PutObjectResult())
65  {
66  executor.start();
67  }
68 
70  bool message_received = false;
71  // Wait 10 seconds for server to start
72  bool client_connected = action_client.waitForActionServerToStart(ros::Duration(10, 0));
73  ASSERT_TRUE(client_connected);
74 
75  auto transition_call_back = [&](UploadFilesActionClient::GoalHandle gh){
76  if (gh.getCommState() == actionlib::CommState::StateEnum::DONE){
77  EXPECT_EQ(goal_terminal_state, gh.getTerminalState().state_);
78  message_received = true;
79  }
80  };
81 
82  auto gh = action_client.sendGoal(goal, transition_call_back);
83  ros::Duration(1, 0).sleep();
84  ASSERT_TRUE(message_received);
85  goal_handle = gh;
86  }
87 };
88 
89 TEST_F(S3UploaderTest, TestActionSucceedsNoUploadManagerProvided)
90 {
91  S3FileUploader file_uploader;
92  SanityGoalCheck(actionlib::TerminalState::StateEnum::SUCCEEDED);
93 }
94 
95 TEST_F(S3UploaderTest, TestMultipleFilesActionSucceeds)
96 {
97  size_t files_count = 1000;
98  std::vector<UploadDescription> uploads(files_count);
99  goal.upload_location = "/my/upload/dir";
100  for (size_t i = 0; i < files_count; i++) {
101  goal.files.push_back("test_file" + i);
102  uploads.at(i) = {goal.files[i], GenerateObjectKey(goal.files[i], goal.upload_location)};
103  }
104 
105  EXPECT_CALL(*upload_manager, IsAvailable())
106  .WillRepeatedly(Return(true));
107  EXPECT_CALL(*upload_manager, UploadFiles(ContainerEq(uploads),_,_))
108  .WillOnce(Return(successful_outcome));
109 
110  S3FileUploader file_uploader(std::move(upload_manager));
111  SanityGoalCheck(actionlib::TerminalState::StateEnum::SUCCEEDED);
112 }
113 
115 {
116  EXPECT_CALL(*upload_manager, IsAvailable())
117  .WillRepeatedly(Return(true));
118 
119  // No parameters defined - should just exit
120  nh.deleteParam("spinner_thread_count");
121  nh.deleteParam("s3_bucket");
122  S3FileUploader file_uploader(std::move(upload_manager));
123  file_uploader.Spin();
124 
125  // Spinner thread count set, but no s3 bucket
126  nh.setParam("spinner_thread_count", 1);
127  file_uploader.Spin();
128 }
129 
130 int main(int argc, char** argv)
131 {
132  Aws::SDKOptions options;
133  Aws::InitAPI(options);
134  ::testing::InitGoogleTest(&argc, argv);
135  ros::init(argc, argv, "test_node");
136  auto result = RUN_ALL_TESTS();
137  ros::shutdown();
138  Aws::ShutdownAPI(options);
139  return result;
140 }
std::unique_ptr< MockS3UploadManager > upload_manager
file_uploader_msgs::UploadFilesGoal goal
int main(int argc, char **argv)
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool waitForActionServerToStart(const ros::Duration &timeout=ros::Duration(0, 0))
Model::PutObjectOutcome successful_outcome
UploadFilesActionClient action_client
TEST_F(S3UploaderTest, TestActionSucceedsNoUploadManagerProvided)
GoalHandle sendGoal(const Goal &goal, TransitionCallback transition_cb=TransitionCallback(), FeedbackCallback feedback_cb=FeedbackCallback())
ros::AsyncSpinner executor
options
void SanityGoalCheck(actionlib::TerminalState::StateEnum goal_terminal_state)
UploadFilesActionClient::GoalHandle goal_handle
void TearDown() override
std::string GenerateObjectKey(const std::string &file_path, const std::string &prefix)
ROSCPP_DECL void shutdown()


s3_file_uploader
Author(s): AWS RoboMaker
autogenerated on Tue Jun 1 2021 02:51:30