From 04ff22e3c9ed9992a08640076150d1ede403b165 Mon Sep 17 00:00:00 2001 From: Bernie Hackett Date: Fri, 2 May 2014 15:15:08 -0700 Subject: [PATCH] Various fixes for auth tests with old mongos versions. --- pymongo/database.py | 5 +++++ pymongo/mongo_client.py | 2 +- test/test_client.py | 6 ++++-- test/test_collection.py | 5 ++++- test/test_database.py | 9 ++++++--- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/pymongo/database.py b/pymongo/database.py index 9b3ab0502..a1fde823e 100644 --- a/pymongo/database.py +++ b/pymongo/database.py @@ -728,6 +728,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 c26103fdb..067a1df88 100644 --- a/pymongo/mongo_client.py +++ b/pymongo/mongo_client.py @@ -1053,7 +1053,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 b5a95a1db..4f05d002d 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -226,6 +226,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. @@ -262,8 +265,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 091afdb7a..25dfe76f1 100644 --- a/test/test_collection.py +++ b/test/test_collection.py @@ -609,7 +609,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 870b9cb51..265f07b45 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -110,7 +110,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): @@ -601,8 +604,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")