def test_commands(self, mock_logger_warn, mock_logger_info, sock_file): wm = WorkerMaster('sock://%s' % sock_file) wm.run() try: s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.connect(sock_file) s.send(bytes('PING\n', 'UTF-8')) self.assertEqual(s.recv(1024).decode('UTF-8').strip(), 'PONG') s.send(bytes('FOOBAR\n', 'UTF-8')) self.assertEqual( s.recv(1024).decode('UTF-8').strip(), 'UNKNOWN COMMAND') mock_logger_info.reset_mock() s.send(bytes('HIT grp 1.1.1.1\n', 'UTF-8')) self.assertEqual(s.recv(1024).decode('UTF-8').strip(), 'BULLSEYE') mock_logger_info.assert_called_once_with('HIT grp 1.1.1.1') s.send(b"\xFF\xFF\n") self.assertEqual(s.recv(1024).decode('UTF-8').strip(), 'WHAT?') mock_logger_warn.assert_called_once_with( 'WorkMasterThread recieved invalid data') s.send(bytes('QUIT\n', 'UTF-8')) self.assertEqual(s.recv(1024).decode('UTF-8').strip(), 'BYE') with self.assertRaises(BrokenPipeError): s.send(bytes('ANYTHING\n', 'UTF-8')) s.close() finally: wm.stop()
def test_match_line(self, mock_logger_error, sock_file): wm = WorkerMaster('sock://%s' % sock_file, filters=['grp1:aaa', 'grp1:bbb', 'grp2:ccc']) wm.run() try: wm.match_line('test line') mock_logger_error.assert_called_once_with('No workers connected - unable to process line "test line"') s1 = utils.socket_connect_from_text("sock://%s" % sock_file) f1 = s1.makefile('r') # ignore filter definitions for _ in range(3): f1.readline().strip() s2 = utils.socket_connect_from_text("sock://%s" % sock_file) f2 = s2.makefile('r') # ignore filter definitions for _ in range(3): f2.readline().strip() wm.match_line('test line2') wm.match_line('test line3') l1 = f1.readline().strip() l2 = f2.readline().strip() self.assertIn(l1, ('MATCH test line2', 'MATCH test line3', )) self.assertIn(l2, ('MATCH test line2', 'MATCH test line3', )) self.assertNotEqual(l1, l2) f1.close() s1.close() f2.close() s2.close() finally: wm.stop()
def test_commands(self, mock_logger_warn, mock_logger_info, sock_file): wm = WorkerMaster('sock://%s' % sock_file) wm.run() try: s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.connect(sock_file) s.send(bytes('PING\n', 'UTF-8')) self.assertEqual(s.recv(1024).decode('UTF-8').strip(), 'PONG') s.send(bytes('FOOBAR\n', 'UTF-8')) self.assertEqual(s.recv(1024).decode('UTF-8').strip(), 'UNKNOWN COMMAND') mock_logger_info.reset_mock() s.send(bytes('HIT grp 1.1.1.1\n', 'UTF-8')) self.assertEqual(s.recv(1024).decode('UTF-8').strip(), 'BULLSEYE') mock_logger_info.assert_called_once_with('HIT grp 1.1.1.1') s.send(b"\xFF\xFF\n") self.assertEqual(s.recv(1024).decode('UTF-8').strip(), 'WHAT?') mock_logger_warn.assert_called_once_with('WorkMasterThread recieved invalid data') s.send(bytes('QUIT\n', 'UTF-8')) self.assertEqual(s.recv(1024).decode('UTF-8').strip(), 'BYE') with self.assertRaises(BrokenPipeError): s.send(bytes('ANYTHING\n', 'UTF-8')) s.close() finally: wm.stop()
def test_listen_sock(self, sock_file): s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) wm = WorkerMaster('sock://%s' % sock_file) wm.run() s.connect(sock_file) s.close() wm.stop()
def test_listen_tcp6(self): s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) wm = WorkerMaster('tcp6://[::1]:4432') wm.run() s.connect(('::1', 4432)) s.close() wm.stop()
def test_listen_tcp(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) wm = WorkerMaster('tcp://0.0.0.0:4432') wm.run() s.connect(('127.0.0.1', 4432)) s.close() wm.stop()
def main(config): log.init(config.get('server', 'log_level')) wm = WorkerMaster(config.get('server', 'worker_listen'), filters=config.get('filters', 'regex').split('\n')) pm = ProviderMaster(config.get('server', 'provider_listen'), worker_master=wm) try: wm.run() pm.run() except KeyboardInterrupt: wm.stop() pm.stop()
def test_client_close_connection(self, sock_file): wm = WorkerMaster('sock://%s' % sock_file) wm.run() try: s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.connect(sock_file) s.send(bytes('PING\n', 'UTF-8')) self.assertEqual(s.recv(1024).decode('UTF-8').strip(), 'PONG') s.close() finally: wm.stop()
def test_filters(self, sock_file): wm = WorkerMaster('sock://%s' % sock_file, filters=['grp1:aaa', 'grp1:bbb', 'grp2:ccc']) wm.run() try: s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.connect(sock_file) f = s.makefile('r') self.assertEqual(sorted([f.readline().strip() for _ in range(3)]), ['FILTER ADD grp1 aaa', 'FILTER ADD grp1 bbb', 'FILTER ADD grp2 ccc']) f.close() s.close() finally: wm.stop()
def test_match_line(self, mock_logger_error, sock_file): wm = WorkerMaster('sock://%s' % sock_file, filters=['grp1:aaa', 'grp1:bbb', 'grp2:ccc']) wm.run() try: wm.match_line('test line') mock_logger_error.assert_called_once_with( 'No workers connected - unable to process line "test line"') s1 = utils.socket_connect_from_text("sock://%s" % sock_file) f1 = s1.makefile('r') # ignore filter definitions for _ in range(3): f1.readline().strip() s2 = utils.socket_connect_from_text("sock://%s" % sock_file) f2 = s2.makefile('r') # ignore filter definitions for _ in range(3): f2.readline().strip() wm.match_line('test line2') wm.match_line('test line3') l1 = f1.readline().strip() l2 = f2.readline().strip() self.assertIn(l1, ( 'MATCH test line2', 'MATCH test line3', )) self.assertIn(l2, ( 'MATCH test line2', 'MATCH test line3', )) self.assertNotEqual(l1, l2) f1.close() s1.close() f2.close() s2.close() finally: wm.stop()
def test_filters(self, sock_file): wm = WorkerMaster('sock://%s' % sock_file, filters=['grp1:aaa', 'grp1:bbb', 'grp2:ccc']) wm.run() try: s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.connect(sock_file) f = s.makefile('r') self.assertEqual(sorted([f.readline().strip() for _ in range(3)]), [ 'FILTER ADD grp1 aaa', 'FILTER ADD grp1 bbb', 'FILTER ADD grp2 ccc' ]) f.close() s.close() finally: wm.stop()