2016-07-18 08:50:21 -05:00
|
|
|
#############################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
|
|
|
# Copyright (C) 2013 - 2016, The pgAdmin Development Team
|
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##############################################################
|
|
|
|
|
|
|
|
""" This file collect all modules/files present in tests directory and add
|
|
|
|
them to TestSuite. """
|
2016-09-14 10:26:12 -05:00
|
|
|
from __future__ import print_function
|
2016-07-18 08:50:21 -05:00
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import sys
|
2016-09-14 10:26:12 -05:00
|
|
|
import signal
|
|
|
|
import atexit
|
2016-07-18 08:50:21 -05:00
|
|
|
import unittest
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from testscenarios.scenarios import generate_scenarios
|
|
|
|
|
|
|
|
CURRENT_PATH = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
|
|
|
# Set sys path to current directory so that we can import pgadmin package
|
|
|
|
root = os.path.dirname(CURRENT_PATH)
|
|
|
|
|
|
|
|
if sys.path[0] != root:
|
|
|
|
sys.path.insert(0, root)
|
2016-09-14 10:26:12 -05:00
|
|
|
os.chdir(root)
|
2016-07-18 08:50:21 -05:00
|
|
|
|
|
|
|
from pgadmin import create_app
|
|
|
|
import config
|
2016-09-14 10:26:12 -05:00
|
|
|
import test_setup
|
|
|
|
|
|
|
|
# Execute setup.py if test SQLite database doesn't exist.
|
|
|
|
if os.path.isfile(config.TEST_SQLITE_PATH):
|
|
|
|
print("The configuration database already exists at '%s'. "
|
|
|
|
"Please remove the database and re-run the test suite." %
|
|
|
|
config.TEST_SQLITE_PATH)
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
config.TESTING_MODE = True
|
|
|
|
pgadmin_credentials = test_setup.config_data
|
|
|
|
|
|
|
|
# Set environment variables for email and password
|
|
|
|
os.environ['PGADMIN_SETUP_EMAIL'] = ''
|
|
|
|
os.environ['PGADMIN_SETUP_PASSWORD'] = ''
|
|
|
|
if pgadmin_credentials:
|
|
|
|
if 'pgAdmin4_login_credentials' in pgadmin_credentials:
|
|
|
|
if all(item in pgadmin_credentials['pgAdmin4_login_credentials']
|
|
|
|
for item in ['login_username', 'login_password']):
|
|
|
|
pgadmin_credentials = pgadmin_credentials[
|
|
|
|
'pgAdmin4_login_credentials']
|
|
|
|
os.environ['PGADMIN_SETUP_EMAIL'] = pgadmin_credentials[
|
|
|
|
'login_username']
|
|
|
|
os.environ['PGADMIN_SETUP_PASSWORD'] = pgadmin_credentials[
|
|
|
|
'login_password']
|
|
|
|
|
|
|
|
# Execute the setup file
|
|
|
|
exec (open("setup.py").read())
|
2016-07-18 08:50:21 -05:00
|
|
|
|
|
|
|
# Get the config database schema version. We store this in pgadmin.model
|
|
|
|
# as it turns out that putting it in the config files isn't a great idea
|
|
|
|
from pgadmin.model import SCHEMA_VERSION
|
2016-09-14 10:26:12 -05:00
|
|
|
|
|
|
|
# Delay the import test_utils as it needs updated config.SQLITE_PATH
|
|
|
|
import test_utils
|
2016-07-18 08:50:21 -05:00
|
|
|
|
|
|
|
config.SETTINGS_SCHEMA_VERSION = SCHEMA_VERSION
|
|
|
|
|
|
|
|
# Override some other defaults
|
|
|
|
from logging import WARNING
|
2016-09-14 10:26:12 -05:00
|
|
|
|
2016-07-18 08:50:21 -05:00
|
|
|
config.CONSOLE_LOG_LEVEL = WARNING
|
|
|
|
|
|
|
|
# Create the app
|
|
|
|
app = create_app()
|
|
|
|
app.config['WTF_CSRF_ENABLED'] = False
|
|
|
|
test_client = app.test_client()
|
|
|
|
|
|
|
|
|
2016-09-14 10:26:12 -05:00
|
|
|
def get_suite(arguments, server, test_app_client):
|
2016-07-18 08:50:21 -05:00
|
|
|
"""
|
|
|
|
This function loads the all modules in the tests directory into testing
|
|
|
|
environment.
|
|
|
|
|
|
|
|
:param arguments: this is command line arguments for module name to
|
|
|
|
which test suite will run
|
|
|
|
:type arguments: str
|
2016-09-14 10:26:12 -05:00
|
|
|
:param server: server details
|
|
|
|
:type server: dict
|
2016-07-18 08:50:21 -05:00
|
|
|
:param test_app_client: test client
|
|
|
|
:type test_app_client: pgadmin app object
|
|
|
|
:return pgadmin_suite: test suite with test cases
|
|
|
|
:rtype: TestSuite
|
|
|
|
"""
|
|
|
|
|
|
|
|
from pgadmin.utils.route import TestsGeneratorRegistry
|
|
|
|
|
|
|
|
modules = []
|
|
|
|
pgadmin_suite = unittest.TestSuite()
|
|
|
|
|
|
|
|
# Load the test modules which are in given package(i.e. in arguments.pkg)
|
2016-07-27 09:33:36 -05:00
|
|
|
if arguments['pkg'] is None or arguments['pkg'] == "all":
|
2016-07-18 08:50:21 -05:00
|
|
|
TestsGeneratorRegistry.load_generators('pgadmin')
|
|
|
|
else:
|
2016-09-14 10:26:12 -05:00
|
|
|
TestsGeneratorRegistry.load_generators('pgadmin.%s.tests' %
|
|
|
|
arguments['pkg'])
|
|
|
|
|
|
|
|
# Sort module list so that test suite executes the test cases sequentially
|
|
|
|
module_list = TestsGeneratorRegistry.registry.items()
|
|
|
|
module_list = sorted(module_list, key=lambda module_tuple: module_tuple[0])
|
2016-07-18 08:50:21 -05:00
|
|
|
|
|
|
|
# Get the each test module and add into list
|
2016-09-14 10:26:12 -05:00
|
|
|
for key, klass in module_list:
|
2016-07-18 08:50:21 -05:00
|
|
|
gen = klass
|
|
|
|
modules.append(gen)
|
|
|
|
|
|
|
|
# Set the test client to each module & generate the scenarios
|
|
|
|
for module in modules:
|
|
|
|
obj = module()
|
2016-09-14 10:26:12 -05:00
|
|
|
obj.setApp(app)
|
2016-07-18 08:50:21 -05:00
|
|
|
obj.setTestClient(test_app_client)
|
2016-09-14 10:26:12 -05:00
|
|
|
obj.setTestServer(server)
|
2016-07-18 08:50:21 -05:00
|
|
|
scenario = generate_scenarios(obj)
|
|
|
|
pgadmin_suite.addTests(scenario)
|
|
|
|
|
|
|
|
return pgadmin_suite
|
|
|
|
|
|
|
|
|
|
|
|
def add_arguments():
|
|
|
|
"""
|
|
|
|
This function parse the command line arguments(project's package name
|
|
|
|
e.g. browser) & add into parser
|
|
|
|
|
|
|
|
:return args: command line argument for pgadmin's package name
|
|
|
|
:rtype: argparse namespace
|
|
|
|
"""
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Test suite for pgAdmin4')
|
|
|
|
parser.add_argument('--pkg', help='Executes the test cases of particular'
|
|
|
|
' package')
|
|
|
|
arg = parser.parse_args()
|
|
|
|
|
|
|
|
return arg
|
|
|
|
|
|
|
|
|
2016-09-14 10:26:12 -05:00
|
|
|
def sig_handler(signo, frame):
|
|
|
|
test_utils.drop_objects()
|
|
|
|
|
|
|
|
|
2016-07-18 08:50:21 -05:00
|
|
|
class StreamToLogger(object):
|
|
|
|
def __init__(self, logger, log_level=logging.INFO):
|
|
|
|
self.terminal = sys.stderr
|
|
|
|
self.logger = logger
|
|
|
|
self.log_level = log_level
|
|
|
|
self.linebuf = ''
|
|
|
|
|
|
|
|
def write(self, buf):
|
|
|
|
"""
|
|
|
|
This function writes the log in the logger file as well as on console
|
|
|
|
|
|
|
|
:param buf: log message
|
|
|
|
:type buf: str
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.terminal.write(buf)
|
|
|
|
for line in buf.rstrip().splitlines():
|
|
|
|
self.logger.log(self.log_level, line.rstrip())
|
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-09-14 10:26:12 -05:00
|
|
|
# Register cleanup function to cleanup on exit
|
|
|
|
atexit.register(test_utils.drop_objects)
|
|
|
|
# Set signal handler for cleanup
|
|
|
|
signal.signal(signal.SIGTERM, sig_handler)
|
|
|
|
signal.signal(signal.SIGABRT, sig_handler)
|
|
|
|
signal.signal(signal.SIGINT, sig_handler)
|
|
|
|
signal.signal(signal.SIGQUIT, sig_handler)
|
|
|
|
|
2016-07-18 08:50:21 -05:00
|
|
|
# Set basic logging configuration for log file
|
|
|
|
logging.basicConfig(level=logging.DEBUG,
|
2016-07-27 09:33:36 -05:00
|
|
|
format='%(asctime)s:%(levelname)s:%(name)s:%(message)s'
|
|
|
|
,
|
2016-07-18 08:50:21 -05:00
|
|
|
filename=CURRENT_PATH + "/" + "regression.log",
|
|
|
|
filemode='w'
|
|
|
|
)
|
|
|
|
|
|
|
|
# Create logger to write log in the logger file as well as on console
|
|
|
|
stderr_logger = logging.getLogger('STDERR')
|
|
|
|
sys.stderr = StreamToLogger(stderr_logger, logging.ERROR)
|
|
|
|
|
2016-07-27 09:33:36 -05:00
|
|
|
args = vars(add_arguments())
|
2016-07-18 08:50:21 -05:00
|
|
|
|
2016-09-14 10:26:12 -05:00
|
|
|
servers_info = test_utils.get_config_data()
|
|
|
|
try:
|
|
|
|
for server in servers_info:
|
|
|
|
print("\n=============Running the test cases for '%s'============="
|
|
|
|
% server['name'], file=sys.stderr)
|
|
|
|
test_utils.create_test_server(server)
|
|
|
|
# Login the test client
|
|
|
|
test_utils.login_tester_account(test_client)
|
|
|
|
|
|
|
|
suite = get_suite(args, server, test_client)
|
|
|
|
tests = unittest.TextTestRunner(stream=sys.stderr,
|
|
|
|
descriptions=True,
|
|
|
|
verbosity=2).run(suite)
|
|
|
|
# Logout the test client
|
|
|
|
test_utils.logout_tester_account(test_client)
|
|
|
|
|
|
|
|
test_utils.delete_test_server(server)
|
|
|
|
except SystemExit:
|
|
|
|
test_utils.drop_objects()
|
2016-07-27 09:33:36 -05:00
|
|
|
|
2016-07-18 08:50:21 -05:00
|
|
|
print("Please check output in file: %s/regression.log " % CURRENT_PATH)
|