From 5a5fc3ec4e6eb870ba5ec1ae487b79acc281f143 Mon Sep 17 00:00:00 2001 From: Aleph Melo Date: Fri, 25 Aug 2023 01:56:18 +0000 Subject: [PATCH 1/6] Fix action logging decorator for un/pausing events --- airflow/www/decorators.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/airflow/www/decorators.py b/airflow/www/decorators.py index a4b613170cfe3..9e455f7e5fd2e 100644 --- a/airflow/www/decorators.py +++ b/airflow/www/decorators.py @@ -93,7 +93,7 @@ def wrapper(*args, **kwargs): user = get_auth_manager().get_user_name() user_display = get_auth_manager().get_user_display_name() - fields_skip_logging = {"csrf_token", "_csrf_token"} + fields_skip_logging = {"csrf_token", "_csrf_token", "is_paused"} extra_fields = [ (k, secrets_masker.redact(v, k)) for k, v in itertools.chain(request.values.items(multi=True), request.view_args.items()) @@ -106,6 +106,8 @@ def wrapper(*args, **kwargs): params = {**request.values, **request.view_args} + if params and "is_paused" in params: + extra_fields.append(("is_paused", params["is_paused"] == "false")) log = Log( event=event or f.__name__, task_instance=None, From 02ab7542964a8c33e48692519790b2f28a6b2fbd Mon Sep 17 00:00:00 2001 From: romsharon98 Date: Tue, 12 Dec 2023 08:57:55 +0200 Subject: [PATCH 2/6] fix --- tests/www/views/test_views_paused.py | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/www/views/test_views_paused.py diff --git a/tests/www/views/test_views_paused.py b/tests/www/views/test_views_paused.py new file mode 100644 index 0000000000000..fcd47024d92f5 --- /dev/null +++ b/tests/www/views/test_views_paused.py @@ -0,0 +1,38 @@ +# 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. +from __future__ import annotations + +from airflow.models.log import Log +from airflow.utils.session import create_session + + +def test_logging_pause_dag(admin_client, create_dummy_dag): + dag, _ = create_dummy_dag() + # is_paused=false mean pause the dag + admin_client.post(f"/paused?is_paused=false&dag_id={dag.dag_id}", follow_redirects=True) + with create_session() as session: + dag_query = session.query(Log).filter(Log.dag_id == dag.dag_id) + assert "('is_paused', True)" in dag_query.first().extra + + +def test_logging_unpuase_dag(admin_client, create_dummy_dag): + dag, _ = create_dummy_dag(is_paused_upon_creation=True) + # is_paused=true mean unpause the dag + admin_client.post(f"/paused?is_paused=true&dag_id={dag.dag_id}", follow_redirects=True) + with create_session() as session: + dag_query = session.query(Log).filter(Log.dag_id == dag.dag_id) + assert "('is_paused', False)" in dag_query.first().extra From 34490825439b6d08cb780e3992a1c755ab82c0ec Mon Sep 17 00:00:00 2001 From: romsharon98 Date: Tue, 12 Dec 2023 12:52:14 +0200 Subject: [PATCH 3/6] fix test for logging --- tests/www/views/test_views_paused.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/tests/www/views/test_views_paused.py b/tests/www/views/test_views_paused.py index fcd47024d92f5..9eb168450cdda 100644 --- a/tests/www/views/test_views_paused.py +++ b/tests/www/views/test_views_paused.py @@ -16,12 +16,25 @@ # under the License. from __future__ import annotations +import pytest + from airflow.models.log import Log from airflow.utils.session import create_session +from tests.test_utils.db import clear_db_dags + + +@pytest.fixture(autouse=True) +def dags(create_dummy_dag): + paused_dag, _ = create_dummy_dag(dag_id="paused_dag", is_paused_upon_creation=True) + dag, _ = create_dummy_dag(dag_id="unpaused_dag") + yield dag, paused_dag -def test_logging_pause_dag(admin_client, create_dummy_dag): - dag, _ = create_dummy_dag() + clear_db_dags() + + +def test_logging_pause_dag(admin_client, dags): + dag, _ = dags # is_paused=false mean pause the dag admin_client.post(f"/paused?is_paused=false&dag_id={dag.dag_id}", follow_redirects=True) with create_session() as session: @@ -29,10 +42,10 @@ def test_logging_pause_dag(admin_client, create_dummy_dag): assert "('is_paused', True)" in dag_query.first().extra -def test_logging_unpuase_dag(admin_client, create_dummy_dag): - dag, _ = create_dummy_dag(is_paused_upon_creation=True) +def test_logging_unpuase_dag(admin_client, dags): + _, paused_dag = dags # is_paused=true mean unpause the dag - admin_client.post(f"/paused?is_paused=true&dag_id={dag.dag_id}", follow_redirects=True) + admin_client.post(f"/paused?is_paused=true&dag_id={paused_dag.dag_id}", follow_redirects=True) with create_session() as session: - dag_query = session.query(Log).filter(Log.dag_id == dag.dag_id) + dag_query = session.query(Log).filter(Log.dag_id == paused_dag.dag_id) assert "('is_paused', False)" in dag_query.first().extra From 5bb2eff487d1420048b353d5fd6a94f77750dc88 Mon Sep 17 00:00:00 2001 From: romsharon98 Date: Tue, 12 Dec 2023 18:01:41 +0200 Subject: [PATCH 4/6] fix test to require db --- tests/www/views/test_views_paused.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/www/views/test_views_paused.py b/tests/www/views/test_views_paused.py index 9eb168450cdda..cc54052fc3848 100644 --- a/tests/www/views/test_views_paused.py +++ b/tests/www/views/test_views_paused.py @@ -33,6 +33,7 @@ def dags(create_dummy_dag): clear_db_dags() +@pytest.mark.db_test def test_logging_pause_dag(admin_client, dags): dag, _ = dags # is_paused=false mean pause the dag @@ -42,6 +43,7 @@ def test_logging_pause_dag(admin_client, dags): assert "('is_paused', True)" in dag_query.first().extra +@pytest.mark.db_test def test_logging_unpuase_dag(admin_client, dags): _, paused_dag = dags # is_paused=true mean unpause the dag From 3882db4d7c4d8ec1e43f48eba50026ff6c4d93e7 Mon Sep 17 00:00:00 2001 From: romsharon98 Date: Wed, 13 Dec 2023 15:56:31 +0200 Subject: [PATCH 5/6] add pytest mark --- tests/www/views/test_views_paused.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/www/views/test_views_paused.py b/tests/www/views/test_views_paused.py index cc54052fc3848..ecbd737029f50 100644 --- a/tests/www/views/test_views_paused.py +++ b/tests/www/views/test_views_paused.py @@ -22,6 +22,8 @@ from airflow.utils.session import create_session from tests.test_utils.db import clear_db_dags +pytestmark = pytest.mark.db_test + @pytest.fixture(autouse=True) def dags(create_dummy_dag): @@ -33,7 +35,6 @@ def dags(create_dummy_dag): clear_db_dags() -@pytest.mark.db_test def test_logging_pause_dag(admin_client, dags): dag, _ = dags # is_paused=false mean pause the dag @@ -43,7 +44,6 @@ def test_logging_pause_dag(admin_client, dags): assert "('is_paused', True)" in dag_query.first().extra -@pytest.mark.db_test def test_logging_unpuase_dag(admin_client, dags): _, paused_dag = dags # is_paused=true mean unpause the dag From a62a2f7cf16ca6352a62cf87210a4558e73fed30 Mon Sep 17 00:00:00 2001 From: romsharon98 Date: Wed, 13 Dec 2023 16:34:16 +0200 Subject: [PATCH 6/6] get session from conftest --- tests/www/views/test_views_paused.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/tests/www/views/test_views_paused.py b/tests/www/views/test_views_paused.py index ecbd737029f50..1f9ac1f4d86ce 100644 --- a/tests/www/views/test_views_paused.py +++ b/tests/www/views/test_views_paused.py @@ -19,7 +19,6 @@ import pytest from airflow.models.log import Log -from airflow.utils.session import create_session from tests.test_utils.db import clear_db_dags pytestmark = pytest.mark.db_test @@ -35,19 +34,17 @@ def dags(create_dummy_dag): clear_db_dags() -def test_logging_pause_dag(admin_client, dags): +def test_logging_pause_dag(admin_client, dags, session): dag, _ = dags # is_paused=false mean pause the dag admin_client.post(f"/paused?is_paused=false&dag_id={dag.dag_id}", follow_redirects=True) - with create_session() as session: - dag_query = session.query(Log).filter(Log.dag_id == dag.dag_id) - assert "('is_paused', True)" in dag_query.first().extra + dag_query = session.query(Log).filter(Log.dag_id == dag.dag_id) + assert "('is_paused', True)" in dag_query.first().extra -def test_logging_unpuase_dag(admin_client, dags): +def test_logging_unpuase_dag(admin_client, dags, session): _, paused_dag = dags # is_paused=true mean unpause the dag admin_client.post(f"/paused?is_paused=true&dag_id={paused_dag.dag_id}", follow_redirects=True) - with create_session() as session: - dag_query = session.query(Log).filter(Log.dag_id == paused_dag.dag_id) - assert "('is_paused', False)" in dag_query.first().extra + dag_query = session.query(Log).filter(Log.dag_id == paused_dag.dag_id) + assert "('is_paused', False)" in dag_query.first().extra