def test_distinct(self): update({'instrument': {'sample_frequency': 1}}) with patch('mongodrums.instrument.push') as push_mock, \ FindWrapper.instrument(): names = self.db.foo.find().distinct('name') self.assertItemsEqual(names, ['alice', 'bob', 'zed', 'yohan']) self.assertEqual(push_mock.call_count, 2)
def test_session_setup(self): update({'collector': {'session': 'collector_test'}}) config = get_config() session_collection_name = SessionCollection.get_collection_name() col = SessionCollection(self.db[session_collection_name]) self._start_server() time.sleep(.2) self.assertEqual( len( col.find({ 'name': config.collector.session, 'start_time': { '$exists': True }, 'end_time': { '$exists': False } })), 1) self._stop_server() time.sleep(.2) self.assertEqual( len( col.find({ 'name': config.collector.session, 'start_time': { '$exists': True }, 'end_time': { '$exists': True } })), 1)
def test_get_source(self): update({'instrument': {'sample_frequency': 1}}) with patch('mongodrums.instrument.push') as push_mock, \ FindWrapper.instrument(): doc = self.db.foo.find_one({'name': 'bob'}) frame_info = inspect.getframeinfo(inspect.currentframe()) source = '%s:%d' % (frame_info[0], frame_info[1] - 1) self.assertEqual(push_mock.call_args[0][0]['source'], source)
def test_count(self): update({'instrument': {'sample_frequency': 1}}) with patch('mongodrums.instrument.push') as push_mock, \ FindWrapper.instrument(): num_records = self.db.foo.find().count() self.assertEqual(num_records, 4) # find + $cmd self.assertEqual(push_mock.call_count, 2)
def setUp(self): self.saved_config = get_config() for config in ['collector', 'index_profile_sink', 'query_profile_sink']: update({config: {'mongo_uri': 'mongodb://127.0.0.1/%s' % (BaseTest.TEST_DB)}}) self.client = pymongo.MongoClient() self.client.drop_database(self.__class__.TEST_DB) self.db = self.client[self.__class__.TEST_DB]
def test_or_query(self): update({'instrument': {'sample_frequency': 1}}) with patch('mongodrums.instrument.push') as push_mock, \ FindWrapper.instrument(): docs = [d for d in self.db.foo.find({'$or': [{'name': 'bob'}, {'name': 'alice'}]}, {'name': 1})] self.assertEqual(len(docs), 2) self.assertItemsEqual(docs, [{'_id': 1, 'name': 'bob'}, {'_id': 2, 'name': 'alice'}]) self.assertEqual(push_mock.call_count, 1)
def test_find_push(self): update({'instrument': {'sample_frequency': 1}}) with patch('mongodrums.instrument.push') as push_mock, \ FindWrapper.instrument(): doc = self.db.foo.find_one({'name': 'bob'}) self.assertEqual(doc, {'_id': 1, 'name': 'bob'}) self.assertEqual(push_mock.call_count, 1) self.assertIn('allPlans', push_mock.call_args[0][0]['explain']) self.assertNotIsInstance(pymongo.collection.Collection.find, FindWrapper) self.assertNotIsInstance(self.db.foo.find, FindWrapper)
def test_cursor_terminator_query(self): update({'instrument': {'sample_frequency': 1}}) with patch('mongodrums.instrument.push') as push_mock, \ FindWrapper.instrument(): q = {'name': 'bob'} curs = self.db.foo.find(q) self.assertIn('_mongodrums', curs.__dict__) curs.next() self.assertNotIn('_mongodrums', curs.__dict__) self.assertDictEqual( json.loads(push_mock.call_args[0][0]['query']), q)
def test_session_setup(self): update({'collector': {'session': 'collector_test'}}) config = get_config() session_collection_name = SessionCollection.get_collection_name() col = SessionCollection(self.db[session_collection_name]) self._start_server() time.sleep(.2) self.assertEqual(len(col.find({'name': config.collector.session, 'start_time': {'$exists': True}, 'end_time': {'$exists': False}})), 1) self._stop_server() time.sleep(.2) self.assertEqual(len(col.find({'name': config.collector.session, 'start_time': {'$exists': True}, 'end_time': {'$exists': True}})), 1)
def test_chained_call(self): update({'instrument': {'sample_frequency': 1}}) with patch('mongodrums.instrument.push') as push_mock, \ FindWrapper.instrument(): # chain call curs = self.db.foo.find({'name': 'bob'}).limit(1) self.assertEqual(push_mock.call_count, 0) self.assertTrue(hasattr(curs.next, 'func')) self.assertIsInstance(curs.next.func, _CursorNextWrapper) # use iter instead of direct call to next doc = [d for d in curs][0] self.assertEqual(push_mock.call_count, 1) self.assertEqual(doc, {'_id': 1, 'name': 'bob'}) self.assertIn('allPlans', push_mock.call_args[0][0]['explain']) self.assertNotIsInstance(pymongo.collection.Collection.find, FindWrapper) self.assertNotIsInstance(self.db.foo.find, FindWrapper)
def setUp(self): super(ProfileSinkTest, self).setUp() self.sink_db = self.client[self.__class__.SINK_TEST_DB] self._setup_test_collection() self._real_push = mongodrums.instrument.push self._msgs = [] mongodrums.instrument.push = self._push update({ 'instrument': {'sample_frequency': 1}, 'index_profile_sink': { 'mongo_uri': 'mongodb://127.0.0.1:27017/%s' % (self.__class__.SINK_TEST_DB) }, 'query_profile_sink': { 'mongo_uri': 'mongodb://127.0.0.1:27017/%s' % (self.__class__.SINK_TEST_DB) } }) self._index_profile_sink = IndexProfileSink() self._query_profile_sink = QueryProfileSink()
def run(self): logging.info('running collector...') update({'collector': { 'session': self.args.session, 'mongo_uri': self.args.uri, 'addr': self.args.addr, 'port': self.args.port }, 'index_profile_sink': { 'mongo_uri': self.args.uri }, 'query_profile_uri': { 'mongo_uri': self.args.uri }}) collector = CollectorRunner([IndexProfileSink(), QueryProfileSink()]) collector.start() while not should_exit: time.sleep(.1) collector.stop() collector.join() logging.info('collector stopped...')
def test_or_query(self): update({'instrument': {'sample_frequency': 1}}) with patch('mongodrums.instrument.push') as push_mock, \ FindWrapper.instrument(): docs = [ d for d in self.db.foo.find( {'$or': [{ 'name': 'bob' }, { 'name': 'alice' }]}, {'name': 1}) ] self.assertEqual(len(docs), 2) self.assertItemsEqual(docs, [{ '_id': 1, 'name': 'bob' }, { '_id': 2, 'name': 'alice' }]) self.assertEqual(push_mock.call_count, 1)
def test_config_update(self): with instrument(): self.assertEqual(pymongo.collection.Collection.find._frequency, self.saved_config.instrument.sample_frequency) update({'instrument': {'sample_frequency': 1}}) self.assertEqual(pymongo.collection.Collection.find._frequency, 1)