def test_cmd_line_sys_info(self): sys.argv = ["testcrash", "--hosts", node.http_url, "--sysinfo"] with patch('sys.stdout', new_callable=StringIO): try: main() except SystemExit as e: self.assertEqual(e.code, 0)
def test_cmd_precedence(self): """Test precedence of SQL passed in via -c vs. stdin SQL passed in via --command should take precedence over stdin """ try: stmt = u"select 'via-command' from information_schema.tables limit 1" orig_argv = sys.argv[:] tmphistory = tempfile.mkstemp()[1] sys.argv = [ 'testcrash', "--command", stmt, '--hosts', node.http_url, '--history', tmphistory ] with patch('sys.stdout', new_callable=StringIO) as output: try: main() except SystemExit as e: exception_code = e.code self.assertEqual(exception_code, 0) output = output.getvalue() self.assertTrue('via-command' in output) self.assertFalse('via-stdin' in output) finally: try: os.remove(tmphistory) except IOError: pass sys.argv = orig_argv sys.stdin.close()
def test_error_exit_code(self): """Test returns an error exit code""" stmt = u"select * from invalid sql statement" sys.argv = ["testcrash", "--command", stmt, '--hosts', node.http_url] try: main() except SystemExit as e: self.assertEqual(e.code, 1)
def _output_format(self, format, func, query="select name from sys.cluster"): orig_argv = sys.argv[:] try: sys.argv = [ "testcrash", "-c", query, "--hosts", node.http_url, '--format', format ] with patch('sys.stdout', new_callable=StringIO) as output: with patch('sys.stderr', new_callable=StringIO) as err: try: main() except SystemExit as e: func(self, e, output, err) finally: sys.argv = orig_argv
def test_multiple_hosts(self): orig_argv = sys.argv[:] try: tmphistory = tempfile.mkstemp()[1] sys.argv = [ "testcrash", "-c", "select * from sys.cluster", "--hosts", node.http_url, "127.0.0.1:1", '--history', tmphistory, '--format', 'tabular', '-v', ] with patch('sys.stdout', new_callable=StringIO) as output: try: main() except SystemExit as e: exception_code = e.code self.assertEqual(exception_code, 0) output = output.getvalue() lines = output.split('\n') self.assertRegex( lines[3], r'^\| http://[\d\.:]+ .*\| NULL .*\| FALSE .*\| Server not available' ) self.assertRegex( lines[4], r'^\| http://[\d\.:]+. *\| crate .*\| TRUE .*\| OK') finally: try: os.remove(tmphistory) except IOError: pass sys.argv = orig_argv
def test_stdin_cmd(self): "Test passing in SQL via stdin" try: orig_argv = sys.argv[:] tmphistory = tempfile.mkstemp()[1] sys.argv = [ 'testcrash', '--hosts', node.http_url, '--history', tmphistory ] with patch('sys.stdout', new_callable=StringIO) as output: try: main() except SystemExit as e: exception_code = e.code self.assertEqual(exception_code, 0) output = output.getvalue() self.assertTrue('via-stdin' in output) finally: try: os.remove(tmphistory) except IOError: pass sys.argv = orig_argv sys.stdin.close()