Skip to content

Commit 04ff22e

Browse files
committed
Various fixes for auth tests with old mongos versions.
1 parent 13cd9be commit 04ff22e

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

pymongo/database.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,11 @@ def _legacy_add_user(self, name, password, read_only, **kwargs):
728728
# See SERVER-4225 for more information.
729729
if 'login' in str(exc):
730730
pass
731+
# First admin user add fails gle from mongos 2.0.x
732+
# and 2.2.x.
733+
elif (exc.details and
734+
'getlasterror' in exc.details.get('note', '')):
735+
pass
731736
else:
732737
raise
733738

pymongo/mongo_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ def __check_response_to_last_error(self, response, is_command):
10531053
# for some errors.
10541054
if "errObjects" in result:
10551055
for errobj in result["errObjects"]:
1056-
if errobj["err"] == error_msg:
1056+
if errobj.get("err") == error_msg:
10571057
details = errobj
10581058
break
10591059

test/test_client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ def test_copy_db(self):
226226
# from a master in a master-slave pair.
227227
if server_is_master_with_slave(c):
228228
raise SkipTest("SERVER-2329")
229+
if (not version.at_least(c, (2, 6, 0)) and
230+
is_mongos(c) and server_started_with_auth(c)):
231+
raise SkipTest("Need mongos >= 2.6.0 to test with authentication")
229232
# We test copy twice; once starting in a request and once not. In
230233
# either case the copy should succeed (because it starts a request
231234
# internally) and should leave us in the same state as before the copy.
@@ -262,8 +265,7 @@ def test_copy_db(self):
262265
self.assertEqual("bar", c.pymongo_test2.test.find_one()["foo"])
263266

264267
# See SERVER-6427 for mongos
265-
if (version.at_least(c, (1, 3, 3, 1)) and
266-
not is_mongos(c) and server_started_with_auth(c)):
268+
if not is_mongos(c) and server_started_with_auth(c):
267269

268270
c.drop_database("pymongo_test1")
269271

test/test_collection.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,10 @@ def test_options(self):
609609
db.drop_collection("test")
610610
if version.at_least(db.connection, (1, 9)):
611611
db.create_collection("test", capped=True, size=4096)
612-
self.assertEqual(db.test.options(), {"capped": True, 'size': 4096})
612+
result = db.test.options()
613+
# mongos 2.2.x adds an $auth field when auth is enabled.
614+
result.pop('$auth', None)
615+
self.assertEqual(result, {"capped": True, 'size': 4096})
613616
else:
614617
db.create_collection("test", capped=True)
615618
self.assertEqual(db.test.options(), {"capped": True})

test/test_database.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ def test_create_collection(self):
110110
db.drop_collection("test.foo")
111111
db.create_collection("test.foo")
112112
self.assertTrue(u"test.foo" in db.collection_names())
113-
self.assertEqual(db.test.foo.options(), {})
113+
result = db.test.foo.options()
114+
# mongos 2.2.x adds an $auth field when auth is enabled.
115+
result.pop('$auth', None)
116+
self.assertEqual(result, {})
114117
self.assertRaises(CollectionInvalid, db.create_collection, "test.foo")
115118

116119
def test_collection_names(self):
@@ -601,8 +604,8 @@ def test_authenticate_and_request(self):
601604
def test_authenticate_multiple(self):
602605
client = get_client()
603606
if (is_mongos(client) and not
604-
version.at_least(self.client, (2, 0, 0))):
605-
raise SkipTest("Auth with sharding requires MongoDB >= 2.0.0")
607+
version.at_least(self.client, (2, 2, 0))):
608+
raise SkipTest("Need mongos >= 2.2.0")
606609
if not server_started_with_auth(client):
607610
raise SkipTest("Authentication is not enabled on server")
608611

0 commit comments

Comments
 (0)