def parse_options(): """Parse and validate options. Returns a dict with all the options.""" usage = "%prog <arguments>" description = ('Run MSSQL tests from sql files') parser = OptionParser(usage=usage, description=description) parser.add_option('--server', action='store', help='MSSQL/Azure server') parser.add_option('--port', action='store', help='MSSQL/Azure TCP port') parser.add_option('--database', action='store', help='Database name') parser.add_option('--schema', action='store', help='Schema to use (and create if needed)') parser.add_option('--username', action='store', help='Username to connect') parser.add_option('--password', action='store', help='Passord to connect') parser.add_option('--azure', action='store_true', default=False, help='If present, will connect as Azure, otherwise as ' 'standard MSSQL') (options, args) = parser.parse_args() # Check for test parameters if (options.server is None or options.port is None or options.database is None or options.schema is None or options.username is None or options.password is None): print_error("Insufficient parameters, check help (-h)") exit(4) else: if options.azure is True: options.username = '******' % (options.username, options.server.split('.')[0]) return (options)
def main(): try: args = parse_options() except Exception as e: print_usage_error(path.basename(__file__), e) exit(2) try: conn = connect(host=args.postgres_server, user=args.postgres_username, password=args.postgres_password, database=args.postgres_database, port=args.postgres_port) replaces = {'@PSCHEMANAME': args.postgres_schema, '@PUSER': args.postgres_username, '@MSERVER': args.mssql_server, '@MPORT': args.mssql_port, '@MUSER': args.mssql_username, '@MPASSWORD': args.mssql_password, '@MDATABASE': args.mssql_database, '@MSCHEMANAME': args.mssql_schema} tests = run_tests('tests/postgresql/*.sql', conn, replaces) print_report(tests['total'], tests['ok'], tests['errors']) if tests['errors'] != 0: exit(5) else: exit(0) except Exception as e: print_error(e) exit(3)
def main(): try: args = parse_options() except Exception as e: print_usage_error(path.basename(__file__), e) exit(2) try: conn = connect(host=args.postgres_server, user=args.postgres_username, password=args.postgres_password, database=args.postgres_database, port=args.postgres_port) replaces = { '@PSCHEMANAME': args.postgres_schema, '@PUSER': args.postgres_username, '@MSERVER': args.mssql_server, '@MPORT': args.mssql_port, '@MUSER': args.mssql_username, '@MPASSWORD': args.mssql_password, '@MDATABASE': args.mssql_database, '@MSCHEMANAME': args.mssql_schema } tests = run_tests('tests/postgresql/*.sql', conn, replaces) print_report(tests['total'], tests['ok'], tests['errors']) if tests['errors'] != 0: exit(5) else: exit(0) except Exception as e: print_error(e) exit(3)
def parse_options(): """Parse and validate options. Returns a dict with all the options.""" usage = "%prog <arguments>" description = ('Run MSSQL tests from sql files') parser = OptionParser(usage=usage, description=description) parser.add_option('--server', action='store', help='MSSQL/Azure server') parser.add_option('--port', action='store', help='MSSQL/Azure TCP port') parser.add_option('--database', action='store', help='Database name') parser.add_option('--schema', action='store', help='Schema to use (and create if needed)') parser.add_option('--username', action='store', help='Username to connect') parser.add_option('--password', action='store', help='Passord to connect') parser.add_option('--azure', action='store_true', default=False, help='If present, will connect as Azure, otherwise as ' 'standard MSSQL') (options, args) = parser.parse_args() # Check for test parameters if (options.server is None or options.port is None or options.database is None or options.schema is None or options.username is None or options.password is None): print_error("Insufficient parameters, check help (-h)") exit(4) else: if options.azure is True: options.username = '******' % ( options.username, options.server.split('.')[0]) return(options)
def get_logs_path(conn, dbtype): """ Get PostgreSQL logs Keyword arguments: conn -- A db connection (according to Python DB API v2.0) dbtye -- A string with the database type (postgresql|mssql). mssql will return an empty a array. """ logs = [] if dbtype == 'mssql': return (logs) cursor = conn.cursor() try: cursor.execute( "SELECT setting FROM pg_catalog.pg_settings WHERE name = 'data_directory';" ) data_dir = cursor.fetchone()[0] except TypeError: print_error("The user does not have SUPERUSER access to PostgreSQL.") print_error( "Cannot access pg_catalog.pg_settings required values, so logs cannot be found" ) return (logs) cursor.execute( "SELECT setting FROM pg_catalog.pg_settings WHERE name = 'log_directory';" ) log_dir = cursor.fetchone()[0] if log_dir[0] != '/': log_dir = "%s/%s" % (data_dir, log_dir) cursor.execute( "SELECT setting FROM pg_catalog.pg_settings WHERE name = 'logging_collector';" ) # No logging collector, add stdout from postmaster (assume stderr is redirected to stdout) if cursor.fetchone()[0] == 'off': with open("%s/postmaster.pid" % data_dir, "r") as f: postmaster_pid = f.readline().rstrip('\n') postmaster_log = "/proc/%s/fd/1" % postmaster_pid if isfile(postmaster_log): logs.append(realpath(postmaster_log)) # Logging collector enabled else: # Add stdout from logger (assume stderr is redirected to stdout) pids = [pid for pid in listdir('/proc') if pid.isdigit()] for pid in pids: try: cmdline = open('/proc/' + pid + '/cmdline', 'rb').read() if 'postgres: logger' in cmdline: logger_log = "/proc/%s/fd/2" % pid if isfile(logger_log): logs.append(realpath(logger_log)) except IOError: # proc has already terminated continue # Add all files from log_dir for f in listdir(log_dir): logs.append(realpath(log_dir + '/' + f)) return (logs)
def main(): try: args = parse_options() except Exception as e: print_usage_error(path.basename(__file__), e) exit(2) try: conn = connect(host=args.postgres_server, user=args.postgres_username, password=args.postgres_password, database=args.postgres_database, port=args.postgres_port) if args.debugging or args.unattended_debugging: curs = conn.cursor() curs.execute("SELECT pg_backend_pid()") print("Backend PID = %d" % curs.fetchone()[0]) if not args.unattended_debugging: print("Press any key to launch tests.") raw_input() replaces = { '@PSCHEMANAME': args.postgres_schema, '@PUSER': args.postgres_username, '@MSERVER': args.mssql_server, '@MPORT': args.mssql_port, '@MUSER': args.mssql_username, '@MPASSWORD': args.mssql_password, '@MDATABASE': args.mssql_database, '@MSCHEMANAME': args.mssql_schema, '@TDSVERSION': args.tds_version } tests = run_tests('tests/postgresql/*.sql', conn, replaces, 'postgresql', args.debugging, args.unattended_debugging) print_report(tests['total'], tests['ok'], tests['errors']) logs = get_logs_path(conn, 'postgresql') if (tests['errors'] != 0 or args.unattended_debugging) and not args.debugging: for fpath in logs: print_info("=========== Content of %s ===========" % fpath) with open(fpath, "r") as f: print(f.read()) if tests['errors'] != 0: exit(5) else: exit(0) except Exception as e: print_error(e) exit(3)
def main(): try: args = parse_options() except Exception as e: print_usage_error(path.basename(__file__), e) exit(2) try: conn = connect(server=args.server, user=args.username, password=args.password, database=args.database, port=args.port) replaces = {'@SCHEMANAME': args.schema} tests = run_tests('tests/mssql/*.sql', conn, replaces, 'mssql') print_report(tests['total'], tests['ok'], tests['errors']) if tests['errors'] != 0: exit(5) else: exit(0) except Exception as e: print_error(e) exit(3)
def main(): try: args = parse_options() except Exception as e: print_usage_error(path.basename(__file__), e) exit(2) try: # For our tests, tds_version 7.1 is enough conn = connect(server=args.server, user=args.username, password=args.password, database=args.database, port=args.port, tds_version='7.1') replaces = {'@SCHEMANAME': args.schema} tests = run_tests('tests/mssql/*.sql', conn, replaces, 'mssql') print_report(tests['total'], tests['ok'], tests['errors']) if tests['errors'] != 0: exit(5) else: exit(0) except Exception as e: print_error(e) exit(3)
def run_tests(path, conn, replaces, dbtype, debugging=False, unattended_debugging=False): """Run SQL tests over a connection, returns a dict with results. Keyword arguments: path -- String with the path having the SQL files for tests conn -- A db connection (according to Python DB API v2.0) replaces -- A dict with replaces to perform at testing code dbtype -- A string with the database type (postgresql|mssql) """ files = sorted(glob(path)) tests = {'total': 0, 'ok': 0, 'errors': 0} for fname in files: test_file = open('%s.json' % fname.rsplit('.', 1)[0], 'r') test_properties = load(test_file) test_desc = test_properties['test_desc'] test_number = basename(fname).split('_')[0] req_ver = test_properties['server']['version'] if check_ver(conn, req_ver['min'], req_ver['max'], dbtype): tests['total'] += 1 f = open(fname, 'r') sentence = f.read() for key, elem in replaces.items(): sentence = sentence.replace(key, elem) print_info("%s: Testing %s" % (test_number, test_desc)) if debugging or unattended_debugging: print_info("Query:") print(sentence) try: cursor = conn.cursor() cursor.execute(sentence) conn.commit() cursor.close() tests['ok'] += 1 except Exception as e: print_error("Error running %s (%s)" % (test_desc, fname)) print_error("Query:") print(sentence) try: print_error(e.pgcode) print_error(e.pgerror) for att in [ member for member in dir(Diagnostics) if not member.startswith("__") ]: print_error("%s: %s" % (att, getattr(e.diag, att))) except: print_error(e) conn.rollback() tests['errors'] += 1 f.close() return (tests)
#!/usr/bin/env python from lib.messages import print_error, print_info, print_ok, print_report from lib.messages import print_usage_error, print_warning from lib.tests import run_tests from optparse import OptionParser from os import path try: from pymssql import connect except: print_error( "pymssql library not available, please install it before usage!") exit(1) def parse_options(): """Parse and validate options. Returns a dict with all the options.""" usage = "%prog <arguments>" description = ('Run MSSQL tests from sql files') parser = OptionParser(usage=usage, description=description) parser.add_option('--server', action='store', help='MSSQL/Azure server') parser.add_option('--port', action='store', help='MSSQL/Azure TCP port') parser.add_option('--database', action='store', help='Database name') parser.add_option('--schema', action='store', help='Schema to use (and create if needed)') parser.add_option('--username', action='store', help='Username to connect') parser.add_option('--password', action='store', help='Passord to connect') parser.add_option('--azure', action='store_true', default=False, help='If present, will connect as Azure, otherwise as '
#!/usr/bin/env python from lib.messages import print_error, print_info, print_ok, print_report from lib.messages import print_usage_error, print_warning from lib.tests import run_tests from optparse import OptionParser from os import path try: from psycopg2 import connect except: print_error( "psycopg2 library not available, please install it before usage!") exit(1) def parse_options(): """Parse and validate options. Returns a dict with all the options.""" usage = "%prog <arguments>" description = ('Run PostgreSQL tests from sql files') parser = OptionParser(usage=usage, description=description) parser.add_option('--postgres_server', action='store', help='PostgreSQL server') parser.add_option('--postgres_port', action='store', help='PostgreSQL TCP port') parser.add_option('--postgres_database', action='store', help='Database name') parser.add_option('--postgres_schema', action='store', help='Schema to use (and create if needed)') parser.add_option('--postgres_username', action='store', help='Username to connect') parser.add_option('--postgres_password', action='store', help='Passord to connect')
#!/usr/bin/env python from lib.messages import print_error, print_info, print_ok, print_report from lib.messages import print_usage_error, print_warning from lib.tests import run_tests from optparse import OptionParser from os import path try: from psycopg2 import connect except: print_error( "psycopg2 library not available, please install it before usage!") exit(1) def parse_options(): """Parse and validate options. Returns a dict with all the options.""" usage = "%prog <arguments>" description = ('Run PostgreSQL tests from sql files') parser = OptionParser(usage=usage, description=description) parser.add_option('--postgres_server', action='store', help='PostgreSQL server') parser.add_option('--postgres_port', action='store', help='PostgreSQL TCP port') parser.add_option('--postgres_database', action='store', help='Database name') parser.add_option('--postgres_schema', action='store', help='Schema to use (and create if needed)')
def parse_options(): """Parse and validate options. Returns a dict with all the options.""" usage = "%prog <arguments>" description = ('Run PostgreSQL tests from sql files') parser = OptionParser(usage=usage, description=description) parser.add_option('--postgres_server', action='store', help='PostgreSQL server') parser.add_option('--postgres_port', action='store', help='PostgreSQL TCP port') parser.add_option('--postgres_database', action='store', help='Database name') parser.add_option('--postgres_schema', action='store', help='Schema to use (and create if needed)') parser.add_option('--postgres_username', action='store', help='Username to connect') parser.add_option('--postgres_password', action='store', help='Passord to connect') parser.add_option('--mssql_server', action='store', help='MSSQL/Azure server') parser.add_option('--mssql_port', action='store', help='MSSQL/Azure TCP port') parser.add_option('--mssql_database', action='store', help='Database name') parser.add_option('--mssql_schema', action='store', help='Schema to use (and create if needed)') parser.add_option('--mssql_username', action='store', help='Username to connect') parser.add_option('--mssql_password', action='store', help='Passord to connect') parser.add_option('--azure', action="store_true", default=False, help='If present, will connect as Azure, otherwise as ' 'standard MSSQL') parser.add_option( '--debugging', action="store_true", default=False, help='If present, will pause after backend PID and before ' 'launching tests (so gdb can be attached. It will also ' 'display contextual SQL queries') parser.add_option( '--unattended_debugging', action="store_true", default=False, help='Same as --debugging, but without pausing and printing ' 'PostgreSQL logs at the end (useful for CI)') parser.add_option('--tds_version', action="store", default=DEFAULT_TDS_VERSION, help='Specifies th TDS protocol version, default="%s"' % DEFAULT_TDS_VERSION) (options, args) = parser.parse_args() # Check for test parameters if (options.postgres_server is None or options.postgres_port is None or options.postgres_database is None or options.postgres_schema is None or options.postgres_username is None or options.postgres_password is None or options.mssql_server is None or options.mssql_port is None or options.mssql_database is None or options.mssql_schema is None or options.mssql_username is None or options.mssql_password is None): print_error("Insufficient parameters, check help (-h)") exit(4) else: if options.azure is True: options.mssql_username = '******' % ( options.mssql_username, options.mssql_server.split('.')[0]) return (options)
#!/usr/bin/env python from lib.messages import print_error, print_info, print_ok, print_report from lib.messages import print_usage_error, print_warning from lib.tests import run_tests from optparse import OptionParser from os import path try: from pymssql import connect except: print_error( "pymssql library not available, please install it before usage!") exit(1) def parse_options(): """Parse and validate options. Returns a dict with all the options.""" usage = "%prog <arguments>" description = ('Run MSSQL tests from sql files') parser = OptionParser(usage=usage, description=description) parser.add_option('--server', action='store', help='MSSQL/Azure server') parser.add_option('--port', action='store', help='MSSQL/Azure TCP port') parser.add_option('--database', action='store', help='Database name') parser.add_option('--schema', action='store', help='Schema to use (and create if needed)') parser.add_option('--username', action='store', help='Username to connect') parser.add_option('--password', action='store', help='Passord to connect')