Skip to content
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

CP-52852: add handler for xmlrpc ProtocolError #722

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions drivers/SRCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
#
# SRCommand: parse SR command-line objects
#
import traceback

import XenAPI # pylint: disable=import-error
import sys
import xs_errors
import xmlrpc.client
from xmlrpc.client import ProtocolError
import SR
import util
import blktap2
Expand Down Expand Up @@ -390,6 +392,14 @@ def run(driver, driver_info):
else:
print(ret)

except ProtocolError:
# xs_errors.XenError.__new__ returns a different class
# pylint: disable-msg=E1101
exception_trace = traceback.format_exc()
util.SMlog(f'XenAPI Protocol error {exception_trace}')
print(xs_errors.XenError(
MarkSymsCtx marked this conversation as resolved.
Show resolved Hide resolved
'APIProtocolError',
opterr=exception_trace).toxml())
except (Exception, xs_errors.SRException) as e:
try:
util.logException(driver_info['name'])
Expand Down
5 changes: 5 additions & 0 deletions drivers/XE_SR_ERRORCODES.xml
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,11 @@
<description>A Failure occurred accessing an API object</description>
<value>153</value>
</code>
<code>
<name>APIProtocolError</name>
<description>A protocol error was received when accessing the API</description>
<value>154</value>
</code>

<!-- Netapp Specific Error codes -->
<code>
Expand Down
36 changes: 36 additions & 0 deletions tests/test_SRCommand.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import unittest
import unittest.mock as mock
import uuid
import xmlrpc.client

import SR
import SRCommand


Expand Down Expand Up @@ -123,6 +126,39 @@ def test_run_wrapped_if_not_SRException(

SRCommand.run(mock_driver, DRIVER_INFO)

@mock.patch("sys.exit", autospec=True)
@mock.patch('util.logException', autospec=True)
@mock.patch('SRCommand.SRCommand', autospec=True)
def test_run_reports_protocol_error(
self,
mock_sr_command,
mock_logException,
mock_exit):

"""
If XenAPI raises a protocol error convert to error code
"""
from DummySR import DRIVER_INFO

def raise_error(sr):
raise xmlrpc.client.ProtocolError(
'XapiUrl', 500,
'Internal Error', {})

# Create function to raise exception in SRCommand.run()
mock_cmd = mock.MagicMock()
mock_cmd.run.side_effect = raise_error
mock_cmd.sr_uuid = str(uuid.uuid4())
mock_sr_command.return_value = mock_cmd

mock_driver = mock.MagicMock(autospec=SR.SR)

with mock.patch('builtins.print') as mock_print:
SRCommand.run(mock_driver, DRIVER_INFO)

self.assertIn('<value><int>154</int></value>',
mock_print.call_args[0][0])

@mock.patch("os.fsencode",
new=lambda s: s.encode("ascii", "surrogateescape"))
@mock.patch("os.fsdecode",
Expand Down