forked from Yelp/paasta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_check_flink_services_health.py
269 lines (252 loc) · 9.78 KB
/
test_check_flink_services_health.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# Copyright 2015-2016 Yelp Inc.
#
# Licensed 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 mock
import pysensu_yelp
import pytest
from paasta_tools import check_flink_services_health
from paasta_tools import check_services_replication_tools
from paasta_tools.check_flink_services_health import check_under_registered_taskmanagers
from paasta_tools.utils import compose_job_id
check_flink_services_health.log = mock.Mock()
check_services_replication_tools.log = mock.Mock()
@pytest.fixture
def instance_config():
service = "fake_service"
instance = "fake_instance"
job_id = compose_job_id(service, instance)
mock_instance_config = mock.Mock(
service=service,
instance=instance,
cluster="fake_cluster",
soa_dir="fake_soa_dir",
job_id=job_id,
config_dict={},
)
mock_instance_config.get_replication_crit_percentage.return_value = 100
mock_instance_config.get_registrations.return_value = [job_id]
return mock_instance_config
@mock.patch(
"paasta_tools.flink_tools.get_flink_jobmanager_overview",
autospec=True,
return_value={"taskmanagers": 3},
)
def test_check_under_registered_taskmanagers_ok(mock_overview, instance_config):
under, output = check_under_registered_taskmanagers(
instance_config, expected_count=3
)
assert not under
assert (
"Service fake_service.fake_instance has 3 out of 3 expected instances of "
"taskmanager reported by dashboard!\n"
"(threshold: 100%)"
) in output
@mock.patch(
"paasta_tools.flink_tools.get_flink_jobmanager_overview",
autospec=True,
return_value={"taskmanagers": 2},
)
def test_check_under_registered_taskmanagers_under(mock_overview, instance_config):
under, output = check_under_registered_taskmanagers(
instance_config, expected_count=3
)
assert under
assert (
"Service fake_service.fake_instance has 2 out of 3 expected instances of "
"taskmanager reported by dashboard!\n"
"(threshold: 100%)"
) in output
assert (
"paasta status -s fake_service -i fake_instance -c fake_cluster -vv" in output
)
@mock.patch(
"paasta_tools.flink_tools.get_flink_jobmanager_overview",
autospec=True,
side_effect=ValueError("dummy exception"),
)
def test_check_under_registered_taskmanagers_error(mock_overview, instance_config):
under, output = check_under_registered_taskmanagers(
instance_config, expected_count=3
)
assert under
assert (
"Dashboard of service fake_service.fake_instance is not available!\n"
"(dummy exception)\n"
"What this alert"
) in output
assert (
"paasta status -s fake_service -i fake_instance -c fake_cluster -vv" in output
)
def test_check_flink_service_health_healthy(instance_config):
all_pods = []
with mock.patch(
"paasta_tools.check_flink_services_health.healthy_flink_containers_cnt",
autospec=True,
return_value=1,
), mock.patch(
"paasta_tools.check_flink_services_health.check_under_replication",
autospec=True,
return_value=(False, "OK"),
) as mock_check_under_replication, mock.patch(
"paasta_tools.check_flink_services_health.check_under_registered_taskmanagers",
autospec=True,
return_value=(False, "OK"),
) as mock_check_under_registered_taskmanagers, mock.patch(
"paasta_tools.check_flink_services_health.send_replication_event", autospec=True
) as mock_send_replication_event:
instance_config.config_dict["taskmanager"] = {"instances": 3}
check_flink_services_health.check_flink_service_health(
instance_config=instance_config,
all_tasks_or_pods=all_pods,
smartstack_replication_checker=None,
)
expected = [
mock.call(
instance_config=instance_config,
expected_count=1,
num_available=1,
sub_component="supervisor",
),
mock.call(
instance_config=instance_config,
expected_count=1,
num_available=1,
sub_component="jobmanager",
),
mock.call(
instance_config=instance_config,
expected_count=3,
num_available=1,
sub_component="taskmanager",
),
]
mock_check_under_replication.assert_has_calls(expected)
mock_check_under_registered_taskmanagers.assert_called_once_with(
instance_config=instance_config, expected_count=3,
)
mock_send_replication_event.assert_called_once_with(
instance_config=instance_config,
status=pysensu_yelp.Status.OK,
output="OK\n########\nOK\n########\nOK\n########\nOK",
)
def test_check_flink_service_health_too_few_taskmanagers(instance_config):
def check_under_replication_side_effect(*args, **kwargs):
if kwargs["sub_component"] == "supervisor":
return False, "OK"
if kwargs["sub_component"] == "jobmanager":
return False, "OK"
if kwargs["sub_component"] == "taskmanager":
return True, "NOPE"
all_pods = []
with mock.patch(
"paasta_tools.check_flink_services_health.healthy_flink_containers_cnt",
autospec=True,
return_value=1,
), mock.patch(
"paasta_tools.check_flink_services_health.check_under_registered_taskmanagers",
autospec=True,
return_value=(True, "NOPE"),
) as mock_check_under_registered_taskmanagers, mock.patch(
"paasta_tools.check_flink_services_health.check_under_replication",
autospec=True,
side_effect=check_under_replication_side_effect,
) as mock_check_under_replication, mock.patch(
"paasta_tools.check_flink_services_health.send_replication_event", autospec=True
) as mock_send_replication_event:
instance_config.config_dict["taskmanager"] = {"instances": 3}
check_flink_services_health.check_flink_service_health(
instance_config=instance_config,
all_tasks_or_pods=all_pods,
smartstack_replication_checker=None,
)
expected = [
mock.call(
instance_config=instance_config,
expected_count=1,
num_available=1,
sub_component="supervisor",
),
mock.call(
instance_config=instance_config,
expected_count=1,
num_available=1,
sub_component="jobmanager",
),
mock.call(
instance_config=instance_config,
expected_count=3,
num_available=1,
sub_component="taskmanager",
),
]
mock_check_under_replication.assert_has_calls(expected)
mock_check_under_registered_taskmanagers.assert_called_once_with(
instance_config=instance_config, expected_count=3,
)
mock_send_replication_event.assert_called_once_with(
instance_config=instance_config,
status=pysensu_yelp.Status.CRITICAL,
output="OK\n########\nOK\n########\nNOPE\n########\nNOPE",
)
def test_check_flink_service_health_under_registered_taskamanagers(instance_config):
all_pods = []
with mock.patch(
"paasta_tools.check_flink_services_health.healthy_flink_containers_cnt",
autospec=True,
return_value=1,
), mock.patch(
"paasta_tools.check_flink_services_health.check_under_replication",
autospec=True,
return_value=(False, "OK"),
) as mock_check_under_replication, mock.patch(
"paasta_tools.check_flink_services_health.check_under_registered_taskmanagers",
autospec=True,
return_value=(True, "NOPE"),
) as mock_check_under_registered_taskmanagers, mock.patch(
"paasta_tools.check_flink_services_health.send_replication_event", autospec=True
) as mock_send_replication_event:
instance_config.config_dict["taskmanager"] = {"instances": 3}
check_flink_services_health.check_flink_service_health(
instance_config=instance_config,
all_tasks_or_pods=all_pods,
smartstack_replication_checker=None,
)
expected = [
mock.call(
instance_config=instance_config,
expected_count=1,
num_available=1,
sub_component="supervisor",
),
mock.call(
instance_config=instance_config,
expected_count=1,
num_available=1,
sub_component="jobmanager",
),
mock.call(
instance_config=instance_config,
expected_count=3,
num_available=1,
sub_component="taskmanager",
),
]
mock_check_under_replication.assert_has_calls(expected)
mock_check_under_registered_taskmanagers.assert_called_once_with(
instance_config=instance_config, expected_count=3,
)
mock_send_replication_event.assert_called_once_with(
instance_config=instance_config,
status=pysensu_yelp.Status.CRITICAL,
output="OK\n########\nOK\n########\nOK\n########\nNOPE",
)