def test_envs_with_rate_limit(self, TsuruLogWriter, gethostname): TsuruLogWriter.return_value = mock.Mock() gethostname.return_value = "myhost" stream = Stream(watcher_name="watcher", envs={ "LOG_RATE_LIMIT_WINDOW": "60", "LOG_RATE_LIMIT_COUNT": "1000", }) TsuruLogWriter.assert_called_with(mock.ANY, stream.queue, "60", "1000")
def test_rate_limit_invalid_config(self): session = mock.Mock() queue = Queue.Queue(maxsize=1000) writer = TsuruLogWriter(session, queue, "", {}) writer.start() for i in xrange(100): queue.put_nowait(LogEntry('url', 1, ['msg-1'])) queue.put_nowait(QUEUE_DONE_MESSAGE) writer.join() self.assertEqual(session.post.call_count, 100) session.post.assert_any_call('url', data='["msg-1"]', timeout=1)
def test_envs_with_rate_limit(self, TsuruLogWriter, gethostname): TsuruLogWriter.return_value = mock.Mock() gethostname.return_value = "myhost" stream = Stream(watcher_name="watcher", envs={ "LOG_RATE_LIMIT_WINDOW": "60", "LOG_RATE_LIMIT_COUNT": "1000", }) TsuruLogWriter.assert_called_with(mock.ANY, stream.queue, "60", "1000", 'host2', '514', 'LOCAL0', 'udp', 'appname1')
def setUp(self, TsuruLogWriter, gethostname): TsuruLogWriter.return_value = log_writer = mock.Mock() gethostname.return_value = "myhost" l_out = "2012-11-06 17:13:55 [12019] [INFO] Starting gunicorn 0.15.0\n" l_err = "2012-11-06 17:13:55 [12019] [ERROR] Error starting gunicorn\n" self.data = {} self.data["stderr"] = {"pid": 12018, "data": l_err, "name": "stderr"} self.data["stdout"] = {"pid": 12018, "data": l_out, "name": "stdout"} self.stream = Stream(watcher_name="mywatcher") TsuruLogWriter.assert_called_with(mock.ANY, self.stream.queue, None, None, 'host2', '514', 'LOCAL0', 'udp', 'appname1') log_writer.start.assert_called_once()
def setUp(self, TsuruLogWriter, gethostname): TsuruLogWriter.return_value = log_writer = mock.Mock() gethostname.return_value = "myhost" l_out = "2012-11-06 17:13:55 [12019] [INFO] Starting gunicorn 0.15.0\n" l_err = "2012-11-06 17:13:55 [12019] [ERROR] Error starting gunicorn\n" self.data = {} self.data["stderr"] = { "pid": 12018, "data": l_err, "name": "stderr" } self.data["stdout"] = { "pid": 12018, "data": l_out, "name": "stdout" } self.stream = Stream(watcher_name="mywatcher") TsuruLogWriter.assert_called_with(mock.ANY, self.stream.queue, None, None) log_writer.start.assert_called_once()
def test_rate_limit_stress(self): session = mock.Mock() queue = Queue.Queue(maxsize=1000) writer = TsuruLogWriter(session, queue, "2", "10") writer.start() t0 = time.time() i = 0 while time.time() - t0 < 10: i += 1 try: queue.put_nowait(LogEntry('url', 1, ['msg-{}'.format(i)])) except: pass while queue.full(): time.sleep(0.1) queue.put_nowait(QUEUE_DONE_MESSAGE) writer.join() self.assertTrue(45 < session.post.call_count <= 66) session.post.assert_any_call('url', data='["msg-1"]', timeout=1)
def test_send_log_to_syslog_as_error(self, s_handler, logger): s_handler.return_value = syslog = mock.Mock() session = mock.Mock() queue = Queue.Queue(maxsize=1000) writer = TsuruLogWriter(session, queue, "", {}, 'host2', '514', 'LOCAL0', 'udp', 'appname1') writer.start() for i in xrange(10): queue.put_nowait(LogEntry('url', 1, ['msg-1'], 'stderr')) queue.put_nowait(QUEUE_DONE_MESSAGE) writer.join() self.assertEqual(session.post.call_count, 10) session.post.assert_any_call('url', data='["msg-1"]', timeout=1) logger.assert_called_with('appname1') my_logger = logger.return_value s_handler.assert_called_with(address=('host2', 514), facility='LOCAL0', socktype=socket.SOCK_DGRAM) my_logger.addHandler.assert_called_with(syslog) my_logger.error.assert_called_with("msg-1")
def test_rate_limit(self): session = mock.Mock() queue = Queue.Queue(maxsize=1000) writer = TsuruLogWriter(session, queue, 2, 10) writer.start() for i in xrange(20): queue.put_nowait(LogEntry('url', 1, ['msg1'])) while not queue.empty(): time.sleep(0.01) self.assertEqual(session.post.call_count, 11) session.post.assert_any_call('url', data='["msg1"]', timeout=1) session.post.assert_any_call( 'url', data='["dropping messages, more than 10 messages in last 2 seconds"]', timeout=1 ) time.sleep(3) for i in xrange(20): queue.put_nowait(LogEntry('url', 1, ['msg2'])) queue.put_nowait(QUEUE_DONE_MESSAGE) writer.join() self.assertEqual(session.post.call_count, 22) session.post.assert_any_call('url', data='["msg2"]', timeout=1)
def test_rate_limit_invalid_config(self): session = mock.Mock() queue = Queue.Queue(maxsize=1000) writer = TsuruLogWriter(session, queue, "", {}, None, None, None, None, None) writer.start() for i in xrange(100): queue.put_nowait(LogEntry('url', 1, ['msg-1'], None)) queue.put_nowait(QUEUE_DONE_MESSAGE) writer.join() self.assertEqual(session.post.call_count, 100) session.post.assert_any_call('url', data='["msg-1"]', timeout=1)
def test_rate_limit_stress(self): session = mock.Mock() queue = Queue.Queue(maxsize=1000) writer = TsuruLogWriter(session, queue, "2", "10", None, None, None, None, None) writer.start() t0 = time.time() i = 0 while time.time() - t0 < 10: i += 1 try: queue.put_nowait(LogEntry('url', 1, ['msg-{}'.format(i)], None)) except: pass while queue.full(): time.sleep(0.1) queue.put_nowait(QUEUE_DONE_MESSAGE) writer.join() self.assertTrue(45 < session.post.call_count <= 66) session.post.assert_any_call('url', data='["msg-1"]', timeout=1)
def test_rate_limit(self): session = mock.Mock() queue = Queue.Queue(maxsize=1000) writer = TsuruLogWriter(session, queue, 2, 10, None, None, None, None, None) writer.start() for i in xrange(20): queue.put_nowait(LogEntry('url', 1, ['msg1'], None)) while not queue.empty(): time.sleep(0.01) self.assertEqual(session.post.call_count, 11) session.post.assert_any_call('url', data='["msg1"]', timeout=1) session.post.assert_any_call( 'url', data= '["dropping messages, more than 10 messages in last 2 seconds"]', timeout=1) time.sleep(3) for i in xrange(20): queue.put_nowait(LogEntry('url', 1, ['msg2'], None)) queue.put_nowait(QUEUE_DONE_MESSAGE) writer.join() self.assertEqual(session.post.call_count, 22) session.post.assert_any_call('url', data='["msg2"]', timeout=1)