From f5e4cf8e3edd1ce0ebb8c0c7256be07d094896de Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Thu, 8 May 2014 16:33:06 -0700 Subject: [PATCH] Big Switch: Check source_address attribute exists Check that the source_address attribute exists on HTTPSConnection objects before referencing it since it's not present on python 2.6 deployments. If it does not exist, create a socket connection without specifying the source. Closes-Bug: #1316382 Change-Id: Ica10d23cc6de81ae56f711937f208c7321c77f36 --- neutron/plugins/bigswitch/servermanager.py | 9 +++++++-- .../tests/unit/bigswitch/test_servermanager.py | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/neutron/plugins/bigswitch/servermanager.py b/neutron/plugins/bigswitch/servermanager.py index 8792290af..2ab629797 100644 --- a/neutron/plugins/bigswitch/servermanager.py +++ b/neutron/plugins/bigswitch/servermanager.py @@ -566,8 +566,13 @@ class HTTPSConnectionWithValidation(httplib.HTTPSConnection): combined_cert = None def connect(self): - sock = socket.create_connection((self.host, self.port), - self.timeout, self.source_address) + try: + sock = socket.create_connection((self.host, self.port), + self.timeout, self.source_address) + except AttributeError: + # python 2.6 doesn't have the source_address attribute + sock = socket.create_connection((self.host, self.port), + self.timeout) if self._tunnel_host: self.sock = sock self._tunnel() diff --git a/neutron/tests/unit/bigswitch/test_servermanager.py b/neutron/tests/unit/bigswitch/test_servermanager.py index 47db82ec6..914a88744 100644 --- a/neutron/tests/unit/bigswitch/test_servermanager.py +++ b/neutron/tests/unit/bigswitch/test_servermanager.py @@ -402,3 +402,20 @@ class ServerManagerTests(test_rp.BigSwitchProxyPluginV2TestCase): self.assertEqual(con._tunnel_host, 'myproxy.local') self.assertEqual(con._tunnel_port, 3128) self.assertEqual(con.sock, self.wrap_mock()) + + +class TestSockets(test_rp.BigSwitchProxyPluginV2TestCase): + + def setUp(self): + super(TestSockets, self).setUp() + # http patch must not be running or it will mangle the servermanager + # import where the https connection classes are defined + self.httpPatch.stop() + self.sm = importutils.import_module(SERVERMANAGER) + + def test_socket_create_attempt(self): + # exercise the socket creation to make sure it works on both python + # versions + con = self.sm.HTTPSConnectionWithValidation('127.0.0.1', 0, timeout=1) + # if httpcon was created, a connect attempt should raise a socket error + self.assertRaises(socket.error, con.connect) -- 2.45.2