def test_command_max_time_ms(self): if not version.at_least(self.client, (2, 5, 3, -1)): raise SkipTest("MaxTimeMS requires MongoDB >= 2.5.3") if "enableTestCommands=1" not in get_command_line(self.client)["argv"]: raise SkipTest("Test commands must be enabled.") self.client.admin.command("configureFailPoint", "maxTimeAlwaysTimeOut", mode="alwaysOn") try: db = self.client.pymongo_test db.command('count', 'test') self.assertRaises(ExecutionTimeout, db.command, 'count', 'test', maxTimeMS=1) pipeline = [{'$project': {'name': 1, 'count': 1}}] # Database command helper. db.command('aggregate', 'test', pipeline=pipeline) self.assertRaises(ExecutionTimeout, db.command, 'aggregate', 'test', pipeline=pipeline, maxTimeMS=1) # Collection helper. db.test.aggregate(pipeline=pipeline) self.assertRaises(ExecutionTimeout, db.test.aggregate, pipeline, maxTimeMS=1) finally: self.client.admin.command("configureFailPoint", "maxTimeAlwaysTimeOut", mode="off")
def setUp(self): super(TestBulkWriteConcern, self).setUp() client = get_client() ismaster = client.test.command('ismaster') self.is_repl = bool(ismaster.get('setName')) self.w = len(ismaster.get("hosts", [])) self.secondary = None if self.w > 1: for member in ismaster['hosts']: if member != ismaster['primary']: host, port = _partition_node(member) self.secondary = MongoClient(host, port) break self.client = client self.coll = client.pymongo_test.test self.coll.remove() # We tested wtimeout errors by specifying a write concern greater than # the number of members, but in MongoDB 2.7.8+ this causes a different # sort of error, "Not enough data-bearing nodes". In recent servers we # use a failpoint to pause replication on a secondary. self.need_replication_stopped = version.at_least(self.client, (2, 7, 8)) self.test_commands_enabled = ("enableTestCommands=1" in get_command_line(self.client)["argv"])
def test_max_time_ms_getmore(self): # Test that Cursor handles server timeout error in response to getmore. if "enableTestCommands=1" not in get_command_line(self.client)["argv"]: raise SkipTest("Need test commands enabled") if not version.at_least(self.db.connection, (2, 5, 3, -1)): raise SkipTest("MaxTimeMS requires MongoDB >= 2.5.3") coll = self.db.pymongo_test coll.insert({} for _ in range(200)) cursor = coll.find().max_time_ms(100) # Send initial query before turning on failpoint. cursor.next() self.client.admin.command("configureFailPoint", "maxTimeAlwaysTimeOut", mode="alwaysOn") try: try: # Iterate up to first getmore. list(cursor) except ExecutionTimeout: pass else: self.fail("ExecutionTimeout not raised") finally: self.client.admin.command("configureFailPoint", "maxTimeAlwaysTimeOut", mode="off")
def setUp(self): client = auth_context.client if not version.at_least(client, (2, 7, 2)): raise SkipTest("SCRAM-SHA-1 requires MongoDB >= 2.7.2") ismaster = client.admin.command('ismaster') self.is_mongos = ismaster.get('msg') == 'isdbgrid' self.set_name = ismaster.get('setName') # SCRAM-SHA-1 is always enabled beginning in 2.7.8. if not version.at_least(client, (2, 7, 8)): cmd_line = get_command_line(client) if 'SCRAM-SHA-1' not in cmd_line.get('parsed', {}).get( 'setParameter', {}).get('authenticationMechanisms', ''): raise SkipTest('SCRAM-SHA-1 mechanism not enabled') if self.set_name: client.pymongo_test.add_user( 'user', 'pass', roles=['userAdmin', 'readWrite'], writeConcern={'w': len(ismaster['hosts'])}) else: client.pymongo_test.add_user('user', 'pass', roles=['userAdmin', 'readWrite'])
def test_max_time_ms(self): if not version.at_least(self.db.connection, (2, 5, 3, -1)): raise SkipTest("MaxTimeMS requires MongoDB >= 2.5.3") db = self.db db.pymongo_test.drop() coll = db.pymongo_test self.assertRaises(TypeError, coll.find().max_time_ms, 'foo') coll.insert({"amalia": 1}) coll.insert({"amalia": 2}) coll.find().max_time_ms(None) coll.find().max_time_ms(1L) cursor = coll.find().max_time_ms(999) self.assertEqual(999, cursor._Cursor__max_time_ms) cursor = coll.find().max_time_ms(10).max_time_ms(1000) self.assertEqual(1000, cursor._Cursor__max_time_ms) cursor = coll.find().max_time_ms(999) c2 = cursor.clone() self.assertEqual(999, c2._Cursor__max_time_ms) self.assertTrue("$maxTimeMS" in cursor._Cursor__query_spec()) self.assertTrue("$maxTimeMS" in c2._Cursor__query_spec()) self.assertTrue(coll.find_one(max_time_ms=1000)) if "enableTestCommands=1" in get_command_line(self.client)["argv"]: # Cursor parses server timeout error in response to initial query. self.client.admin.command("configureFailPoint", "maxTimeAlwaysTimeOut", mode="alwaysOn") try: cursor = coll.find().max_time_ms(1) try: cursor.next() except ExecutionTimeout: pass else: self.fail("ExecutionTimeout not raised") self.assertRaises(ExecutionTimeout, coll.find_one, max_time_ms=1) finally: self.client.admin.command("configureFailPoint", "maxTimeAlwaysTimeOut", mode="off")
def test_mongodb_x509_auth(self): # Expects the server to be running with the the server.pem, ca.pem # and crl.pem provided in mongodb and the server tests as well as # --auth # # --sslPEMKeyFile=jstests/libs/server.pem # --sslCAFile=jstests/libs/ca.pem # --sslCRLFile=jstests/libs/crl.pem # --auth if not MONGODB_X509_USERNAME: raise SkipTest("MONGODB_X509_USERNAME " "must be set to test MONGODB-X509") if not CERT_SSL: raise SkipTest("No mongod available over SSL with certs") client = MongoClient(host, port, ssl=True, ssl_certfile=CLIENT_PEM) if not version.at_least(client, (2, 5, 3, -1)): raise SkipTest("MONGODB-X509 tests require MongoDB 2.5.3 or newer") argv = get_command_line(client) if '--auth' not in argv: raise SkipTest("Mongo must be started with " "--auth to test MONGODB-X509") # Give admin all necessary priviledges. client['$external'].add_user(MONGODB_X509_USERNAME, roles=[{ 'role': 'readWriteAnyDatabase', 'db': 'admin' }, { 'role': 'userAdminAnyDatabase', 'db': 'admin' }]) client = MongoClient(host, port, ssl=True, ssl_certfile=CLIENT_PEM) coll = client.pymongo_test.test self.assertRaises(OperationFailure, coll.count) self.assertTrue( client.admin.authenticate(MONGODB_X509_USERNAME, mechanism='MONGODB-X509')) self.assertTrue(coll.remove()) uri = ('mongodb://%s@%s:%d/?authMechanism=' 'MONGODB-X509' % (quote_plus(MONGODB_X509_USERNAME), host, port)) # SSL options aren't supported in the URI... self.assertTrue(MongoClient(uri, ssl=True, ssl_certfile=CLIENT_PEM)) # Cleanup client['$external'].command('dropUsersFromDatabase') client['$external'].logout()
def test_mongodb_x509_auth(self): # Expects the server to be running with the the server.pem, ca.pem # and crl.pem provided in mongodb and the server tests as well as # --auth # # --sslPEMKeyFile=jstests/libs/server.pem # --sslCAFile=jstests/libs/ca.pem # --sslCRLFile=jstests/libs/crl.pem # --auth if not MONGODB_X509_USERNAME: raise SkipTest("MONGODB_X509_USERNAME " "must be set to test MONGODB-X509") if not CERT_SSL: raise SkipTest("No mongod available over SSL with certs") client = MongoClient(host, port, ssl=True, ssl_certfile=CLIENT_PEM) if not version.at_least(client, (2, 5, 3, -1)): raise SkipTest("MONGODB-X509 tests require MongoDB 2.5.3 or newer") argv = get_command_line(client) if '--auth' not in argv: raise SkipTest("Mongo must be started with " "--auth to test MONGODB-X509") # Give admin all necessary priviledges. client['$external'].add_user(MONGODB_X509_USERNAME, roles=[ {'name': 'readWriteAnyDatabase', 'db': 'admin', 'hasRole': True, 'canDelegate': False}, {'name': 'userAdminAnyDatabase', 'db': 'admin', 'hasRole': True, 'canDelegate': False}]) client = MongoClient(host, port, ssl=True, ssl_certfile=CLIENT_PEM) coll = client.pymongo_test.test self.assertRaises(OperationFailure, coll.count) self.assertTrue(client.admin.authenticate(MONGODB_X509_USERNAME, mechanism='MONGODB-X509')) self.assertTrue(coll.remove()) uri = ('mongodb://%s@%s:%d/?authMechanism=' 'MONGODB-X509' % (quote_plus(MONGODB_X509_USERNAME), host, port)) # SSL options aren't supported in the URI... self.assertTrue(MongoClient(uri, ssl=True, ssl_certfile=CLIENT_PEM)) # Cleanup client['$external'].command('dropUsersFromDatabase') client['$external'].logout()
def setUp(self): client = auth_context.client if not version.at_least(client, (2, 7, 2)): raise SkipTest("SCRAM-SHA-1 requires MongoDB >= 2.7.2") ismaster = client.admin.command('ismaster') self.is_mongos = ismaster.get('msg') == 'isdbgrid' self.set_name = ismaster.get('setName') # SCRAM-SHA-1 is always enabled beginning in 2.7.8. if not version.at_least(client, (2, 7, 8)): cmd_line = get_command_line(client) if 'SCRAM-SHA-1' not in cmd_line.get( 'parsed', {}).get('setParameter', {}).get('authenticationMechanisms', ''): raise SkipTest('SCRAM-SHA-1 mechanism not enabled') if self.set_name: client.pymongo_test.add_user('user', 'pass', roles=['userAdmin', 'readWrite'], writeConcern={'w': len(ismaster['hosts'])}) else: client.pymongo_test.add_user( 'user', 'pass', roles=['userAdmin', 'readWrite'])
def test_max_time_ms(self): if not version.at_least(self.db.connection, (2, 5, 3, -1)): raise SkipTest("MaxTimeMS requires MongoDB >= 2.5.3") max_time_ms_response = { '$err': 'operation exceeded time limit', 'code': 50 } bson_response = BSON.encode(max_time_ms_response) response_flags = pack("<i", 2) cursor_id = pack("<q", 0) starting_from = pack("<i", 0) number_returned = pack("<i", 1) op_reply = (response_flags + cursor_id + starting_from + number_returned + bson_response) self.assertRaises(ExecutionTimeout, _unpack_response, op_reply) command_response = { 'ok': 0, 'errmsg': 'operation exceeded time limit', 'code': 50 } self.assertRaises(ExecutionTimeout, _check_command_response, command_response, None) db = self.db db.pymongo_test.drop() coll = db.pymongo_test self.assertRaises(TypeError, coll.find().max_time_ms, 'foo') coll.insert({"amalia": 1}) coll.insert({"amalia": 2}) coll.find().max_time_ms(None) coll.find().max_time_ms(1L) cursor = coll.find().max_time_ms(999) self.assertEqual(999, cursor._Cursor__max_time_ms) cursor = coll.find().max_time_ms(10).max_time_ms(1000) self.assertEqual(1000, cursor._Cursor__max_time_ms) cursor = coll.find().max_time_ms(999) c2 = cursor.clone() self.assertEqual(999, c2._Cursor__max_time_ms) self.assertTrue("$maxTimeMS" in cursor._Cursor__query_spec()) self.assertTrue("$maxTimeMS" in c2._Cursor__query_spec()) self.assertTrue(coll.find_one(max_time_ms=1000)) reducer = Code("""function(obj, prev){prev.count++;}""") coll.group(key={"amalia": 1}, condition={}, initial={"count": 0}, reduce=reducer, maxTimeMS=1000) if "enableTestCommands=1" in get_command_line(self.client)["argv"]: self.client.admin.command("configureFailPoint", "maxTimeAlwaysTimeOut", mode="alwaysOn") self.assertRaises(ExecutionTimeout, coll.find_one, max_time_ms=1) self.client.admin.command("configureFailPoint", "maxTimeAlwaysTimeOut", mode="off")
def maybe_skip(self): if not (yield version.at_least(self.cx, (2, 5, 3, -1))): raise SkipTest("maxTimeMS requires MongoDB >= 2.5.3") if "enableTestCommands=1" not in (yield get_command_line(self.cx)): raise SkipTest("testing maxTimeMS requires failpoints")