From bc247650c5fe42e29882c3f7fa96aef9ccd2e018 Mon Sep 17 00:00:00 2001 From: Minglei Jin Date: Tue, 22 Oct 2024 12:35:32 +0800 Subject: [PATCH] test/az: use env vars for az testing cases --- source/libs/azure/src/az.cpp | 9 +- source/libs/azure/test/azTest.cpp | 161 ++++++++++++++++++++++++++---- source/libs/tcs/test/tcsTest.cpp | 15 +++ 3 files changed, 157 insertions(+), 28 deletions(-) diff --git a/source/libs/azure/src/az.cpp b/source/libs/azure/src/az.cpp index 1422705011..b3b5c7704a 100644 --- a/source/libs/azure/src/az.cpp +++ b/source/libs/azure/src/az.cpp @@ -204,9 +204,9 @@ _next: int32_t azPutObjectFromFileOffset(const char *file, const char *object_name, int64_t offset, int64_t size) { int32_t code = 0; - std::string endpointUrl = tsS3Hostname[0]; // GetEndpointUrl(); - std::string accountName = tsS3AccessKeyId[0]; // GetAccountName(); - std::string accountKey = tsS3AccessKeySecret[0]; // GetAccountKey(); + std::string endpointUrl = tsS3Hostname[0]; + std::string accountName = tsS3AccessKeyId[0]; + std::string accountKey = tsS3AccessKeySecret[0]; try { auto sharedKeyCredential = std::make_shared(accountName, accountKey); @@ -226,9 +226,6 @@ int32_t azPutObjectFromFileOffset(const char *file, const char *object_name, int // TDBlockBlobClient blobClient(containerClient.GetBlobClient(blobName)); TDBlockBlobClient blobClient(containerClient.GetBlobClient(object_name)); - // Upload the blob - // std::cout << "Uploading blob: " << blobName << std::endl; - // blobClient.UploadFrom(blobContent, sizeof(blobContent)); blobClient.UploadFrom(file, offset, size); } catch (const Azure::Core::RequestFailedException &e) { /* diff --git a/source/libs/azure/test/azTest.cpp b/source/libs/azure/test/azTest.cpp index 8d428fbb69..bb15bf11ec 100644 --- a/source/libs/azure/test/azTest.cpp +++ b/source/libs/azure/test/azTest.cpp @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + #include #include #include @@ -24,15 +39,38 @@ int32_t azInitEnv() { tsS3BucketName = ""; */ - const char *hostname = "endpoint/.blob.core.windows.net"; + const char *hostname = "/.blob.core.windows.net"; const char *accessKeyId = ""; const char *accessKeySecret = ""; const char *bucketName = ""; - tstrncpy(&tsS3Hostname[0][0], hostname, TSDB_FQDN_LEN); - tstrncpy(&tsS3AccessKeyId[0][0], accessKeyId, TSDB_FQDN_LEN); - tstrncpy(&tsS3AccessKeySecret[0][0], accessKeySecret, TSDB_FQDN_LEN); - tstrncpy(tsS3BucketName, bucketName, TSDB_FQDN_LEN); + if (hostname[0] != '<') { + tstrncpy(&tsS3Hostname[0][0], hostname, TSDB_FQDN_LEN); + tstrncpy(&tsS3AccessKeyId[0][0], accessKeyId, TSDB_FQDN_LEN); + tstrncpy(&tsS3AccessKeySecret[0][0], accessKeySecret, TSDB_FQDN_LEN); + tstrncpy(tsS3BucketName, bucketName, TSDB_FQDN_LEN); + } else { + const char *accountId = getenv("ablob_account_id"); + if (!accountId) { + return -1; + } + + const char *accountSecret = getenv("ablob_account_secret"); + if (!accountSecret) { + return -1; + } + + const char *containerName = getenv("ablob_container"); + if (!containerName) { + return -1; + } + + TAOS_STRCPY(&tsS3Hostname[0][0], accountId); + TAOS_STRCAT(&tsS3Hostname[0][0], ".blob.core.windows.net"); + TAOS_STRCPY(&tsS3AccessKeyId[0][0], accountId); + TAOS_STRCPY(&tsS3AccessKeySecret[0][0], accountSecret); + TAOS_STRCPY(tsS3BucketName, containerName); + } tstrncpy(tsTempDir, "/tmp/", PATH_MAX); @@ -41,9 +79,11 @@ int32_t azInitEnv() { return code; } -TEST(AzTest, DISABLED_InterfaceTest) { - // TEST(AzTest, InterfaceTest) { - int code = 0; +// TEST(AzTest, DISABLED_InterfaceTest) { +TEST(AzTest, InterfaceTest) { + int code = 0; + bool check = false; + bool withcp = false; code = azInitEnv(); GTEST_ASSERT_EQ(code, 0); @@ -54,26 +94,103 @@ TEST(AzTest, DISABLED_InterfaceTest) { code = azCheckCfg(); GTEST_ASSERT_EQ(code, 0); - /* - code = azPutObjectFromFileOffset(file, object_name, offset, size); - GTEST_ASSERT_EQ(code, 0); - code = azGetObjectBlock(object_name, offset, size, check, ppBlock); + const int size = 4096; + char data[size] = {0}; + for (int i = 0; i < size / 2; ++i) { + data[i * 2 + 1] = 1; + } + + const char object_name[] = "azut.bin"; + char path[PATH_MAX] = {0}; + char path_download[PATH_MAX] = {0}; + int ds_len = strlen(TD_DIRSEP); + int tmp_len = strlen(tsTempDir); + + (void)snprintf(path, PATH_MAX, "%s", tsTempDir); + if (strncmp(tsTempDir + tmp_len - ds_len, TD_DIRSEP, ds_len) != 0) { + (void)snprintf(path + tmp_len, PATH_MAX - tmp_len, "%s", TD_DIRSEP); + (void)snprintf(path + tmp_len + ds_len, PATH_MAX - tmp_len - ds_len, "%s", object_name); + } else { + (void)snprintf(path + tmp_len, PATH_MAX - tmp_len, "%s", object_name); + } + + tstrncpy(path_download, path, strlen(path) + 1); + tstrncpy(path_download + strlen(path), ".download", strlen(".download") + 1); + + TdFilePtr fp = taosOpenFile(path, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_WRITE_THROUGH); + GTEST_ASSERT_NE(fp, nullptr); + + int n = taosWriteFile(fp, data, size); + GTEST_ASSERT_EQ(n, size); + + code = taosCloseFile(&fp); GTEST_ASSERT_EQ(code, 0); - azDeleteObjectsByPrefix(prefix); + code = azPutObjectFromFileOffset(path, object_name, 0, size); + GTEST_ASSERT_EQ(code, 0); + + uint8_t *pBlock = NULL; + code = azGetObjectBlock(object_name, 0, size, check, &pBlock); + GTEST_ASSERT_EQ(code, 0); + + for (int i = 0; i < size / 2; ++i) { + GTEST_ASSERT_EQ(pBlock[i * 2], 0); + GTEST_ASSERT_EQ(pBlock[i * 2 + 1], 1); + } + + taosMemoryFree(pBlock); + + code = azGetObjectToFile(object_name, path_download); + GTEST_ASSERT_EQ(code, 0); + + { + TdFilePtr fp = taosOpenFile(path, TD_FILE_READ); + GTEST_ASSERT_NE(fp, nullptr); + + (void)memset(data, 0, size); + + int64_t n = taosReadFile(fp, data, size); + GTEST_ASSERT_EQ(n, size); + + code = taosCloseFile(&fp); + GTEST_ASSERT_EQ(code, 0); + + for (int i = 0; i < size / 2; ++i) { + GTEST_ASSERT_EQ(data[i * 2], 0); + GTEST_ASSERT_EQ(data[i * 2 + 1], 1); + } + } + + azDeleteObjectsByPrefix(object_name); // list object to check - code = azPutObjectFromFile2(file, object, withcp); - GTEST_ASSERT_EQ(code, 0); - code = azGetObjectsByPrefix(prefix, path); - GTEST_ASSERT_EQ(code, 0); - code = azDeleteObjects(object_name, nobject); - GTEST_ASSERT_EQ(code, 0); - code = azGetObjectToFile(object_name, fileName); + code = azPutObjectFromFile2(path, object_name, withcp); GTEST_ASSERT_EQ(code, 0); - // GTEST_ASSERT_NE(pEnv, nullptr); - */ + code = azGetObjectsByPrefix(object_name, tsTempDir); + GTEST_ASSERT_EQ(code, 0); + + { + TdFilePtr fp = taosOpenFile(path, TD_FILE_READ); + GTEST_ASSERT_NE(fp, nullptr); + + (void)memset(data, 0, size); + + int64_t n = taosReadFile(fp, data, size); + GTEST_ASSERT_EQ(n, size); + + code = taosCloseFile(&fp); + GTEST_ASSERT_EQ(code, 0); + + for (int i = 0; i < size / 2; ++i) { + GTEST_ASSERT_EQ(data[i * 2], 0); + GTEST_ASSERT_EQ(data[i * 2 + 1], 1); + } + } + + const char *object_name_arr[] = {object_name}; + code = azDeleteObjects(object_name_arr, 1); + GTEST_ASSERT_EQ(code, 0); azEnd(); } diff --git a/source/libs/tcs/test/tcsTest.cpp b/source/libs/tcs/test/tcsTest.cpp index 5e17d09fc7..4b5afc5b85 100644 --- a/source/libs/tcs/test/tcsTest.cpp +++ b/source/libs/tcs/test/tcsTest.cpp @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + #include #include