forked from sonic-net/sonic-platform-daemons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_chassisd.py
More file actions
1776 lines (1481 loc) · 63.2 KB
/
Copy pathtest_chassisd.py
File metadata and controls
1776 lines (1481 loc) · 63.2 KB
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
import os
import sys
import mock
import tempfile
import json
from imp import load_source
from mock import Mock, MagicMock, patch, mock_open
from sonic_py_common import daemon_base
from .mock_platform import MockChassis, MockSmartSwitchChassis, MockModule
from .mock_module_base import ModuleBase
# Assuming OBJECT should be a specific value, define it manually
SELECT_OBJECT = 1 # Replace with the actual value for OBJECT if know
SYSLOG_IDENTIFIER = 'chassisd_test'
NOT_AVAILABLE = 'N/A'
daemon_base.db_connect = MagicMock()
test_path = os.path.dirname(os.path.abspath(__file__))
# Add mocked_libs path so that the file under test can load mocked modules from there
mocked_libs_path = os.path.join(test_path, 'mocked_libs')
sys.path.insert(0, mocked_libs_path)
modules_path = os.path.dirname(test_path)
scripts_path = os.path.join(modules_path, "scripts")
sys.path.insert(0, modules_path)
os.environ["CHASSISD_UNIT_TESTING"] = "1"
load_source('chassisd', scripts_path + '/chassisd')
from chassisd import *
CHASSIS_MODULE_INFO_NAME_FIELD = 'name'
CHASSIS_MODULE_INFO_DESC_FIELD = 'desc'
CHASSIS_MODULE_INFO_SLOT_FIELD = 'slot'
CHASSIS_MODULE_INFO_OPERSTATUS_FIELD = 'oper_status'
CHASSIS_MODULE_INFO_SERIAL_FIELD = 'serial'
CHASSIS_MODULE_INFO_PRESENCE_FIELD = 'presence'
CHASSIS_MODULE_INFO_MODEL_FIELD = 'model'
CHASSIS_MODULE_INFO_REPLACEABLE_FIELD = 'is_replaceable'
CHASSIS_INFO_KEY_TEMPLATE = 'CHASSIS {}'
CHASSIS_INFO_CARD_NUM_FIELD = 'module_num'
CHASSIS_ASIC_PCI_ADDRESS_FIELD = 'asic_pci_address'
CHASSIS_ASIC_ID_IN_MODULE_FIELD = 'asic_id_in_module'
CHASSIS_MODULE_REBOOT_TIMESTAMP_FIELD = 'timestamp'
CHASSIS_MODULE_REBOOT_REBOOT_FIELD = 'reboot'
PLATFORM_ENV_CONF_FILE = "/usr/share/sonic/platform/platform_env.conf"
PLATFORM_JSON_FILE = "/usr/share/sonic/platform/platform.json"
DEFAULT_DPU_REBOOT_TIMEOUT = 360
def setup_function():
ModuleUpdater.log_notice = MagicMock()
ModuleUpdater.log_warning = MagicMock()
def teardown_function():
ModuleUpdater.log_notice.reset()
ModuleUpdater.log_warning.reset()
def test_moduleupdater_check_valid_fields():
chassis = MockChassis()
index = 0
name = "FABRIC-CARD0"
desc = "Switch Fabric Module"
slot = 10
serial = "FC1000101"
module_type = ModuleBase.MODULE_TYPE_FABRIC
module = MockModule(index, name, desc, module_type, slot, serial)
replaceable = True
presence = True
model = 'N/A'
# Set initial state
status = ModuleBase.MODULE_STATUS_ONLINE
module.set_oper_status(status)
module.set_replaceable(replaceable)
module.set_presence(presence)
module.set_model(model)
chassis.module_list.append(module)
module_updater = ModuleUpdater(SYSLOG_IDENTIFIER, chassis, slot,
module.supervisor_slot)
module_updater.module_db_update()
fvs = module_updater.module_table.get(name)
if isinstance(fvs, list):
fvs = dict(fvs[-1])
assert desc == fvs[CHASSIS_MODULE_INFO_DESC_FIELD]
assert status == fvs[CHASSIS_MODULE_INFO_OPERSTATUS_FIELD]
assert serial == fvs[CHASSIS_MODULE_INFO_SERIAL_FIELD]
assert model == fvs[CHASSIS_MODULE_INFO_MODEL_FIELD]
assert str(replaceable) == fvs[CHASSIS_MODULE_INFO_REPLACEABLE_FIELD]
assert str(presence) == fvs[CHASSIS_MODULE_INFO_PRESENCE_FIELD]
def test_moduleupdater_check_phyentity_fields():
chassis = MockChassis()
index = 0
name = "FABRIC-CARD0"
desc = "Switch Fabric Module"
slot = 10
serial = "FC1000101"
module_type = ModuleBase.MODULE_TYPE_FABRIC
module = MockModule(index, name, desc, module_type, slot, serial)
replaceable = True
presence = True
model = 'N/A'
parent_name = 'chassis 1'
# Set initial state
status = ModuleBase.MODULE_STATUS_ONLINE
module.set_oper_status(status)
module.set_replaceable(replaceable)
module.set_presence(presence)
module.set_model(model)
chassis.module_list.append(module)
module_updater = ModuleUpdater(SYSLOG_IDENTIFIER, chassis, slot,
module.supervisor_slot)
module_updater.module_db_update()
fvs = module_updater.phy_entity_table.get(name)
if isinstance(fvs, list):
fvs = dict(fvs[-1])
assert str(index) == fvs['position_in_parent']
assert parent_name == fvs['parent_name']
assert serial == fvs[CHASSIS_MODULE_INFO_SERIAL_FIELD]
assert model == fvs[CHASSIS_MODULE_INFO_MODEL_FIELD]
assert str(replaceable) == fvs[CHASSIS_MODULE_INFO_REPLACEABLE_FIELD]
def test_smartswitch_moduleupdater_check_valid_fields():
chassis = MockSmartSwitchChassis()
index = 0
name = "DPU0"
desc = "DPU Module 0"
slot = 0
serial = "DPU0-0000"
module_type = ModuleBase.MODULE_TYPE_DPU
module = MockModule(index, name, desc, module_type, slot, serial)
# Set initial state
status = ModuleBase.MODULE_STATUS_ONLINE
module.set_oper_status(status)
chassis.module_list.append(module)
module_updater = SmartSwitchModuleUpdater(SYSLOG_IDENTIFIER, chassis)
module_updater.module_db_update()
fvs = module_updater.module_table.get(name)
if isinstance(fvs, list):
fvs = dict(fvs[-1])
assert desc == fvs[CHASSIS_MODULE_INFO_DESC_FIELD]
assert NOT_AVAILABLE == fvs[CHASSIS_MODULE_INFO_SLOT_FIELD]
assert status == fvs[CHASSIS_MODULE_INFO_OPERSTATUS_FIELD]
assert serial == fvs[CHASSIS_MODULE_INFO_SERIAL_FIELD]
def test_smartswitch_moduleupdater_status_transitions():
# Mock the chassis and module
chassis = MockSmartSwitchChassis()
index = 0
name = "DPU0"
desc = "DPU Module 0"
slot = 0
serial = "DPU0-0000"
module_type = ModuleBase.MODULE_TYPE_DPU
module = MockModule(index, name, desc, module_type, slot, serial)
# Add module to chassis and initialize with ONLINE status
initial_status_online = ModuleBase.MODULE_STATUS_ONLINE
module.set_oper_status(initial_status_online)
chassis.module_list.append(module)
# Create the updater
module_updater = SmartSwitchModuleUpdater(SYSLOG_IDENTIFIER, chassis)
# Mock dependent methods
with patch.object(module_updater, 'retrieve_dpu_reboot_time', return_value="2024-11-19T00:00:00") \
as mock_retrieve_reboot_time, \
patch.object(module_updater, '_is_first_boot', return_value=False) as mock_is_first_boot, \
patch.object(module_updater, 'persist_dpu_reboot_cause') as mock_persist_reboot_cause, \
patch.object(module_updater, 'update_dpu_reboot_cause_to_db') as mock_update_reboot_db, \
patch("os.makedirs") as mock_makedirs, \
patch("builtins.open", mock_open()) as mock_file, \
patch.object(module_updater, '_get_history_path',
return_value="/tmp/prev_reboot_time.txt") as mock_get_history_path:
# Transition from ONLINE to OFFLINE
offline_status = ModuleBase.MODULE_STATUS_OFFLINE
module.set_oper_status(offline_status)
module_updater.module_db_update()
assert module.get_oper_status() == offline_status
# Reset mocks for next transition
mock_file.reset_mock()
mock_makedirs.reset_mock()
mock_persist_reboot_cause.reset_mock()
mock_update_reboot_db.reset_mock()
# Ensure ONLINE transition is handled correctly
online_status = ModuleBase.MODULE_STATUS_ONLINE
module.set_oper_status(online_status)
module_updater.module_db_update()
assert module.get_oper_status() == online_status
# Validate mock calls for ONLINE transition
mock_persist_reboot_cause.assert_called_once()
mock_update_reboot_db.assert_called_once()
def test_smartswitch_moduleupdater_check_invalid_name():
chassis = MockSmartSwitchChassis()
index = 0
name = "TEST-CARD0"
desc = "36 port 400G card"
slot = 2
serial = "TS1000101"
module_type = ModuleBase.MODULE_TYPE_DPU
module = MockModule(index, name, desc, module_type, slot, serial)
# Set initial state
status = ModuleBase.MODULE_STATUS_PRESENT
module.set_oper_status(status)
chassis.module_list.append(module)
module_updater = SmartSwitchModuleUpdater(SYSLOG_IDENTIFIER, chassis)
module_updater.module_db_update()
fvs = module_updater.module_table.get(name)
assert fvs == None
config_updater = SmartSwitchModuleConfigUpdater(SYSLOG_IDENTIFIER, chassis)
admin_state = 0
config_updater.module_config_update(name, admin_state)
# No change since invalid key
assert module.get_admin_state() != admin_state
def test_smartswitch_moduleupdater_check_invalid_admin_state():
chassis = MockSmartSwitchChassis()
index = 0
name = "DPU0"
desc = "DPU Module 0"
slot = 0
serial = "DPU0-0000"
module_type = ModuleBase.MODULE_TYPE_DPU
module = MockModule(index, name, desc, module_type, slot, serial)
# Set initial state
status = ModuleBase.MODULE_STATUS_PRESENT
module.set_oper_status(status)
chassis.module_list.append(module)
module_updater = SmartSwitchModuleUpdater(SYSLOG_IDENTIFIER, chassis)
module_updater.module_db_update()
fvs = module_updater.module_table.get(name)
config_updater = SmartSwitchModuleConfigUpdater(SYSLOG_IDENTIFIER, chassis)
admin_state = 2
config_updater.module_config_update(name, admin_state)
# No change since invalid key
assert module.get_admin_state() != admin_state
def test_smartswitch_moduleupdater_check_invalid_slot():
chassis = MockSmartSwitchChassis()
index = 0
name = "DPU0"
desc = "DPU Module 0"
slot = -1
serial = "TS1000101"
module_type = ModuleBase.MODULE_TYPE_DPU
module = MockModule(index, name, desc, module_type, slot, serial)
# Set initial state
status = ModuleBase.MODULE_STATUS_PRESENT
module.set_oper_status(status)
chassis.module_list.append(module)
module_updater = SmartSwitchModuleUpdater(SYSLOG_IDENTIFIER, chassis)
module_updater.module_db_update()
fvs = module_updater.module_table.get(name)
assert fvs != None
def test_moduleupdater_check_invalid_name():
chassis = MockChassis()
index = 0
name = "TEST-CARD0"
desc = "36 port 400G card"
slot = 2
serial = "TS1000101"
module_type = ModuleBase.MODULE_TYPE_LINE
module = MockModule(index, name, desc, module_type, slot, serial)
# Set initial state
status = ModuleBase.MODULE_STATUS_PRESENT
module.set_oper_status(status)
chassis.module_list.append(module)
module_updater = ModuleUpdater(SYSLOG_IDENTIFIER, chassis, slot,
module.supervisor_slot)
module_updater.module_db_update()
fvs = module_updater.module_table.get(name)
assert fvs == None
def test_smartswitch_moduleupdater_check_invalid_index():
chassis = MockSmartSwitchChassis()
index = -1
name = "DPU0"
desc = "DPU Module 0"
slot = 0
serial = "TS1000101"
module_type = ModuleBase.MODULE_TYPE_DPU
module = MockModule(index, name, desc, module_type, slot, serial)
# Set initial state
status = ModuleBase.MODULE_STATUS_PRESENT
module.set_oper_status(status)
chassis.module_list.append(module)
module_updater = SmartSwitchModuleUpdater(SYSLOG_IDENTIFIER, chassis)
module_updater.module_db_update()
fvs = module_updater.module_table.get(name)
assert fvs != None
# Run chassis db clean up
module_updater.module_down_chassis_db_cleanup()
def test_moduleupdater_check_status_update():
chassis = MockChassis()
index = 0
name = "LINE-CARD0"
desc = "36 port 400G card"
slot = 1
serial = "LC1000101"
module_type = ModuleBase.MODULE_TYPE_LINE
module = MockModule(index, name, desc, module_type, slot, serial)
# Set initial state
status = ModuleBase.MODULE_STATUS_ONLINE
module.set_oper_status(status)
chassis.module_list.append(module)
module_updater = ModuleUpdater(SYSLOG_IDENTIFIER, chassis, slot,
module.supervisor_slot)
module_updater.module_db_update()
fvs = module_updater.module_table.get(name)
if isinstance(fvs, list):
fvs = dict(fvs[-1])
print('Initial DB-entry {}'.format(fvs))
assert status == fvs[CHASSIS_MODULE_INFO_OPERSTATUS_FIELD]
# Update status
status = ModuleBase.MODULE_STATUS_OFFLINE
module.set_oper_status(status)
fvs = module_updater.module_table.get(name)
if isinstance(fvs, list):
fvs = dict(fvs[-1])
print('Not updated DB-entry {}'.format(fvs))
assert status != fvs[CHASSIS_MODULE_INFO_OPERSTATUS_FIELD]
# Update status and db
module_updater.module_db_update()
fvs = module_updater.module_table.get(name)
if isinstance(fvs, list):
fvs = dict(fvs[-1])
print('Updated DB-entry {}'.format(fvs))
assert status == fvs[CHASSIS_MODULE_INFO_OPERSTATUS_FIELD]
# Run chassis db clean up from LC.
module_updater.module_down_chassis_db_cleanup()
def test_moduleupdater_check_deinit():
chassis = MockChassis()
index = 0
name = "LINE-CARD0"
desc = "36 port 400G card"
slot = 1
serial = "LC1000101"
module_type = ModuleBase.MODULE_TYPE_LINE
module = MockModule(index, name, desc, module_type, slot, serial)
# Set initial state
status = ModuleBase.MODULE_STATUS_ONLINE
module.set_oper_status(status)
chassis.module_list.append(module)
module_updater = ModuleUpdater(SYSLOG_IDENTIFIER, chassis, slot,
module.supervisor_slot)
module_updater.modules_num_update()
module_updater.module_db_update()
fvs = module_updater.module_table.get(name)
if isinstance(fvs, list):
fvs = dict(fvs[-1])
assert status == fvs[CHASSIS_MODULE_INFO_OPERSTATUS_FIELD]
module_table = module_updater.module_table
module_updater.deinit()
fvs = module_table.get(name)
assert fvs == None
def test_smartswitch_moduleupdater_check_deinit():
chassis = MockSmartSwitchChassis()
index = 0
name = "DPU0"
desc = "DPU Module 0"
slot = 0
serial = "DPU0-0000"
module_type = ModuleBase.MODULE_TYPE_DPU
module = MockModule(index, name, desc, module_type, slot, serial)
# Set initial state
status = ModuleBase.MODULE_STATUS_ONLINE
module.set_oper_status(status)
chassis.module_list.append(module)
module_updater = SmartSwitchModuleUpdater(SYSLOG_IDENTIFIER, chassis)
module_updater.modules_num_update()
module_updater.module_db_update()
fvs = module_updater.module_table.get(name)
# if isinstance(fvs, list):
# fvs = dict(fvs[-1])
# assert status == fvs[CHASSIS_MODULE_INFO_OPERSTATUS_FIELD]
module_table = module_updater.module_table
module_updater.deinit()
fvs = module_table.get(name)
assert fvs == None
def test_configupdater_check_valid_names():
chassis = MockChassis()
index = 0
name = "TEST-CARD0"
desc = "36 port 400G card"
slot = 1
serial = "TC1000101"
module_type = ModuleBase.MODULE_TYPE_LINE
module = MockModule(index, name, desc, module_type, slot, serial)
# Set initial state
status = ModuleBase.MODULE_STATUS_ONLINE
module.set_oper_status(status)
chassis.module_list.append(module)
config_updater = ModuleConfigUpdater(SYSLOG_IDENTIFIER, chassis)
admin_state = 0
config_updater.module_config_update(name, admin_state)
# No change since invalid key
assert module.get_admin_state() != admin_state
def test_configupdater_check_valid_index():
chassis = MockChassis()
index = -1
name = "LINE-CARD0"
desc = "36 port 400G card"
slot = 1
serial = "LC1000101"
module_type = ModuleBase.MODULE_TYPE_LINE
module = MockModule(index, name, desc, module_type, slot, serial)
# Set initial state
status = ModuleBase.MODULE_STATUS_ONLINE
module.set_oper_status(status)
chassis.module_list.append(module)
config_updater = ModuleConfigUpdater(SYSLOG_IDENTIFIER, chassis)
admin_state = 0
config_updater.module_config_update(name, admin_state)
# No change since invalid index
assert module.get_admin_state() != admin_state
def test_configupdater_check_admin_state():
chassis = MockChassis()
index = 0
name = "LINE-CARD0"
desc = "36 port 400G card"
slot = 1
serial = "LC1000101"
module_type = ModuleBase.MODULE_TYPE_LINE
module = MockModule(index, name, desc, module_type, slot, serial)
# Set initial state
status = ModuleBase.MODULE_STATUS_ONLINE
module.set_oper_status(status)
chassis.module_list.append(module)
config_updater = ModuleConfigUpdater(SYSLOG_IDENTIFIER, chassis)
admin_state = 0
config_updater.module_config_update(name, admin_state)
assert module.get_admin_state() == admin_state
admin_state = 1
config_updater.module_config_update(name, admin_state)
assert module.get_admin_state() == admin_state
def test_smartswitch_configupdater_check_admin_state():
chassis = MockSmartSwitchChassis()
index = 0
name = "DPU0"
desc = "DPU Module 0"
slot = 1
serial = "DPU0-0000"
module_type = ModuleBase.MODULE_TYPE_DPU
module = MockModule(index, name, desc, module_type, slot, serial)
# Set initial state
status = ModuleBase.MODULE_STATUS_ONLINE
module.set_oper_status(status)
chassis.module_list.append(module)
config_updater = SmartSwitchModuleConfigUpdater(SYSLOG_IDENTIFIER, chassis)
# Test setting admin state to down
admin_state = 0
with patch.object(module, 'module_pre_shutdown') as mock_module_pre_shutdown, \
patch.object(module, 'set_admin_state') as mock_set_admin_state:
config_updater.module_config_update(name, admin_state)
mock_module_pre_shutdown.assert_called_once()
mock_set_admin_state.assert_called_once_with(admin_state)
# Test setting admin state to up
admin_state = 1
with patch.object(module, 'set_admin_state') as mock_set_admin_state, \
patch.object(module, 'module_post_startup') as mock_module_post_startup:
config_updater.module_config_update(name, admin_state)
mock_set_admin_state.assert_called_once_with(admin_state)
mock_module_post_startup.assert_called_once()
@patch("your_module.glob.glob")
@patch("your_module.open", new_callable=mock_open)
def test_update_dpu_reboot_cause_to_db(self, mock_open, mock_glob):
# Set up the SmartSwitchModuleUpdater and test inputs
module_updater = SmartSwitchModuleUpdater(SYSLOG_IDENTIFIER, chassis=MagicMock())
module = "dpu0"
module_updater.chassis_state_db = MagicMock()
# Case 1: No history files found
mock_glob.return_value = []
with patch.object(module_updater, "log_warning") as mock_log_warning:
module_updater.update_dpu_reboot_cause_to_db(module)
mock_log_warning.assert_called_once_with(f"No reboot cause history files found for module: {module}")
# Case 2: Valid JSON file with reboot cause
mock_glob.return_value = ["/host/reboot-cause/module/dpu0/history/file1.txt"]
mock_open().read.return_value = json.dumps({"name": "reboot_2024", "reason": "Power loss"})
with patch.object(module_updater, "log_warning") as mock_log_warning:
module_updater.update_dpu_reboot_cause_to_db(module)
mock_log_warning.assert_not_called() # No warnings expected
module_updater.chassis_state_db.hset.assert_any_call("REBOOT_CAUSE|DPU0|reboot_2024", "name", "reboot_2024")
module_updater.chassis_state_db.hset.assert_any_call("REBOOT_CAUSE|DPU0|reboot_2024", "reason", "Power loss")
# Case 3: Empty JSON object in file
mock_open().read.return_value = json.dumps({})
with patch.object(module_updater, "log_warning") as mock_log_warning:
module_updater.update_dpu_reboot_cause_to_db(module)
mock_log_warning.assert_any_call(f"{module} reboot_cause_dict is empty")
# Case 4: Invalid JSON in file
mock_open().read.side_effect = json.JSONDecodeError("Expecting value", "", 0)
with patch.object(module_updater, "log_warning") as mock_log_warning:
module_updater.update_dpu_reboot_cause_to_db(module)
mock_log_warning.assert_any_call("Failed to decode JSON from file: /host/reboot-cause/module/dpu0/history/file1.txt")
# Case 5: General exception handling
mock_open.side_effect = IOError("Unable to read file")
with patch.object(module_updater, "log_warning") as mock_log_warning:
module_updater.update_dpu_reboot_cause_to_db(module)
mock_log_warning.assert_any_call("Error processing file /host/reboot-cause/module/dpu0/history/file1.txt: Unable to read file")
def test_smartswitch_module_db_update():
chassis = MockSmartSwitchChassis()
reboot_cause = "Power loss"
key = "DPU0"
index = 0
name = "DPU0"
desc = "DPU Module 0"
slot = 0
serial = "DPU0-0000"
module_type = ModuleBase.MODULE_TYPE_DPU
module = MockModule(index, name, desc, module_type, slot, serial)
# Set initial state
status = ModuleBase.MODULE_STATUS_ONLINE
module.set_oper_status(status)
chassis.module_list.append(module)
module_updater = SmartSwitchModuleUpdater(SYSLOG_IDENTIFIER, chassis)
expected_path = "/host/reboot-cause/module/reboot_cause/dpu0/history/2024_11_13_15_06_40_reboot_cause.txt"
symlink_path = "/host/reboot-cause/module/dpu0/previous-reboot-cause.json"
with patch("os.path.exists", return_value=True), \
patch("os.makedirs") as mock_makedirs, \
patch("builtins.open", mock_open(read_data="Power loss")) as mock_file, \
patch("os.remove") as mock_remove, \
patch("os.symlink") as mock_symlink:
# Call the function to test
module_updater.persist_dpu_reboot_cause(reboot_cause, key)
module_updater._is_first_boot(name)
module_updater.persist_dpu_reboot_time(name)
module_updater.update_dpu_reboot_cause_to_db(name)
def test_platform_json_file_exists_and_valid():
"""Test case where the platform JSON file exists with valid data."""
chassis = MockSmartSwitchChassis()
# Define the custom mock_open function to handle specific file paths
def custom_mock_open(*args, **kwargs):
if args and args[0] == PLATFORM_JSON_FILE:
return mock_open(read_data='{"dpu_reboot_timeout": 360}')(*args, **kwargs)
return open(*args, **kwargs) # Call the real open for other files
with patch("os.path.isfile", return_value=True), \
patch("builtins.open", custom_mock_open):
# Initialize the updater; it should read the mocked JSON data
updater = SmartSwitchModuleUpdater("SYSLOG", chassis)
# Check that the extracted dpu_reboot_timeout value is as expected
assert updater.dpu_reboot_timeout == 360
def test_platform_json_file_exists_fail_init():
"""Test case where the platform JSON file exists with valid data."""
chassis = MockSmartSwitchChassis()
# Define the custom mock_open function to handle specific file paths
def custom_mock_open(*args, **kwargs):
if args and args[0] == PLATFORM_JSON_FILE:
return mock_open(read_data='{"dpu_reboot_timeout": 360}')(*args, **kwargs)
return open(*args, **kwargs) # Call the real open for other files
with patch("os.path.isfile", return_value=True), \
patch("builtins.open", custom_mock_open):
# Initialize the updater; it should read the mocked JSON data
updater = SmartSwitchModuleUpdater("SYSLOG", chassis)
updater.midplane_initialized = False
# Check that the extracted dpu_reboot_timeout value is as expected
assert updater.dpu_reboot_timeout == 360
def test_configupdater_check_num_modules():
chassis = MockChassis()
index = 0
name = "LINE-CARD0"
desc = "36 port 400G card"
slot = 1
serial = "LC1000101"
module_type = ModuleBase.MODULE_TYPE_LINE
module = MockModule(index, name, desc, module_type, slot, serial)
# No modules
module_updater = ModuleUpdater(SYSLOG_IDENTIFIER, chassis, slot,
module.supervisor_slot)
module_updater.modules_num_update()
fvs = module_updater.chassis_table.get(CHASSIS_INFO_KEY_TEMPLATE.format(1))
assert fvs == None
# Add a module
chassis.module_list.append(module)
module_updater.modules_num_update()
fvs = module_updater.chassis_table.get(CHASSIS_INFO_KEY_TEMPLATE.format(1))
if isinstance(fvs, list):
fvs = dict(fvs[-1])
assert chassis.get_num_modules() == int(fvs[CHASSIS_INFO_CARD_NUM_FIELD])
module_updater.deinit()
fvs = module_updater.chassis_table.get(CHASSIS_INFO_KEY_TEMPLATE.format(1))
assert fvs == None
def test_moduleupdater_check_string_slot():
chassis = MockChassis()
#Supervisor
index = 0
name = "SUPERVISOR0"
desc = "Supervisor card"
slot = "A"
serial = "RP1000101"
module_type = ModuleBase.MODULE_TYPE_SUPERVISOR
supervisor = MockModule(index, name, desc, module_type, slot, serial)
supervisor.set_midplane_ip()
chassis.module_list.append(supervisor)
#Linecard
index = 1
name = "LINE-CARD0"
desc = "36 port 400G card"
slot = "1"
serial = "LC1000101"
module_type = ModuleBase.MODULE_TYPE_LINE
module = MockModule(index, name, desc, module_type, slot, serial)
module.set_midplane_ip()
chassis.module_list.append(module)
#Fabric-card
index = 1
name = "FABRIC-CARD0"
desc = "Switch fabric card"
slot = "17"
serial = "FC1000101"
module_type = ModuleBase.MODULE_TYPE_FABRIC
fabric = MockModule(index, name, desc, module_type, slot, serial)
chassis.module_list.append(fabric)
#Run on supervisor
module_updater = ModuleUpdater(SYSLOG_IDENTIFIER, chassis, slot,
module.supervisor_slot)
module_updater.supervisor_slot = supervisor.get_slot()
module_updater.my_slot = supervisor.get_slot()
module_updater.modules_num_update()
module_updater.module_db_update()
module_updater.check_midplane_reachability()
midplane_table = module_updater.midplane_table
#Check only one entry in database
assert 1 == midplane_table.size()
def test_midplane_presence_modules():
chassis = MockChassis()
#Supervisor
index = 0
name = "SUPERVISOR0"
desc = "Supervisor card"
slot = 16
serial = "RP1000101"
module_type = ModuleBase.MODULE_TYPE_SUPERVISOR
supervisor = MockModule(index, name, desc, module_type, slot, serial)
supervisor.set_midplane_ip()
chassis.module_list.append(supervisor)
#Linecard
index = 1
name = "LINE-CARD0"
desc = "36 port 400G card"
slot = 1
serial = "LC1000101"
module_type = ModuleBase.MODULE_TYPE_LINE
module = MockModule(index, name, desc, module_type, slot, serial)
module.set_midplane_ip()
chassis.module_list.append(module)
#Fabric-card
index = 1
name = "FABRIC-CARD0"
desc = "Switch fabric card"
slot = 17
serial = "FC1000101"
module_type = ModuleBase.MODULE_TYPE_FABRIC
fabric = MockModule(index, name, desc, module_type, slot, serial)
chassis.module_list.append(fabric)
#Run on supervisor
module_updater = ModuleUpdater(SYSLOG_IDENTIFIER, chassis, slot,
module.supervisor_slot)
module_updater.supervisor_slot = supervisor.get_slot()
module_updater.my_slot = supervisor.get_slot()
module_updater.modules_num_update()
module_updater.module_db_update()
module_updater.check_midplane_reachability()
midplane_table = module_updater.midplane_table
#Check only one entry in database
assert 1 == midplane_table.size()
#Check fields in database
name = "LINE-CARD0"
fvs = midplane_table.get(name)
assert fvs != None
if isinstance(fvs, list):
fvs = dict(fvs[-1])
assert module.get_midplane_ip() == fvs[CHASSIS_MIDPLANE_INFO_IP_FIELD]
assert str(module.is_midplane_reachable()) == fvs[CHASSIS_MIDPLANE_INFO_ACCESS_FIELD]
#Set access of line-card to Up (midplane connectivity is down initially)
module.set_midplane_reachable(True)
module_updater.check_midplane_reachability()
fvs = midplane_table.get(name)
assert fvs != None
if isinstance(fvs, list):
fvs = dict(fvs[-1])
assert module.get_midplane_ip() == fvs[CHASSIS_MIDPLANE_INFO_IP_FIELD]
assert str(module.is_midplane_reachable()) == fvs[CHASSIS_MIDPLANE_INFO_ACCESS_FIELD]
#Set access of line-card to Down (to mock midplane connectivity state change)
module.set_midplane_reachable(False)
module_updater.check_midplane_reachability()
fvs = midplane_table.get(name)
assert fvs != None
if isinstance(fvs, list):
fvs = dict(fvs[-1])
assert module.get_midplane_ip() == fvs[CHASSIS_MIDPLANE_INFO_IP_FIELD]
assert str(module.is_midplane_reachable()) == fvs[CHASSIS_MIDPLANE_INFO_ACCESS_FIELD]
#Deinit
module_updater.deinit()
fvs = midplane_table.get(name)
assert fvs == None
@patch('os.makedirs')
@patch('builtins.open', new_callable=mock_open)
def test_midplane_presence_dpu_modules(mock_open, mock_makedirs):
with tempfile.TemporaryDirectory() as temp_dir:
# Assume your method uses a path variable that you can set for testing
path = os.path.join(temp_dir, 'subdir')
# Set up your mock or variable to use temp_dir
mock_makedirs.side_effect = lambda x, **kwargs: None # Prevent actual call
chassis = MockSmartSwitchChassis()
#DPU0
index = 0
name = "DPU0"
desc = "DPU Module 0"
slot = 0
sup_slot = 0
serial = "DPU0-0000"
module_type = ModuleBase.MODULE_TYPE_DPU
module = MockModule(index, name, desc, module_type, slot, serial)
module.set_midplane_ip()
module.prev_reboot_time = "2024_10_30_02_44_50"
chassis.module_list.append(module)
#Run on supervisor
module_updater = SmartSwitchModuleUpdater(SYSLOG_IDENTIFIER, chassis)
module_updater.midplane_initialized = True
module_updater.modules_num_update()
module_updater.module_db_update()
module_updater.check_midplane_reachability()
midplane_table = module_updater.midplane_table
#Check only one entry in database
assert 1 == midplane_table.size()
#Check fields in database
fvs = midplane_table.get(name)
assert fvs != None
if isinstance(fvs, list):
fvs = dict(fvs[-1])
assert module.get_midplane_ip() == fvs[CHASSIS_MIDPLANE_INFO_IP_FIELD]
assert str(module.is_midplane_reachable()) == fvs[CHASSIS_MIDPLANE_INFO_ACCESS_FIELD]
#Set access of DPU0 to Up (midplane connectivity is down initially)
module.set_midplane_reachable(True)
module_updater.check_midplane_reachability()
fvs = midplane_table.get(name)
assert fvs != None
if isinstance(fvs, list):
fvs = dict(fvs[-1])
assert module.get_midplane_ip() == fvs[CHASSIS_MIDPLANE_INFO_IP_FIELD]
assert str(module.is_midplane_reachable()) == fvs[CHASSIS_MIDPLANE_INFO_ACCESS_FIELD]
#Set access of DPU0 to Down (to mock midplane connectivity state change)
module.set_midplane_reachable(False)
module_updater.check_midplane_reachability()
fvs = midplane_table.get(name)
assert fvs != None
if isinstance(fvs, list):
fvs = dict(fvs[-1])
assert module.get_midplane_ip() == fvs[CHASSIS_MIDPLANE_INFO_IP_FIELD]
assert str(module.is_midplane_reachable()) == fvs[CHASSIS_MIDPLANE_INFO_ACCESS_FIELD]
# Run chassis db clean up
module_updater.module_down_chassis_db_cleanup()
module_updater.chassis_state_db = None
module_updater.module_down_chassis_db_cleanup()
#Deinit
module_updater.deinit()
fvs = midplane_table.get(name)
assert fvs == None
@patch('os.makedirs')
@patch('builtins.open', new_callable=mock_open)
def test_midplane_presence_uninitialized_dpu_modules(mock_open, mock_makedirs):
with tempfile.TemporaryDirectory() as temp_dir:
# Assume your method uses a path variable that you can set for testing
path = os.path.join(temp_dir, 'subdir')
# Set up your mock or variable to use temp_dir
mock_makedirs.side_effect = lambda x, **kwargs: None # Prevent actual call
chassis = MockSmartSwitchChassis()
#DPU0
index = 0
name = "DPU0"
desc = "DPU Module 0"
slot = 0
sup_slot = 0
serial = "DPU0-0000"
module_type = ModuleBase.MODULE_TYPE_DPU
module = MockModule(index, name, desc, module_type, slot, serial)
module.set_midplane_ip()
module.prev_reboot_time = "2024_10_30_02_44_50"
chassis.module_list.append(module)
#Run on supervisor
module_updater = SmartSwitchModuleUpdater(SYSLOG_IDENTIFIER, chassis)
module_updater.midplane_initialized = False
module_updater.modules_num_update()
module_updater.module_db_update()
module_updater.check_midplane_reachability()
midplane_table = module_updater.midplane_table
#Check only one entry in database
assert 1 != midplane_table.size()
builtin_open = open # save the unpatched version
def lc_mock_open(*args, **kwargs):
if args and args[0] == PLATFORM_ENV_CONF_FILE:
return mock.mock_open(read_data="dummy=1\nlinecard_reboot_timeout=240\n")(*args, **kwargs)
# unpatched version for every other path
return builtin_open(*args, **kwargs)
@patch("builtins.open", lc_mock_open)
@patch('os.path.isfile', MagicMock(return_value=True))
def test_midplane_presence_modules_linecard_reboot():
chassis = MockChassis()
#Supervisor
index = 0
name = "SUPERVISOR0"
desc = "Supervisor card"
slot = 16
serial = "RP1000101"
module_type = ModuleBase.MODULE_TYPE_SUPERVISOR
supervisor = MockModule(index, name, desc, module_type, slot, serial)
supervisor.set_midplane_ip()
chassis.module_list.append(supervisor)
#Linecard
index = 1
name = "LINE-CARD0"
desc = "36 port 400G card"
slot = 1
serial = "LC1000101"
module_type = ModuleBase.MODULE_TYPE_LINE
module = MockModule(index, name, desc, module_type, slot, serial)
module.set_midplane_ip()
chassis.module_list.append(module)
#Fabric-card
index = 1
name = "FABRIC-CARD0"
desc = "Switch fabric card"
slot = 17
serial = "FC1000101"
module_type = ModuleBase.MODULE_TYPE_FABRIC
fabric = MockModule(index, name, desc, module_type, slot, serial)
chassis.module_list.append(fabric)
#Run on supervisor
module_updater = ModuleUpdater(SYSLOG_IDENTIFIER, chassis, slot,
module.supervisor_slot)
module_updater.supervisor_slot = supervisor.get_slot()
module_updater.my_slot = supervisor.get_slot()
module_updater.modules_num_update()
module_updater.module_db_update()
module_updater.check_midplane_reachability()
midplane_table = module_updater.midplane_table
#Check only one entry in database
assert 1 == midplane_table.size()
#Check fields in database
name = "LINE-CARD0"
fvs = midplane_table.get(name)
assert fvs != None
if isinstance(fvs, list):
fvs = dict(fvs[-1])
assert module.get_midplane_ip() == fvs[CHASSIS_MIDPLANE_INFO_IP_FIELD]
assert str(module.is_midplane_reachable()) == fvs[CHASSIS_MIDPLANE_INFO_ACCESS_FIELD]
#Set access of line-card to Up (midplane connectivity is down initially)
module.set_midplane_reachable(True)
module_updater.check_midplane_reachability()