|
1 | 1 | #include "s3_recipe_ut_helpers.h" |
2 | 2 |
|
3 | 3 | #include <ydb/library/yql/providers/s3/actors/yql_s3_actors_factory_impl.h> |
4 | | -#include <library/cpp/testing/hook/hook.h> |
5 | | - |
6 | | -#include <util/string/builder.h> |
7 | | - |
8 | | -#include <aws/core/Aws.h> |
9 | | - |
10 | | -Y_TEST_HOOK_BEFORE_RUN(InitAwsAPI) { |
11 | | - Aws::InitAPI(Aws::SDKOptions()); |
12 | | -} |
13 | | - |
14 | | -Y_TEST_HOOK_AFTER_RUN(ShutdownAwsAPI) { |
15 | | - Aws::ShutdownAPI(Aws::SDKOptions()); |
16 | | -} |
17 | 4 |
|
18 | 5 | namespace NTestUtils { |
19 | 6 |
|
20 | | - extern const TString TEST_SCHEMA = R"(["StructType";[["key";["DataType";"Utf8";];];["value";["DataType";"Utf8";];];];])"; |
21 | | - |
22 | | - extern const TString TEST_SCHEMA_IDS = R"(["StructType";[["key";["DataType";"Utf8";];];];])"; |
23 | | - |
24 | | - std::shared_ptr<NKikimr::NKqp::TKikimrRunner> MakeKikimrRunner(std::optional<NKikimrConfig::TAppConfig> appConfig, const TString& domainRoot) { |
25 | | - return NKikimr::NKqp::NFederatedQueryTest::MakeKikimrRunner(true, nullptr, nullptr, appConfig, NYql::NDq::CreateS3ActorsFactory(), domainRoot); |
26 | | - } |
27 | | - |
28 | | - Aws::S3::S3Client MakeS3Client() { |
29 | | - Aws::Client::ClientConfiguration s3ClientConfig; |
30 | | - s3ClientConfig.endpointOverride = GetEnv("S3_ENDPOINT"); |
31 | | - s3ClientConfig.scheme = Aws::Http::Scheme::HTTP; |
32 | | - return Aws::S3::S3Client( |
33 | | - std::make_shared<Aws::Auth::AnonymousAWSCredentialsProvider>(), |
34 | | - s3ClientConfig, |
35 | | - Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, |
36 | | - /*useVirtualAddressing=*/true); |
37 | | - } |
38 | | - |
39 | | - void CreateBucket(const TString& bucket, Aws::S3::S3Client& s3Client) { |
40 | | - Aws::S3::Model::CreateBucketRequest req; |
41 | | - req.SetBucket(bucket); |
42 | | - req.SetACL(Aws::S3::Model::BucketCannedACL::public_read_write); |
43 | | - const Aws::S3::Model::CreateBucketOutcome result = s3Client.CreateBucket(req); |
44 | | - UNIT_ASSERT_C(result.IsSuccess(), "Error creating bucket \"" << bucket << "\": " << result.GetError().GetExceptionName() << ": " << result.GetError().GetMessage()); |
45 | | - } |
46 | | - |
47 | | - void CreateBucket(const TString& bucket) { |
48 | | - Aws::S3::S3Client s3Client = MakeS3Client(); |
49 | | - |
50 | | - CreateBucket(bucket, s3Client); |
51 | | - } |
52 | | - |
53 | | - void UploadObject(const TString& bucket, const TString& object, const TStringBuf& content, Aws::S3::S3Client& s3Client) { |
54 | | - Aws::S3::Model::PutObjectRequest req; |
55 | | - req.WithBucket(bucket).WithKey(object); |
56 | | - |
57 | | - auto inputStream = std::make_shared<std::stringstream>(); |
58 | | - *inputStream << content; |
59 | | - req.SetBody(inputStream); |
60 | | - const Aws::S3::Model::PutObjectOutcome result = s3Client.PutObject(req); |
61 | | - UNIT_ASSERT_C(result.IsSuccess(), "Error uploading object \"" << object << "\" to a bucket \"" << bucket << "\": " << result.GetError().GetExceptionName() << ": " << result.GetError().GetMessage()); |
62 | | - } |
63 | | - |
64 | | - void UploadObject(const TString& bucket, const TString& object, const TStringBuf& content) { |
65 | | - Aws::S3::S3Client s3Client = MakeS3Client(); |
66 | | - |
67 | | - UploadObject(bucket, object, content, s3Client); |
68 | | - } |
| 7 | +using std::shared_ptr; |
| 8 | +using namespace NKikimr::NKqp; |
69 | 9 |
|
70 | | - void CreateBucketWithObject(const TString& bucket, const TString& object, const TStringBuf& content, Aws::S3::S3Client& s3Client) { |
71 | | - CreateBucket(bucket, s3Client); |
72 | | - UploadObject(bucket, object, content, s3Client); |
73 | | - } |
| 10 | +const TString TEST_SCHEMA = R"(["StructType";[["key";["DataType";"Utf8";];];["value";["DataType";"Utf8";];];];])"; |
| 11 | +const TString TEST_SCHEMA_IDS = R"(["StructType";[["key";["DataType";"Utf8";];];];])"; |
74 | 12 |
|
75 | | - void CreateBucketWithObject(const TString& bucket, const TString& object, const TStringBuf& content) { |
76 | | - Aws::S3::S3Client s3Client = MakeS3Client(); |
77 | | - |
78 | | - CreateBucketWithObject(bucket, object, content, s3Client); |
79 | | - } |
80 | | - |
81 | | - TString GetObject(const TString& bucket, const TString& object, Aws::S3::S3Client& s3Client) { |
82 | | - Aws::S3::Model::GetObjectRequest req; |
83 | | - req.WithBucket(bucket).WithKey(object); |
84 | | - |
85 | | - Aws::S3::Model::GetObjectOutcome outcome = s3Client.GetObject(req); |
86 | | - UNIT_ASSERT(outcome.IsSuccess()); |
87 | | - Aws::S3::Model::GetObjectResult& result = outcome.GetResult(); |
88 | | - std::istreambuf_iterator<char> eos; |
89 | | - std::string objContent(std::istreambuf_iterator<char>(result.GetBody()), eos); |
90 | | - Cerr << "Got object content from \"" << bucket << "." << object << "\"\n" |
91 | | - << objContent << Endl; |
92 | | - return objContent; |
93 | | - } |
94 | | - |
95 | | - TString GetObject(const TString& bucket, const TString& object) { |
96 | | - Aws::S3::S3Client s3Client = MakeS3Client(); |
97 | | - |
98 | | - return GetObject(bucket, object, s3Client); |
99 | | - } |
100 | | - |
101 | | - std::vector<TString> GetObjectKeys(const TString& bucket, Aws::S3::S3Client& s3Client) { |
102 | | - Aws::S3::Model::ListObjectsRequest listReq; |
103 | | - listReq.WithBucket(bucket); |
104 | | - |
105 | | - Aws::S3::Model::ListObjectsOutcome outcome = s3Client.ListObjects(listReq); |
106 | | - UNIT_ASSERT(outcome.IsSuccess()); |
107 | | - |
108 | | - std::vector<TString> keys; |
109 | | - for (auto& obj : outcome.GetResult().GetContents()) { |
110 | | - keys.push_back(TString(obj.GetKey())); |
111 | | - Cerr << "Found S3 object: \"" << obj.GetKey() << "\"" << Endl; |
112 | | - } |
113 | | - return keys; |
114 | | - } |
115 | | - |
116 | | - std::vector<TString> GetObjectKeys(const TString& bucket) { |
117 | | - Aws::S3::S3Client s3Client = MakeS3Client(); |
118 | | - |
119 | | - return GetObjectKeys(bucket, s3Client); |
120 | | - } |
121 | | - |
122 | | - TString GetAllObjects(const TString& bucket, TStringBuf separator, Aws::S3::S3Client& s3Client) { |
123 | | - std::vector<TString> keys = GetObjectKeys(bucket, s3Client); |
124 | | - TString result; |
125 | | - bool firstObject = true; |
126 | | - for (const TString& key : keys) { |
127 | | - result += GetObject(bucket, key, s3Client); |
128 | | - if (!firstObject) { |
129 | | - result += separator; |
130 | | - } |
131 | | - firstObject = false; |
132 | | - } |
133 | | - return result; |
134 | | - } |
135 | | - |
136 | | - TString GetAllObjects(const TString& bucket, TStringBuf separator) { |
137 | | - Aws::S3::S3Client s3Client = MakeS3Client(); |
138 | | - |
139 | | - return GetAllObjects(bucket, separator, s3Client); |
140 | | - } |
141 | | - |
142 | | - TString GetBucketLocation(const TStringBuf bucket) { |
143 | | - return TStringBuilder() << GetEnv("S3_ENDPOINT") << '/' << bucket << '/'; |
144 | | - } |
| 13 | +shared_ptr<TKikimrRunner> MakeKikimrRunner(std::optional<NKikimrConfig::TAppConfig> appConfig, const TString& domainRoot) |
| 14 | +{ |
| 15 | + return NFederatedQueryTest::MakeKikimrRunner(true, nullptr, nullptr, appConfig, NYql::NDq::CreateS3ActorsFactory(), domainRoot); |
| 16 | +} |
145 | 17 |
|
146 | | -} // namespace NTestUtils |
| 18 | +} |
0 commit comments