diff --git a/releasenotes/notes/add-custom-connector-support-0a49c6649d5f7eaf.yaml b/releasenotes/notes/add-custom-connector-support-0a49c6649d5f7eaf.yaml new file mode 100644 index 00000000..881de7c7 --- /dev/null +++ b/releasenotes/notes/add-custom-connector-support-0a49c6649d5f7eaf.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Adds the ability to specify user-defined connector object on creation + of a root Sushy instance. diff --git a/sushy/main.py b/sushy/main.py index bff147cb..ffe29bee 100644 --- a/sushy/main.py +++ b/sushy/main.py @@ -15,7 +15,7 @@ import logging from sushy import auth as sushy_auth -from sushy import connector +from sushy import connector as sushy_connector from sushy.resources import base from sushy.resources.manager import manager from sushy.resources.sessionservice import session @@ -48,7 +48,7 @@ class Sushy(base.ResourceBase): def __init__(self, base_url, username=None, password=None, root_prefix='/redfish/v1/', verify=True, - auth=None): + auth=None, connector=None): """A class representing a RootService :param base_url: The base URL to the Redfish controller. It @@ -66,6 +66,7 @@ class Sushy(base.ResourceBase): a path the driver will use the specified certificate or one of the certificates in the directory. Defaults to True. :param auth: An authentication mechanism to utilize. + :param connector: A user-defined connector object. Defaults to None. """ self._root_prefix = root_prefix if (auth is not None and (password is not None or @@ -78,7 +79,7 @@ class Sushy(base.ResourceBase): password=password) super(Sushy, self).__init__( - connector.Connector(base_url, verify), + connector or sushy_connector.Connector(base_url, verify), path=self._root_prefix) self._auth = auth self._auth.set_context(self, self._conn) diff --git a/sushy/tests/unit/test_main.py b/sushy/tests/unit/test_main.py index 95a65c1e..293e9307 100644 --- a/sushy/tests/unit/test_main.py +++ b/sushy/tests/unit/test_main.py @@ -64,6 +64,18 @@ class MainTestCase(base.TestCase): ValueError, main.Sushy, 'http://foo.bar:1234', 'foo', 'bar', auth=mock.MagicMock()) + @mock.patch.object(connector, 'Connector', autospec=True) + def test_custom_connector(self, mock_Sushy_Connector): + connector_mock = mock.MagicMock() + with open('sushy/tests/unit/json_samples/root.json', 'r') as f: + connector_mock.get.return_value.json.return_value = ( + json.loads(f.read())) + main.Sushy('http://foo.bar:1234', 'foo', 'bar', + connector=connector_mock) + self.assertTrue(connector_mock.post.called) + self.assertTrue(connector_mock.get.called) + self.assertFalse(mock_Sushy_Connector.called) + @mock.patch.object(system, 'SystemCollection', autospec=True) def test_get_system_collection(self, mock_system_collection): self.root.get_system_collection()