You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/07/12 08:07:45 UTC

[GitHub] [arrow] shefali163 commented on a diff in pull request #12914: ARROW-2034: [C++] Filesystem implementation for Azure Blob Storage

shefali163 commented on code in PR #12914:
URL: https://github.com/apache/arrow/pull/12914#discussion_r918673883


##########
cpp/src/arrow/filesystem/azurefs_test.cc:
##########
@@ -0,0 +1,532 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/filesystem/azurefs.h"
+
+#include <gmock/gmock-matchers.h>
+#include <gmock/gmock-more-matchers.h>
+#include <gtest/gtest.h>
+#include <azure/storage/files/datalake.hpp>
+#include <boost/process.hpp>
+#include <chrono>
+#include <thread>
+
+#include "arrow/filesystem/test_util.h"
+#include "arrow/testing/future_util.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/testing/util.h"
+#include "arrow/util/io_util.h"
+#include "arrow/util/key_value_metadata.h"
+#include "arrow/util/logging.h"
+#include "arrow/util/uri.h"
+
+namespace arrow {
+
+using internal::Uri;
+
+namespace fs {
+namespace internal {
+
+namespace bp = boost::process;
+
+using ::arrow::internal::TemporaryDir;
+using ::testing::IsEmpty;
+using ::testing::NotNull;
+
+class AzuriteEmulator : public ::testing::Environment {
+ public:
+  AzuriteEmulator() {
+    account_name_ = "devstoreaccount1";
+    account_key_ =
+        "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/"
+        "KBHBeksoGMGw==";
+    auto exe_path = bp::search_path("azurite");
+    if (exe_path.empty()) {
+      auto error = std::string("Could not find Azurite emulator.");
+      error_ = std::move(error);
+      return;
+    }
+    auto temp_dir_ = TemporaryDir::Make("azurefs-test-").ValueOrDie();
+    server_process_ = bp::child(boost::this_process::environment(), exe_path, "--silent",
+                                "--location", temp_dir_->path().ToString(), "--debug",
+                                temp_dir_->path().ToString() + "/debug.log");
+    if (!(server_process_.valid() && server_process_.running())) {
+      auto error = "Could not start Azurite emulator.";
+      server_process_.terminate();
+      server_process_.wait();
+      error_ = std::move(error);
+    }
+  }
+
+  ~AzuriteEmulator() override {
+    server_process_.terminate();
+    server_process_.wait();
+  }
+
+  const std::string& account_name() const { return account_name_; }
+  const std::string& account_key() const { return account_key_; }
+  const std::string& error() const { return error_; }
+
+ private:
+  std::string account_name_;
+  std::string account_key_;
+  bp::child server_process_;
+  std::string error_;
+  std::unique_ptr<TemporaryDir> temp_dir_;
+};
+
+AzuriteEmulator* TestAzure() {
+  static auto* const environment = [] { return new AzuriteEmulator; }();
+  return environment;
+}
+
+auto* testazure_env = ::testing::AddGlobalTestEnvironment(TestAzure());
+
+class TestAzureFileSystem : public ::testing::Test {
+ public:
+  std::shared_ptr<FileSystem> fs_;
+  std::shared_ptr<Azure::Storage::Files::DataLake::DataLakeServiceClient> gen2Client_;
+  AzureOptions options_;
+
+  void MakeFileSystem() {
+    const std::string& account_name = TestAzure()->account_name();
+    const std::string& account_key = TestAzure()->account_key();
+    options_.account_blob_url =
+        "http://127.0.0.1:10000/" + TestAzure()->account_name() + "/";
+    options_.account_dfs_url =
+        "http://127.0.0.1:10000/" + TestAzure()->account_name() + "/";
+    options_.isTestEnabled = true;
+    options_.storage_credentials_provider =
+        std::make_shared<Azure::Storage::StorageSharedKeyCredential>(account_name,
+                                                                     account_key);
+    options_.credentials_kind = AzureCredentialsKind::StorageCredentials;
+    gen2Client_ =
+        std::make_shared<Azure::Storage::Files::DataLake::DataLakeServiceClient>(
+            options_.account_dfs_url, options_.storage_credentials_provider);
+    auto result = AzureBlobFileSystem::Make(options_);
+    fs_ = *result;
+  }
+
+  void SetUp() override {
+    ASSERT_THAT(TestAzure(), NotNull());
+    ASSERT_THAT(TestAzure()->error(), IsEmpty());
+
+    MakeFileSystem();
+    auto fileSystemClient = gen2Client_->GetFileSystemClient("container");
+    fileSystemClient.CreateIfNotExists();
+    fileSystemClient = gen2Client_->GetFileSystemClient("empty-container");
+    fileSystemClient.CreateIfNotExists();
+    auto fileClient =
+        std::make_shared<Azure::Storage::Files::DataLake::DataLakeFileClient>(
+            options_.account_blob_url + "container/somefile",
+            options_.storage_credentials_provider);
+    std::string s = "some data";
+    fileClient->UploadFrom(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(&s[0])),
+                           s.size());
+  }
+  void TearDown() override {
+    auto containers = gen2Client_->ListFileSystems();
+    for (auto c : containers.FileSystems) {
+      auto fileSystemClient = gen2Client_->GetFileSystemClient(c.Name);
+      fileSystemClient.DeleteIfExists();
+    }
+  }
+  void AssertObjectContents(
+      Azure::Storage::Files::DataLake::DataLakeServiceClient* client,
+      const std::string& container, const std::string& path_to_file,
+      const std::string& expected) {
+    auto pathClient_ =
+        std::make_shared<Azure::Storage::Files::DataLake::DataLakePathClient>(
+            client->GetUrl() + container + "/" + path_to_file,
+            options_.storage_credentials_provider);
+    auto size = pathClient_->GetProperties().Value.FileSize;
+    if (size == 0) {
+      return;
+    }
+    auto buf = AllocateResizableBuffer(size, fs_->io_context().pool());
+    Azure::Storage::Blobs::DownloadBlobToOptions downloadOptions;
+    Azure::Core::Http::HttpRange range;
+    range.Offset = 0;
+    range.Length = size;
+    downloadOptions.Range = Azure::Nullable<Azure::Core::Http::HttpRange>(range);
+    auto fileClient_ =
+        std::make_shared<Azure::Storage::Files::DataLake::DataLakeFileClient>(
+            client->GetUrl() + container + "/" + path_to_file,
+            options_.storage_credentials_provider);
+    auto result = fileClient_
+                      ->DownloadTo(reinterpret_cast<uint8_t*>(buf->get()->mutable_data()),
+                                   size, downloadOptions)
+                      .Value;
+    buf->get()->Equals(
+        Buffer(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(&expected[0])),
+               expected.size()));
+  }
+};
+
+TEST_F(TestAzureFileSystem, CreateDir) {
+  // New container
+  AssertFileInfo(fs_.get(), "container3", FileType::NotFound);
+  ASSERT_OK(fs_->CreateDir("container3"));
+  AssertFileInfo(fs_.get(), "container3", FileType::Directory);
+
+  // Existing container
+  ASSERT_OK(fs_->CreateDir("container"));
+  AssertFileInfo(fs_.get(), "container", FileType::Directory);
+
+  ASSERT_RAISES(IOError, fs_->CreateDir(""));
+
+  // Existing "file", should fail
+  ASSERT_RAISES(IOError, fs_->CreateDir("container/somefile"));
+
+  // directory, false
+  ASSERT_RAISES(IOError, fs_->CreateDir("container/newdir/newsub/newsubsub", false));
+
+  // directory, true
+  ASSERT_RAISES(IOError, fs_->CreateDir("container/newdir/newsub/newsubsub", true));

Review Comment:
   This fails because Azurite creates an [Azure Blob type](https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blobs-introduction) storage account that doesn't support hierarchical namespace. There is no concept of directories inside a container.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org