Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage unittest fix and improvement #3667

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include <azure/core/cryptography/hash.hpp>
#include <azure/storage/common/crypt.hpp>
#include <azure/storage/common/internal/file_io.hpp>

namespace Azure { namespace Storage { namespace Blobs { namespace Models {

Expand Down Expand Up @@ -191,10 +190,7 @@ namespace Azure { namespace Storage { namespace Test {
}
{
const std::string tempFilename = "file" + testName;
{
Azure::Storage::_internal::FileWriter fileWriter(tempFilename);
fileWriter.Write(blobContent.data(), blobContent.size(), 0);
}
WriteFile(tempFilename, blobContent);
client.UploadFrom(tempFilename, options);
EXPECT_EQ(client.GetTags().Value, tags);
client.Delete();
Expand Down Expand Up @@ -1122,9 +1118,7 @@ namespace Azure { namespace Storage { namespace Test {
EXPECT_NO_THROW(blockBlobClient.Delete());

std::string emptyFilename(testName);
{
_internal::FileWriter writer(emptyFilename);
}
WriteFile(emptyFilename, std::vector<uint8_t>{});
blockBlobClient.UploadFrom(emptyFilename);
EXPECT_NO_THROW(blockBlobClient.Delete());

Expand Down Expand Up @@ -1281,7 +1275,7 @@ namespace Azure { namespace Storage { namespace Test {
SetOptions();
UploadBlockBlob::ParamType const& p(GetParam());
auto const blobSize = p.Size;
std::vector<uint8_t> blobContent(static_cast<size_t>(8_MB), 'x');
std::vector<uint8_t> blobContent(static_cast<size_t>(p.Size), 'x');

Azure::Storage::Blobs::UploadBlockBlobFromOptions options;
options.TransferOptions.ChunkSize = 1_MB;
Expand All @@ -1292,10 +1286,7 @@ namespace Azure { namespace Storage { namespace Test {
options.AccessTier = m_blobUploadOptions.AccessTier;

std::string tempFilename(testName);
{
Azure::Storage::_internal::FileWriter fileWriter(tempFilename);
fileWriter.Write(blobContent.data(), static_cast<size_t>(blobSize), 0);
}
WriteFile(tempFilename, blobContent);
auto res = blockBlobClient.UploadFrom(tempFilename, options);
EXPECT_TRUE(res.Value.ETag.HasValue());
EXPECT_TRUE(IsValidTime(res.Value.LastModified));
Expand Down
14 changes: 12 additions & 2 deletions sdk/storage/azure-storage-common/test/ut/test_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <limits>
#include <random>
Expand Down Expand Up @@ -294,13 +295,22 @@ namespace Azure { namespace Storage { namespace Test {
return fileContent;
}

void StorageTest::WriteFile(const std::string& filename, const std::vector<uint8_t>& content)
{
std::ofstream f(filename, std::ofstream::binary);
f.write(reinterpret_cast<const char*>(content.data()), content.size());
}

void StorageTest::DeleteFile(const std::string& filename) { std::remove(filename.data()); }

std::vector<uint8_t> StorageTest::RandomBuffer(size_t length)
{
std::vector<uint8_t> result(length);
char* dataPtr = reinterpret_cast<char*>(&result[0]);
RandomBuffer(dataPtr, length);
if (length != 0)
{
char* dataPtr = reinterpret_cast<char*>(&result[0]);
RandomBuffer(dataPtr, length);
}
return result;
}

Expand Down
36 changes: 19 additions & 17 deletions sdk/storage/azure-storage-common/test/ut/test_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ namespace Azure { namespace Storage {
return Azure::Core::_internal::StringExtensions::ToLower(name);
}

std::string GetTestEncryptionScope()
static std::string GetTestEncryptionScope()
{
static const std::string TestEncryptionScope("EncryptionScopeForTest");
return TestEncryptionScope;
}

std::string AppendQueryParameters(
static std::string AppendQueryParameters(
const Azure::Core::Url& url,
const std::string& queryParameters);

Expand All @@ -87,50 +87,52 @@ namespace Azure { namespace Storage {
/* cspell:disable-next-line */
constexpr static const char* DummyCrc64 = "+DNR5PON4EM=";

uint64_t RandomInt(
static uint64_t RandomInt(
uint64_t minNumber = std::numeric_limits<uint64_t>::min(),
uint64_t maxNumber = std::numeric_limits<uint64_t>::max());

std::string RandomString(size_t size = 10);
static std::string RandomString(size_t size = 10);

std::string GetStringOfSize(size_t size = 10, bool lowercase = false);

std::string LowercaseRandomString(size_t size = 10);
static std::string LowercaseRandomString(size_t size = 10);

Storage::Metadata GetMetadata(size_t size = 5);
static Storage::Metadata GetMetadata(size_t size = 5);

void RandomBuffer(char* buffer, size_t length);
inline void RandomBuffer(uint8_t* buffer, size_t length)
static void RandomBuffer(char* buffer, size_t length);
static void RandomBuffer(uint8_t* buffer, size_t length)
{
RandomBuffer(reinterpret_cast<char*>(buffer), length);
}
std::vector<uint8_t> RandomBuffer(size_t length);
static std::vector<uint8_t> RandomBuffer(size_t length);

inline std::vector<uint8_t> ReadBodyStream(
static std::vector<uint8_t> ReadBodyStream(
std::unique_ptr<Azure::Core::IO::BodyStream>& stream)
{
Azure::Core::Context context;
return stream->ReadToEnd(context);
}

inline std::vector<uint8_t> ReadBodyStream(
static std::vector<uint8_t> ReadBodyStream(
std::unique_ptr<Azure::Core::IO::BodyStream>&& stream)
{
return ReadBodyStream(stream);
}

std::vector<uint8_t> ReadFile(const std::string& filename);
static std::vector<uint8_t> ReadFile(const std::string& filename);

void DeleteFile(const std::string& filename);
static void WriteFile(const std::string& filename, const std::vector<uint8_t>& content);

std::string InferSecondaryUrl(const std::string primaryUri);
static void DeleteFile(const std::string& filename);

inline std::string Base64EncodeText(const std::string& text)
static std::string InferSecondaryUrl(const std::string primaryUri);

static std::string Base64EncodeText(const std::string& text)
{
return Azure::Core::Convert::Base64Encode(std::vector<uint8_t>(text.begin(), text.end()));
}

virtual void SetUp() override
void SetUp() override
{
Azure::Core::Test::TestBase::SetUpTestBase(AZURE_TEST_RECORDING_DIR);
}
Expand Down Expand Up @@ -176,7 +178,7 @@ namespace Azure { namespace Storage {
return *m_client;
}

virtual void SetUp() override
void SetUp() override
{
StorageTest::SetUp();
m_containerName = Azure::Core::_internal::StringExtensions::ToLower(GetTestName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include <azure/identity/client_secret_credential.hpp>
#include <azure/storage/blobs.hpp>
#include <azure/storage/common/internal/file_io.hpp>
#include <azure/storage/common/internal/shared_key_policy.hpp>

namespace Azure { namespace Storage { namespace Blobs { namespace Models {
Expand Down Expand Up @@ -462,7 +461,7 @@ namespace Azure { namespace Storage { namespace Test {
TEST_P(UploadFile, fromFile)
{
UploadFile::ParamType const& p(GetParam());
std::vector<uint8_t> fileContent(static_cast<size_t>(8_MB), 'x');
std::vector<uint8_t> fileContent(static_cast<size_t>(p.FileSize), 'x');
auto fileClient = m_fileSystemClient->GetFileClient(GetTestNameLowerCase());

Azure::Storage::Files::DataLake::UploadFileFromOptions options;
Expand All @@ -472,10 +471,7 @@ namespace Azure { namespace Storage { namespace Test {
options.Metadata = GetMetadata();

std::string tempFilename = GetTestNameLowerCase();
{
Azure::Storage::_internal::FileWriter fileWriter(tempFilename);
fileWriter.Write(fileContent.data(), static_cast<size_t>(p.FileSize), 0);
}
WriteFile(tempFilename, fileContent);
auto res = fileClient.UploadFrom(tempFilename, options);
auto lastModified = fileClient.GetProperties().Value.LastModified;
EXPECT_TRUE(res.Value.ETag.HasValue());
Expand All @@ -495,9 +491,7 @@ namespace Azure { namespace Storage { namespace Test {
fileContent.begin(), fileContent.begin() + static_cast<size_t>(p.FileSize)));
std::string tempFileDestinationName = RandomString();
fileClient.DownloadTo(tempFileDestinationName);
Azure::Storage::_internal::FileReader fileReader(tempFileDestinationName);
auto size = fileReader.GetFileSize();
EXPECT_EQ(p.FileSize, size);
EXPECT_EQ(ReadFile(tempFileDestinationName), fileContent);
DeleteFile(tempFileDestinationName);
DeleteFile(tempFilename);
fileClient.Delete();
Expand Down
Loading