1
+ #from unittest.mock import DEFAULT
1
2
from xcvrd .xcvrd_utilities .port_mapping import *
2
3
from xcvrd .xcvrd_utilities .sfp_status_helper import *
3
4
from xcvrd .xcvrd import *
28
29
modules_path = os .path .dirname (test_path )
29
30
scripts_path = os .path .join (modules_path , "xcvrd" )
30
31
sys .path .insert (0 , modules_path )
32
+ DEFAULT_NAMESPACE = ['' ]
31
33
32
34
os .environ ["XCVRD_UNIT_TESTING" ] = "1"
33
35
@@ -242,7 +244,7 @@ def test_post_port_sfp_dom_info_to_db(self):
242
244
port_change_event = PortChangeEvent ('Ethernet0' , 1 , 0 , PortChangeEvent .PORT_ADD )
243
245
port_mapping .handle_port_change_event (port_change_event )
244
246
stop_event = threading .Event ()
245
- xcvr_table_helper = XcvrTableHelper ()
247
+ xcvr_table_helper = XcvrTableHelper (DEFAULT_NAMESPACE )
246
248
post_port_sfp_dom_info_to_db (True , port_mapping , xcvr_table_helper , stop_event )
247
249
248
250
@patch ('xcvrd.xcvrd_utilities.port_mapping.PortMapping.logical_port_name_to_physical_port_list' , MagicMock (return_value = [0 ]))
@@ -255,7 +257,7 @@ def test_init_port_sfp_status_tbl(self):
255
257
port_change_event = PortChangeEvent ('Ethernet0' , 1 , 0 , PortChangeEvent .PORT_ADD )
256
258
port_mapping .handle_port_change_event (port_change_event )
257
259
stop_event = threading .Event ()
258
- xcvr_table_helper = XcvrTableHelper ()
260
+ xcvr_table_helper = XcvrTableHelper (DEFAULT_NAMESPACE )
259
261
init_port_sfp_status_tbl (port_mapping , xcvr_table_helper , stop_event )
260
262
261
263
def test_get_media_settings_key (self ):
@@ -339,7 +341,7 @@ def test_handle_port_config_change(self, mock_select, mock_sub_table):
339
341
mock_select .return_value = (swsscommon .Select .OBJECT , mock_selectable )
340
342
mock_sub_table .return_value = mock_selectable
341
343
342
- sel , asic_context = subscribe_port_config_change ()
344
+ sel , asic_context = subscribe_port_config_change (DEFAULT_NAMESPACE )
343
345
port_mapping = PortMapping ()
344
346
stop_event = threading .Event ()
345
347
stop_event .is_set = MagicMock (return_value = False )
@@ -367,7 +369,7 @@ def test_get_port_mapping(self, mock_swsscommon_table):
367
369
mock_table .getKeys = MagicMock (return_value = ['Ethernet0' , 'Ethernet4' , 'Ethernet-IB0' ])
368
370
mock_table .get = MagicMock (side_effect = [(True , (('index' , 1 ), )), (True , (('index' , 2 ), )), (True , (('index' , 3 ), ))])
369
371
mock_swsscommon_table .return_value = mock_table
370
- port_mapping = get_port_mapping ()
372
+ port_mapping = get_port_mapping (DEFAULT_NAMESPACE )
371
373
assert port_mapping .logical_port_list .count ('Ethernet0' )
372
374
assert port_mapping .get_asic_id_for_logical_port ('Ethernet0' ) == 0
373
375
assert port_mapping .get_physical_to_logical (1 ) == ['Ethernet0' ]
@@ -382,7 +384,7 @@ def test_get_port_mapping(self, mock_swsscommon_table):
382
384
assert port_mapping .get_asic_id_for_logical_port ('Ethernet-IB0' ) == None
383
385
assert port_mapping .get_physical_to_logical (3 ) == None
384
386
assert port_mapping .get_logical_to_physical ('Ethernet-IB0' ) == None
385
-
387
+
386
388
@patch ('swsscommon.swsscommon.Select.addSelectable' , MagicMock ())
387
389
@patch ('swsscommon.swsscommon.SubscriberStateTable' )
388
390
@patch ('swsscommon.swsscommon.Select.select' )
@@ -418,7 +420,7 @@ def test_DaemonXcvrd_run(self, mock_task_stop1, mock_task_stop2, mock_task_run1,
418
420
@patch ('xcvrd.xcvrd._wrapper_get_sfp_type' , MagicMock (return_value = 'QSFP_DD' ))
419
421
def test_CmisManagerTask_handle_port_change_event (self ):
420
422
port_mapping = PortMapping ()
421
- task = CmisManagerTask (port_mapping )
423
+ task = CmisManagerTask (DEFAULT_NAMESPACE , port_mapping )
422
424
423
425
assert not task .isPortConfigDone
424
426
port_change_event = PortChangeEvent ('PortConfigDone' , - 1 , 0 , PortChangeEvent .PORT_SET )
@@ -450,7 +452,7 @@ def test_CmisManagerTask_task_run_stop(self, mock_chassis):
450
452
mock_chassis .get_all_sfps = MagicMock (return_value = [mock_object , mock_object ])
451
453
452
454
port_mapping = PortMapping ()
453
- task = CmisManagerTask (port_mapping )
455
+ task = CmisManagerTask (DEFAULT_NAMESPACE , port_mapping )
454
456
task .task_run ()
455
457
task .task_stop ()
456
458
assert task .task_process is None
@@ -536,7 +538,7 @@ def test_CmisManagerTask_task_worker(self, mock_chassis):
536
538
mock_chassis .get_sfp = MagicMock (return_value = mock_sfp )
537
539
538
540
port_mapping = PortMapping ()
539
- task = CmisManagerTask (port_mapping )
541
+ task = CmisManagerTask (DEFAULT_NAMESPACE , port_mapping )
540
542
541
543
port_change_event = PortChangeEvent ('PortConfigDone' , - 1 , 0 , PortChangeEvent .PORT_SET )
542
544
task .on_port_update_event (port_change_event )
@@ -547,33 +549,44 @@ def test_CmisManagerTask_task_worker(self, mock_chassis):
547
549
task .on_port_update_event (port_change_event )
548
550
assert len (task .port_dict ) == 1
549
551
552
+ task .get_host_tx_status = MagicMock (return_value = 'true' )
553
+ task .get_port_admin_status = MagicMock (return_value = 'up' )
554
+
550
555
# Case 1: Module Inserted --> DP_DEINIT
556
+ task .task_stopping_event .is_set = MagicMock (side_effect = [False , False , True ])
557
+ task .task_worker ()
558
+ assert task .port_dict ['Ethernet0' ]['cmis_state' ] == 'DP_DEINIT'
559
+
551
560
task .task_stopping_event .is_set = MagicMock (side_effect = [False , False , True ])
552
561
task .task_worker ()
553
562
assert mock_xcvr_api .set_datapath_deinit .call_count == 1
554
563
assert mock_xcvr_api .tx_disable_channel .call_count == 1
555
- assert mock_xcvr_api .set_lpmode .call_count == 2
564
+ assert mock_xcvr_api .set_lpmode .call_count == 1
565
+ assert task .port_dict ['Ethernet0' ]['cmis_state' ] == 'AP_CONFIGURED'
556
566
557
567
# Case 2: DP_DEINIT --> AP Configured
558
568
task .task_stopping_event .is_set = MagicMock (side_effect = [False , False , True ])
559
569
task .task_worker ()
560
570
assert mock_xcvr_api .set_application .call_count == 1
571
+ assert task .port_dict ['Ethernet0' ]['cmis_state' ] == 'DP_INIT'
561
572
562
573
# Case 3: AP Configured --> DP_INIT
563
574
task .task_stopping_event .is_set = MagicMock (side_effect = [False , False , True ])
564
575
task .task_worker ()
565
576
assert mock_xcvr_api .set_datapath_init .call_count == 1
577
+ assert task .port_dict ['Ethernet0' ]['cmis_state' ] == 'DP_TXON'
566
578
567
579
# Case 4: DP_INIT --> DP_TXON
568
580
task .task_stopping_event .is_set = MagicMock (side_effect = [False , False , True ])
569
581
task .task_worker ()
570
582
assert mock_xcvr_api .tx_disable_channel .call_count == 2
583
+ assert task .port_dict ['Ethernet0' ]['cmis_state' ] == 'DP_ACTIVATION'
571
584
572
585
@patch ('xcvrd.xcvrd.XcvrTableHelper' , MagicMock ())
573
586
def test_DomInfoUpdateTask_handle_port_change_event (self ):
574
587
port_mapping = PortMapping ()
575
- task = DomInfoUpdateTask (port_mapping )
576
- task .xcvr_table_helper = XcvrTableHelper ()
588
+ task = DomInfoUpdateTask (DEFAULT_NAMESPACE , port_mapping )
589
+ task .xcvr_table_helper = XcvrTableHelper (DEFAULT_NAMESPACE )
577
590
port_change_event = PortChangeEvent ('Ethernet0' , 1 , 0 , PortChangeEvent .PORT_ADD )
578
591
task .on_port_config_change (port_change_event )
579
592
assert task .port_mapping .logical_port_list .count ('Ethernet0' )
@@ -592,7 +605,7 @@ def test_DomInfoUpdateTask_handle_port_change_event(self):
592
605
@patch ('xcvrd.xcvrd_utilities.port_mapping.handle_port_config_change' , MagicMock ())
593
606
def test_DomInfoUpdateTask_task_run_stop (self ):
594
607
port_mapping = PortMapping ()
595
- task = DomInfoUpdateTask (port_mapping )
608
+ task = DomInfoUpdateTask (DEFAULT_NAMESPACE , port_mapping )
596
609
task .task_run ()
597
610
task .task_stop ()
598
611
assert not task .task_thread .is_alive ()
@@ -612,8 +625,8 @@ def test_DomInfoUpdateTask_task_worker(self, mock_select, mock_sub_table, mock_p
612
625
mock_sub_table .return_value = mock_selectable
613
626
614
627
port_mapping = PortMapping ()
615
- task = DomInfoUpdateTask (port_mapping )
616
- task .xcvr_table_helper = XcvrTableHelper ()
628
+ task = DomInfoUpdateTask (DEFAULT_NAMESPACE , port_mapping )
629
+ task .xcvr_table_helper = XcvrTableHelper (DEFAULT_NAMESPACE )
617
630
task .task_stopping_event .wait = MagicMock (side_effect = [False , True ])
618
631
mock_detect_error .return_value = True
619
632
task .task_worker ()
@@ -640,8 +653,8 @@ def test_SfpStateUpdateTask_handle_port_change_event(self, mock_table_helper):
640
653
stopping_event = multiprocessing .Event ()
641
654
port_mapping = PortMapping ()
642
655
retry_eeprom_set = set ()
643
- task = SfpStateUpdateTask (port_mapping , retry_eeprom_set )
644
- task .xcvr_table_helper = XcvrTableHelper ()
656
+ task = SfpStateUpdateTask (DEFAULT_NAMESPACE , port_mapping , retry_eeprom_set )
657
+ task .xcvr_table_helper = XcvrTableHelper (DEFAULT_NAMESPACE )
645
658
port_change_event = PortChangeEvent ('Ethernet0' , 1 , 0 , PortChangeEvent .PORT_ADD )
646
659
wait_time = 5
647
660
while wait_time > 0 :
@@ -671,7 +684,7 @@ def test_SfpStateUpdateTask_handle_port_change_event(self, mock_table_helper):
671
684
def test_SfpStateUpdateTask_task_run_stop (self ):
672
685
port_mapping = PortMapping ()
673
686
retry_eeprom_set = set ()
674
- task = SfpStateUpdateTask (port_mapping , retry_eeprom_set )
687
+ task = SfpStateUpdateTask (DEFAULT_NAMESPACE , port_mapping , retry_eeprom_set )
675
688
sfp_error_event = multiprocessing .Event ()
676
689
task .task_run (sfp_error_event )
677
690
assert wait_until (5 , 1 , task .task_process .is_alive )
@@ -686,8 +699,8 @@ def test_SfpStateUpdateTask_retry_eeprom_reading(self, mock_post_sfp_info):
686
699
687
700
port_mapping = PortMapping ()
688
701
retry_eeprom_set = set ()
689
- task = SfpStateUpdateTask (port_mapping , retry_eeprom_set )
690
- task .xcvr_table_helper = XcvrTableHelper ()
702
+ task = SfpStateUpdateTask (DEFAULT_NAMESPACE , port_mapping , retry_eeprom_set )
703
+ task .xcvr_table_helper = XcvrTableHelper (DEFAULT_NAMESPACE )
691
704
task .xcvr_table_helper .get_intf_tbl = MagicMock (return_value = mock_table )
692
705
task .xcvr_table_helper .get_dom_tbl = MagicMock (return_value = mock_table )
693
706
task .xcvr_table_helper .get_app_port_tbl = MagicMock (return_value = mock_table )
@@ -712,7 +725,7 @@ def test_SfpStateUpdateTask_retry_eeprom_reading(self, mock_post_sfp_info):
712
725
def test_SfpStateUpdateTask_mapping_event_from_change_event (self ):
713
726
port_mapping = PortMapping ()
714
727
retry_eeprom_set = set ()
715
- task = SfpStateUpdateTask (port_mapping , retry_eeprom_set )
728
+ task = SfpStateUpdateTask (DEFAULT_NAMESPACE , port_mapping , retry_eeprom_set )
716
729
port_dict = {}
717
730
assert task ._mapping_event_from_change_event (False , port_dict ) == SYSTEM_FAIL
718
731
assert port_dict [EVENT_ON_ALL_SFP ] == SYSTEM_FAIL
@@ -744,8 +757,8 @@ def test_SfpStateUpdateTask_mapping_event_from_change_event(self):
744
757
def test_SfpStateUpdateTask_task_worker (self , mock_updata_status , mock_post_sfp_info , mock_post_dom_info , mock_post_dom_th , mock_update_media_setting , mock_del_dom , mock_change_event , mock_mapping_event , mock_os_kill ):
745
758
port_mapping = PortMapping ()
746
759
retry_eeprom_set = set ()
747
- task = SfpStateUpdateTask (port_mapping , retry_eeprom_set )
748
- task .xcvr_table_helper = XcvrTableHelper ()
760
+ task = SfpStateUpdateTask (DEFAULT_NAMESPACE , port_mapping , retry_eeprom_set )
761
+ task .xcvr_table_helper = XcvrTableHelper (DEFAULT_NAMESPACE )
749
762
stop_event = multiprocessing .Event ()
750
763
sfp_error_event = multiprocessing .Event ()
751
764
mock_change_event .return_value = (True , {0 : 0 }, {})
@@ -854,8 +867,8 @@ class MockTable:
854
867
855
868
port_mapping = PortMapping ()
856
869
retry_eeprom_set = set ()
857
- task = SfpStateUpdateTask (port_mapping , retry_eeprom_set )
858
- task .xcvr_table_helper = XcvrTableHelper ()
870
+ task = SfpStateUpdateTask (DEFAULT_NAMESPACE , port_mapping , retry_eeprom_set )
871
+ task .xcvr_table_helper = XcvrTableHelper (DEFAULT_NAMESPACE )
859
872
task .xcvr_table_helper .get_status_tbl = mock_table_helper .get_status_tbl
860
873
task .xcvr_table_helper .get_intf_tbl = mock_table_helper .get_intf_tbl
861
874
task .xcvr_table_helper .get_dom_tbl = mock_table_helper .get_dom_tbl
0 commit comments