|
| 1 | +#include "config.h" |
| 2 | + |
| 3 | +#if USE_AZURE_BLOB_STORAGE |
| 4 | + |
| 5 | +#include <algorithm> |
| 6 | +#include <cstring> |
| 7 | +#include <memory> |
| 8 | +#include <optional> |
| 9 | +#include <string> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +#include <Core/Defines.h> |
| 13 | +#include <Disks/DiskObjectStorage/ObjectStorages/AzureBlobStorage/AzureBlobStorageCommon.h> |
| 14 | +#include <Disks/IO/ReadBufferFromAzureBlobStorage.h> |
| 15 | +#include <IO/ReadHelpers.h> |
| 16 | + |
| 17 | +#include <azure/core/http/raw_response.hpp> |
| 18 | +#include <azure/core/http/transport.hpp> |
| 19 | +#include <azure/core/io/body_stream.hpp> |
| 20 | +#include <azure/storage/blobs/blob_container_client.hpp> |
| 21 | + |
| 22 | +#include <gtest/gtest.h> |
| 23 | + |
| 24 | +namespace DB::ErrorCodes |
| 25 | +{ |
| 26 | + extern const int AZURE_BLOB_STORAGE_ERROR; |
| 27 | +} |
| 28 | + |
| 29 | +namespace |
| 30 | +{ |
| 31 | + |
| 32 | +class OwningBodyStream : public Azure::Core::IO::BodyStream |
| 33 | +{ |
| 34 | +public: |
| 35 | + explicit OwningBodyStream(std::vector<uint8_t> data_) : data(std::move(data_)) { } |
| 36 | + |
| 37 | + int64_t Length() const override { return static_cast<int64_t>(data.size()); } |
| 38 | + |
| 39 | + void Rewind() override { position = 0; } |
| 40 | + |
| 41 | +private: |
| 42 | + size_t OnRead(uint8_t * buffer, size_t count, const Azure::Core::Context &) override |
| 43 | + { |
| 44 | + const size_t to_read = std::min(count, data.size() - position); |
| 45 | + if (to_read != 0) |
| 46 | + memcpy(buffer, data.data() + position, to_read); |
| 47 | + position += to_read; |
| 48 | + return to_read; |
| 49 | + } |
| 50 | + |
| 51 | + std::vector<uint8_t> data; |
| 52 | + size_t position = 0; |
| 53 | +}; |
| 54 | + |
| 55 | +/// A fake Azure endpoint that serves a 10-byte blob in a response without the optional `ETag` header. |
| 56 | +class EndpointWithoutETag : public Azure::Core::Http::HttpTransport |
| 57 | +{ |
| 58 | +public: |
| 59 | + std::unique_ptr<Azure::Core::Http::RawResponse> Send(Azure::Core::Http::Request &, const Azure::Core::Context &) override |
| 60 | + { |
| 61 | + constexpr size_t blob_size = 10; |
| 62 | + auto response = std::make_unique<Azure::Core::Http::RawResponse>( |
| 63 | + 1, 1, Azure::Core::Http::HttpStatusCode::PartialContent, "Partial Content"); |
| 64 | + response->SetHeader("Content-Length", std::to_string(blob_size)); |
| 65 | + response->SetHeader("Content-Range", "bytes 0-" + std::to_string(blob_size - 1) + "/" + std::to_string(blob_size)); |
| 66 | + response->SetHeader("Last-Modified", "Wed, 21 Oct 2015 07:28:00 GMT"); |
| 67 | + response->SetHeader("x-ms-blob-type", "BlockBlob"); |
| 68 | + response->SetBodyStream(std::make_unique<OwningBodyStream>(std::vector<uint8_t>(blob_size, 'a'))); |
| 69 | + return response; |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +} |
| 74 | + |
| 75 | +/// `Azure::ETag::ToString` aborts the process on an absent tag, also in release builds. |
| 76 | +/// A download response without `ETag` must fail the read with an exception instead. |
| 77 | +TEST(AzureReadBuffer, MissingETagIsAnException) |
| 78 | +{ |
| 79 | + Azure::Storage::Blobs::BlobClientOptions client_options; |
| 80 | + client_options.Retry.MaxRetries = 0; |
| 81 | + client_options.Transport.Transport = std::make_shared<EndpointWithoutETag>(); |
| 82 | + |
| 83 | + auto container_client = std::make_shared<const DB::AzureBlobStorage::ContainerClient>( |
| 84 | + Azure::Storage::Blobs::BlobContainerClient("http://azure.invalid/container", client_options), /* blob_prefix */ ""); |
| 85 | + |
| 86 | + DB::ReadBufferFromAzureBlobStorage buffer( |
| 87 | + container_client, |
| 88 | + "blob", |
| 89 | + DB::ReadSettings{}, |
| 90 | + /* max_single_read_retries */ 1, |
| 91 | + /* max_single_download_retries */ 1); |
| 92 | + |
| 93 | + std::optional<int> error_code; |
| 94 | + try |
| 95 | + { |
| 96 | + std::string data; |
| 97 | + DB::readStringUntilEOF(data, buffer); |
| 98 | + } |
| 99 | + catch (const DB::Exception & e) |
| 100 | + { |
| 101 | + error_code = e.code(); |
| 102 | + } |
| 103 | + ASSERT_EQ(error_code, std::optional<int>(DB::ErrorCodes::AZURE_BLOB_STORAGE_ERROR)); |
| 104 | +} |
| 105 | + |
| 106 | +#endif |
0 commit comments