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/17 08:41:00 UTC

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

mik-laj commented on a change in pull request #9861:
URL: https://github.com/apache/airflow/pull/9861#discussion_r471329162



##########
File path: tests/providers/google/cloud/log/test_gcs_task_handler_system.py
##########
@@ -0,0 +1,98 @@
+# 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 importlib
+import random
+import string
+import subprocess
+from unittest import mock
+
+import pytest
+
+from airflow import settings
+from airflow.example_dags import example_complex
+from airflow.models import DagBag, TaskInstance
+from airflow.utils.log.log_reader import TaskLogReader
+from airflow.utils.session import provide_session
+from tests.providers.google.cloud.utils.gcp_authenticator import GCP_GCS_KEY
+from tests.test_utils.config import conf_vars
+from tests.test_utils.db import clear_db_connections, clear_db_runs
+from tests.test_utils.gcp_system_helpers import (
+    GoogleSystemTest, provide_gcp_context, resolve_full_gcp_key_path,
+)
+
+
+@pytest.mark.system("google")
+@pytest.mark.credential_file(GCP_GCS_KEY)
+class TestGCSTaskHandlerSystemTest(GoogleSystemTest):
+
+    @classmethod
+    def setUpClass(cls) -> None:
+        unique_suffix = ''.join(random.sample(string.ascii_lowercase, 16))
+        cls.bucket_name = f"airflow-gcs-task-handler-tests-{unique_suffix}"  # type: ignore
+        cls.create_gcs_bucket(cls.bucket_name)  # type: ignore
+        clear_db_connections()
+
+    @classmethod
+    def tearDownClass(cls) -> None:
+        cls.delete_gcs_bucket(cls.bucket_name)  # type: ignore
+
+    def setUp(self) -> None:
+        clear_db_runs()
+
+    def tearDown(self) -> None:
+        from airflow.config_templates import airflow_local_settings
+        importlib.reload(airflow_local_settings)
+        settings.configure_logging()
+        clear_db_runs()
+
+    @provide_session
+    def test_should_read_logs(self, session):
+        with mock.patch.dict(
+            'os.environ',
+            AIRFLOW__LOGGING__REMOTE_LOGGING="true",
+            AIRFLOW__LOGGING__REMOTE_BASE_LOG_FOLDER=f"gs://{self.bucket_name}/path/to/logs",
+            AIRFLOW__LOGGING__REMOTE_LOG_CONN_ID="google_cloud_default",
+            AIRFLOW__CORE__LOAD_EXAMPLES="false",
+            AIRFLOW__CORE__DAGS_FOLDER=example_complex.__file__,
+            GOOGLE_APPLICATION_CREDENTIALS=resolve_full_gcp_key_path(GCP_GCS_KEY)
+        ):
+            self.assertEqual(0, subprocess.Popen(
+                ["airflow", "dags", "trigger", "example_complex"]
+            ).wait())
+            self.assertEqual(0, subprocess.Popen(
+                ["airflow", "scheduler", "--num-runs", "1"]
+            ).wait())

Review comment:
       We can try, but it is not advisable in this case:
   1. These are system tests, not unit tests, so they can take longer than other tests.
   2. Sometimes, due to the fact that we have recurring imports, we may not be able to load the configuration, but only when we have a new process. For example: https://github.com/apache/airflow/pull/4601
   3. I want to get rid of the side effects, because in this way I reload the configuration from scratch.
   4. Now I am sure that in the new process I have the same configuration as I have set. Here, too, it is a workaround for Airflow's problems, which has many global variables that store the configuration.
   5.  I avoid creating side effects through my test because changing the logger configuration takes place in a separate process.




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