forked from Yelp/paasta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_mesos_maintenance.py
1064 lines (916 loc) · 38.7 KB
/
test_mesos_maintenance.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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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 argparse
import datetime
import json
from socket import gaierror
import mock
import pytest
from dateutil import tz
from requests.exceptions import HTTPError
from paasta_tools.mesos_maintenance import _make_request_payload
from paasta_tools.mesos_maintenance import are_hosts_forgotten_down
from paasta_tools.mesos_maintenance import are_hosts_forgotten_draining
from paasta_tools.mesos_maintenance import build_maintenance_payload
from paasta_tools.mesos_maintenance import build_maintenance_schedule_payload
from paasta_tools.mesos_maintenance import build_reservation_payload
from paasta_tools.mesos_maintenance import components_to_hosts
from paasta_tools.mesos_maintenance import datetime_seconds_from_now
from paasta_tools.mesos_maintenance import datetime_to_nanoseconds
from paasta_tools.mesos_maintenance import down
from paasta_tools.mesos_maintenance import drain
from paasta_tools.mesos_maintenance import friendly_status
from paasta_tools.mesos_maintenance import get_down_hosts
from paasta_tools.mesos_maintenance import get_draining_hosts
from paasta_tools.mesos_maintenance import get_hosts_forgotten_down
from paasta_tools.mesos_maintenance import get_hosts_forgotten_draining
from paasta_tools.mesos_maintenance import get_hosts_past_maintenance_end
from paasta_tools.mesos_maintenance import get_hosts_past_maintenance_start
from paasta_tools.mesos_maintenance import get_hosts_with_state
from paasta_tools.mesos_maintenance import get_machine_ids
from paasta_tools.mesos_maintenance import get_maintenance_schedule
from paasta_tools.mesos_maintenance import get_maintenance_status
from paasta_tools.mesos_maintenance import Hostname
from paasta_tools.mesos_maintenance import hostnames_to_components
from paasta_tools.mesos_maintenance import is_host_down
from paasta_tools.mesos_maintenance import is_host_drained
from paasta_tools.mesos_maintenance import is_host_draining
from paasta_tools.mesos_maintenance import is_host_past_maintenance_end
from paasta_tools.mesos_maintenance import is_host_past_maintenance_start
from paasta_tools.mesos_maintenance import load_credentials
from paasta_tools.mesos_maintenance import parse_datetime
from paasta_tools.mesos_maintenance import parse_timedelta
from paasta_tools.mesos_maintenance import raw_status
from paasta_tools.mesos_maintenance import reserve
from paasta_tools.mesos_maintenance import Resource
from paasta_tools.mesos_maintenance import schedule
from paasta_tools.mesos_maintenance import seconds_to_nanoseconds
from paasta_tools.mesos_maintenance import status
from paasta_tools.mesos_maintenance import undrain
from paasta_tools.mesos_maintenance import unreserve
from paasta_tools.mesos_maintenance import up
def test_parse_timedelta_none():
with pytest.raises(argparse.ArgumentTypeError):
parse_timedelta(value=None)
def test_parse_timedelta_invalid():
with pytest.raises(argparse.ArgumentTypeError):
parse_timedelta(value="fake value")
def test_parse_timedelta():
assert parse_timedelta(value="1 hour") == 3600 * 1000000000
def test_parse_datetime_none():
with pytest.raises(argparse.ArgumentTypeError):
parse_datetime(value=None)
def test_parse_datetime_invalid():
with pytest.raises(argparse.ArgumentTypeError):
parse_datetime(value="fake value")
def test_parse_datetime():
assert parse_datetime("November 11, 2011 11:11:11Z") == 1321009871000000000
@mock.patch("paasta_tools.mesos_maintenance.now", autospec=True)
def test_datetime_seconds_from_now(
mock_now,
):
mock_now.return_value = datetime.datetime(
2016, 4, 16, 0, 23, 25, 157145, tzinfo=tz.tzutc()
)
expected = datetime.datetime(2016, 4, 16, 0, 23, 40, 157145, tzinfo=tz.tzutc())
assert datetime_seconds_from_now(15) == expected
def test_seconds_to_nanoseconds():
assert seconds_to_nanoseconds(60) == 60 * 1000000000
def test_datetime_to_nanoseconds():
dt = datetime.datetime(2016, 4, 16, 0, 23, 40, 157145, tzinfo=tz.tzutc())
expected = 1460766220000000000
assert datetime_to_nanoseconds(dt) == expected
@mock.patch("paasta_tools.mesos_maintenance.gethostbyname", autospec=True)
def test_build_maintenance_payload(
mock_gethostbyname,
):
ip = "169.254.121.212"
mock_gethostbyname.return_value = ip
hostname = "fqdn1.example.org"
hostnames = [hostname]
assert build_maintenance_payload(hostnames, "start_maintenance")[
"start_maintenance"
]["machines"] == get_machine_ids(hostnames)
@mock.patch("paasta_tools.mesos_maintenance.gethostbyname", autospec=True)
def test_get_machine_ids_one_host(
mock_gethostbyname,
):
ip = "169.254.121.212"
mock_gethostbyname.return_value = ip
hostname = "fqdn1.example.org"
hostnames = [hostname]
expected = [{"hostname": hostname, "ip": ip}]
assert get_machine_ids(hostnames) == expected
@mock.patch("paasta_tools.mesos_maintenance.gethostbyname", autospec=True)
def test_get_machine_ids_multiple_hosts(
mock_gethostbyname,
):
ip1 = "169.254.121.212"
ip2 = "169.254.121.213"
ip3 = "169.254.121.214"
mock_gethostbyname.side_effect = [ip1, ip2, ip3]
hostname1 = "fqdn1.example.org"
hostname2 = "fqdn2.example.org"
hostname3 = "fqdn3.example.org"
hostnames = [hostname1, hostname2, hostname3]
expected = [
{"hostname": hostname1, "ip": ip1},
{"hostname": hostname2, "ip": ip2},
{"hostname": hostname3, "ip": ip3},
]
assert get_machine_ids(hostnames) == expected
def test_get_machine_ids_multiple_hosts_ips():
ip1 = "169.254.121.212"
ip2 = "169.254.121.213"
ip3 = "169.254.121.214"
hostname1 = "fqdn1.example.org"
hostname2 = "fqdn2.example.org"
hostname3 = "fqdn3.example.org"
hostnames = [hostname1 + "|" + ip1, hostname2 + "|" + ip2, hostname3 + "|" + ip3]
expected = [
{"hostname": hostname1, "ip": ip1},
{"hostname": hostname2, "ip": ip2},
{"hostname": hostname3, "ip": ip3},
]
assert get_machine_ids(hostnames) == expected
@mock.patch("paasta_tools.mesos_maintenance.get_maintenance_schedule", autospec=True)
@mock.patch("paasta_tools.mesos_maintenance.get_machine_ids", autospec=True)
def test_build_maintenance_schedule_payload_no_schedule(
mock_get_machine_ids, mock_get_maintenance_schedule
):
mock_get_maintenance_schedule.return_value.json.return_value = {
"get_maintenance_schedule": {"schedule": {}}
}
machine_ids = [{"hostname": "machine2", "ip": "10.0.0.2"}]
mock_get_machine_ids.return_value = machine_ids
hostnames = ["fake-hostname"]
start = "1443830400000000000"
duration = "3600000000000"
actual = build_maintenance_schedule_payload(hostnames, start, duration, drain=True)
assert mock_get_maintenance_schedule.call_count == 1
assert mock_get_machine_ids.call_count == 1
assert mock_get_machine_ids.call_args == mock.call(hostnames)
expected = {
"type": "UPDATE_MAINTENANCE_SCHEDULE",
"update_maintenance_schedule": {
"schedule": {
"windows": [
{
"machine_ids": machine_ids,
"unavailability": {
"start": {"nanoseconds": int(start)},
"duration": {"nanoseconds": int(duration)},
},
}
]
}
},
}
assert actual == expected
@mock.patch("paasta_tools.mesos_maintenance.get_maintenance_schedule", autospec=True)
@mock.patch("paasta_tools.mesos_maintenance.get_machine_ids", autospec=True)
def test_build_maintenance_schedule_payload_no_schedule_undrain(
mock_get_machine_ids, mock_get_maintenance_schedule
):
mock_get_maintenance_schedule.return_value.json.return_value = {
"get_maintenance_schedule": {"schedule": {}}
}
machine_ids = [{"hostname": "machine2", "ip": "10.0.0.2"}]
mock_get_machine_ids.return_value = machine_ids
hostnames = ["fake-hostname"]
start = "1443830400000000000"
duration = "3600000000000"
actual = build_maintenance_schedule_payload(hostnames, start, duration, drain=False)
assert mock_get_maintenance_schedule.call_count == 1
assert mock_get_machine_ids.call_count == 1
assert mock_get_machine_ids.call_args == mock.call(hostnames)
expected = {
"type": "UPDATE_MAINTENANCE_SCHEDULE",
"update_maintenance_schedule": {"schedule": {"windows": []}},
}
assert actual == expected
@mock.patch("paasta_tools.mesos_maintenance.load_credentials", autospec=True)
def test_build_reservation_payload(
mock_load_credentials,
):
fake_username = "username"
mock_load_credentials.return_value = mock.MagicMock(
principal=fake_username, secret="password"
)
resource = "cpus"
amount = 42
resources = [Resource(name=resource, amount=amount)]
actual = build_reservation_payload(resources)
expected = [
{
"name": resource,
"type": "SCALAR",
"scalar": {"value": amount},
"role": "maintenance",
"reservation": {"principal": fake_username},
}
]
assert actual == expected
def test_make_request_payload():
ret = _make_request_payload("slave_id", {"name": "res+ource"})
assert ret == {"slaveId": b"slave_id", "resources": b'{"name": "res%20ource"}'}
assert type(ret["slaveId"]) is bytes
assert type(ret["resources"]) is bytes
@mock.patch("paasta_tools.mesos_maintenance.get_maintenance_schedule", autospec=True)
@mock.patch("paasta_tools.mesos_maintenance.get_machine_ids", autospec=True)
def test_build_maintenance_schedule_payload_schedule(
mock_get_machine_ids, mock_get_maintenance_schedule
):
mock_get_maintenance_schedule.return_value.json.return_value = {
"type": "GET_MAINTENANCE_SCHEDULE",
"get_maintenance_schedule": {
"schedule": {
"windows": [
{
"machine_ids": [
{"hostname": "machine1", "ip": "10.0.0.1"},
{"hostname": "machine2", "ip": "10.0.0.2"},
],
"unavailability": {
"start": {"nanoseconds": 1443830400000000000},
"duration": {"nanoseconds": 3600000000000},
},
},
{
"machine_ids": [{"hostname": "machine3", "ip": "10.0.0.3"}],
"unavailability": {
"start": {"nanoseconds": 1443834000000000000},
"duration": {"nanoseconds": 3600000000000},
},
},
]
}
},
}
machine_ids = [{"hostname": "machine2", "ip": "10.0.0.2"}]
mock_get_machine_ids.return_value = machine_ids
hostnames = ["machine2"]
start = "1443830400000000000"
duration = "3600000000000"
actual = build_maintenance_schedule_payload(hostnames, start, duration, drain=True)
assert mock_get_maintenance_schedule.call_count == 1
assert mock_get_machine_ids.call_count == 1
assert mock_get_machine_ids.call_args == mock.call(hostnames)
expected = {
"type": "UPDATE_MAINTENANCE_SCHEDULE",
"update_maintenance_schedule": {
"schedule": {
"windows": [
{
"machine_ids": [{"hostname": "machine1", "ip": "10.0.0.1"}],
"unavailability": {
"start": {"nanoseconds": 1443830400000000000},
"duration": {"nanoseconds": 3600000000000},
},
},
{
"machine_ids": [{"hostname": "machine3", "ip": "10.0.0.3"}],
"unavailability": {
"start": {"nanoseconds": 1443834000000000000},
"duration": {"nanoseconds": 3600000000000},
},
},
{
"machine_ids": machine_ids,
"unavailability": {
"start": {"nanoseconds": int(start)},
"duration": {"nanoseconds": int(duration)},
},
},
]
}
},
}
assert actual == expected
@mock.patch("paasta_tools.mesos_maintenance.get_maintenance_schedule", autospec=True)
@mock.patch("paasta_tools.mesos_maintenance.get_machine_ids", autospec=True)
def test_build_maintenance_schedule_payload_schedule_undrain(
mock_get_machine_ids, mock_get_maintenance_schedule
):
mock_get_maintenance_schedule.return_value.json.return_value = {
"type": "GET_MAINTENANCE_SCHEDULE",
"get_maintenance_schedule": {
"schedule": {
"windows": [
{
"machine_ids": [
{"hostname": "machine1", "ip": "10.0.0.1"},
{"hostname": "machine2", "ip": "10.0.0.2"},
],
"unavailability": {
"start": {"nanoseconds": 1443830400000000000},
"duration": {"nanoseconds": 3600000000000},
},
},
{
"machine_ids": [{"hostname": "machine3", "ip": "10.0.0.3"}],
"unavailability": {
"start": {"nanoseconds": 1443834000000000000},
"duration": {"nanoseconds": 3600000000000},
},
},
]
}
},
}
machine_ids = [{"hostname": "machine2", "ip": "10.0.0.2"}]
mock_get_machine_ids.return_value = machine_ids
hostnames = ["machine2"]
start = "1443830400000000000"
duration = "3600000000000"
actual = build_maintenance_schedule_payload(hostnames, start, duration, drain=False)
assert mock_get_maintenance_schedule.call_count == 1
assert mock_get_machine_ids.call_count == 1
assert mock_get_machine_ids.call_args == mock.call(hostnames)
expected = {
"type": "UPDATE_MAINTENANCE_SCHEDULE",
"update_maintenance_schedule": {
"schedule": {
"windows": [
{
"machine_ids": [{"hostname": "machine1", "ip": "10.0.0.1"}],
"unavailability": {
"start": {"nanoseconds": 1443830400000000000},
"duration": {"nanoseconds": 3600000000000},
},
},
{
"machine_ids": [{"hostname": "machine3", "ip": "10.0.0.3"}],
"unavailability": {
"start": {"nanoseconds": 1443834000000000000},
"duration": {"nanoseconds": 3600000000000},
},
},
]
}
},
}
assert actual == expected
@mock.patch("paasta_tools.mesos_maintenance.open", create=True, autospec=None)
def test_load_credentials(
mock_open,
):
principal = "username"
secret = "password"
credentials = {"principal": principal, "secret": secret}
mock_open.side_effect = mock.mock_open(read_data=json.dumps(credentials))
credentials = load_credentials()
assert credentials.principal == principal
assert credentials.secret == secret
@mock.patch(
"paasta_tools.mesos_maintenance.open",
create=True,
side_effect=IOError,
autospec=None,
)
def test_load_credentials_missing_file(
mock_open,
):
with pytest.raises(IOError):
assert load_credentials()
@mock.patch("paasta_tools.mesos_maintenance.open", create=True, autospec=None)
def test_load_credentials_keyerror(
mock_open,
):
credentials = {}
mock_open.side_effect = mock.mock_open(read_data=json.dumps(credentials))
with pytest.raises(KeyError):
assert load_credentials()
@mock.patch("paasta_tools.mesos_maintenance.operator_api", autospec=True)
def test_get_maintenance_status(
mock_operator_api,
):
get_maintenance_status()
assert mock_operator_api.call_count == 1
assert mock_operator_api.return_value.call_count == 1
assert mock_operator_api.return_value.call_args == mock.call(
data={"type": "GET_MAINTENANCE_STATUS"}
)
@mock.patch("paasta_tools.mesos_maintenance.operator_api", autospec=True)
def test_get_maintenance_schedule(
mock_operator_api,
):
get_maintenance_schedule()
assert mock_operator_api.call_count == 1
assert mock_operator_api.return_value.call_count == 1
assert mock_operator_api.return_value.call_args == mock.call(
data={"type": "GET_MAINTENANCE_SCHEDULE"}
)
@mock.patch("paasta_tools.mesos_maintenance.operator_api", autospec=True)
@mock.patch(
"paasta_tools.mesos_maintenance.build_maintenance_schedule_payload", autospec=True
)
@mock.patch("paasta_tools.mesos_maintenance.reserve_all_resources", autospec=True)
def test_drain(
mock_reserve_all_resources,
mock_build_maintenance_schedule_payload,
mock_operator_api,
):
fake_schedule = {"fake_schedule": "fake_value"}
mock_build_maintenance_schedule_payload.return_value = fake_schedule
drain(hostnames=["some-host"], start="some-start", duration="some-duration")
assert mock_build_maintenance_schedule_payload.call_count == 1
expected_args = mock.call(["some-host"], "some-start", "some-duration", drain=True)
assert mock_build_maintenance_schedule_payload.call_args == expected_args
assert mock_reserve_all_resources.call_count == 1
expected_args = mock.call(["some-host"])
assert mock_reserve_all_resources.call_args == expected_args
assert mock_operator_api.call_count == 1
assert mock_operator_api.return_value.call_count == 1
expected_args = mock.call(data=fake_schedule)
assert mock_operator_api.return_value.call_args == expected_args
mock_reserve_all_resources.side_effect = HTTPError()
drain(hostnames=["some-host"], start="some-start", duration="some-duration")
assert mock_operator_api.call_count == 2
mock_reserve_all_resources.reset_mock()
mock_operator_api.reset_mock()
drain(
hostnames=["some-host"],
start="some-start",
duration="some-duration",
reserve_resources=False,
)
assert mock_reserve_all_resources.call_count == 0
assert mock_operator_api.return_value.call_count == 1
@mock.patch("paasta_tools.mesos_maintenance.operator_api", autospec=True)
@mock.patch(
"paasta_tools.mesos_maintenance.build_maintenance_schedule_payload", autospec=True
)
@mock.patch("paasta_tools.mesos_maintenance.unreserve_all_resources", autospec=True)
def test_undrain(
mock_unreserve_all_resources,
mock_build_maintenance_schedule_payload,
mock_operator_api,
):
fake_schedule = {"fake_schedule": "fake_value"}
mock_build_maintenance_schedule_payload.return_value = fake_schedule
undrain(hostnames=["some-host"])
assert mock_build_maintenance_schedule_payload.call_count == 1
expected_args = mock.call(["some-host"], drain=False)
assert mock_build_maintenance_schedule_payload.call_args == expected_args
assert mock_unreserve_all_resources.call_count == 1
expected_args = mock.call(["some-host"])
assert mock_unreserve_all_resources.call_args == expected_args
assert mock_operator_api.call_count == 1
assert mock_operator_api.return_value.call_count == 1
expected_args = mock.call(data=fake_schedule)
assert mock_operator_api.return_value.call_args == expected_args
mock_unreserve_all_resources.side_effect = HTTPError()
undrain(hostnames=["some-host"])
assert mock_operator_api.call_count == 2
mock_operator_api.reset_mock()
mock_unreserve_all_resources.reset_mock()
undrain(hostnames=["some-host"], unreserve_resources=False)
assert mock_operator_api.call_count == 1
assert mock_unreserve_all_resources.call_count == 0
@mock.patch(
"paasta_tools.mesos_maintenance.build_reservation_payload",
autospec=True,
return_value={"name": "payload"},
)
@mock.patch("paasta_tools.mesos_maintenance.operator_api", autospec=True)
def test_reserve(mock_operator_api, mock_build_reservation_payload):
fake_slave_id = "fake-id"
fake_resource = "cpus"
fake_amount = 42
resources = [Resource(name=fake_resource, amount=fake_amount)]
reserve(fake_slave_id, resources)
assert mock_build_reservation_payload.call_count == 1
expected_args = mock.call(resources)
assert mock_build_reservation_payload.call_args == expected_args
assert mock_operator_api.call_count == 1
assert mock_operator_api.return_value.call_count == 1
@mock.patch(
"paasta_tools.mesos_maintenance.build_reservation_payload",
autospec=True,
return_value={"name": "payload"},
)
@mock.patch("paasta_tools.mesos_maintenance.operator_api", autospec=True)
def test_unreserve(mock_operator_api, mock_build_reservation_payload):
fake_slave_id = "fake-id"
fake_resource = "cpus"
fake_amount = 42
resources = [Resource(name=fake_resource, amount=fake_amount)]
unreserve(fake_slave_id, resources)
assert mock_build_reservation_payload.call_count == 1
expected_args = mock.call(resources)
assert mock_build_reservation_payload.call_args == expected_args
assert mock_operator_api.call_count == 1
assert mock_operator_api.return_value.call_count == 1
@mock.patch("paasta_tools.mesos_maintenance.operator_api", autospec=True)
@mock.patch("paasta_tools.mesos_maintenance.build_maintenance_payload", autospec=True)
def test_down(mock_build_maintenance_payload, mock_operator_api):
fake_payload = [{"fake_schedule": "fake_value"}]
mock_build_maintenance_payload.return_value = fake_payload
down(hostnames=["some-host"])
assert mock_build_maintenance_payload.call_count == 1
assert mock_build_maintenance_payload.call_args == mock.call(
["some-host"], "start_maintenance"
)
assert mock_operator_api.call_count == 1
assert mock_operator_api.return_value.call_count == 1
expected_args = mock.call(data=fake_payload)
assert mock_operator_api.return_value.call_args == expected_args
@mock.patch("paasta_tools.mesos_maintenance.operator_api", autospec=True)
@mock.patch("paasta_tools.mesos_maintenance.build_maintenance_payload", autospec=True)
def test_up(mock_build_maintenance_payload, mock_operator_api):
fake_payload = [{"fake_schedule": "fake_value"}]
mock_build_maintenance_payload.return_value = fake_payload
up(hostnames=["some-host"])
assert mock_build_maintenance_payload.call_count == 1
assert mock_build_maintenance_payload.call_args == mock.call(
["some-host"], "stop_maintenance"
)
assert mock_operator_api.call_count == 1
assert mock_operator_api.return_value.call_count == 1
expected_args = mock.call(data=fake_payload)
assert mock_operator_api.return_value.call_args == expected_args
@mock.patch("paasta_tools.mesos_maintenance.get_maintenance_status", autospec=True)
def test_raw_status(
mock_get_maintenance_status,
):
raw_status()
assert mock_get_maintenance_status.call_count == 1
@mock.patch("paasta_tools.mesos_maintenance.raw_status", autospec=True)
def test_status(
mock_raw_status,
):
status()
assert mock_raw_status.call_count == 1
@mock.patch("paasta_tools.mesos_maintenance.raw_status", autospec=True)
def test_friendly_status(
mock_raw_status,
):
friendly_status()
assert mock_raw_status.call_count == 1
@mock.patch("paasta_tools.mesos_maintenance.get_maintenance_schedule", autospec=True)
def test_schedule(
mock_get_maintenance_schedule,
):
schedule()
assert mock_get_maintenance_schedule.call_count == 1
@mock.patch("paasta_tools.mesos_maintenance.get_mesos_config_path", autospec=True)
@mock.patch("paasta_tools.mesos_maintenance.get_maintenance_status", autospec=True)
def test_get_hosts_with_state_none(
mock_get_maintenance_status, mock_get_mesos_config_path
):
mock_get_mesos_config_path.return_value = "/dev/null"
fake_status = {"get_maintenance_status": {"status": {}}}
mock_get_maintenance_status.return_value = mock.Mock()
mock_get_maintenance_status.return_value.json.return_value = fake_status
assert get_hosts_with_state(state="fake_state") == []
@mock.patch("paasta_tools.mesos_maintenance.get_mesos_config_path", autospec=True)
@mock.patch("paasta_tools.mesos_maintenance.get_maintenance_status", autospec=True)
def test_get_hosts_with_state_draining(
mock_get_maintenance_status, mock_get_mesos_config_path
):
fake_status = {
"type": "GET_MAINTENANCE_STATUS",
"get_maintenance_status": {
"status": {
"draining_machines": [
{"hostname": "fake-host1.fakesite.something", "ip": "0.0.0.0"},
{"hostname": "fake-host2.fakesite.something", "ip": "0.0.0.1"},
]
}
},
}
mock_get_maintenance_status.return_value = mock.Mock()
mock_get_maintenance_status.return_value.json.return_value = fake_status
expected = sorted(
["fake-host1.fakesite.something", "fake-host2.fakesite.something"]
)
assert sorted(get_hosts_with_state(state="draining_machines")) == expected
@mock.patch("paasta_tools.mesos_maintenance.get_mesos_config_path", autospec=True)
@mock.patch("paasta_tools.mesos_maintenance.get_maintenance_status", autospec=True)
def test_get_hosts_with_state_down(
mock_get_maintenance_status, mock_get_mesos_config_path
):
fake_status = {
"type": "GET_MAINTENANCE_STATUS",
"get_maintenance_status": {
"status": {
"down_machines": [
{"hostname": "fake-host1.fakesite.something", "ip": "0.0.0.0"},
{"hostname": "fake-host2.fakesite.something", "ip": "0.0.0.1"},
]
}
},
}
mock_get_maintenance_status.return_value = mock.Mock()
mock_get_maintenance_status.return_value.json.return_value = fake_status
expected = sorted(
["fake-host1.fakesite.something", "fake-host2.fakesite.something"]
)
assert sorted(get_hosts_with_state(state="down_machines")) == expected
@mock.patch("paasta_tools.mesos_maintenance.get_hosts_with_state", autospec=True)
def test_get_draining_hosts(
mock_get_hosts_with_state,
):
get_draining_hosts()
assert mock_get_hosts_with_state.call_count == 1
expected_args = mock.call(state="draining_machines", system_paasta_config=None)
assert mock_get_hosts_with_state.call_args == expected_args
@mock.patch("paasta_tools.mesos_maintenance.get_hosts_with_state", autospec=True)
def test_get_down_hosts(
mock_get_hosts_with_state,
):
get_down_hosts()
assert mock_get_hosts_with_state.call_count == 1
expected_args = mock.call(state="down_machines")
assert mock_get_hosts_with_state.call_args == expected_args
@mock.patch("paasta_tools.mesos_maintenance.get_draining_hosts", autospec=True)
def test_is_host_draining(
mock_get_draining_hosts,
):
mock_get_draining_hosts.return_value = [
"fake-host1.fakesite.something",
"fake-host2.fakesite.something",
]
assert is_host_draining("fake-host1.fakesite.something")
assert not is_host_draining("fake-host3.fakesite.something")
@mock.patch("paasta_tools.mesos_maintenance.get_down_hosts", autospec=True)
def test_is_host_down(
mock_get_down_hosts,
):
mock_get_down_hosts.return_value = [
"fake-host1.fakesite.something",
"fake-host2.fakesite.something",
]
assert is_host_down("fake-host1.fakesite.something")
assert not is_host_down("fake-host3.fakesite.something")
def sideeffect_mock_get_count_running_tasks_on_slave(hostname):
if hostname == "host1":
return 3
else:
return 0
@mock.patch(
"paasta_tools.mesos_maintenance.get_count_running_tasks_on_slave", autospec=True
)
@mock.patch("paasta_tools.mesos_maintenance.is_host_draining", autospec=True)
def test_is_host_drained(mock_is_host_draining, mock_get_count_running_tasks_on_slave):
mock_get_count_running_tasks_on_slave.side_effect = (
sideeffect_mock_get_count_running_tasks_on_slave
)
mock_is_host_draining.return_value = True
assert not is_host_drained("host1")
mock_get_count_running_tasks_on_slave.assert_called_with("host1")
mock_is_host_draining.assert_called_with(hostname="host1")
mock_is_host_draining.return_value = True
assert is_host_drained("host2")
mock_is_host_draining.return_value = False
assert not is_host_drained("host1")
mock_is_host_draining.return_value = False
assert not is_host_drained("host2")
assert not is_host_drained("host3")
mock_is_host_draining.return_value = True
assert is_host_drained("host3")
@mock.patch("paasta_tools.mesos_maintenance.get_maintenance_schedule", autospec=True)
@mock.patch("paasta_tools.mesos_maintenance.datetime_to_nanoseconds", autospec=True)
def test_get_hosts_past_maintenance_start(
mock_datetime_to_nanoseconds, mock_get_maintenance_schedule
):
mock_schedule = {
"type": "GET_MAINTENANCE_SCHEDULE",
"get_maintenance_schedule": {
"schedule": {
"windows": [
{
"machine_ids": [{"hostname": "host3"}],
"unavailability": {"start": {"nanoseconds": 10}},
},
{
"machine_ids": [{"hostname": "host2"}],
"unavailability": {"start": {"nanoseconds": 5}},
},
]
}
},
}
mock_maintenance_dict = mock.Mock(return_value=mock_schedule)
mock_get_maintenance_schedule.return_value = mock.Mock(json=mock_maintenance_dict)
mock_datetime_to_nanoseconds.return_value = 7
ret = get_hosts_past_maintenance_start()
assert ret == ["host2"]
mock_schedule = {
"type": "GET_MAINTENANCE_SCHEDULE",
"get_maintenance_schedule": {"schedule": {}},
}
mock_maintenance_dict = mock.Mock(return_value=mock_schedule)
mock_get_maintenance_schedule.return_value = mock.Mock(json=mock_maintenance_dict)
ret = get_hosts_past_maintenance_start()
assert ret == []
@mock.patch("paasta_tools.mesos_maintenance.get_maintenance_schedule", autospec=True)
@mock.patch("paasta_tools.mesos_maintenance.datetime_to_nanoseconds", autospec=True)
def test_get_hosts_past_maintenance_start_grace(
mock_datetime_to_nanoseconds, mock_get_maintenance_schedule
):
mock_schedule = {
"type": "GET_MAINTENANCE_SCHEDULE",
"get_maintenance_schedule": {
"schedule": {
"windows": [
{
"machine_ids": [{"hostname": "host3"}],
"unavailability": {"start": {"nanoseconds": 10}},
},
{
"machine_ids": [{"hostname": "host2"}],
"unavailability": {"start": {"nanoseconds": 5}},
},
]
}
},
}
mock_maintenance_dict = mock.Mock(return_value=mock_schedule)
mock_get_maintenance_schedule.return_value = mock.Mock(json=mock_maintenance_dict)
mock_datetime_to_nanoseconds.return_value = 7
ret = get_hosts_past_maintenance_start(grace=1)
assert ret == ["host2"]
ret = get_hosts_past_maintenance_start(grace=5)
assert ret == []
@mock.patch("paasta_tools.mesos_maintenance.get_maintenance_schedule", autospec=True)
@mock.patch("paasta_tools.mesos_maintenance.datetime_to_nanoseconds", autospec=True)
def test_get_hosts_past_maintenance_end(
mock_datetime_to_nanoseconds, mock_get_maintenance_schedule
):
mock_schedule = {
"type": "GET_MAINTENANCE_SCHEDULE",
"get_maintenance_schedule": {
"schedule": {
"windows": [
{
"machine_ids": [{"hostname": "host3"}],
"unavailability": {
"start": {"nanoseconds": 10},
"duration": {"nanoseconds": 20},
},
},
{
"machine_ids": [{"hostname": "host2"}],
"unavailability": {
"start": {"nanoseconds": 5},
"duration": {"nanoseconds": 10},
},
},
]
}
},
}
mock_maintenance_dict = mock.Mock(return_value=mock_schedule)
mock_get_maintenance_schedule.return_value = mock.Mock(json=mock_maintenance_dict)
mock_datetime_to_nanoseconds.return_value = 19
actual = get_hosts_past_maintenance_end()
assert actual == ["host2"]
mock_schedule = {
"type": "GET_MAINTENANCE_SCHEDULE",
"get_maintenance_schedule": {"schedule": {}},
}
mock_maintenance_dict = mock.Mock(return_value=mock_schedule)
mock_get_maintenance_schedule.return_value = mock.Mock(json=mock_maintenance_dict)
mock_datetime_to_nanoseconds.return_value = 19
actual = get_hosts_past_maintenance_end()
assert actual == []
@mock.patch("paasta_tools.mesos_maintenance.get_maintenance_schedule", autospec=True)
@mock.patch("paasta_tools.mesos_maintenance.datetime_to_nanoseconds", autospec=True)
def test_get_hosts_past_maintenance_end_grace(
mock_datetime_to_nanoseconds, mock_get_maintenance_schedule
):
mock_schedule = {
"type": "GET_MAINTENANCE_SCHEDULE",
"get_maintenance_schedule": {
"schedule": {
"windows": [
{
"machine_ids": [{"hostname": "host3"}],
"unavailability": {
"start": {"nanoseconds": 10},
"duration": {"nanoseconds": 20},
},
},
{
"machine_ids": [{"hostname": "host2"}],
"unavailability": {
"start": {"nanoseconds": 5},
"duration": {"nanoseconds": 10},
},
},
]
}
},
}
mock_maintenance_dict = mock.Mock(return_value=mock_schedule)
mock_get_maintenance_schedule.return_value = mock.Mock(json=mock_maintenance_dict)
mock_datetime_to_nanoseconds.return_value = 19
actual = get_hosts_past_maintenance_end(grace=2)
assert actual == ["host2"]
actual = get_hosts_past_maintenance_end(grace=5)
assert actual == []
@mock.patch(
"paasta_tools.mesos_maintenance.get_hosts_past_maintenance_start", autospec=True
)
def test_is_host_past_maintenance_start(
mock_get_hosts_past_maintenance_start,
):
mock_get_hosts_past_maintenance_start.return_value = ["fake_host"]
assert is_host_past_maintenance_start("fake_host")
assert not is_host_past_maintenance_start("fake_host2")
@mock.patch(
"paasta_tools.mesos_maintenance.get_hosts_past_maintenance_end", autospec=True
)
def test_is_host_past_maintenance_end(
mock_get_hosts_past_maintenance_end,
):
mock_get_hosts_past_maintenance_end.return_value = ["fake_host"]
assert is_host_past_maintenance_end("fake_host")
assert not is_host_past_maintenance_end("fake_host2")
def test_hostnames_to_components_simple():
hostname = "fake-host"
ip = None
expected = [Hostname(host=hostname, ip=ip)]
actual = hostnames_to_components([hostname])
assert actual == expected
def test_hostnames_to_components_pipe():
hostname = "fake-host"
ip = "127.0.0.1"
expected = [Hostname(host=hostname, ip=ip)]
actual = hostnames_to_components([f"{hostname}|{ip}"])
assert actual == expected
@mock.patch("paasta_tools.mesos_maintenance.gethostbyname", autospec=True)
def test_hostnames_to_components_resolve(
mock_gethostbyname,
):
hostname = "fake-host"
ip = "127.0.0.1"
mock_gethostbyname.return_value = ip
expected = [Hostname(host=hostname, ip=ip)]
actual = hostnames_to_components([hostname], resolve=True)
assert actual == expected