Skip to content

Commit

Permalink
CP-52852: add handler for xmlrpc ProtocolError
Browse files Browse the repository at this point in the history
It is possible that a XenAPI call to xapi will result in an xmlrpc
ProtocolError being returned, typically a 500 Internal Error. As it is
not practical to wrap all the calls to xapi via the xenapi session in
a specific exception handler add one into the outer run method to
ensure that it is converted to an error code and not an unknown error.

Signed-off-by: Mark Syms <mark.syms@cloud.com>
  • Loading branch information
MarkSymsCtx committed Dec 10, 2024
1 parent ae57b0f commit d160bfb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
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(
'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

0 comments on commit d160bfb

Please sign in to comment.