forked from Yelp/paasta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_check_marathon_services_replication.py
839 lines (776 loc) · 39 KB
/
test_check_marathon_services_replication.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
# 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.
from __future__ import absolute_import
from __future__ import unicode_literals
from datetime import datetime
from datetime import timedelta
import mock
import pysensu_yelp
from paasta_tools import check_marathon_services_replication
from paasta_tools.marathon_tools import MarathonServiceConfig
from paasta_tools.utils import compose_job_id
from paasta_tools.utils import SystemPaastaConfig
check_marathon_services_replication.log = mock.Mock()
def test_send_event_users_monitoring_tools_send_event_properly():
fake_service_name = 'superfast'
fake_namespace = 'jellyfish'
fake_status = '999999'
fake_output = 'YOU DID IT'
fake_cluster = 'fake_cluster'
fake_monitoring_overrides = {'fake_key': 'fake_value'}
fake_soa_dir = '/hi/hello/hey'
fake_cluster = 'fake_cluster'
expected_check_name = 'check_marathon_services_replication.%s' % compose_job_id(fake_service_name, fake_namespace)
with mock.patch(
"paasta_tools.monitoring_tools.send_event", autospec=True,
) as send_event_patch, mock.patch(
'paasta_tools.check_marathon_services_replication.load_system_paasta_config', autospec=True,
), mock.patch(
"paasta_tools.check_marathon_services_replication._log", autospec=True,
), mock.patch(
"paasta_tools.marathon_tools.load_marathon_service_config", autospec=True,
) as load_marathon_service_config_patch:
load_marathon_service_config_patch.return_value.get_monitoring.return_value = fake_monitoring_overrides
check_marathon_services_replication.send_event(fake_service_name,
fake_namespace,
fake_cluster,
fake_soa_dir,
fake_status,
fake_output)
send_event_patch.assert_called_once_with(
fake_service_name,
expected_check_name,
mock.ANY,
fake_status,
fake_output,
fake_soa_dir
)
# The overrides dictionary is mutated in the function under test, so
# we expect the send_event_patch to be called with something that is a
# superset of what we originally put in (fake_monitoring_overrides)
actual_overrides_used = send_event_patch.call_args[0][2]
assert set({'alert_after': '2m'}.items()).issubset(set(actual_overrides_used.items()))
assert 'runbook' in actual_overrides_used
def test_send_event_users_monitoring_tools_send_event_respects_alert_after():
fake_service_name = 'superfast'
fake_namespace = 'jellyfish'
fake_status = '999999'
fake_output = 'YOU DID IT'
fake_cluster = 'fake_cluster'
fake_monitoring_overrides = {'alert_after': '666m'}
fake_soa_dir = '/hi/hello/hey'
fake_cluster = 'fake_cluster'
expected_check_name = 'check_marathon_services_replication.%s' % compose_job_id(fake_service_name, fake_namespace)
with mock.patch(
"paasta_tools.monitoring_tools.send_event", autospec=True,
) as send_event_patch, mock.patch(
'paasta_tools.check_marathon_services_replication.load_system_paasta_config', autospec=True,
), mock.patch(
"paasta_tools.check_marathon_services_replication._log", autospec=True,
), mock.patch(
"paasta_tools.marathon_tools.load_marathon_service_config", autospec=True,
) as load_marathon_service_config_patch:
load_marathon_service_config_patch.return_value.get_monitoring.return_value = fake_monitoring_overrides
check_marathon_services_replication.send_event(fake_service_name,
fake_namespace,
fake_cluster,
fake_soa_dir,
fake_status,
fake_output)
send_event_patch.call_count == 1
send_event_patch.assert_called_once_with(
fake_service_name,
expected_check_name,
mock.ANY,
fake_status,
fake_output,
fake_soa_dir
)
# The overrides dictionary is mutated in the function under test, so
# we expect the send_event_patch to be called with something that is a
# superset of what we originally put in (fake_monitoring_overrides)
actual_overrides_used = send_event_patch.call_args[0][2]
assert set({'alert_after': '666m'}.items()).issubset(set(actual_overrides_used.items()))
assert not set({'alert_after': '2m'}.items()).issubset(set(actual_overrides_used.items()))
def test_check_smartstack_replication_for_instance_ok_when_expecting_zero():
service = 'test'
instance = 'main'
cluster = 'fake_cluster'
available = {'fake_region': {'test.main': 1, 'test.three': 4, 'test.four': 8}}
expected_replication_count = 0
soa_dir = 'test_dir'
fake_system_paasta_config = SystemPaastaConfig({}, '/fake/config')
crit = 90
with mock.patch(
'paasta_tools.check_marathon_services_replication.send_event', autospec=True,
) as mock_send_event, mock.patch(
'paasta_tools.marathon_tools.read_registration_for_service_instance',
autospec=True, return_value=compose_job_id(service, instance),
), mock.patch(
'paasta_tools.check_marathon_services_replication.load_smartstack_info_for_service', autospec=True,
) as mock_load_smartstack_info_for_service, mock.patch(
'paasta_tools.marathon_tools.load_marathon_service_config', autospec=True,
) as mock_load_marathon_service_config:
mock_load_smartstack_info_for_service.return_value = available
mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
mock_service_job_config.get_replication_crit_percentage.return_value = crit
mock_load_marathon_service_config.return_value = mock_service_job_config
check_marathon_services_replication.check_smartstack_replication_for_instance(
service, instance, cluster, soa_dir, expected_replication_count, fake_system_paasta_config,
)
mock_send_event.assert_called_once_with(
service=service,
namespace=instance,
cluster=cluster,
soa_dir=soa_dir,
status=pysensu_yelp.Status.OK,
output=mock.ANY,
)
def test_check_smartstack_replication_for_instance_crit_when_absent():
service = 'test'
instance = 'some_absent_instance'
cluster = 'fake_cluster'
available = {'fake_region': {'test.two': 1, 'test.three': 4, 'test.four': 8}}
expected_replication_count = 8
soa_dir = 'test_dir'
fake_system_paasta_config = SystemPaastaConfig({}, '/fake/config')
crit = 90
with mock.patch(
'paasta_tools.check_marathon_services_replication.send_event', autospec=True,
) as mock_send_event, mock.patch(
'paasta_tools.marathon_tools.read_registration_for_service_instance',
autospec=True, return_value=compose_job_id(service, instance),
), mock.patch(
'paasta_tools.check_marathon_services_replication.load_smartstack_info_for_service', autospec=True,
) as mock_load_smartstack_info_for_service, mock.patch(
'paasta_tools.marathon_tools.load_marathon_service_config', autospec=True,
) as mock_load_marathon_service_config:
mock_load_smartstack_info_for_service.return_value = available
mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
mock_service_job_config.get_replication_crit_percentage.return_value = crit
mock_load_marathon_service_config.return_value = mock_service_job_config
check_marathon_services_replication.check_smartstack_replication_for_instance(
service, instance, cluster, soa_dir, expected_replication_count, fake_system_paasta_config,
)
mock_send_event.assert_called_once_with(
service=service,
namespace=instance,
cluster=cluster,
soa_dir=soa_dir,
status=pysensu_yelp.Status.CRITICAL,
output=mock.ANY)
def test_check_smartstack_replication_for_instance_crit_when_zero_replication():
service = 'test'
instance = 'zero_running'
cluster = 'fake_cluster'
available = {'fake_region': {'test.zero_running': 0, 'test.main': 8, 'test.fully_replicated': 8}}
expected_replication_count = 8
soa_dir = 'test_dir'
fake_system_paasta_config = SystemPaastaConfig({}, '/fake/config')
crit = 90
with mock.patch(
'paasta_tools.check_marathon_services_replication.send_event', autospec=True,
) as mock_send_event, mock.patch(
'paasta_tools.marathon_tools.read_registration_for_service_instance',
autospec=True, return_value=compose_job_id(service, instance),
), mock.patch(
'paasta_tools.check_marathon_services_replication.load_smartstack_info_for_service', autospec=True,
) as mock_load_smartstack_info_for_service, mock.patch(
'paasta_tools.marathon_tools.load_marathon_service_config', autospec=True,
) as mock_load_marathon_service_config:
mock_load_smartstack_info_for_service.return_value = available
mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
mock_service_job_config.get_replication_crit_percentage.return_value = crit
mock_load_marathon_service_config.return_value = mock_service_job_config
check_marathon_services_replication.check_smartstack_replication_for_instance(
service, instance, cluster, soa_dir, expected_replication_count, fake_system_paasta_config,
)
mock_send_event.assert_called_once_with(
service=service,
namespace=instance,
cluster=cluster,
soa_dir=soa_dir,
status=pysensu_yelp.Status.CRITICAL,
output=mock.ANY,
)
_, send_event_kwargs = mock_send_event.call_args
alert_output = send_event_kwargs["output"]
assert 'Service test.zero_running has 0 out of 8 expected instances in fake_region' in alert_output
assert "paasta status -s test -i zero_running -c fake_cluster -vv" in alert_output
def test_check_smartstack_replication_for_instance_crit_when_low_replication():
service = 'test'
instance = 'not_enough'
cluster = 'fake_cluster'
available = {'fake_region': {'test.canary': 1, 'test.not_enough': 4, 'test.fully_replicated': 8}}
expected_replication_count = 8
soa_dir = 'test_dir'
fake_system_paasta_config = SystemPaastaConfig({}, '/fake/config')
crit = 90
with mock.patch(
'paasta_tools.check_marathon_services_replication.send_event', autospec=True,
) as mock_send_event, mock.patch(
'paasta_tools.marathon_tools.read_registration_for_service_instance',
autospec=True, return_value=compose_job_id(service, instance),
), mock.patch(
'paasta_tools.check_marathon_services_replication.load_smartstack_info_for_service', autospec=True,
) as mock_load_smartstack_info_for_service, mock.patch(
'paasta_tools.marathon_tools.load_marathon_service_config', autospec=True,
) as mock_load_marathon_service_config:
mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
mock_service_job_config.get_replication_crit_percentage.return_value = crit
mock_load_marathon_service_config.return_value = mock_service_job_config
mock_load_smartstack_info_for_service.return_value = available
check_marathon_services_replication.check_smartstack_replication_for_instance(
service, instance, cluster, soa_dir, expected_replication_count, fake_system_paasta_config,
)
mock_send_event.assert_called_once_with(
service=service,
namespace=instance,
cluster=cluster,
soa_dir=soa_dir,
status=pysensu_yelp.Status.CRITICAL,
output=mock.ANY,
)
_, send_event_kwargs = mock_send_event.call_args
alert_output = send_event_kwargs["output"]
assert 'Service test.not_enough has 4 out of 8 expected instances in fake_region' in alert_output
assert "paasta status -s test -i not_enough -c fake_cluster -vv" in alert_output
def test_check_smartstack_replication_for_instance_ok_with_enough_replication():
service = 'test'
instance = 'everything_up'
cluster = 'fake_cluster'
available = {'fake_region': {'test.canary': 1, 'test.low_replication': 4, 'test.everything_up': 8}}
expected_replication_count = 8
soa_dir = 'test_dir'
fake_system_paasta_config = SystemPaastaConfig({}, '/fake/config')
crit = 90
with mock.patch(
'paasta_tools.check_marathon_services_replication.send_event', autospec=True,
) as mock_send_event, mock.patch(
'paasta_tools.marathon_tools.read_registration_for_service_instance',
autospec=True, return_value=compose_job_id(service, instance),
), mock.patch(
'paasta_tools.check_marathon_services_replication.load_smartstack_info_for_service', autospec=True,
) as mock_load_smartstack_info_for_service, mock.patch(
'paasta_tools.marathon_tools.load_marathon_service_config', autospec=True,
) as mock_load_marathon_service_config:
mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
mock_service_job_config.get_replication_crit_percentage.return_value = crit
mock_load_marathon_service_config.return_value = mock_service_job_config
mock_load_smartstack_info_for_service.return_value = available
check_marathon_services_replication.check_smartstack_replication_for_instance(
service, instance, cluster, soa_dir, expected_replication_count, fake_system_paasta_config,
)
mock_send_event.assert_called_once_with(
service=service,
namespace=instance,
cluster=cluster,
soa_dir=soa_dir,
status=pysensu_yelp.Status.OK,
output=mock.ANY,
)
_, send_event_kwargs = mock_send_event.call_args
alert_output = send_event_kwargs["output"]
assert "test.everything_up has 8 out of 8 expected instances in fake_region (OK: 100%)" in alert_output
def test_check_smartstack_replication_for_instance_ignores_things_under_a_different_namespace():
service = 'test'
instance = 'main'
namespace = 'canary'
cluster = 'fake_cluster'
available = {'fake_region': {'test.canary': 1, 'test.main': 4, 'test.fully_replicated': 8}}
expected_replication_count = 8
soa_dir = 'test_dir'
fake_system_paasta_config = SystemPaastaConfig({}, '/fake/config')
crit = 90
with mock.patch(
'paasta_tools.check_marathon_services_replication.send_event_if_under_replication', autospec=True,
) as mock_send_event_if_under_replication, mock.patch(
'paasta_tools.marathon_tools.read_registration_for_service_instance',
autospec=True, return_value=namespace,
), mock.patch(
'paasta_tools.check_marathon_services_replication.load_smartstack_info_for_service', autospec=True,
) as mock_load_smartstack_info_for_service, mock.patch(
'paasta_tools.marathon_tools.load_marathon_service_config', autospec=True,
) as mock_load_marathon_service_config:
mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
mock_service_job_config.get_replication_crit_percentage.return_value = crit
mock_load_marathon_service_config.return_value = mock_service_job_config
mock_load_smartstack_info_for_service.return_value = available
check_marathon_services_replication.check_smartstack_replication_for_instance(
service, instance, cluster, soa_dir, expected_replication_count, fake_system_paasta_config,
)
mock_send_event_if_under_replication.call_count == 0
def test_check_smartstack_replication_for_instance_ok_with_enough_replication_multilocation():
service = 'test'
instance = 'everything_up'
cluster = 'fake_cluster'
available = {'fake_region': {'test.everything_up': 1}, 'fake_other_region': {'test.everything_up': 1}}
expected_replication_count = 2
soa_dir = 'test_dir'
fake_system_paasta_config = SystemPaastaConfig({}, '/fake/config')
crit = 90
with mock.patch(
'paasta_tools.check_marathon_services_replication.send_event', autospec=True,
) as mock_send_event, mock.patch(
'paasta_tools.marathon_tools.read_registration_for_service_instance',
autospec=True, return_value=compose_job_id(service, instance),
), mock.patch(
'paasta_tools.check_marathon_services_replication.load_smartstack_info_for_service', autospec=True,
) as mock_load_smartstack_info_for_service, mock.patch(
'paasta_tools.marathon_tools.load_marathon_service_config', autospec=True,
) as mock_load_marathon_service_config:
mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
mock_service_job_config.get_replication_crit_percentage.return_value = crit
mock_load_marathon_service_config.return_value = mock_service_job_config
mock_load_smartstack_info_for_service.return_value = available
check_marathon_services_replication.check_smartstack_replication_for_instance(
service, instance, cluster, soa_dir, expected_replication_count, fake_system_paasta_config,
)
mock_send_event.assert_called_once_with(
service=service,
namespace=instance,
cluster=cluster,
soa_dir=soa_dir,
status=pysensu_yelp.Status.OK,
output=mock.ANY,
)
_, send_event_kwargs = mock_send_event.call_args
alert_output = send_event_kwargs["output"]
assert "test.everything_up has 1 out of 1 expected instances in fake_region" in alert_output
assert "test.everything_up has 1 out of 1 expected instances in fake_other_region" in alert_output
def test_check_smartstack_replication_for_instance_crit_when_low_replication_multilocation():
service = 'test'
instance = 'low_replication'
cluster = 'fake_cluster'
available = {'fake_region': {'test.low_replication': 1}, 'fake_other_region': {'test.low_replication': 0}}
expected_replication_count = 2
soa_dir = 'test_dir'
fake_system_paasta_config = SystemPaastaConfig({}, '/fake/config')
crit = 90
with mock.patch(
'paasta_tools.check_marathon_services_replication.send_event', autospec=True,
) as mock_send_event, mock.patch(
'paasta_tools.marathon_tools.read_registration_for_service_instance',
autospec=True, return_value=compose_job_id(service, instance),
), mock.patch(
'paasta_tools.check_marathon_services_replication.load_smartstack_info_for_service', autospec=True,
) as mock_load_smartstack_info_for_service, mock.patch(
'paasta_tools.marathon_tools.load_marathon_service_config', autospec=True,
) as mock_load_marathon_service_config:
mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
mock_service_job_config.get_replication_crit_percentage.return_value = crit
mock_load_marathon_service_config.return_value = mock_service_job_config
mock_load_smartstack_info_for_service.return_value = available
check_marathon_services_replication.check_smartstack_replication_for_instance(
service, instance, cluster, soa_dir, expected_replication_count, fake_system_paasta_config,
)
mock_send_event.assert_called_once_with(
service=service,
namespace=instance,
cluster=cluster,
soa_dir=soa_dir,
status=pysensu_yelp.Status.CRITICAL,
output=mock.ANY,
)
_, send_event_kwargs = mock_send_event.call_args
alert_output = send_event_kwargs["output"]
assert "test.low_replication has 1 out of 1 expected instances in fake_region" in alert_output
assert "test.low_replication has 0 out of 1 expected instances in fake_other_region" in alert_output
assert "paasta status -s test -i low_replication -c fake_cluster -vv" in alert_output
def test_check_smartstack_replication_for_instance_crit_when_zero_replication_multilocation():
service = 'test'
instance = 'zero_running'
cluster = 'fake_cluster'
available = {'fake_region': {'test.zero_running': 0}, 'fake_other_region': {'test.zero_running': 0}}
expected_replication_count = 2
soa_dir = 'test_dir'
fake_system_paasta_config = SystemPaastaConfig({}, '/fake/config')
crit = 90
with mock.patch(
'paasta_tools.check_marathon_services_replication.send_event', autospec=True,
) as mock_send_event, mock.patch(
'paasta_tools.marathon_tools.read_registration_for_service_instance',
autospec=True, return_value=compose_job_id(service, instance),
), mock.patch(
'paasta_tools.check_marathon_services_replication.load_smartstack_info_for_service', autospec=True,
) as mock_load_smartstack_info_for_service, mock.patch(
'paasta_tools.marathon_tools.load_marathon_service_config', autospec=True,
) as mock_load_marathon_service_config:
mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
mock_service_job_config.get_replication_crit_percentage.return_value = crit
mock_load_marathon_service_config.return_value = mock_service_job_config
mock_load_smartstack_info_for_service.return_value = available
check_marathon_services_replication.check_smartstack_replication_for_instance(
service, instance, cluster, soa_dir, expected_replication_count, fake_system_paasta_config,
)
mock_send_event.assert_called_once_with(
service=service,
namespace=instance,
cluster=cluster,
soa_dir=soa_dir,
status=pysensu_yelp.Status.CRITICAL,
output=mock.ANY,
)
_, send_event_kwargs = mock_send_event.call_args
alert_output = send_event_kwargs["output"]
assert "test.zero_running has 0 out of 1 expected instances in fake_region (CRITICAL: 0%)" in alert_output
assert "test.zero_running has 0 out of 1 expected instances in fake_other_region (CRITICAL: 0%)" in alert_output
assert "paasta status -s test -i zero_running -c fake_cluster -vv" in alert_output
def test_check_smartstack_replication_for_instance_crit_when_missing_replication_multilocation():
service = 'test'
instance = 'missing_instance'
cluster = 'fake_cluster'
available = {'fake_region': {'test.main': 0}, 'fake_other_region': {'test.main': 0}}
expected_replication_count = 2
soa_dir = 'test_dir'
fake_system_paasta_config = SystemPaastaConfig({}, '/fake/config')
crit = 90
with mock.patch(
'paasta_tools.check_marathon_services_replication.send_event', autospec=True,
) as mock_send_event, mock.patch(
'paasta_tools.marathon_tools.read_registration_for_service_instance',
autospec=True, return_value=compose_job_id(service, instance),
), mock.patch(
'paasta_tools.check_marathon_services_replication.load_smartstack_info_for_service', autospec=True,
) as mock_load_smartstack_info_for_service, mock.patch(
'paasta_tools.marathon_tools.load_marathon_service_config', autospec=True,
) as mock_load_marathon_service_config:
mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
mock_service_job_config.get_replication_crit_percentage.return_value = crit
mock_load_marathon_service_config.return_value = mock_service_job_config
mock_load_smartstack_info_for_service.return_value = available
check_marathon_services_replication.check_smartstack_replication_for_instance(
service, instance, cluster, soa_dir, expected_replication_count, fake_system_paasta_config,
)
mock_send_event.assert_called_once_with(
service=service,
namespace=instance,
cluster=cluster,
soa_dir=soa_dir,
status=pysensu_yelp.Status.CRITICAL,
output=mock.ANY,
)
_, send_event_kwargs = mock_send_event.call_args
alert_output = send_event_kwargs["output"]
assert "test.missing_instance has 0 out of 1 expected instances in fake_region" in alert_output
assert "test.missing_instance has 0 out of 1 expected instances in fake_other_region" in alert_output
def test_check_smartstack_replication_for_instance_crit_when_no_smartstack_info():
service = 'test'
instance = 'some_instance'
cluster = 'fake_cluster'
available = {}
expected_replication_count = 2
soa_dir = 'test_dir'
fake_system_paasta_config = SystemPaastaConfig({}, '/fake/config')
crit = 90
with mock.patch(
'paasta_tools.check_marathon_services_replication.send_event', autospec=True,
) as mock_send_event, mock.patch(
'paasta_tools.marathon_tools.read_registration_for_service_instance',
autospec=True, return_value=compose_job_id(service, instance),
), mock.patch(
'paasta_tools.check_marathon_services_replication.load_smartstack_info_for_service', autospec=True,
) as mock_load_smartstack_info_for_service, mock.patch(
'paasta_tools.marathon_tools.load_marathon_service_config', autospec=True,
) as mock_load_marathon_service_config:
mock_load_smartstack_info_for_service.return_value = available
mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
mock_service_job_config.get_replication_crit_percentage.return_value = crit
mock_load_marathon_service_config.return_value = mock_service_job_config
check_marathon_services_replication.check_smartstack_replication_for_instance(
service, instance, cluster, soa_dir, expected_replication_count, fake_system_paasta_config
)
mock_send_event.assert_called_once_with(
service=service,
namespace=instance,
cluster=cluster,
soa_dir=soa_dir,
status=pysensu_yelp.Status.CRITICAL,
output=mock.ANY,
)
_, send_event_kwargs = mock_send_event.call_args
alert_output = send_event_kwargs["output"]
assert "test.some_instance has no Smartstack replication info." in alert_output
def test_check_service_replication_for_normal_smartstack():
service = 'test_service'
instance = 'test_instance'
cluster = 'fake_cluster'
fake_system_paasta_config = SystemPaastaConfig({}, '/fake/config')
all_tasks = []
with mock.patch(
'paasta_tools.marathon_tools.get_proxy_port_for_instance',
autospec=True, return_value=666,
), mock.patch(
'paasta_tools.marathon_tools.get_expected_instance_count_for_namespace',
autospec=True, return_value=100,
), mock.patch(
'paasta_tools.check_marathon_services_replication.check_smartstack_replication_for_instance',
autospec=True,
) as mock_check_smartstack_replication_for_service:
mock_client = mock.Mock()
check_marathon_services_replication.check_service_replication(
client=mock_client, service=service, instance=instance, cluster=cluster, soa_dir=None,
system_paasta_config=fake_system_paasta_config, all_tasks=all_tasks)
mock_check_smartstack_replication_for_service.assert_called_once_with(
service=service,
instance=instance,
cluster=cluster,
soa_dir=None,
expected_count=100,
system_paasta_config=fake_system_paasta_config,
)
def test_check_service_replication_for_non_smartstack():
service = 'test_service'
instance = 'worker'
cluster = 'fake_cluster'
fake_system_paasta_config = SystemPaastaConfig({}, '/fake/config')
with mock.patch(
'paasta_tools.marathon_tools.get_proxy_port_for_instance', autospec=True, return_value=None,
), mock.patch(
'paasta_tools.marathon_tools.get_expected_instance_count_for_namespace',
autospec=True, return_value=100,
), mock.patch(
'paasta_tools.check_marathon_services_replication.check_healthy_marathon_tasks_for_service_instance',
autospec=True,
) as mock_check_healthy_marathon_tasks:
mock_client = mock.Mock()
check_marathon_services_replication.check_service_replication(
client=mock_client,
all_tasks=[],
service=service,
instance=instance,
cluster=cluster,
soa_dir=None,
system_paasta_config=fake_system_paasta_config
)
mock_check_healthy_marathon_tasks.assert_called_once_with(
service=service,
instance=instance,
cluster=cluster,
soa_dir=None,
expected_count=100,
all_tasks=[]
)
def _make_fake_task(app_id, **kwargs):
kwargs.setdefault('started_at', datetime(1991, 7, 5, 6, 13, 0))
return mock.Mock(app_id=app_id, **kwargs)
def test_filter_healthy_marathon_instances_for_short_app_id_correctly_counts_alive_tasks():
fakes = []
for i in range(0, 4):
fake_task = _make_fake_task('/service.instance.foo%s.bar%s' % (i, i))
mock_result = mock.Mock(alive=i % 2 == 0)
fake_task.health_check_results = [mock_result]
fakes.append(fake_task)
actual = check_marathon_services_replication.filter_healthy_marathon_instances_for_short_app_id(
app_id='service.instance',
all_tasks=fakes
)
assert actual == 2
def test_filter_healthy_marathon_instances_for_short_app_id_considers_new_tasks_not_healthy_yet():
one_minute = timedelta(minutes=1)
fakes = []
for i in range(0, 4):
fake_task = _make_fake_task(
'/service.instance.foo%s.bar%s' % (i, i),
# when i == 0, produces a task that has just started (not healthy yet)
# otherwise produces a task that was started over a minute ago (healthy)
started_at=datetime.now() - one_minute * i,
)
mock_result = mock.Mock(alive=True)
fake_task.health_check_results = [mock_result]
fakes.append(fake_task)
actual = check_marathon_services_replication.filter_healthy_marathon_instances_for_short_app_id(
all_tasks=fakes,
app_id='service.instance'
)
assert actual == 3
def test_get_healthy_marathon_instances_for_short_app_id_considers_none_start_time_unhealthy():
fake_task = _make_fake_task('/service.instance.foo.bar', started_at=None)
mock_result = mock.Mock(alive=True)
fake_task.health_check_results = [mock_result]
fakes = [fake_task]
actual = check_marathon_services_replication.filter_healthy_marathon_instances_for_short_app_id(
all_tasks=fakes,
app_id='service.instance',
)
assert actual == 0
@mock.patch('paasta_tools.check_marathon_services_replication.send_event_if_under_replication', autospec=True)
@mock.patch('paasta_tools.check_marathon_services_replication.filter_healthy_marathon_instances_for_short_app_id', autospec=True) # noqa
def test_check_healthy_marathon_tasks_for_service_instance(mock_healthy_instances,
mock_send_event_if_under_replication):
service = 'service'
instance = 'instance'
cluster = 'cluster'
soa_dir = 'soa_dir'
mock_healthy_instances.return_value = 2
check_marathon_services_replication.check_healthy_marathon_tasks_for_service_instance(
service=service,
instance=instance,
cluster=cluster,
soa_dir=soa_dir,
expected_count=10,
all_tasks=mock.Mock(),
)
mock_send_event_if_under_replication.assert_called_once_with(
service=service,
instance=instance,
cluster=cluster,
expected_count=10,
num_available=2,
soa_dir=soa_dir
)
def test_check_service_replication_for_namespace_with_no_deployments():
service = 'test_service'
instance = 'worker'
cluster = 'fake_cluster'
fake_system_paasta_config = SystemPaastaConfig({}, '/fake/config')
with mock.patch(
'paasta_tools.marathon_tools.get_proxy_port_for_instance', autospec=True, return_value=None,
) as mock_get_proxy_port_for_instance, mock.patch(
'paasta_tools.marathon_tools.get_expected_instance_count_for_namespace',
autospec=True,
) as mock_get_expected_count:
mock_client = mock.Mock()
mock_get_expected_count.side_effect = check_marathon_services_replication.NoDeploymentsAvailable
check_marathon_services_replication.check_service_replication(
client=mock_client, service=service, instance=instance, cluster=cluster, soa_dir=None,
system_paasta_config=fake_system_paasta_config, all_tasks=[])
assert mock_get_proxy_port_for_instance.call_count == 0
def test_send_event_if_under_replication_handles_0_expected():
service = 'test_service'
instance = 'worker'
cluster = 'fake_cluster'
crit = 90
expected_count = 0
available = 0
soa_dir = '/dne'
with mock.patch(
'paasta_tools.check_marathon_services_replication.send_event', autospec=True,
) as mock_send_event, mock.patch(
'paasta_tools.marathon_tools.load_marathon_service_config', autospec=True,
) as mock_load_marathon_service_config:
mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
mock_service_job_config.get_replication_crit_percentage.return_value = crit
mock_load_marathon_service_config.return_value = mock_service_job_config
check_marathon_services_replication.send_event_if_under_replication(
service, instance, cluster, expected_count, available, soa_dir)
mock_send_event.assert_called_once_with(
service=service,
namespace=instance,
cluster=cluster,
soa_dir=soa_dir,
status=0,
output=mock.ANY,
)
_, send_event_kwargs = mock_send_event.call_args
alert_output = send_event_kwargs["output"]
assert "test_service.worker has 0 out of 0 expected instances available!\n(threshold: 90%)" in alert_output
def test_send_event_if_under_replication_good():
service = 'test_service'
instance = 'worker'
cluster = 'fake_cluster'
crit = 90
expected_count = 100
available = 100
soa_dir = '/dne'
with mock.patch(
'paasta_tools.check_marathon_services_replication.send_event', autospec=True,
) as mock_send_event, mock.patch(
'paasta_tools.marathon_tools.load_marathon_service_config', autospec=True,
) as mock_load_marathon_service_config:
mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
mock_service_job_config.get_replication_crit_percentage.return_value = crit
mock_load_marathon_service_config.return_value = mock_service_job_config
check_marathon_services_replication.send_event_if_under_replication(
service, instance, cluster, expected_count, available, soa_dir)
mock_send_event.assert_called_once_with(
service=service,
namespace=instance,
cluster=cluster,
soa_dir=soa_dir,
status=0,
output=mock.ANY,
)
_, send_event_kwargs = mock_send_event.call_args
alert_output = send_event_kwargs["output"]
assert "test_service.worker has 100 out of 100 expected instances available!\n(threshold: 90%)" in alert_output
def test_send_event_if_under_replication_critical():
service = 'test_service'
instance = 'worker'
cluster = 'fake_cluster'
crit = 90
expected_count = 100
available = 89
soa_dir = '/dne'
with mock.patch(
'paasta_tools.check_marathon_services_replication.send_event', autospec=True,
) as mock_send_event, mock.patch(
'paasta_tools.marathon_tools.load_marathon_service_config', autospec=True,
) as mock_load_marathon_service_config:
mock_service_job_config = mock.MagicMock(spec_set=MarathonServiceConfig)
mock_service_job_config.get_replication_crit_percentage.return_value = crit
mock_load_marathon_service_config.return_value = mock_service_job_config
check_marathon_services_replication.send_event_if_under_replication(
service=service,
instance=instance,
cluster=cluster,
expected_count=expected_count,
num_available=available,
soa_dir=soa_dir)
mock_send_event.assert_called_once_with(
service=service,
namespace=instance,
cluster=cluster,
soa_dir=soa_dir,
status=2,
output=mock.ANY,
)
_, send_event_kwargs = mock_send_event.call_args
alert_output = send_event_kwargs["output"]
assert "test_service.worker has 89 out of 100 expected instances available!\n(threshold: 90%)" in alert_output
assert "paasta status -s test_service -i worker -c fake_cluster -vv" in alert_output
def test_main():
soa_dir = 'anw'
crit = 1
services = [('a', 'main'), ('b', 'main'), ('c', 'main')]
args = mock.Mock(soa_dir=soa_dir, crit=crit, verbose=False)
with mock.patch(
'paasta_tools.check_marathon_services_replication.parse_args',
return_value=args, autospec=True,
) as mock_parse_args, mock.patch(
'paasta_tools.check_marathon_services_replication.get_services_for_cluster',
return_value=services, autospec=True,
) as mock_get_services_for_cluster, mock.patch(
'paasta_tools.check_marathon_services_replication.check_service_replication',
autospec=True,
), mock.patch(
'paasta_tools.check_marathon_services_replication.load_system_paasta_config',
autospec=True,
) as mock_load_system_paasta_config, mock.patch(
'paasta_tools.check_marathon_services_replication.marathon_tools.load_marathon_config',
autospec=True,
) as mock_load_marathon_config, mock.patch(
'paasta_tools.check_marathon_services_replication.marathon_tools.get_marathon_client',
autospec=True,
) as mock_get_marathon_client:
mock_client = mock.Mock()
mock_client.list_tasks.return_value = []
mock_get_marathon_client.return_value = mock_client
mock_config = mock.Mock()
mock_load_marathon_config.return_value = mock_config
mock_load_system_paasta_config.return_value.get_cluster = mock.Mock(return_value='fake_cluster')
check_marathon_services_replication.main()
mock_parse_args.assert_called_once_with()
mock_get_services_for_cluster.assert_called_once_with(
cluster='fake_cluster', instance_type='marathon', soa_dir=soa_dir)