You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "kaxil (via GitHub)" <gi...@apache.org> on 2024/11/19 22:20:23 UTC

[PR] AIP-72: Add a basic test for a task run [airflow]

kaxil opened a new pull request, #44203:
URL: https://github.com/apache/airflow/pull/44203

   This PR adds a very basic test to parse & run. We will start adding more things here and porting things from core.
   
   <!--
    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.
    -->
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
   


-- 
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: commits-unsubscribe@airflow.apache.org

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


Re: [PR] AIP-72: Add a basic test for a task run [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44203:
URL: https://github.com/apache/airflow/pull/44203#discussion_r1850211793


##########
task_sdk/tests/execution_time/test_task_runner.py:
##########
@@ -73,3 +74,15 @@ def test_parse(test_dags_dir: Path):
     assert ti.task.dag
     assert isinstance(ti.task, BaseOperator)
     assert isinstance(ti.task.dag, DAG)
+
+
+def test_run_basic(test_dags_dir: Path):
+    """Test running a basic task."""
+    what = StartupDetails(
+        ti=TaskInstance(id=uuid7(), task_id="hello", dag_id="super_basic_run", run_id="c", try_number=1),
+        file=str(test_dags_dir / "super_basic_run.py"),
+        requests_fd=0,
+    )
+
+    ti = parse(what)
+    run(ti, log=mock.MagicMock())

Review Comment:
   Will be good if we can assert the log produced too just to check if the run is working as expected. Otherwise LGTM 



-- 
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: commits-unsubscribe@airflow.apache.org

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


Re: [PR] AIP-72: Add a basic test for a task run [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #44203:
URL: https://github.com/apache/airflow/pull/44203#discussion_r1850818216


##########
task_sdk/tests/execution_time/test_task_runner.py:
##########
@@ -73,3 +74,15 @@ def test_parse(test_dags_dir: Path):
     assert ti.task.dag
     assert isinstance(ti.task, BaseOperator)
     assert isinstance(ti.task.dag, DAG)
+
+
+def test_run_basic(test_dags_dir: Path):
+    """Test running a basic task."""
+    what = StartupDetails(
+        ti=TaskInstance(id=uuid7(), task_id="hello", dag_id="super_basic_run", run_id="c", try_number=1),
+        file=str(test_dags_dir / "super_basic_run.py"),
+        requests_fd=0,
+    )
+
+    ti = parse(what)
+    run(ti, log=mock.MagicMock())

Review Comment:
   This is verified in `test_run_simple_dag`:
   
   https://github.com/apache/airflow/blob/760f079ce47dde4ea5c8557444a1c17205f10667/task_sdk/tests/execution_time/test_supervisor.py#L220-L227
   
   We can't verify it in `task_sdk/tests/execution_time/test_task_runner.py` since the `run` method currently doesn't use `log` kw



-- 
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: commits-unsubscribe@airflow.apache.org

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


Re: [PR] AIP-72: Add a basic test for a task run [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #44203:
URL: https://github.com/apache/airflow/pull/44203#discussion_r1849380524


##########
task_sdk/tests/execution_time/test_supervisor.py:
##########
@@ -191,3 +192,44 @@ def subprocess_main():
         assert spy.called_with(id, pid=proc.pid)  # noqa: PGH005
         # The exact number we get will depend on timing behaviour, so be a little lenient
         assert 1 <= len(spy.calls) <= 4
+
+    def test_run_simple_dag(self, test_dags_dir, captured_logs, time_machine):
+        """Test running a simple DAG in a subprocess and capturing the output."""
+
+        # Ignore anything lower than INFO for this test.
+        structlog.configure(wrapper_class=structlog.make_filtering_bound_logger(logging.INFO))
+
+        instant = tz.datetime(2024, 11, 7, 12, 34, 56, 78901)
+        time_machine.move_to(instant, tick=False)
+
+        dagfile_path = test_dags_dir / "super_basic_run.py"
+        task_activity = ExecuteTaskActivity(
+            ti=TaskInstance(
+                id=UUID("4d828a62-a417-4936-a7a6-2b3fabacecab"),
+                task_id="hello",
+                dag_id="super_basic_run",
+                run_id="c",
+                try_number=1,
+            ),
+            path=dagfile_path,
+            token="",
+        )
+        # Assert Exit Code is 0
+        assert supervise(activity=task_activity, server="", dry_run=True) == 0
+
+        # We should have 2 logs, one for the DAG parsing and one for the task output from the task!
+        assert captured_logs == [
+            {
+                "event": f"Filling up the DagBag from {dagfile_path}",
+                "level": "info",
+                "logger": "airflow.models.dagbag.DagBag",
+                "timestamp": instant.replace(tzinfo=None),
+            },
+            {
+                "chan": "stdout",
+                "event": "Hello World hello!",
+                "level": "info",
+                "logger": "task",
+                "timestamp": "2024-11-07T12:34:56.078901Z",
+            },
+        ]

Review Comment:
   I could also change this to just:
   
   ```python
           assert {
               "chan": "stdout",
               "event": "Hello World hello!",
               "level": "info",
               "logger": "task",
               "timestamp": "2024-11-07T12:34:56.078901Z",
           } in captured_logs
   ```



-- 
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: commits-unsubscribe@airflow.apache.org

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


Re: [PR] AIP-72: Add a basic test for a task run [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil merged PR #44203:
URL: https://github.com/apache/airflow/pull/44203


-- 
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: commits-unsubscribe@airflow.apache.org

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