-
Notifications
You must be signed in to change notification settings - Fork 209
[ycabled] fix exception-handling logic for ycabled #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 23 commits
e3cd1a0
68e9412
cdcdc2e
aa70dcf
cc96144
81cf7ca
6421d9e
5320c94
b898ffa
2807278
3cfc033
2c14791
a71b11b
bfc979c
e92c74f
9745e6a
03c43d1
1ac2a2b
56c48ef
2e19b16
e23540c
0bd7cfb
2061ab1
419e742
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import os | ||
| import sys | ||
| import time | ||
| import traceback | ||
|
|
||
| if sys.version_info >= (3, 3): | ||
| from unittest.mock import MagicMock, patch | ||
|
|
@@ -32,6 +33,20 @@ | |
|
|
||
| os.environ["YCABLE_UNIT_TESTING"] = "1" | ||
|
|
||
| exception_output_expected= """\ | ||
| Traceback (most recent call last): | ||
| File "/sonic/src/sonic-platform-daemons/sonic-ycabled/tests/test_ycable.py", line 357, in test_ycable_helper_class_run_loop_with_exception | ||
| Y_cable_cli_task.task_cli_worker() | ||
| File "/sonic/src/sonic-platform-daemons/sonic-ycabled/ycable/ycable_utilities/y_cable_helper.py", line 3655, in task_cli_worker | ||
| sel = swsscommon.Select() | ||
| File "/usr/lib/python3.9/unittest/mock.py", line 1093, in __call__ | ||
| return self._mock_call(*args, **kwargs) | ||
| File "/usr/lib/python3.9/unittest/mock.py", line 1097, in _mock_call | ||
| return self._execute_mock_call(*args, **kwargs) | ||
| File "/usr/lib/python3.9/unittest/mock.py", line 1152, in _execute_mock_call | ||
| raise effect | ||
| NotImplementedError | ||
| """ | ||
|
|
||
| class TestYcableScript(object): | ||
|
|
||
|
|
@@ -40,20 +55,20 @@ def test_ycable_info_helper_class_run(self, mocked_sleep): | |
| with patch('ycable.ycable.platform_sfputil') as patched_util: | ||
| patched_util.logical.return_value = ['Ethernet0', 'Ethernet4'] | ||
| patched_util.get_asic_id_for_logical_port.return_value = 0 | ||
| Y_cable_state_task = YcableStateUpdateTask() | ||
| Y_cable_state_task.task_process = MagicMock() | ||
| Y_cable_state_task.task_stopping_event = MagicMock() | ||
| y_cable_presence = [True] | ||
| stopping_event = MagicMock() | ||
| sfp_error_event = MagicMock() | ||
| y_cable_presence = [True] | ||
| Y_cable_state_task.task_run(sfp_error_event, y_cable_presence) | ||
| Y_cable_state_task.task_stop() | ||
| Y_cable_task = YcableInfoUpdateTask() | ||
| Y_cable_state_task = YcableStateUpdateTask(sfp_error_event, y_cable_presence) | ||
| Y_cable_state_task.task_process = MagicMock() | ||
| Y_cable_state_task.task_stopping_event = MagicMock() | ||
| Y_cable_state_task.start() | ||
| Y_cable_state_task.join() | ||
| Y_cable_task = YcableInfoUpdateTask(y_cable_presence) | ||
| Y_cable_task.task_thread = MagicMock() | ||
| Y_cable_task.task_stopping_event = MagicMock() | ||
| Y_cable_task.task_stopping_event.is_set = MagicMock() | ||
| Y_cable_task.task_run(y_cable_presence) | ||
| Y_cable_task.task_stop() | ||
| Y_cable_task.start() | ||
| Y_cable_task.join() | ||
| Y_cable_state_task.task_stopping_event.return_value.is_set.return_value = True | ||
| #Y_cable_state_task.task_worker(stopping_event, sfp_error_event, y_cable_presence) | ||
| # For now just check if exception is thrown for UT purposes | ||
|
|
@@ -67,19 +82,20 @@ def test_ycable_info_helper_class_run(self, mocked_sleep): | |
| @patch("swsscommon.swsscommon.Select.select", MagicMock()) | ||
| def test_ycable_helper_class_run_loop(self): | ||
| Y_cable_task = YCableTableUpdateTask() | ||
| Y_cable_cli_task = YCableCliUpdateTask() | ||
| Y_cable_task.task_stopping_event = MagicMock() | ||
| Y_cable_cli_task.task_stopping_event = MagicMock() | ||
| Y_cable_task.task_thread = MagicMock() | ||
| Y_cable_task.task_thread.start = MagicMock() | ||
| Y_cable_task.task_thread.join = MagicMock() | ||
| Y_cable_task.task_cli_thead = MagicMock() | ||
| Y_cable_task.task_cli_thead.start = MagicMock() | ||
| Y_cable_task.task_cli_thead.join = MagicMock() | ||
| #Y_cable_task.task_stopping_event.return_value.is_set.return_value = False | ||
| swsscommon.SubscriberStateTable.return_value.pop.return_value = (True, True, {"read_side": "2"}) | ||
| Y_cable_task.task_worker() | ||
| Y_cable_task.task_cli_worker() | ||
| Y_cable_task.task_run() | ||
| Y_cable_task.task_stop() | ||
| Y_cable_task.start() | ||
| Y_cable_task.join() | ||
| Y_cable_cli_task.task_cli_worker() | ||
| Y_cable_cli_task.start() | ||
| Y_cable_cli_task.join() | ||
|
|
||
| @patch("swsscommon.swsscommon.Select", MagicMock()) | ||
| @patch("swsscommon.swsscommon.Select.addSelectable", MagicMock()) | ||
|
|
@@ -89,14 +105,10 @@ def test_ycable_helper_class_run(self): | |
| Y_cable_task.task_thread = MagicMock() | ||
| Y_cable_task.task_thread.start = MagicMock() | ||
| Y_cable_task.task_thread.join = MagicMock() | ||
| Y_cable_task.task_cli_thead = MagicMock() | ||
| Y_cable_task.task_cli_thead.start = MagicMock() | ||
| Y_cable_task.task_cli_thead.join = MagicMock() | ||
| Y_cable_task.task_stopping_event.return_value.is_set.return_value = True | ||
| Y_cable_task.task_worker() | ||
| Y_cable_task.task_cli_worker() | ||
| Y_cable_task.task_run() | ||
| Y_cable_task.task_stop() | ||
| Y_cable_task.start() | ||
| Y_cable_task.join() | ||
|
|
||
| def test_detect_port_in_error_status(self): | ||
|
|
||
|
|
@@ -291,9 +303,7 @@ def test_DaemonYcable_init_deinit(self): | |
| @patch('ycable.ycable.platform_sfputil', MagicMock()) | ||
| @patch('ycable.ycable.DaemonYcable.load_platform_util', MagicMock()) | ||
| @patch('ycable.ycable.YcableInfoUpdateTask', MagicMock()) | ||
| @patch('ycable.ycable.YcableInfoUpdateTask.task_run', MagicMock()) | ||
| @patch('ycable.ycable.YcableStateUpdateTask', MagicMock()) | ||
| @patch('ycable.ycable.YcableStateUpdateTask.task_run', MagicMock()) | ||
| @patch('ycable.ycable_utilities.y_cable_helper.init_ports_status_for_y_cable', MagicMock()) | ||
| def test_DaemonYcable_init_deinit_full(self): | ||
| ycable = DaemonYcable(SYSLOG_IDENTIFIER) | ||
|
|
@@ -327,3 +337,44 @@ def wait_until(total_wait_time, interval, call_back, *args, **kwargs): | |
| time.sleep(interval) | ||
| wait_time += interval | ||
| return False | ||
|
|
||
|
|
||
| class TestYcableScriptException(object): | ||
|
|
||
| @patch("swsscommon.swsscommon.Select", MagicMock(side_effect=NotImplementedError)) | ||
| @patch("swsscommon.swsscommon.Select.addSelectable", MagicMock(side_effect=NotImplementedError)) | ||
| @patch("swsscommon.swsscommon.Select.select", MagicMock(side_effect=NotImplementedError)) | ||
| def test_ycable_helper_class_run_loop_with_exception(self): | ||
|
|
||
|
|
||
|
|
||
| Y_cable_cli_task = YCableCliUpdateTask() | ||
| expected_exception_start = None | ||
| expected_exception_join = None | ||
| trace = None | ||
| try: | ||
| Y_cable_cli_task.start() | ||
| Y_cable_cli_task.task_cli_worker() | ||
| except Exception as e1: | ||
| expected_exception_start = e1 | ||
| trace = traceback.format_exc() | ||
|
|
||
|
|
||
| try: | ||
| Y_cable_cli_task.join() | ||
| except Exception as e2: | ||
| expected_exception_join = e2 | ||
|
|
||
|
|
||
| #Handy debug Helpers or else use import logging | ||
| f = open("newfile", "w") | ||
| #f.write(format(e2)) | ||
| #f.write(format(m1)) | ||
| f.write(trace) | ||
|
|
||
| assert(type(expected_exception_start) == type(expected_exception_join)) | ||
| assert(expected_exception_start.args == expected_exception_join.args) | ||
| assert("NotImplementedError" in str(trace) and "effect" in str(trace)) | ||
| assert("sonic-ycabled/ycable/ycable_utilities/y_cable_helper.py" in str(trace)) | ||
| assert("line 3655" in str(trace)) | ||
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will this work if python version changes in future?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would not be backwards compatible, thats why I had added some string checks only,
Better to omit this check I think.