]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Big Switch: Check source_address attribute exists
authorKevin Benton <blak111@gmail.com>
Thu, 8 May 2014 23:33:06 +0000 (16:33 -0700)
committerThomas Goirand <thomas@goirand.fr>
Mon, 9 Jun 2014 15:06:54 +0000 (23:06 +0800)
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

Conflicts:

neutron/tests/unit/bigswitch/test_servermanager.py

Change-Id: Ica10d23cc6de81ae56f711937f208c7321c77f36
(cherry picked from commit f5e4cf8e3edd1ce0ebb8c0c7256be07d094896de)

neutron/plugins/bigswitch/servermanager.py
neutron/tests/unit/bigswitch/test_servermanager.py

index 8792290af6cf8582184f805031a8fe634e6cfe63..2ab629797e4cb3fbd73925a7e95e620d72d363f2 100644 (file)
@@ -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()
index ddc863dd1724f455d046663f82e68d52058418f7..69f0b4a19f2e75437b9ca56adb9a881317a54d0d 100644 (file)
@@ -22,6 +22,7 @@ import mock
 from oslo.config import cfg
 
 from neutron.manager import NeutronManager
+from neutron.openstack.common import importutils
 from neutron.plugins.bigswitch import servermanager
 from neutron.tests.unit.bigswitch import test_restproxy_plugin as test_rp
 
@@ -147,3 +148,20 @@ class ServerManagerTests(test_rp.BigSwitchProxyPluginV2TestCase):
             conmock.return_value.request.side_effect = socket.timeout()
             resp = sp.servers[0].rest_call('GET', '/')
             self.assertEqual(resp, (0, None, None, None))
+
+
+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)