diff options
| author | 彭宣正 <[email protected]> | 2021-05-26 11:10:59 +0800 |
|---|---|---|
| committer | 彭宣正 <[email protected]> | 2021-06-16 11:06:25 +0800 |
| commit | 678bb1c6f93c1bb72924d2bf5d29d500e24de465 (patch) | |
| tree | cecb92252afd7f47768c433317ab4726d6128f9c /src | |
| parent | 4c9a8f89c4f64f1300d221b6eaefa68a34a0a5fe (diff) | |
TSG-6704 增加mock
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 19 | ||||
| -rw-r--r-- | src/hos_client.cpp | 135 | ||||
| -rw-r--r-- | src/hos_common.h | 107 | ||||
| -rw-r--r-- | src/mock/hos_mock.cpp | 211 | ||||
| -rw-r--r-- | src/mock/hos_mock.h | 24 |
5 files changed, 391 insertions, 105 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6b2dbae1..557d66ca 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,7 +5,24 @@ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -shared -fPIC -std=c++11") include_directories(${CMAKE_INSTALL_PREFIX}/include/MESA) link_directories(${CMAKE_INSTALL_PREFIX}/lib) -add_library(${lib_name}_shared SHARED hos_client.cpp hos_hash.cpp) +option(HOS_MOCK "If enabled, the SDK will be built using a MOCK .cpp file for S3." OFF) + +file(GLOB HOS_HEADERS "*.h") +file(GLOB HOS_SOURCE "*.cpp") +if (HOS_MOCK) + add_definitions(-DHOS_MOCK) + file(GLOB HOS_MOCK_HEADERS "mock/hos_mock.h") + file(GLOB HOS_MOCK_SOURCE "mock/hos_mock.cpp") +endif() + +file(GLOB HOS_SRC + ${HOS_SOURCE} + ${HOS_HEADERS} + ${HOS_MOCK_SOURCE} + ${HOS_MOCK_HEADERS} +) + +add_library(${lib_name}_shared SHARED ${HOS_SRC}) target_link_libraries(${lib_name}_shared "-Wl,--whole-archive" libaws-c-common.a diff --git a/src/hos_client.cpp b/src/hos_client.cpp index 5132182d..ace52c9f 100644 --- a/src/hos_client.cpp +++ b/src/hos_client.cpp @@ -8,119 +8,32 @@ extern "C" #include <string.h> #include <sys/stat.h> #include <unistd.h> -#include <netinet/in.h> } -#include <aws/core/Aws.h> -#include <aws/s3/S3Client.h> #include <aws/s3/model/PutObjectRequest.h> #include <aws/s3/model/CreateBucketRequest.h> -#include <aws/core/auth/AWSCredentials.h> -#include <aws/core/utils/threading/Executor.h> #include <fstream> #include <iostream> -#include <mutex> #include <aws/external/gtest.h> #include <aws/testing/platform/PlatformTesting.h> #include <aws/testing/TestingEnvironment.h> #include <aws/testing/MemoryTesting.h> +#ifdef HOS_MOCK +#include "mock/hos_mock.h" +#endif #include "hos_client.h" -#include "hos_hash.h" -#include "field_stat2.h" #include "MESA_handle_logger.h" #include "MESA_prof_load.h" +#include "hos_common.h" -#if(__GNUC__ * 100 + __GNUC_MINOR__ * 10 + __GNUC_PATCHLEVEL__ >= 410) -#define atomic_add(x,y) __sync_add_and_fetch((x),(y)) -#define atomic_read(x) __sync_add_and_fetch((x),0) -#define atomic_sub(x,y) __sync_sub_and_fetch((x),(y)) -#else -#define atomic_add(x,y) ((*(x))+=(y)) -#define atomic_read(x) (*(x)) -#define atomic_sub(x,y) ((*(x))-=(y)) -#endif - -#define MAX_HOS_STRING_LEN 1024 -#define HOS_ERROR_MESSAGE_SIZE (MAX_HOS_STRING_LEN - 1) -#define MAX_HOS_CLIENT_FD_NUM 65535 -#define HOS_LOG_PATH "./tsglog/hoslog" - -typedef struct data_info_s -{ - size_t *tx_pkts; - size_t *tx_bytes; - size_t *rx_pkts; - size_t *rx_bytes; - size_t *tx_failed_pkts; - size_t *tx_failed_bytes; - size_t *cache; -}data_info_t; - -typedef struct fs2_info_s -{ - screen_stat_handle_t fs2_handle; - int *line_ids; - int *column_ids; - void *reserved; //预留给每个fs2 handle用来存储自定义的数据 -}fs2_info_t; - -enum -{ - FS2_DATA_FLOW_STATE = 0, - FS2_POOL_THREAD_STATE, - FS2_RECORD_EVENTS, -}; - -typedef struct hos_config_s -{ - char ip[INET6_ADDRSTRLEN]; - char fs2_ip[INET6_ADDRSTRLEN]; - char accesskeyid[MAX_HOS_STRING_LEN]; - char secretkey[MAX_HOS_STRING_LEN]; - char log_path[MAX_HOS_STRING_LEN]; - char fs2_path[MAX_HOS_STRING_LEN]; - - uint32_t port; - uint32_t fs2_port; - uint32_t fs2_fmt; - uint32_t log_level; - uint32_t pool_thread_size; - uint32_t thread_num; - uint32_t cache_size; - uint32_t cache_count; - uint32_t timeout; -}hos_config_t; - -typedef struct hos_func_thread_s -{ - /* fd 管理线程 */ - pthread_t fd_thread; - int fd_thread_status; - /* fs2 管理线程 */ - fs2_info_t fs2_info[FS2_RECORD_EVENTS]; //0: data info; 1: fd info; 2 cache info; 3 PoolThread state - pthread_t fs2_thread; - int fs2_status; -#define HOS_FS2_START 1 -#define HOS_FS2_STOP 2 -}hos_func_thread_t; - -typedef struct hos_client_handle_s -{ - Aws::S3::S3Client *S3Client; - Aws::Vector<Aws::S3::Model::Bucket> buckets; - std::shared_ptr<Aws::Utils::Threading::PooledThreadExecutor> executor; - size_t count; /* 记录了有多少个对象在使用hos */ - hos_config_t hos_config; - hos_func_thread_t hos_func; - void *log; -}hos_client_handle_t; - -static struct hos_instance_s g_hos_instance; -static hos_client_handle_t g_hos_handle;//一个进程只允许有一个g_hos_handle +struct hos_instance_s g_hos_instance; +hos_client_handle_t g_hos_handle;//一个进程只允许有一个g_hos_handle static std::mutex m_client_lock; -static hos_fd_context_t **g_fd_context; -static size_t (*g_fd_info)[MAX_HOS_CLIENT_FD_NUM + 1]; //fd 实际从3开始, fd[thread_id][0]记录register的fd,fd[thread_id][1]记录inject的fd +hos_fd_context_t **g_fd_context; +size_t (*g_fd_info)[MAX_HOS_CLIENT_FD_NUM + 1]; //fd 实际从3开始, fd[thread_id][0]记录register的fd,fd[thread_id][1]记录inject的fd static Aws::SDKOptions g_options; +static void *hos_fd_manage(void *ptr); + static inline size_t get_current_ms() { struct timespec timenow; @@ -268,7 +181,11 @@ static void hos_client_create() //同步模式 } + #ifndef HOS_MOCK g_hos_handle.S3Client = new Aws::S3::S3Client(credentials, config, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, false); + #else + g_hos_handle.S3Client = new Aws::S3::S3ClientMock(credentials, config, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, false); + #endif /* 获取当前用户的所有的buckets */ Aws::S3::Model::ListBucketsOutcome outcome = g_hos_handle.S3Client->ListBuckets(); @@ -571,7 +488,7 @@ static int hos_putobject_async(Aws::S3::Model::PutObjectRequest& request, size_t sprintf(buf, "%lu %lu %lu", thread_id, fd, stream_len); context->SetUUID(buf); - Aws::S3::S3Client& S3Client = *(g_hos_handle.S3Client); + auto &S3Client = *(g_hos_handle.S3Client); ret = S3Client.PutObjectAsync(request, PutObjectAsyncFinished, context); if (ret) { @@ -605,7 +522,7 @@ static int hos_putobject_sync(Aws::S3::Model::PutObjectRequest& request, size_t hos_func_thread_t *hos_func = &g_hos_handle.hos_func; data_info_t *data_info = NULL; - Aws::S3::S3Client& S3Client = *(g_hos_handle.S3Client); + auto& S3Client = *(g_hos_handle.S3Client); Aws::S3::Model::PutObjectOutcome Outcome = S3Client.PutObject(request); if (Outcome.IsSuccess()) { @@ -650,6 +567,7 @@ hos_instance hos_get_instance() g_hos_instance.result = true; return &g_hos_instance; } + memset(&g_hos_instance, 0, sizeof(g_hos_instance)); g_hos_instance.result = false; return &g_hos_instance; } @@ -742,7 +660,7 @@ int hos_create_bucket(const char *bucket) "error:bucket:%s, s3client:%s", bucket, g_hos_handle.S3Client?"not null":"null"); return HOS_PARAMETER_ERROR; } - Aws::S3::S3Client& S3Client = *g_hos_handle.S3Client; + auto& S3Client = *g_hos_handle.S3Client; /* 本地检查是否已经存在该bucket */ for (Aws::S3::Model::Bucket& new_bucket : g_hos_handle.buckets) @@ -777,7 +695,6 @@ int hos_create_bucket(const char *bucket) static int hos_upload_stream(const char *bucket, const char *object, const char *data, size_t data_len, put_finished_callback callback, void *userdata, size_t thread_id) { - char buf[128]; data_info_t *data_info = NULL; hos_config_t *hos_conf = &g_hos_handle.hos_config; hos_func_thread_t *hos_func = &g_hos_handle.hos_func; @@ -833,6 +750,15 @@ static int hos_upload_stream(const char *bucket, const char *object, const char hos_fd_context_t info = {fd, 0, (char *)bucket, (char *)object, (void *)callback, userdata, NULL, 0, 0, 0 }; add_fd_context(&g_fd_context[thread_id], &info); + { + std::lock_guard<std::mutex> locker(m_client_lock); + if (g_hos_handle.hos_func.fd_thread == 0) + { + g_hos_handle.hos_func.fd_thread_status = 0; + pthread_create(&g_hos_handle.hos_func.fd_thread, NULL, hos_fd_manage, NULL); + } + } + if (hos_conf->pool_thread_size > 0) { ret = hos_putobject_async(request, data_len, thread_id, fd, bucket, object); @@ -942,13 +868,14 @@ int hos_open_fd(const char *bucket, const char *object, put_finished_callback ca { MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, __FUNCTION__, "error:g_hos_instance.result:%d, g_hos_handle.S3CLient:%s", - g_hos_instance.result, (g_hos_handle.S3Client == NULL)?(NULL):("not null")); + g_hos_instance.result, (g_hos_handle.S3Client == NULL)?("null"):("not null")); return HOS_INSTANCE_NOT_INIT; } if ((bucket == NULL) || (object == NULL) || (thread_id > g_hos_handle.hos_config.thread_num) || strlen(bucket) == 0 || strlen(object) == 0) { MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, "hos_open_fd", - "bucket:%s, obejct:%s, thread_id:%s", + "bucket:%s, obejct:%s, thread_id:%d", + //(bucket == NULL)?"null":bucket, (object == NULL)?"null":object, thread_id); bucket, object, thread_id); return HOS_PARAMETER_ERROR; } @@ -1007,7 +934,7 @@ int hos_write(size_t fd, const char *stream, size_t stream_len, size_t thread_id if ((fd < 3) || fd > MAX_HOS_CLIENT_FD_NUM || (stream == NULL) || (thread_id > hos_conf->thread_num)) { MESA_handle_runtime_log(g_hos_handle.log, RLOG_LV_FATAL, - "hos_write", "error: fd:%d, stream:%s, stream_len:%s, thread_id:%d.", + "hos_write", "error: fd:%d, stream:%s, stream_len:%d, thread_id:%d.", fd, stream?"not null":"null", stream_len, thread_id); return HOS_PARAMETER_ERROR; } diff --git a/src/hos_common.h b/src/hos_common.h new file mode 100644 index 00000000..85d415e8 --- /dev/null +++ b/src/hos_common.h @@ -0,0 +1,107 @@ +#ifndef __HOS_COMMON_H__ +#define __HOS_COMMON_H__ + +#include <netinet/in.h> +#include <mutex> +#include "field_stat2.h" +#include "hos_hash.h" +#include <aws/core/Aws.h> +#include <aws/s3/S3Client.h> +#include <aws/core/auth/AWSCredentials.h> +#include <aws/core/utils/threading/Executor.h> + +#if(__GNUC__ * 100 + __GNUC_MINOR__ * 10 + __GNUC_PATCHLEVEL__ >= 410) +#define atomic_add(x,y) __sync_add_and_fetch((x),(y)) +#define atomic_read(x) __sync_add_and_fetch((x),0) +#define atomic_sub(x,y) __sync_sub_and_fetch((x),(y)) +#else +#define atomic_add(x,y) ((*(x))+=(y)) +#define atomic_read(x) (*(x)) +#define atomic_sub(x,y) ((*(x))-=(y)) +#endif + +#define MAX_HOS_STRING_LEN 1024 +#define HOS_ERROR_MESSAGE_SIZE (MAX_HOS_STRING_LEN - 1) +#define MAX_HOS_CLIENT_FD_NUM 65535 +#define HOS_LOG_PATH "./tsglog/hoslog" + +typedef struct data_info_s +{ + size_t *tx_pkts; + size_t *tx_bytes; + size_t *rx_pkts; + size_t *rx_bytes; + size_t *tx_failed_pkts; + size_t *tx_failed_bytes; + size_t *cache; +}data_info_t; + +typedef struct fs2_info_s +{ + screen_stat_handle_t fs2_handle; + int *line_ids; + int *column_ids; + void *reserved; //预留给每个fs2 handle用来存储自定义的数据 +}fs2_info_t; + +enum +{ + FS2_DATA_FLOW_STATE = 0, + FS2_POOL_THREAD_STATE, + FS2_RECORD_EVENTS, +}; + +typedef struct hos_config_s +{ + char ip[INET6_ADDRSTRLEN]; + char fs2_ip[INET6_ADDRSTRLEN]; + char accesskeyid[MAX_HOS_STRING_LEN]; + char secretkey[MAX_HOS_STRING_LEN]; + char log_path[MAX_HOS_STRING_LEN]; + char fs2_path[MAX_HOS_STRING_LEN]; + + uint32_t port; + uint32_t fs2_port; + uint32_t fs2_fmt; + uint32_t log_level; + uint32_t pool_thread_size; + uint32_t thread_num; + uint32_t cache_size; + uint32_t cache_count; + uint32_t timeout; +}hos_config_t; + +typedef struct hos_func_thread_s +{ + /* fd 管理线程 */ + pthread_t fd_thread; + int fd_thread_status; + /* fs2 管理线程 */ + fs2_info_t fs2_info[FS2_RECORD_EVENTS]; //0: data info; 1: fd info; 2 cache info; 3 PoolThread state + pthread_t fs2_thread; + int fs2_status; +#define HOS_FS2_START 1 +#define HOS_FS2_STOP 2 +}hos_func_thread_t; + +typedef struct hos_client_handle_s +{ + #ifndef HOS_MOCK + Aws::S3::S3Client *S3Client; + #else + Aws::S3::S3ClientMock *S3Client; + #endif + Aws::Vector<Aws::S3::Model::Bucket> buckets; + std::shared_ptr<Aws::Utils::Threading::PooledThreadExecutor> executor; + size_t count; /* 记录了有多少个对象在使用hos */ + hos_config_t hos_config; + hos_func_thread_t hos_func; + void *log; +}hos_client_handle_t; + +extern struct hos_instance_s g_hos_instance; +extern hos_client_handle_t g_hos_handle;//一个进程只允许有一个g_hos_handle +extern hos_fd_context_t **g_fd_context; +extern size_t (*g_fd_info)[MAX_HOS_CLIENT_FD_NUM + 1]; //fd 实际从3开始, fd[thread_id][0]记录register的fd,fd[thread_id][1]记录inject的fd + +#endif
\ No newline at end of file diff --git a/src/mock/hos_mock.cpp b/src/mock/hos_mock.cpp new file mode 100644 index 00000000..0e349965 --- /dev/null +++ b/src/mock/hos_mock.cpp @@ -0,0 +1,211 @@ +#include <aws/core/utils/Outcome.h> +#include <aws/core/auth/AWSAuthSigner.h> +#include <aws/core/client/CoreErrors.h> +#include <aws/core/client/RetryStrategy.h> +#include <aws/core/http/HttpClient.h> +#include <aws/core/http/HttpResponse.h> +#include <aws/core/http/HttpClientFactory.h> +#include <aws/core/auth/AWSCredentialsProviderChain.h> +#include <aws/core/utils/xml/XmlSerializer.h> +#include <aws/core/utils/memory/stl/AWSStringStream.h> +#include <aws/core/utils/threading/Executor.h> +#include <aws/core/utils/DNS.h> +#include <aws/core/utils/logging/LogMacros.h> + +#include <aws/core/utils/event/EventStream.h> +#include "hos_mock.h" +#include <aws/s3/S3Endpoint.h> +#include <aws/s3/S3ErrorMarshaller.h> +#include <aws/s3/S3ARN.h> +#include <aws/s3/model/AbortMultipartUploadRequest.h> +#include <aws/s3/model/CompleteMultipartUploadRequest.h> +#include <aws/s3/model/CopyObjectRequest.h> +#include <aws/s3/model/CreateBucketRequest.h> +#include <aws/s3/model/CreateMultipartUploadRequest.h> +#include <aws/s3/model/DeleteBucketRequest.h> +#include <aws/s3/model/DeleteBucketAnalyticsConfigurationRequest.h> +#include <aws/s3/model/DeleteBucketCorsRequest.h> +#include <aws/s3/model/DeleteBucketEncryptionRequest.h> +#include <aws/s3/model/DeleteBucketInventoryConfigurationRequest.h> +#include <aws/s3/model/DeleteBucketLifecycleRequest.h> +#include <aws/s3/model/DeleteBucketMetricsConfigurationRequest.h> +#include <aws/s3/model/DeleteBucketPolicyRequest.h> +#include <aws/s3/model/DeleteBucketReplicationRequest.h> +#include <aws/s3/model/DeleteBucketTaggingRequest.h> +#include <aws/s3/model/DeleteBucketWebsiteRequest.h> +#include <aws/s3/model/DeleteObjectRequest.h> +#include <aws/s3/model/DeleteObjectTaggingRequest.h> +#include <aws/s3/model/DeleteObjectsRequest.h> +#include <aws/s3/model/DeletePublicAccessBlockRequest.h> +#include <aws/s3/model/GetBucketAccelerateConfigurationRequest.h> +#include <aws/s3/model/GetBucketAclRequest.h> +#include <aws/s3/model/GetBucketAnalyticsConfigurationRequest.h> +#include <aws/s3/model/GetBucketCorsRequest.h> +#include <aws/s3/model/GetBucketEncryptionRequest.h> +#include <aws/s3/model/GetBucketInventoryConfigurationRequest.h> +#include <aws/s3/model/GetBucketLifecycleConfigurationRequest.h> +#include <aws/s3/model/GetBucketLocationRequest.h> +#include <aws/s3/model/GetBucketLoggingRequest.h> +#include <aws/s3/model/GetBucketMetricsConfigurationRequest.h> +#include <aws/s3/model/GetBucketNotificationConfigurationRequest.h> +#include <aws/s3/model/GetBucketPolicyRequest.h> +#include <aws/s3/model/GetBucketPolicyStatusRequest.h> +#include <aws/s3/model/GetBucketReplicationRequest.h> +#include <aws/s3/model/GetBucketRequestPaymentRequest.h> +#include <aws/s3/model/GetBucketTaggingRequest.h> +#include <aws/s3/model/GetBucketVersioningRequest.h> +#include <aws/s3/model/GetBucketWebsiteRequest.h> +#include <aws/s3/model/GetObjectRequest.h> +#include <aws/s3/model/GetObjectAclRequest.h> +#include <aws/s3/model/GetObjectLegalHoldRequest.h> +#include <aws/s3/model/GetObjectLockConfigurationRequest.h> +#include <aws/s3/model/GetObjectRetentionRequest.h> +#include <aws/s3/model/GetObjectTaggingRequest.h> +#include <aws/s3/model/GetObjectTorrentRequest.h> +#include <aws/s3/model/GetPublicAccessBlockRequest.h> +#include <aws/s3/model/HeadBucketRequest.h> +#include <aws/s3/model/HeadObjectRequest.h> +#include <aws/s3/model/ListBucketAnalyticsConfigurationsRequest.h> +#include <aws/s3/model/ListBucketInventoryConfigurationsRequest.h> +#include <aws/s3/model/ListBucketMetricsConfigurationsRequest.h> +#include <aws/s3/model/ListMultipartUploadsRequest.h> +#include <aws/s3/model/ListObjectVersionsRequest.h> +#include <aws/s3/model/ListObjectsRequest.h> +#include <aws/s3/model/ListObjectsV2Request.h> +#include <aws/s3/model/ListPartsRequest.h> +#include <aws/s3/model/PutBucketAccelerateConfigurationRequest.h> +#include <aws/s3/model/PutBucketAclRequest.h> +#include <aws/s3/model/PutBucketAnalyticsConfigurationRequest.h> +#include <aws/s3/model/PutBucketCorsRequest.h> +#include <aws/s3/model/PutBucketEncryptionRequest.h> +#include <aws/s3/model/PutBucketInventoryConfigurationRequest.h> +#include <aws/s3/model/PutBucketLifecycleConfigurationRequest.h> +#include <aws/s3/model/PutBucketLoggingRequest.h> +#include <aws/s3/model/PutBucketMetricsConfigurationRequest.h> +#include <aws/s3/model/PutBucketNotificationConfigurationRequest.h> +#include <aws/s3/model/PutBucketPolicyRequest.h> +#include <aws/s3/model/PutBucketReplicationRequest.h> +#include <aws/s3/model/PutBucketRequestPaymentRequest.h> +#include <aws/s3/model/PutBucketTaggingRequest.h> +#include <aws/s3/model/PutBucketVersioningRequest.h> +#include <aws/s3/model/PutBucketWebsiteRequest.h> +#include <aws/s3/model/PutObjectRequest.h> +#include <aws/s3/model/PutObjectAclRequest.h> +#include <aws/s3/model/PutObjectLegalHoldRequest.h> +#include <aws/s3/model/PutObjectLockConfigurationRequest.h> +#include <aws/s3/model/PutObjectRetentionRequest.h> +#include <aws/s3/model/PutObjectTaggingRequest.h> +#include <aws/s3/model/PutPublicAccessBlockRequest.h> +#include <aws/s3/model/RestoreObjectRequest.h> +#include <aws/s3/model/SelectObjectContentRequest.h> +#include <aws/s3/model/UploadPartRequest.h> +#include <aws/s3/model/UploadPartCopyRequest.h> + +using namespace Aws; +using namespace Aws::Auth; +using namespace Aws::Client; +using namespace Aws::S3; +using namespace Aws::S3::Model; +using namespace Aws::Http; +using namespace Aws::Utils::Xml; + +S3ClientMock::S3ClientMock(const AWSCredentials &credentials, const Client::ClientConfiguration &clientConfiguration, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy signPayloads, bool useVirtualAddressing) +{ + Aws::String accessKeyId = credentials.GetAWSAccessKeyId(); + Aws::String secretKey = credentials.GetAWSSecretKey(); + m_hosBuckets.push_back(Bucket().WithName("session_record_hos_bucket")); + m_hosBuckets.push_back(Bucket().WithName("firewall_hos_bucket")); + + init(clientConfiguration); + if (memcmp(clientConfiguration.endpointOverride.c_str(), "http://127.0.0.1:9098/hos/", clientConfiguration.endpointOverride.length()) != 0) + { + m_errorCode = S3Errors::NETWORK_CONNECTION; + } + if (memcmp(clientConfiguration.endpointOverride.c_str(), "127.0.0.2", clientConfiguration.endpointOverride.length()) == 0) + { + m_mockMode = 2; + m_errorCode = S3Errors::REQUEST_TIMEOUT; + } + if ((int)m_errorCode == 0) + { + if (memcmp(accessKeyId.c_str(), "default", accessKeyId.length()) != 0) + { + m_errorCode = S3Errors::INVALID_ACCESS_KEY_ID; + } + else if (memcmp(secretKey.c_str(), "default", secretKey.length()) != 0) + { + m_errorCode = S3Errors::INVALID_SIGNATURE; + } + } +} + +S3ClientMock::~S3ClientMock() +{ +} + +void S3ClientMock::init(const ClientConfiguration &config) +{ + SetServiceClientName("S3"); + if (config.endpointOverride.empty()) + { + } + else + { + OverrideEndpoint(config.endpointOverride); + } + m_errorCode = (Aws::S3::S3Errors)0; +} + +ListBucketsOutcome S3ClientMock::ListBuckets() const +{ + switch (m_errorCode) + { + case (S3Errors)0: + return ListBucketsOutcome(Aws::S3::Model::ListBucketsResult().WithBuckets(m_hosBuckets)); + case S3Errors::NETWORK_CONNECTION: + return ListBucketsOutcome(Aws::Client::AWSError<S3Errors>(S3Errors::NETWORK_CONNECTION, "NETWORK_CONNECTION", "curlCode: 7, Couldn't connect to server", false)); + case S3Errors::REQUEST_TIMEOUT: + return ListBucketsOutcome(Aws::Client::AWSError<S3Errors>(S3Errors::REQUEST_TIMEOUT, "REQUEST_TIMEOUT", "RequestTimeout", false)); + case S3Errors::INVALID_ACCESS_KEY_ID: + return ListBucketsOutcome(Aws::Client::AWSError<S3Errors>(S3Errors::INVALID_ACCESS_KEY_ID, "INVALID_ACCESS_KEY_ID", "invalid_access_key_id", false)); + case S3Errors::INVALID_SIGNATURE: + return ListBucketsOutcome(Aws::Client::AWSError<S3Errors>(S3Errors::INVALID_SIGNATURE, "INVALID_SIGNATURE", "Ivalid Signature", false)); + default: + return ListBucketsOutcome(Aws::Client::AWSError<S3Errors>(S3Errors::UNKNOWN, "UNKNOWN", "unknown", false)); + } +} + +CreateBucketOutcome S3ClientMock::CreateBucket(const CreateBucketRequest &request) const +{ + return CreateBucketOutcome(); +} + +PutObjectOutcome S3ClientMock::PutObject(const PutObjectRequest &request) const +{ + if (!request.BucketHasBeenSet()) + { + AWS_LOGSTREAM_ERROR("PutObject", "Required field: Bucket, is not set"); + return PutObjectOutcome(Aws::Client::AWSError<S3Errors>(S3Errors::MISSING_PARAMETER, "MISSING_PARAMETER", "Missing required field [Bucket]", false)); + } + if (!request.KeyHasBeenSet()) + { + AWS_LOGSTREAM_ERROR("PutObject", "Required field: Key, is not set"); + return PutObjectOutcome(Aws::Client::AWSError<S3Errors>(S3Errors::MISSING_PARAMETER, "MISSING_PARAMETER", "Missing required field [Key]", false)); + } + + auto bucket = request.GetBucket().c_str(); + for (auto &new_bucket : m_hosBuckets) + { + if (strcmp(new_bucket.GetName().c_str(), bucket) == 0) + { + return PutObjectOutcome(Aws::S3::Model::PutObjectResult()); + } + } + return PutObjectOutcome(Aws::Client::AWSError<S3Errors>(S3Errors::NO_SUCH_BUCKET, "NO_SUCH_BUCKET", "The specified bucket does not exist.", false)); +} + +bool S3ClientMock::PutObjectAsync(const PutObjectRequest &request, const PutObjectResponseReceivedHandler &handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext> &context) const +{ + handler(this, request, PutObject(request), context); + return true; +}
\ No newline at end of file diff --git a/src/mock/hos_mock.h b/src/mock/hos_mock.h new file mode 100644 index 00000000..05310ed5 --- /dev/null +++ b/src/mock/hos_mock.h @@ -0,0 +1,24 @@ +#include <aws/s3/S3Client.h> + +namespace Aws +{ + namespace S3 + { + class AWS_S3_API S3ClientMock : public S3Client + { + public: + S3ClientMock(const Aws::Auth::AWSCredentials &credentials, const Aws::Client::ClientConfiguration &clientConfiguration, Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy signPayloads, bool useVirtualAddressing); + ~S3ClientMock(); + Model::ListBucketsOutcome ListBuckets() const; + Model::CreateBucketOutcome CreateBucket(const Model::CreateBucketRequest& request) const; + Model::PutObjectOutcome PutObject(const Model::PutObjectRequest& request) const; + bool PutObjectAsync(const Model::PutObjectRequest& request, const PutObjectResponseReceivedHandler& handler, const std::shared_ptr<const Aws::Client::AsyncCallerContext>& context = nullptr) const; + + private: + void init(const Aws::Client::ClientConfiguration &ClientConfiguration); + size_t m_mockMode; + S3Errors m_errorCode; + Aws::Vector<Model::Bucket> m_hosBuckets; + }; // class S3ClientMock + } // namespace S3 +} |
