You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/08/15 21:59:07 UTC

[GitHub] [airflow] OmairK commented on a change in pull request #9861: [WIP] Add test for GCSTaskHandler (#9600)

OmairK commented on a change in pull request #9861:
URL: https://github.com/apache/airflow/pull/9861#discussion_r471037771



##########
File path: tests/providers/google/cloud/log/test_gcs_task_handler.py
##########
@@ -0,0 +1,133 @@
+# 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.
+import logging
+import unittest
+from datetime import datetime
+from unittest import mock
+
+from airflow.models import TaskInstance
+from airflow.models.dag import DAG
+from airflow.operators.dummy_operator import DummyOperator
+from airflow.providers.google.cloud.hooks.gcs import GCSHook
+from airflow.providers.google.cloud.log.gcs_task_handler import GCSTaskHandler
+from airflow.utils.state import State
+from tests.test_utils.config import conf_vars
+from tests.test_utils.db import clear_db_runs
+
+
+class TestGCSTaskHandler(unittest.TestCase):
+    def setUp(self) -> None:
+        date = datetime(2020, 1, 1)
+        self.gcs_log_folder = "test"
+        self.logger = logging.getLogger("logger")
+        self.dag = DAG("dag_for_testing_task_handler", start_date=date)
+        task = DummyOperator(task_id="task_for_testing_gcs_task_handler")
+        self.ti = TaskInstance(task=task, execution_date=date)
+        self.ti.try_number = 1
+        self.ti.state = State.RUNNING
+        self.remote_log_base = "gs://bucket/remote/log/location"
+        self.remote_log_location = "gs://my-bucket/path/to/1.log"
+        self.local_log_location = "local/log/location"
+        self.filename_template = "{try_number}.log"
+        self.addCleanup(self.dag.clear)
+        self.gcs_task_handler = GCSTaskHandler(
+            self.local_log_location, self.remote_log_base, self.filename_template
+        )
+
+    def tearDown(self) -> None:
+        clear_db_runs()
+
+    def test_hook(self):
+        self.assertIsInstance(self.gcs_task_handler.hook, GCSHook)
+
+    @conf_vars({("logging", "remote_log_conn_id"): "gcs_default"})
+    def test_hook_raises(self):
+        conn = "gcs_default"
+        exp_messages = "Failed to connect"
+        with mock.patch.object(self.gcs_task_handler.log, "error") as mock_error:
+            with mock.patch(
+                "airflow.providers.google.cloud.hooks.gcs.GCSHook"
+            ) as mock_hook:
+                mock_hook.side_effect = Exception(exp_messages)
+                self.gcs_task_handler.hook
+
+            mock_error.assert_called_once_with(
+                "Could not create a GoogleCloudStorageHook with connection id "
+                '"%s". %s\n\nPlease make sure that airflow[gcp] is installed '
+                "and the GCS connection exists.",
+                conn,
+                exp_messages,
+            )
+
+    @conf_vars({("logging", "remote_log_conn_id"): "gcs_default"})
+    def test_should_read_logs_from_remote(self):
+        with mock.patch(
+            "airflow.providers.google.cloud.log.gcs_task_handler.GCSTaskHandler.gcs_read"
+        ) as mock_remote_read:
+            self.gcs_task_handler._read(self.ti, self.ti.try_number)
+            mock_remote_read.assert_called_once()
+
+    def test_should_read_from_local(self):
+        with mock.patch(
+            "airflow.providers.google.cloud.log.gcs_task_handler.GCSTaskHandler.gcs_read"
+        ) as mock_remote_read:
+            mock_remote_read.side_effect = Exception("Failed to connect")
+            self.gcs_task_handler.set_context(self.ti)
+            return_val = self.gcs_task_handler._read(self.ti, self.ti.try_number)
+            self.assertEqual(len(return_val), 2)
+            self.assertEqual(
+                return_val[0],
+                "*** Unable to read remote log from gs://bucket/remote/log/location/1.log\n*** "
+                "Failed to connect\n\n*** Reading local file: local/log/location/1.log\n",
+            )
+            self.assertDictEqual(return_val[1], {"end_of_log": True})
+            mock_remote_read.assert_called_once()
+
+    def test_write_to_remote_on_close(self):
+        with mock.patch(
+            "airflow.providers.google.cloud.log.gcs_task_handler.GCSTaskHandler.gcs_write"
+        ) as mock_remote_write:
+            self.gcs_task_handler.set_context(self.ti)
+            self.gcs_task_handler.close()
+            mock_remote_write.assert_called_once()
+            self.assertEqual(self.gcs_task_handler.closed, True)
+
+    @mock.patch.object(GCSTaskHandler, "hook")
+    def test_gcs_read(self, mock_hook):
+        mock_hook.download.return_value = "Test log".encode("utf-8")
+        return_val = self.gcs_task_handler.gcs_read(self.remote_log_location)
+        self.assertEqual(return_val, "Test log")
+
+    @mock.patch.object(GCSTaskHandler, "gcs_read")
+    @mock.patch.object(GCSTaskHandler, "hook")
+    def test_gcs_write(self, mock_hook, mock_gcs_read):
+        mock_gcs_read.return_value = "Old log"
+        self.gcs_task_handler.gcs_write("New log", self.remote_log_location)
+        mock_gcs_read.assert_called_once()
+
+    @mock.patch.object(GCSTaskHandler, "hook")
+    @mock.patch.object(GCSTaskHandler, "gcs_read")
+    def test_gcs_write_fail_remote(self, mock_remote_read, mock_hook):
+        with mock.patch.object(self.gcs_task_handler.log, "error") as mock_error:
+            mock_hook.upload.side_effect = Exception("Failed to connect")
+            mock_remote_read.return_value = "Old log"
+            self.gcs_task_handler.gcs_write("New log", self.remote_log_location)
+            mock_error.assert_called_once_with(
+                "Could not write logs to %s: %s",
+                self.remote_log_location,
+                Exception("Failed to connect",),

Review comment:
       Fixed, thanks.




----------------------------------------------------------------
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.

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