|
| 1 | +#include <ydb/public/sdk/cpp/client/ydb_driver/driver.h> |
| 2 | +#include <ydb/public/sdk/cpp/client/ydb_export/export.h> |
| 3 | +#include <ydb/public/sdk/cpp/client/ydb_import/import.h> |
| 4 | +#include <ydb/public/sdk/cpp/client/ydb_operation/operation.h> |
| 5 | +#include <ydb/public/sdk/cpp/client/ydb_table/table.h> |
| 6 | +#include <ydb/library/testlib/s3_recipe_helper/s3_recipe_helper.h> |
| 7 | + |
| 8 | +#include <library/cpp/testing/unittest/registar.h> |
| 9 | +#include <util/system/env.h> |
| 10 | + |
| 11 | +using namespace NYdb; |
| 12 | +using namespace NYdb::NTable; |
| 13 | + |
| 14 | +namespace { |
| 15 | + template<typename TOp> |
| 16 | + void WaitOp(TMaybe<TOperation>& op, NOperation::TOperationClient& opClient) { |
| 17 | + int attempt = 20; |
| 18 | + while (--attempt) { |
| 19 | + op = opClient.Get<TOp>(op->Id()).GetValueSync(); |
| 20 | + if (op->Ready()) { |
| 21 | + break; |
| 22 | + } |
| 23 | + Sleep(TDuration::Seconds(1)); |
| 24 | + } |
| 25 | + UNIT_ASSERT_C(attempt, "Unable to wait completion of backup"); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +Y_UNIT_TEST_SUITE(S3PathStyleBackup) |
| 30 | +{ |
| 31 | + Y_UNIT_TEST(DisableVirtualAddressing) |
| 32 | + { |
| 33 | + TString connectionString = GetEnv("YDB_ENDPOINT") + "/?database=" + GetEnv("YDB_DATABASE"); |
| 34 | + auto config = TDriverConfig(connectionString); |
| 35 | + auto driver = TDriver(config); |
| 36 | + auto tableClient = TTableClient(driver); |
| 37 | + auto session = tableClient.GetSession().GetValueSync().GetSession(); |
| 38 | + |
| 39 | + { |
| 40 | + auto res = session.ExecuteSchemeQuery(R"( |
| 41 | + CREATE TABLE `/local/Table` ( |
| 42 | + Key Uint32, |
| 43 | + PRIMARY KEY (Key) |
| 44 | + ); |
| 45 | + )").GetValueSync(); |
| 46 | + UNIT_ASSERT_C(res.IsSuccess(), res.GetIssues().ToString()); |
| 47 | + } |
| 48 | + |
| 49 | + Aws::Client::ClientConfiguration s3ClientConfig; |
| 50 | + s3ClientConfig.endpointOverride = GetEnv("S3_ENDPOINT"); |
| 51 | + s3ClientConfig.scheme = Aws::Http::Scheme::HTTP; |
| 52 | + auto s3Client = Aws::S3::S3Client( |
| 53 | + std::make_shared<Aws::Auth::AnonymousAWSCredentialsProvider>(), |
| 54 | + s3ClientConfig, |
| 55 | + Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never, |
| 56 | + false |
| 57 | + ); |
| 58 | + const TString bucketName = "my-bucket"; |
| 59 | + NTestUtils::CreateBucket(bucketName, s3Client); |
| 60 | + |
| 61 | + auto fillS3Settings = [bucketName](auto& settings) { |
| 62 | + settings.Endpoint(GetEnv("S3_ENDPOINT")); |
| 63 | + settings.Bucket(bucketName); |
| 64 | + settings.AccessKey("minio"); |
| 65 | + settings.SecretKey("minio123"); |
| 66 | + settings.UseVirtualAddressing(false); |
| 67 | + }; |
| 68 | + |
| 69 | + { |
| 70 | + NExport::TExportToS3Settings settings; |
| 71 | + fillS3Settings(settings); |
| 72 | + |
| 73 | + settings.AppendItem({"/local/Table", "Table"}); |
| 74 | + |
| 75 | + auto exportClient = NExport::TExportClient(driver); |
| 76 | + auto operationClient = NOperation::TOperationClient(driver); |
| 77 | + |
| 78 | + const auto backupOp = exportClient.ExportToS3(settings).GetValueSync(); |
| 79 | + |
| 80 | + if (backupOp.Ready()) { |
| 81 | + UNIT_ASSERT_C(backupOp.Status().IsSuccess(), backupOp.Status().GetIssues().ToString()); |
| 82 | + } else { |
| 83 | + TMaybe<TOperation> op = backupOp; |
| 84 | + WaitOp<NExport::TExportToS3Response>(op, operationClient); |
| 85 | + UNIT_ASSERT_C(op->Status().IsSuccess(), op->Status().GetIssues().ToString()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + { |
| 90 | + NImport::TImportFromS3Settings settings; |
| 91 | + fillS3Settings(settings); |
| 92 | + |
| 93 | + settings.AppendItem({"Table", "/local/Restored"}); |
| 94 | + |
| 95 | + auto importClient = NImport::TImportClient(driver); |
| 96 | + auto operationClient = NOperation::TOperationClient(driver); |
| 97 | + |
| 98 | + const auto restoreOp = importClient.ImportFromS3(settings).GetValueSync(); |
| 99 | + |
| 100 | + if (restoreOp.Ready()) { |
| 101 | + UNIT_ASSERT_C(restoreOp.Status().IsSuccess(), restoreOp.Status().GetIssues().ToString()); |
| 102 | + } else { |
| 103 | + TMaybe<TOperation> op = restoreOp; |
| 104 | + WaitOp<NImport::TImportFromS3Response>(op, operationClient); |
| 105 | + UNIT_ASSERT_C(op->Status().IsSuccess(), op->Status().GetIssues().ToString()); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
0 commit comments