You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2022/03/16 14:34:21 UTC

[GitHub] [nifi-minifi-cpp] fgerlits commented on a change in pull request #1268: MINIFICPP-1743 Added PutGCSObject processor

fgerlits commented on a change in pull request #1268:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1268#discussion_r823939849



##########
File path: docker/test/integration/features/google_cloud_storage.feature
##########
@@ -0,0 +1,19 @@
+Feature: Sending data to Google Cloud Storage using PutGcsObject
+
+  Background:
+    Given the content of "/tmp/output" is monitored
+
+  Scenario: A MiNiFi instance can upload data to Google Cloud storage
+    Given a GetFile processor with the "Input Directory" property set to "/tmp/input"
+    And a file with the content "hello_gcs" is present in "/tmp/input"
+    And a Google Cloud storage server is set up
+    And a PutGcsObject processor
+    And PutGcsObject processor is set up with a GcpCredentialsControllerService to communicate with the Google Cloud storage server

Review comment:
       nitpicking, but this is missing an article:
   ```suggestion
       And the PutGcsObject processor is set up with a GcpCredentialsControllerService to communicate with the Google Cloud storage server
   ```

##########
File path: extensions/gcp/tests/PutGCSObjectTests.cpp
##########
@@ -0,0 +1,314 @@
+/**
+ * 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 "../processors/PutGCSObject.h"
+#include "GCPAttributes.h"
+#include "core/Resource.h"
+#include "SingleInputTestController.h"
+#include "ProcessContextExpr.h"
+#include "google/cloud/storage/testing/mock_client.h"
+#include "google/cloud/storage/internal/object_metadata_parser.h"
+#include "google/cloud/storage/retry_policy.h"
+#include "google/cloud/storage/testing/canonical_errors.h"
+
+namespace gcs = ::google::cloud::storage;
+namespace minifi_gcp = org::apache::nifi::minifi::extensions::gcp;
+
+using PutGCSObject = org::apache::nifi::minifi::extensions::gcp::PutGCSObject;
+using GCPCredentialsControllerService = org::apache::nifi::minifi::extensions::gcp::GCPCredentialsControllerService;
+using ResumableUploadRequest = gcs::internal::ResumableUploadRequest;
+using ResumableUploadResponse = gcs::internal::ResumableUploadResponse;
+using ResumableUploadSession = gcs::internal::ResumableUploadSession;
+using ::google::cloud::storage::testing::canonical_errors::TransientError;
+using ::google::cloud::storage::testing::canonical_errors::PermanentError;
+
+namespace {
+class PutGCSObjectMocked : public PutGCSObject {
+  using org::apache::nifi::minifi::extensions::gcp::PutGCSObject::PutGCSObject;
+ public:
+  gcs::Client getClient(const gcs::ClientOptions&) const override {
+    return gcs::testing::ClientFromMock(mock_client_, *retry_policy_);
+  }
+  std::shared_ptr<gcs::testing::MockClient> mock_client_ = std::make_shared<gcs::testing::MockClient>();
+};
+REGISTER_RESOURCE(PutGCSObjectMocked, "PutGCSObjectMocked");
+}  // namespace
+
+class PutGCSObjectTests : public ::testing::Test {
+ public:
+  void SetUp() override {
+    gcp_credentials_node_ = test_controller_.plan->addController("GCPCredentialsControllerService", "gcp_credentials_controller_service");
+    test_controller_.plan->setProperty(gcp_credentials_node_,
+                                       GCPCredentialsControllerService::CredentialsLoc.getName(),
+                                       toString(GCPCredentialsControllerService::CredentialsLocation::USE_ANONYMOUS_CREDENTIALS));
+    test_controller_.plan->setProperty(put_gcs_object_,
+                                       PutGCSObject::GCPCredentials.getName(),
+                                       "gcp_credentials_controller_service");
+  }
+  std::shared_ptr<PutGCSObjectMocked> put_gcs_object_ = std::make_shared<PutGCSObjectMocked>("PutGCSObjectMocked");
+  org::apache::nifi::minifi::test::SingleInputTestController test_controller_{put_gcs_object_};
+  std::shared_ptr<minifi::core::controller::ControllerServiceNode>  gcp_credentials_node_;
+
+  static auto return_upload_in_progress() {
+    return testing::Return(google::cloud::make_status_or(ResumableUploadResponse{"fake-url", ResumableUploadResponse::kInProgress, 0, {}, {}}));
+  }
+
+  static auto return_upload_done(const ResumableUploadRequest& request) {
+    using ObjectMetadataParser = gcs::internal::ObjectMetadataParser;
+    nlohmann::json metadata_json;
+    metadata_json["name"] = request.object_name();
+    metadata_json["bucket"] = request.bucket_name();
+    metadata_json["size"] = 10;
+    if (request.HasOption<gcs::EncryptionKey>()) {
+      metadata_json["customerEncryption"]["encryptionAlgorithm"] = "AES256";
+      metadata_json["customerEncryption"]["keySha256"] = "zkeXIcAB56dkHp0z1023TQZ+mzm+fZ5JRVgmAQ3bEVE=";
+    }
+    return testing::Return(google::cloud::make_status_or(ResumableUploadResponse{"fake-url",
+                                                                                 ResumableUploadResponse::kDone, 0,
+                                                                                 *ObjectMetadataParser::FromJson(metadata_json), {}}));
+  }
+};
+
+TEST_F(PutGCSObjectTests, MissingBucket) {
+  EXPECT_CALL(*put_gcs_object_->mock_client_, CreateResumableSession).Times(0);
+  EXPECT_TRUE(test_controller_.plan->setProperty(put_gcs_object_, PutGCSObject::Bucket.getName(), ""));
+  const auto& result = test_controller_.trigger("hello world");
+  EXPECT_EQ(0, result.at(PutGCSObject::Success).size());
+  ASSERT_EQ(1, result.at(PutGCSObject::Failure).size());
+  EXPECT_EQ(std::nullopt, result.at(PutGCSObject::Failure)[0]->getAttribute(minifi_gcp::GCS_ERROR_DOMAIN));
+  EXPECT_EQ(std::nullopt, result.at(PutGCSObject::Failure)[0]->getAttribute(minifi_gcp::GCS_ERROR_REASON));
+  EXPECT_EQ("hello world", test_controller_.plan->getContent(result.at(PutGCSObject::Failure)[0]));
+}
+
+TEST_F(PutGCSObjectTests, BucketFromAttribute) {
+  EXPECT_CALL(*put_gcs_object_->mock_client_, CreateResumableSession)
+      .WillOnce([](const ResumableUploadRequest& request) {
+        EXPECT_EQ("bucket-from-attribute", request.bucket_name());
+
+        auto mock_upload_session = std::make_unique<gcs::testing::MockResumableUploadSession>();
+        EXPECT_CALL(*mock_upload_session, done()).WillRepeatedly(testing::Return(false));
+        EXPECT_CALL(*mock_upload_session, next_expected_byte()).WillRepeatedly(testing::Return(0));
+        EXPECT_CALL(*mock_upload_session, UploadChunk).WillRepeatedly(return_upload_in_progress());
+        EXPECT_CALL(*mock_upload_session, UploadFinalChunk).WillOnce(return_upload_done(request));
+        return google::cloud::make_status_or(std::unique_ptr<gcs::internal::ResumableUploadSession>(std::move(mock_upload_session)));
+      });
+  EXPECT_TRUE(test_controller_.plan->setProperty(put_gcs_object_, PutGCSObject::Bucket.getName(), "${gcs.bucket}"));
+  const auto& result = test_controller_.trigger("hello world", {{minifi_gcp::GCS_BUCKET_ATTR, "bucket-from-attribute"}});
+  ASSERT_EQ(1, result.at(PutGCSObject::Success).size());
+  EXPECT_EQ(0, result.at(PutGCSObject::Failure).size());
+  EXPECT_EQ("hello world", test_controller_.plan->getContent(result.at(PutGCSObject::Success)[0]));
+}
+
+TEST_F(PutGCSObjectTests, ServerGivesTransientErrors) {
+  auto return_temp_error = [](ResumableUploadRequest const&) {
+    return google::cloud::StatusOr<std::unique_ptr<ResumableUploadSession>>(
+        TransientError());
+  };
+
+  EXPECT_CALL(*put_gcs_object_->mock_client_, CreateResumableSession)
+      .WillOnce(return_temp_error)
+      .WillOnce(return_temp_error)
+      .WillOnce(return_temp_error);
+  EXPECT_TRUE(test_controller_.plan->setProperty(put_gcs_object_, PutGCSObject::NumberOfRetries.getName(), "2"));
+  EXPECT_TRUE(test_controller_.plan->setProperty(put_gcs_object_, PutGCSObject::Bucket.getName(), "bucket-from-property"));
+  EXPECT_TRUE(test_controller_.plan->setProperty(put_gcs_object_, PutGCSObject::Key.getName(), "object-name-from-property"));
+  const auto& result = test_controller_.trigger("hello world");
+  EXPECT_EQ(0, result.at(PutGCSObject::Success).size());
+  ASSERT_EQ(1, result.at(PutGCSObject::Failure).size());
+  EXPECT_NE(std::nullopt, result.at(PutGCSObject::Failure)[0]->getAttribute(minifi_gcp::GCS_ERROR_DOMAIN));
+  EXPECT_NE(std::nullopt, result.at(PutGCSObject::Failure)[0]->getAttribute(minifi_gcp::GCS_ERROR_REASON));
+  EXPECT_EQ("hello world", test_controller_.plan->getContent(result.at(PutGCSObject::Failure)[0]));
+}
+
+TEST_F(PutGCSObjectTests, ServerGivesPermaError) {
+  auto return_permanent_error = [](ResumableUploadRequest const&) {
+    return google::cloud::StatusOr<std::unique_ptr<ResumableUploadSession>>(
+        PermanentError());
+  };
+
+  EXPECT_CALL(*put_gcs_object_->mock_client_, CreateResumableSession)
+      .WillOnce(return_permanent_error);
+  EXPECT_TRUE(test_controller_.plan->setProperty(put_gcs_object_, PutGCSObject::Bucket.getName(), "bucket-from-property"));
+  EXPECT_TRUE(test_controller_.plan->setProperty(put_gcs_object_, PutGCSObject::Key.getName(), "object-name-from-property"));
+  const auto& result = test_controller_.trigger("hello world");
+  EXPECT_EQ(0, result.at(PutGCSObject::Success).size());
+  ASSERT_EQ(1, result.at(PutGCSObject::Failure).size());
+  EXPECT_NE(std::nullopt, result.at(PutGCSObject::Failure)[0]->getAttribute(minifi_gcp::GCS_ERROR_DOMAIN));
+  EXPECT_NE(std::nullopt, result.at(PutGCSObject::Failure)[0]->getAttribute(minifi_gcp::GCS_ERROR_REASON));
+  EXPECT_EQ("hello world", test_controller_.plan->getContent(result.at(PutGCSObject::Failure)[0]));
+}
+
+TEST_F(PutGCSObjectTests, NonRequiredPropertiesAreMissing) {
+  EXPECT_CALL(*put_gcs_object_->mock_client_, CreateResumableSession)
+      .WillOnce([](const ResumableUploadRequest& request) {
+        EXPECT_FALSE(request.HasOption<gcs::MD5HashValue>());
+        EXPECT_FALSE(request.HasOption<gcs::Crc32cChecksumValue>());
+        EXPECT_FALSE(request.HasOption<gcs::PredefinedAcl>());
+        EXPECT_FALSE(request.HasOption<gcs::IfGenerationMatch>());
+        auto mock_upload_session = std::make_unique<gcs::testing::MockResumableUploadSession>();
+        EXPECT_CALL(*mock_upload_session, done()).WillRepeatedly(testing::Return(false));
+        EXPECT_CALL(*mock_upload_session, next_expected_byte()).WillRepeatedly(testing::Return(0));
+        EXPECT_CALL(*mock_upload_session, UploadChunk).WillRepeatedly(return_upload_in_progress());
+        EXPECT_CALL(*mock_upload_session, UploadFinalChunk).WillOnce(return_upload_done(request));
+        return google::cloud::make_status_or(std::unique_ptr<gcs::internal::ResumableUploadSession>(std::move(mock_upload_session)));
+      });
+  EXPECT_TRUE(test_controller_.plan->setProperty(put_gcs_object_, PutGCSObject::Bucket.getName(), "bucket-from-property"));
+  EXPECT_TRUE(test_controller_.plan->setProperty(put_gcs_object_, PutGCSObject::Key.getName(), "object-name-from-property"));
+  const auto& result = test_controller_.trigger("hello world");
+  EXPECT_EQ(1, result.at(PutGCSObject::Success).size());
+  EXPECT_EQ(0, result.at(PutGCSObject::Failure).size());
+}
+
+TEST_F(PutGCSObjectTests, Crc32cMD5LocationTest) {
+  EXPECT_CALL(*put_gcs_object_->mock_client_, CreateResumableSession)
+      .WillOnce([](const ResumableUploadRequest& request) {
+        EXPECT_TRUE(request.HasOption<gcs::Crc32cChecksumValue>());
+        EXPECT_TRUE(request.HasOption<gcs::MD5HashValue>());

Review comment:
       we could also check that the values are the same as what we set in the flow file attributes




-- 
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: issues-unsubscribe@nifi.apache.org

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