Skip to content

Commit

Permalink
Merge branch '2.12-dev' into 2.13-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ipanova committed May 31, 2017
2 parents b9fa19d + 84fcc19 commit 3586536
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
9 changes: 8 additions & 1 deletion server/pulp/server/db/connection.py
Expand Up @@ -8,6 +8,13 @@
from gettext import gettext as _

import mongoengine

# in mongoengine 0.11 ConnectionError was renamed to MongoEngineConnectionError
try:
from mongoengine.connection import MongoEngineConnectionError
except ImportError:
from mongoengine.connection import ConnectionError as MongoEngineConnectionError

from pymongo.collection import Collection
from pymongo.errors import AutoReconnect, OperationFailure
from pymongo.son_manipulator import NamespaceInjector
Expand Down Expand Up @@ -189,7 +196,7 @@ def _connect_to_one_of_seeds(connection_kwargs, seeds_list, db_name):
_logger.debug(_('Connection Arguments: %s') % shadow_connection_kwargs)
connection = mongoengine.connect(db_name, **connection_kwargs)
return connection
except mongoengine.connection.ConnectionError as e:
except MongoEngineConnectionError as e:
msg = _("Could not connect to MongoDB at %(url)s:\n%(e)s\n")
_logger.info(msg % {'url': seed, 'e': str(e)})

Expand Down
18 changes: 9 additions & 9 deletions server/test/unit/server/db/test_connection.py
Expand Up @@ -15,7 +15,7 @@
MONGO_MIN_TEST_VERSION = "2.4.0"


class MongoEngineConnectionError(Exception):
class TestMongoEngineConnectionError(Exception):
pass


Expand Down Expand Up @@ -714,13 +714,13 @@ def test_initialize_password_no_username(self, mock_mongoengine):
MONGO_MIN_TEST_VERSION}
self.assertRaises(Exception, connection.initialize)

@patch('pulp.server.db.connection.OperationFailure', new=MongoEngineConnectionError)
@patch('pulp.server.db.connection.OperationFailure', new=TestMongoEngineConnectionError)
@patch('pulp.server.db.connection.mongoengine')
def test_authentication_fails_with_RuntimeError(self, mock_mongoengine):
mock_mongoengine_instance = mock_mongoengine.connect.return_value
mock_mongoengine_instance.server_info.return_value = {"version":
MONGO_MIN_TEST_VERSION}
exc = MongoEngineConnectionError()
exc = TestMongoEngineConnectionError()
exc.code = 18
mock_mongoengine.connection.get_db.side_effect = exc
self.assertRaises(RuntimeError, connection.initialize)
Expand All @@ -730,45 +730,45 @@ class TestDatabaseRetryOnInitialConnectionSupport(unittest.TestCase):

@patch('pulp.server.db.connection._CONNECTION', None)
@patch('pulp.server.db.connection._DATABASE', None)
@patch('pulp.server.db.connection.MongoEngineConnectionError', TestMongoEngineConnectionError)
@patch('pulp.server.db.connection.mongoengine')
@patch('pulp.server.db.connection.time.sleep', Mock())
def test_retry_waits_when_mongoengine_connection_error_is_raised(self, mock_mongoengine):
def break_out_on_second(*args, **kwargs):
mock_mongoengine.connect.side_effect = StopIteration()
raise MongoEngineConnectionError()
raise TestMongoEngineConnectionError()

mock_mongoengine.connect.side_effect = break_out_on_second
mock_mongoengine.connection.ConnectionError = MongoEngineConnectionError

self.assertRaises(StopIteration, connection.initialize)

@patch('pulp.server.db.connection._CONNECTION', None)
@patch('pulp.server.db.connection._DATABASE', None)
@patch('pulp.server.db.connection.MongoEngineConnectionError', TestMongoEngineConnectionError)
@patch('pulp.server.db.connection.time.sleep')
@patch('pulp.server.db.connection.mongoengine')
def test_retry_sleeps_with_backoff(self, mock_mongoengine, mock_sleep):
def break_out_on_second(*args, **kwargs):
mock_sleep.side_effect = StopIteration()

mock_sleep.side_effect = break_out_on_second
mock_mongoengine.connect.side_effect = MongoEngineConnectionError()
mock_mongoengine.connection.ConnectionError = MongoEngineConnectionError
mock_mongoengine.connect.side_effect = TestMongoEngineConnectionError()

self.assertRaises(StopIteration, connection.initialize)

mock_sleep.assert_has_calls([call(1), call(2)])

@patch('pulp.server.db.connection._CONNECTION', None)
@patch('pulp.server.db.connection._DATABASE', None)
@patch('pulp.server.db.connection.MongoEngineConnectionError', TestMongoEngineConnectionError)
@patch('pulp.server.db.connection.time.sleep')
@patch('pulp.server.db.connection.mongoengine')
def test_retry_with_max_timeout(self, mock_mongoengine, mock_sleep):
def break_out_on_second(*args, **kwargs):
mock_sleep.side_effect = StopIteration()

mock_sleep.side_effect = break_out_on_second
mock_mongoengine.connect.side_effect = MongoEngineConnectionError()
mock_mongoengine.connection.ConnectionError = MongoEngineConnectionError
mock_mongoengine.connect.side_effect = TestMongoEngineConnectionError()

self.assertRaises(StopIteration, connection.initialize, max_timeout=1)

Expand Down

0 comments on commit 3586536

Please sign in to comment.