forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sentry.py
102 lines (86 loc) · 2.99 KB
/
test_sentry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#
# 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 datetime
import unittest
from unittest.mock import MagicMock, Mock
from freezegun import freeze_time
from sentry_sdk import configure_scope
from airflow.models import TaskInstance
from airflow.sentry import ConfiguredSentry
from airflow.settings import Session
from airflow.utils import timezone
from airflow.utils.state import State
EXECUTION_DATE = timezone.utcnow()
DAG_ID = "test_dag"
TASK_ID = "test_task"
OPERATOR = "test_operator"
TRY_NUMBER = 1
STATE = State.SUCCESS
TEST_SCOPE = {
"dag_id": DAG_ID,
"task_id": TASK_ID,
"execution_date": EXECUTION_DATE,
"operator": OPERATOR,
"try_number": TRY_NUMBER,
}
TASK_DATA = {
"task_id": TASK_ID,
"state": STATE,
"operator": OPERATOR,
"duration": None,
}
CRUMB_DATE = datetime.datetime(2019, 5, 15)
CRUMB = {
"timestamp": CRUMB_DATE,
"type": "default",
"category": "completed_tasks",
"data": TASK_DATA,
"level": "info",
}
class TestSentryHook(unittest.TestCase):
def setUp(self):
self.sentry = ConfiguredSentry()
# Mock the Dag
self.dag = Mock(dag_id=DAG_ID, params=[])
self.dag.task_ids = [TASK_ID]
# Mock the task
self.task = Mock(dag=self.dag, dag_id=DAG_ID, task_id=TASK_ID, params=[], pool_slots=1)
self.task.__class__.__name__ = OPERATOR
self.ti = TaskInstance(self.task, execution_date=EXECUTION_DATE)
self.ti.operator = OPERATOR
self.ti.state = STATE
self.dag.get_task_instances = MagicMock(return_value=[self.ti])
self.session = Session()
def test_add_tagging(self):
"""
Test adding tags.
"""
self.sentry.add_tagging(task_instance=self.ti)
with configure_scope() as scope:
for key, value in scope._tags.items():
self.assertEqual(TEST_SCOPE[key], value)
@freeze_time(CRUMB_DATE.isoformat())
def test_add_breadcrumbs(self):
"""
Test adding breadcrumbs.
"""
self.sentry.add_tagging(task_instance=self.ti)
self.sentry.add_breadcrumbs(task_instance=self.ti, session=self.session)
with configure_scope() as scope:
test_crumb = scope._breadcrumbs.pop()
self.assertEqual(CRUMB, test_crumb)