Unittest Fails - test_wsgi.py with 'Mock' object has no attribute '__getitem__'

Bug #1777701 reported by Bharath Vedartham
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
OpenStack Object Storage (swift)
Fix Released
Medium
Alistair Coles

Bug Description

Description :
When running unittests for openstack swift. I have encountered some errors. The errors come in the unit tests from the file test_wsgi.py.
The following functions were being tested : (i) test_multiple_requests_with_proxy
(ii) test_request_with_proxy
(iii)test_request_with_proxy_https
(iv)test_unknown_client_addr

Version of Swift : 2.18.1.dev83

Actual Error Message(its pretty long) :
ERROR: test_multiple_requests_with_proxy (test.unit.common.test_wsgi.TestProxyProtocol)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/bharath1234/swift/test/unit/common/test_wsgi.py", line 1174, in test_multiple_requests_with_proxy
    ), wsgi.SwiftHttpProxiedProtocol)
  File "/home/bharath1234/swift/test/unit/common/test_wsgi.py", line 1128, in _run_bytes_through_protocol
    log_output=False, # quiet the test run
  File "/usr/lib/python2.7/dist-packages/eventlet/wsgi.py", line 869, in server
    pool.spawn_n(serv.process_request, client_socket)
  File "/home/bharath1234/swift/test/unit/common/test_wsgi.py", line 1089, in spawn_n
    a_callable(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/eventlet/wsgi.py", line 719, in process_request
    proto.__init__(sock, address, self)
  File "/home/bharath1234/swift/swift/common/wsgi.py", line 452, in __init__
    SwiftHttpProtocol.__init__(self, *a, **kw)
  File "/usr/lib/python2.7/SocketServer.py", line 652, in __init__
    self.handle()
  File "/home/bharath1234/swift/swift/common/wsgi.py", line 510, in handle
    return SwiftHttpProtocol.handle(self)
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
    self.handle_one_request()
  File "/usr/lib/python2.7/dist-packages/eventlet/wsgi.py", line 379, in handle_one_request
    self.environ = self.get_environ()
  File "/home/bharath1234/swift/swift/common/wsgi.py", line 513, in get_environ
    environ = SwiftHttpProtocol.get_environ(self)
  File "/usr/lib/python2.7/dist-packages/eventlet/wsgi.py", line 590, in get_environ
    host, port = self.request.getsockname()[:2]
TypeError: 'Mock' object has no attribute '__getitem__'

======================================================================
ERROR: test_request_with_proxy (test.unit.common.test_wsgi.TestProxyProtocol)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/bharath1234/swift/test/unit/common/test_wsgi.py", line 1138, in test_request_with_proxy
    ), wsgi.SwiftHttpProxiedProtocol)
  File "/home/bharath1234/swift/test/unit/common/test_wsgi.py", line 1128, in _run_bytes_through_protocol
    log_output=False, # quiet the test run
  File "/usr/lib/python2.7/dist-packages/eventlet/wsgi.py", line 869, in server
    pool.spawn_n(serv.process_request, client_socket)
  File "/home/bharath1234/swift/test/unit/common/test_wsgi.py", line 1089, in spawn_n
    a_callable(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/eventlet/wsgi.py", line 719, in process_request
    proto.__init__(sock, address, self)
  File "/home/bharath1234/swift/swift/common/wsgi.py", line 452, in __init__
    SwiftHttpProtocol.__init__(self, *a, **kw)
  File "/usr/lib/python2.7/SocketServer.py", line 652, in __init__
    self.handle()
  File "/home/bharath1234/swift/swift/common/wsgi.py", line 510, in handle
    return SwiftHttpProtocol.handle(self)
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
    self.handle_one_request()
  File "/usr/lib/python2.7/dist-packages/eventlet/wsgi.py", line 379, in handle_one_request
    self.environ = self.get_environ()
  File "/home/bharath1234/swift/swift/common/wsgi.py", line 513, in get_environ
    environ = SwiftHttpProtocol.get_environ(self)
  File "/usr/lib/python2.7/dist-packages/eventlet/wsgi.py", line 590, in get_environ
    host, port = self.request.getsockname()[:2]
TypeError: 'Mock' object has no attribute '__getitem__'

======================================================================
ERROR: test_request_with_proxy_https (test.unit.common.test_wsgi.TestProxyProtocol)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/bharath1234/swift/test/unit/common/test_wsgi.py", line 1154, in test_request_with_proxy_https
    ), wsgi.SwiftHttpProxiedProtocol)
  File "/home/bharath1234/swift/test/unit/common/test_wsgi.py", line 1128, in _run_bytes_through_protocol
    log_output=False, # quiet the test run
  File "/usr/lib/python2.7/dist-packages/eventlet/wsgi.py", line 869, in server
    pool.spawn_n(serv.process_request, client_socket)
  File "/home/bharath1234/swift/test/unit/common/test_wsgi.py", line 1089, in spawn_n
    a_callable(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/eventlet/wsgi.py", line 719, in process_request
    proto.__init__(sock, address, self)
  File "/home/bharath1234/swift/swift/common/wsgi.py", line 452, in __init__
    SwiftHttpProtocol.__init__(self, *a, **kw)
  File "/usr/lib/python2.7/SocketServer.py", line 652, in __init__
    self.handle()
  File "/home/bharath1234/swift/swift/common/wsgi.py", line 510, in handle
    return SwiftHttpProtocol.handle(self)
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
    self.handle_one_request()
  File "/usr/lib/python2.7/dist-packages/eventlet/wsgi.py", line 379, in handle_one_request
    self.environ = self.get_environ()
  File "/home/bharath1234/swift/swift/common/wsgi.py", line 513, in get_environ
    environ = SwiftHttpProtocol.get_environ(self)
  File "/usr/lib/python2.7/dist-packages/eventlet/wsgi.py", line 590, in get_environ
    host, port = self.request.getsockname()[:2]
TypeError: 'Mock' object has no attribute '__getitem__'

======================================================================
ERROR: test_unknown_client_addr (test.unit.common.test_wsgi.TestProxyProtocol)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/bharath1234/swift/test/unit/common/test_wsgi.py", line 1220, in test_unknown_client_addr
    ), wsgi.SwiftHttpProxiedProtocol)
  File "/home/bharath1234/swift/test/unit/common/test_wsgi.py", line 1128, in _run_bytes_through_protocol
    log_output=False, # quiet the test run
  File "/usr/lib/python2.7/dist-packages/eventlet/wsgi.py", line 869, in server
    pool.spawn_n(serv.process_request, client_socket)
  File "/home/bharath1234/swift/test/unit/common/test_wsgi.py", line 1089, in spawn_n
    a_callable(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/eventlet/wsgi.py", line 719, in process_request
    proto.__init__(sock, address, self)
  File "/home/bharath1234/swift/swift/common/wsgi.py", line 452, in __init__
    SwiftHttpProtocol.__init__(self, *a, **kw)
  File "/usr/lib/python2.7/SocketServer.py", line 652, in __init__
    self.handle()
  File "/home/bharath1234/swift/swift/common/wsgi.py", line 510, in handle
    return SwiftHttpProtocol.handle(self)
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
    self.handle_one_request()
  File "/usr/lib/python2.7/dist-packages/eventlet/wsgi.py", line 379, in handle_one_request
    self.environ = self.get_environ()
  File "/home/bharath1234/swift/swift/common/wsgi.py", line 513, in get_environ
    environ = SwiftHttpProtocol.get_environ(self)
  File "/usr/lib/python2.7/dist-packages/eventlet/wsgi.py", line 590, in get_environ
    host, port = self.request.getsockname()[:2]
TypeError: 'Mock' object has no attribute '__getitem__'

Solution :
This issue was solved by updating the eventlet package to 0.20.1. User must check the version of eventlet using pip show eventlet and proceed to update eventlet to 0.20.1(first remove the original version of eventlet by pip uninstall eventlet) by pip install eventlet==0.20.1.

Revision history for this message
Alistair Coles (alistair-coles) wrote :

I can reproduce this.

The cause is that older versions of eventlet expect getsocketname() to return a tuple, but the test mock returns a string.

Changed in swift:
status: New → Confirmed
importance: Undecided → Medium
assignee: nobody → Alistair Coles (alistair-coles)
summary: - Unittest Fails - test_wsgi.py(Solved)
+ Unittest Fails - test_wsgi.py with 'Mock' object has no attribute
+ '__getitem__'
tags: added: flakey-test
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to swift (master)

Fix proposed to branch: master
Review: https://review.openstack.org/576800

Changed in swift:
status: Confirmed → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Related fix proposed to swift (master)

Related fix proposed to branch: master
Review: https://review.openstack.org/576801

Revision history for this message
Bharath Vedartham (bhar12345) wrote :

Updating the test mock is required. The test mock needs to be changed to return a tuple instead of a string.

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to swift (master)

Reviewed: https://review.openstack.org/576800
Committed: https://git.openstack.org/cgit/openstack/swift/commit/?id=642d8748c0f3401d702e86aab2fdc719a314e367
Submitter: Zuul
Branch: master

commit 642d8748c0f3401d702e86aab2fdc719a314e367
Author: Alistair Coles <email address hidden>
Date: Wed Jun 20 09:59:55 2018 +0100

    Fix test_wsgi.py tests failing with eventlet <=0.18.4

    Older versions of eventlet expect getsocketname() to return a tuple,
    and newer versions are happy to receive a tuple, so fix the mock to
    return a tuple.

    Change-Id: I68fd9d0263a6720aaf0d90b8ea6da1158105ac01
    Closes-Bug: #1777701

Changed in swift:
status: In Progress → Fix Released
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Related fix merged to swift (master)

Reviewed: https://review.openstack.org/576801
Committed: https://git.openstack.org/cgit/openstack/swift/commit/?id=a1cb91993096971934755cae08aa8013c10fc8f9
Submitter: Zuul
Branch: master

commit a1cb91993096971934755cae08aa8013c10fc8f9
Author: Alistair Coles <email address hidden>
Date: Wed Jun 20 10:09:38 2018 +0100

    Use valid eventlet logger method

    eventlet<=0.17.4 LoggerFileWrapper does not have an error() method
    so don't try to call it. Instead, use info() which even in newer
    eventlet versions does exactly the same as error().

    This bug only manifests in unit tests once the Related-Bug has been fixed
    (see Related-Change).

    Closes-Bug: #1777836
    Related-Bug: #1777701
    Related-Change: I68fd9d0263a6720aaf0d90b8ea6da1158105ac01

    Change-Id: I46190567549826c811ffa51e9a71a38a20d9ce97

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix included in openstack/swift 2.19.0

This issue was fixed in the openstack/swift 2.19.0 release.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.