diff --git a/pymongo/database.py b/pymongo/database.py index 184bad7d6..c6eb449b5 100644 --- a/pymongo/database.py +++ b/pymongo/database.py @@ -704,6 +704,11 @@ class Database(common.BaseObject): # See SERVER-4225 for more information. if 'login' in str(exc): pass + # First admin user add fails gle from mongos 2.0.x + # and 2.2.x. + elif (exc.details and + 'getlasterror' in exc.details.get('note', '')): + pass else: raise diff --git a/pymongo/mongo_client.py b/pymongo/mongo_client.py index 7f9b84d6d..c300dde88 100644 --- a/pymongo/mongo_client.py +++ b/pymongo/mongo_client.py @@ -1067,7 +1067,7 @@ class MongoClient(common.BaseObject): # for some errors. if "errObjects" in result: for errobj in result["errObjects"]: - if errobj["err"] == error_msg: + if errobj.get("err") == error_msg: details = errobj break diff --git a/test/test_client.py b/test/test_client.py index ad7b9ace8..5db0e61e0 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -220,6 +220,9 @@ class TestClient(unittest.TestCase, TestRequestMixin): # from a master in a master-slave pair. if server_is_master_with_slave(c): raise SkipTest("SERVER-2329") + if (not version.at_least(c, (2, 6, 0)) and + is_mongos(c) and server_started_with_auth(c)): + raise SkipTest("Need mongos >= 2.6.0 to test with authentication") # We test copy twice; once starting in a request and once not. In # either case the copy should succeed (because it starts a request # internally) and should leave us in the same state as before the copy. @@ -256,8 +259,7 @@ class TestClient(unittest.TestCase, TestRequestMixin): self.assertEqual("bar", c.pymongo_test2.test.find_one()["foo"]) # See SERVER-6427 for mongos - if (version.at_least(c, (1, 3, 3, 1)) and - not is_mongos(c) and server_started_with_auth(c)): + if not is_mongos(c) and server_started_with_auth(c): c.drop_database("pymongo_test1") diff --git a/test/test_collection.py b/test/test_collection.py index 4d7f1024a..2e738fe19 100644 --- a/test/test_collection.py +++ b/test/test_collection.py @@ -594,7 +594,10 @@ class TestCollection(unittest.TestCase): db.drop_collection("test") if version.at_least(db.connection, (1, 9)): db.create_collection("test", capped=True, size=4096) - self.assertEqual(db.test.options(), {"capped": True, 'size': 4096}) + result = db.test.options() + # mongos 2.2.x adds an $auth field when auth is enabled. + result.pop('$auth', None) + self.assertEqual(result, {"capped": True, 'size': 4096}) else: db.create_collection("test", capped=True) self.assertEqual(db.test.options(), {"capped": True}) diff --git a/test/test_database.py b/test/test_database.py index 16522aaa8..f33095692 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -111,7 +111,10 @@ class TestDatabase(unittest.TestCase): db.drop_collection("test.foo") db.create_collection("test.foo") self.assertTrue(u("test.foo") in db.collection_names()) - self.assertEqual(db.test.foo.options(), {}) + result = db.test.foo.options() + # mongos 2.2.x adds an $auth field when auth is enabled. + result.pop('$auth', None) + self.assertEqual(result, {}) self.assertRaises(CollectionInvalid, db.create_collection, "test.foo") def test_collection_names(self): @@ -581,8 +584,8 @@ class TestDatabase(unittest.TestCase): def test_authenticate_multiple(self): client = get_client() if (is_mongos(client) and not - version.at_least(self.client, (2, 0, 0))): - raise SkipTest("Auth with sharding requires MongoDB >= 2.0.0") + version.at_least(self.client, (2, 2, 0))): + raise SkipTest("Need mongos >= 2.2.0") if not server_started_with_auth(client): raise SkipTest("Authentication is not enabled on server")