@@ -26,18 +26,16 @@ def test_service_checks_cannot_connect(aggregator):
2626 'json_resp, expected_healthy_status, expected_healthy_value' ,
2727 [({'status' : 'OK' }, AgentCheck .OK , 1 ), ({'status' : 'KO' }, AgentCheck .CRITICAL , 0 ), ({}, AgentCheck .CRITICAL , 0 )],
2828)
29- def test_service_checks_healthy_exp (aggregator , json_resp , expected_healthy_status , expected_healthy_value ):
29+ def test_service_checks_healthy_exp (aggregator , mock_http , json_resp , expected_healthy_status , expected_healthy_value ):
3030 instance = common .FULL_CONFIG ['instances' ][0 ]
3131 check = AirflowCheck ('airflow' , common .FULL_CONFIG , [instance ])
3232
33- with mock .patch ('datadog_checks.airflow.airflow.AirflowCheck._get_version' , return_value = None ):
34- mock_session = mock .MagicMock ()
35- with mock .patch ('datadog_checks.base.utils.http.requests.Session' , return_value = mock_session ):
36- mock_resp = mock .MagicMock (status_code = 200 )
37- mock_resp .json .side_effect = [json_resp ]
38- mock_session .get .return_value = mock_resp
33+ mock_resp = mock .MagicMock (status_code = 200 )
34+ mock_resp .json .side_effect = [json_resp ]
35+ mock_http .get .return_value = mock_resp
3936
40- check .check (None )
37+ with mock .patch ('datadog_checks.airflow.airflow.AirflowCheck._get_version' , return_value = None ):
38+ check .check (None )
4139
4240 tags = ['key:my-tag' , 'url:http://localhost:8080' ]
4341
@@ -54,65 +52,60 @@ def test_service_checks_healthy_exp(aggregator, json_resp, expected_healthy_stat
5452 ],
5553)
5654def test_service_checks_healthy_stable (
57- aggregator , metadb_status , scheduler_status , expected_healthy_status , expected_healthy_value
55+ aggregator , mock_http , metadb_status , scheduler_status , expected_healthy_status , expected_healthy_value
5856): # Stable is only defined in the context of Airflow 2
5957 instance = common .FULL_CONFIG ['instances' ][0 ]
6058 check = AirflowCheck ('airflow' , common .FULL_CONFIG , [instance ])
6159
62- with mock .patch ('datadog_checks.airflow.airflow.AirflowCheck._get_version' , return_value = '2.6.2' ):
63- mock_session = mock .MagicMock ()
64- with mock .patch ('datadog_checks.base.utils.http.requests.Session' , return_value = mock_session ):
65- mock_resp = mock .MagicMock (status_code = 200 )
66- mock_resp .json .side_effect = [
67- {'metadatabase' : {'status' : metadb_status }, 'scheduler' : {'status' : scheduler_status }},
68- {'status' : 'OK' },
69- ]
70- mock_session .get .return_value = mock_resp
60+ mock_resp = mock .MagicMock (status_code = 200 )
61+ mock_resp .json .side_effect = [
62+ {'metadatabase' : {'status' : metadb_status }, 'scheduler' : {'status' : scheduler_status }},
63+ {'status' : 'OK' },
64+ ]
65+ mock_http .get .return_value = mock_resp
7166
72- check .check (None )
67+ with mock .patch ('datadog_checks.airflow.airflow.AirflowCheck._get_version' , return_value = '2.6.2' ):
68+ check .check (None )
7369
7470 tags = ['key:my-tag' , 'url:http://localhost:8080' ]
7571
7672 aggregator .assert_service_check ('airflow.healthy' , expected_healthy_status , tags = tags , count = 1 )
7773 aggregator .assert_metric ('airflow.healthy' , expected_healthy_value , tags = tags , count = 1 )
7874
7975
80- def test_dag_total_tasks (aggregator , task_instance ):
76+ def test_dag_total_tasks (aggregator , mock_http , task_instance ):
8177 instance = common .FULL_CONFIG ['instances' ][0 ]
8278 check = AirflowCheck ('airflow' , common .FULL_CONFIG , [instance ])
8379
84- with mock .patch ('datadog_checks.airflow.airflow.AirflowCheck._get_version' , return_value = '2.6.2' ):
85- req = mock .MagicMock ()
86- with mock .patch ('datadog_checks.base.utils.http.requests.Session' , return_value = req ):
87- mock_resp = mock .MagicMock (status_code = 200 )
88- mock_resp .json .side_effect = [
89- {'metadatabase' : {'status' : 'healthy' }, 'scheduler' : {'status' : 'healthy' }},
90- task_instance ,
91- ]
92- req .get .return_value = mock_resp
80+ mock_resp = mock .MagicMock (status_code = 200 )
81+ mock_resp .json .side_effect = [
82+ {'metadatabase' : {'status' : 'healthy' }, 'scheduler' : {'status' : 'healthy' }},
83+ task_instance ,
84+ ]
85+ mock_http .get .return_value = mock_resp
9386
94- check .check (None )
87+ with mock .patch ('datadog_checks.airflow.airflow.AirflowCheck._get_version' , return_value = '2.6.2' ):
88+ check .check (None )
9589
9690 aggregator .assert_metric ('airflow.dag.task.total_running' , value = 1 , count = 1 )
9791
9892
99- def test_dag_task_ongoing_duration (aggregator , task_instance ):
93+ def test_dag_task_ongoing_duration (aggregator , mock_http , task_instance ):
10094 instance = common .FULL_CONFIG ['instances' ][0 ]
10195 check = AirflowCheck ('airflow' , common .FULL_CONFIG , [instance ])
10296
97+ mock_resp = mock .MagicMock (status_code = 200 )
98+ mock_resp .json .side_effect = [
99+ {'metadatabase' : {'status' : 'healthy' }, 'scheduler' : {'status' : 'healthy' }},
100+ ]
101+ mock_http .get .return_value = mock_resp
102+
103103 with mock .patch ('datadog_checks.airflow.airflow.AirflowCheck._get_version' , return_value = '2.6.2' ):
104- req = mock .MagicMock ()
105- with mock .patch ('datadog_checks.base.utils.http.requests.Session' , return_value = req ):
106- mock_resp = mock .MagicMock (status_code = 200 )
107- mock_resp .json .side_effect = [
108- {'metadatabase' : {'status' : 'healthy' }, 'scheduler' : {'status' : 'healthy' }},
109- ]
110- req .get .return_value = mock_resp
111- with mock .patch (
112- 'datadog_checks.airflow.airflow.AirflowCheck._get_all_task_instances' ,
113- return_value = task_instance .get ('task_instances' ),
114- ):
115- check .check (None )
104+ with mock .patch (
105+ 'datadog_checks.airflow.airflow.AirflowCheck._get_all_task_instances' ,
106+ return_value = task_instance .get ('task_instances' ),
107+ ):
108+ check .check (None )
116109
117110 aggregator .assert_metric (
118111 'airflow.dag.task.ongoing_duration' ,
@@ -141,23 +134,21 @@ def test_dag_task_ongoing_duration(aggregator, task_instance):
141134 ),
142135 ],
143136)
144- def test_config_collect_ongoing_duration (collect_ongoing_duration , should_call_method ):
137+ def test_config_collect_ongoing_duration (mock_http , collect_ongoing_duration , should_call_method ):
145138 instance = {** common .FULL_CONFIG ['instances' ][0 ], 'collect_ongoing_duration' : collect_ongoing_duration }
146139 check = AirflowCheck ('airflow' , common .FULL_CONFIG , [instance ])
147140
141+ mock_resp = mock .MagicMock (status_code = 200 )
142+ mock_resp .json .side_effect = [
143+ {'metadatabase' : {'status' : 'healthy' }, 'scheduler' : {'status' : 'healthy' }},
144+ ]
145+ mock_http .get .return_value = mock_resp
146+
148147 with mock .patch ('datadog_checks.airflow.airflow.AirflowCheck._get_version' , return_value = '2.6.2' ):
149- req = mock .MagicMock ()
150- with mock .patch ('datadog_checks.base.utils.http.requests.Session' , return_value = req ):
151- mock_resp = mock .MagicMock (status_code = 200 )
152- mock_resp .json .side_effect = [
153- {'metadatabase' : {'status' : 'healthy' }, 'scheduler' : {'status' : 'healthy' }},
154- ]
155- req .get .return_value = mock_resp
156-
157- with mock .patch (
158- 'datadog_checks.airflow.airflow.AirflowCheck._get_all_task_instances'
159- ) as mock_get_all_task_instances :
160- check .check (None )
161-
162- # Assert method calls
163- mock_get_all_task_instances .assert_has_calls (should_call_method , any_order = False )
148+ with mock .patch (
149+ 'datadog_checks.airflow.airflow.AirflowCheck._get_all_task_instances'
150+ ) as mock_get_all_task_instances :
151+ check .check (None )
152+
153+ # Assert method calls
154+ mock_get_all_task_instances .assert_has_calls (should_call_method , any_order = False )
0 commit comments