2021-04-08 06:23:31 -04:00
|
|
|
# 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.
|
2022-10-31 08:32:49 -04:00
|
|
|
|
2022-10-24 17:07:21 -04:00
|
|
|
from random import randint
|
2021-04-08 06:23:31 -04:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2022-10-31 08:32:49 -04:00
|
|
|
import pytest
|
|
|
|
from flask_appbuilder.security.sqla.models import User
|
2021-04-08 06:23:31 -04:00
|
|
|
from freezegun import freeze_time
|
2023-05-12 03:47:14 -04:00
|
|
|
from freezegun.api import FakeDatetime
|
2021-04-08 06:23:31 -04:00
|
|
|
|
|
|
|
from superset.extensions import db
|
2022-07-26 20:01:56 -04:00
|
|
|
from superset.reports.models import ReportScheduleType
|
2022-10-24 17:07:21 -04:00
|
|
|
from superset.tasks.scheduler import execute, scheduler
|
2021-07-01 11:03:07 -04:00
|
|
|
from tests.integration_tests.reports.utils import insert_report_schedule
|
|
|
|
from tests.integration_tests.test_app import app
|
2021-04-08 06:23:31 -04:00
|
|
|
|
|
|
|
|
2022-10-31 08:32:49 -04:00
|
|
|
@pytest.fixture
|
2023-06-01 15:01:10 -04:00
|
|
|
def owners(get_user) -> list[User]:
|
2022-10-31 08:32:49 -04:00
|
|
|
return [get_user("admin")]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("owners")
|
2021-07-27 21:28:24 -04:00
|
|
|
@patch("superset.tasks.scheduler.execute.apply_async")
|
2022-10-31 08:32:49 -04:00
|
|
|
def test_scheduler_celery_timeout_ny(execute_mock, owners):
|
2021-04-08 06:23:31 -04:00
|
|
|
"""
|
2021-07-27 21:28:24 -04:00
|
|
|
Reports scheduler: Test scheduler setting celery soft and hard timeout
|
2021-04-08 06:23:31 -04:00
|
|
|
"""
|
|
|
|
with app.app_context():
|
2021-07-27 21:28:24 -04:00
|
|
|
report_schedule = insert_report_schedule(
|
|
|
|
type=ReportScheduleType.ALERT,
|
|
|
|
name="report",
|
|
|
|
crontab="0 4 * * *",
|
|
|
|
timezone="America/New_York",
|
2022-10-31 08:32:49 -04:00
|
|
|
owners=owners,
|
2021-07-27 21:28:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
with freeze_time("2020-01-01T09:00:00Z"):
|
|
|
|
scheduler()
|
|
|
|
assert execute_mock.call_args[1]["soft_time_limit"] == 3601
|
|
|
|
assert execute_mock.call_args[1]["time_limit"] == 3610
|
|
|
|
db.session.delete(report_schedule)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
2022-10-31 08:32:49 -04:00
|
|
|
@pytest.mark.usefixtures("owners")
|
2021-07-27 21:28:24 -04:00
|
|
|
@patch("superset.tasks.scheduler.execute.apply_async")
|
2022-10-31 08:32:49 -04:00
|
|
|
def test_scheduler_celery_no_timeout_ny(execute_mock, owners):
|
2021-07-27 21:28:24 -04:00
|
|
|
"""
|
|
|
|
Reports scheduler: Test scheduler setting celery soft and hard timeout
|
|
|
|
"""
|
|
|
|
with app.app_context():
|
|
|
|
app.config["ALERT_REPORTS_WORKING_TIME_OUT_KILL"] = False
|
|
|
|
report_schedule = insert_report_schedule(
|
|
|
|
type=ReportScheduleType.ALERT,
|
|
|
|
name="report",
|
|
|
|
crontab="0 4 * * *",
|
|
|
|
timezone="America/New_York",
|
2022-10-31 08:32:49 -04:00
|
|
|
owners=owners,
|
2021-07-27 21:28:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
with freeze_time("2020-01-01T09:00:00Z"):
|
|
|
|
scheduler()
|
|
|
|
assert execute_mock.call_args[1] == {"eta": FakeDatetime(2020, 1, 1, 9, 0)}
|
|
|
|
db.session.delete(report_schedule)
|
|
|
|
db.session.commit()
|
|
|
|
app.config["ALERT_REPORTS_WORKING_TIME_OUT_KILL"] = True
|
2021-04-08 06:23:31 -04:00
|
|
|
|
|
|
|
|
2022-10-31 08:32:49 -04:00
|
|
|
@pytest.mark.usefixtures("owners")
|
2021-04-08 06:23:31 -04:00
|
|
|
@patch("superset.tasks.scheduler.execute.apply_async")
|
2022-10-31 08:32:49 -04:00
|
|
|
def test_scheduler_celery_timeout_utc(execute_mock, owners):
|
2021-04-08 06:23:31 -04:00
|
|
|
"""
|
|
|
|
Reports scheduler: Test scheduler setting celery soft and hard timeout
|
|
|
|
"""
|
|
|
|
with app.app_context():
|
|
|
|
report_schedule = insert_report_schedule(
|
2021-07-27 21:28:24 -04:00
|
|
|
type=ReportScheduleType.ALERT,
|
|
|
|
name="report",
|
|
|
|
crontab="0 9 * * *",
|
|
|
|
timezone="UTC",
|
2022-10-31 08:32:49 -04:00
|
|
|
owners=owners,
|
2021-04-08 06:23:31 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
with freeze_time("2020-01-01T09:00:00Z"):
|
|
|
|
scheduler()
|
|
|
|
assert execute_mock.call_args[1]["soft_time_limit"] == 3601
|
|
|
|
assert execute_mock.call_args[1]["time_limit"] == 3610
|
|
|
|
db.session.delete(report_schedule)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
2022-10-31 08:32:49 -04:00
|
|
|
@pytest.mark.usefixtures("owners")
|
2021-04-08 06:23:31 -04:00
|
|
|
@patch("superset.tasks.scheduler.execute.apply_async")
|
2022-10-31 08:32:49 -04:00
|
|
|
def test_scheduler_celery_no_timeout_utc(execute_mock, owners):
|
2021-04-08 06:23:31 -04:00
|
|
|
"""
|
|
|
|
Reports scheduler: Test scheduler setting celery soft and hard timeout
|
|
|
|
"""
|
|
|
|
with app.app_context():
|
|
|
|
app.config["ALERT_REPORTS_WORKING_TIME_OUT_KILL"] = False
|
|
|
|
report_schedule = insert_report_schedule(
|
2021-07-27 21:28:24 -04:00
|
|
|
type=ReportScheduleType.ALERT,
|
|
|
|
name="report",
|
|
|
|
crontab="0 9 * * *",
|
|
|
|
timezone="UTC",
|
2022-10-31 08:32:49 -04:00
|
|
|
owners=owners,
|
2021-04-08 06:23:31 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
with freeze_time("2020-01-01T09:00:00Z"):
|
|
|
|
scheduler()
|
|
|
|
assert execute_mock.call_args[1] == {"eta": FakeDatetime(2020, 1, 1, 9, 0)}
|
|
|
|
db.session.delete(report_schedule)
|
|
|
|
db.session.commit()
|
2021-07-27 21:28:24 -04:00
|
|
|
app.config["ALERT_REPORTS_WORKING_TIME_OUT_KILL"] = True
|
2021-09-10 08:37:21 -04:00
|
|
|
|
|
|
|
|
2022-10-31 08:32:49 -04:00
|
|
|
@pytest.mark.usefixtures("owners")
|
2021-09-10 08:37:21 -04:00
|
|
|
@patch("superset.tasks.scheduler.is_feature_enabled")
|
|
|
|
@patch("superset.tasks.scheduler.execute.apply_async")
|
2022-10-31 08:32:49 -04:00
|
|
|
def test_scheduler_feature_flag_off(execute_mock, is_feature_enabled, owners):
|
2021-09-10 08:37:21 -04:00
|
|
|
"""
|
|
|
|
Reports scheduler: Test scheduler with feature flag off
|
|
|
|
"""
|
|
|
|
with app.app_context():
|
|
|
|
is_feature_enabled.return_value = False
|
|
|
|
report_schedule = insert_report_schedule(
|
|
|
|
type=ReportScheduleType.ALERT,
|
|
|
|
name="report",
|
|
|
|
crontab="0 9 * * *",
|
|
|
|
timezone="UTC",
|
2022-10-31 08:32:49 -04:00
|
|
|
owners=owners,
|
2021-09-10 08:37:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
with freeze_time("2020-01-01T09:00:00Z"):
|
|
|
|
scheduler()
|
|
|
|
execute_mock.assert_not_called()
|
|
|
|
db.session.delete(report_schedule)
|
|
|
|
db.session.commit()
|
2022-10-24 17:07:21 -04:00
|
|
|
|
|
|
|
|
2022-10-31 08:32:49 -04:00
|
|
|
@pytest.mark.usefixtures("owners")
|
2022-10-24 17:07:21 -04:00
|
|
|
@patch("superset.reports.commands.execute.AsyncExecuteReportScheduleCommand.__init__")
|
|
|
|
@patch("superset.reports.commands.execute.AsyncExecuteReportScheduleCommand.run")
|
|
|
|
@patch("superset.tasks.scheduler.execute.update_state")
|
2022-10-31 08:32:49 -04:00
|
|
|
def test_execute_task(update_state_mock, command_mock, init_mock, owners):
|
2022-10-24 17:07:21 -04:00
|
|
|
from superset.reports.commands.exceptions import ReportScheduleUnexpectedError
|
|
|
|
|
|
|
|
with app.app_context():
|
|
|
|
report_schedule = insert_report_schedule(
|
|
|
|
type=ReportScheduleType.ALERT,
|
|
|
|
name=f"report-{randint(0,1000)}",
|
|
|
|
crontab="0 4 * * *",
|
|
|
|
timezone="America/New_York",
|
2022-10-31 08:32:49 -04:00
|
|
|
owners=owners,
|
2022-10-24 17:07:21 -04:00
|
|
|
)
|
|
|
|
init_mock.return_value = None
|
|
|
|
command_mock.side_effect = ReportScheduleUnexpectedError("Unexpected error")
|
|
|
|
with freeze_time("2020-01-01T09:00:00Z"):
|
|
|
|
execute(report_schedule.id, "2020-01-01T09:00:00Z")
|
|
|
|
update_state_mock.assert_called_with(state="FAILURE")
|
|
|
|
|
|
|
|
db.session.delete(report_schedule)
|
|
|
|
db.session.commit()
|
2022-12-12 13:58:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures("owners")
|
|
|
|
@patch("superset.reports.commands.execute.AsyncExecuteReportScheduleCommand.__init__")
|
|
|
|
@patch("superset.reports.commands.execute.AsyncExecuteReportScheduleCommand.run")
|
|
|
|
@patch("superset.tasks.scheduler.execute.update_state")
|
|
|
|
@patch("superset.utils.log.logger")
|
|
|
|
def test_execute_task_with_command_exception(
|
|
|
|
logger_mock, update_state_mock, command_mock, init_mock, owners
|
|
|
|
):
|
|
|
|
from superset.commands.exceptions import CommandException
|
|
|
|
|
|
|
|
with app.app_context():
|
|
|
|
report_schedule = insert_report_schedule(
|
|
|
|
type=ReportScheduleType.ALERT,
|
|
|
|
name=f"report-{randint(0,1000)}",
|
|
|
|
crontab="0 4 * * *",
|
|
|
|
timezone="America/New_York",
|
|
|
|
owners=owners,
|
|
|
|
)
|
|
|
|
init_mock.return_value = None
|
|
|
|
command_mock.side_effect = CommandException("Unexpected error")
|
|
|
|
with freeze_time("2020-01-01T09:00:00Z"):
|
|
|
|
execute(report_schedule.id, "2020-01-01T09:00:00Z")
|
|
|
|
update_state_mock.assert_called_with(state="FAILURE")
|
|
|
|
logger_mock.exception.assert_called_with(
|
|
|
|
"A downstream exception occurred while generating a report: None. Unexpected error",
|
|
|
|
exc_info=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
db.session.delete(report_schedule)
|
|
|
|
db.session.commit()
|