def app(request):
    # setup test database with corresponding testdata.  Returns None on error

    print "\n"  # separate logs from which test is running when printed during pytest

    # testdata to pre-load into db, indexed by db collection name containing json file with testdata
    # or directory to scan and insert into collection
    testdata = {}

    from app import create_app
    app = create_app("config.py")
    db = get_db()
    app.db = db
    app.client = app.test_client()

    logger.debug("setting up db!")
    assert db_setup(sharding=False, force=True)
    logger.debug("db initialized")

    # teardown called after all tests in session have completed
    def teardown():
        pass

    request.addfinalizer(teardown)

    logger.debug("app setup completed")
    return app
Ejemplo n.º 2
0
def app(request):
    # setup test database with corresponding testdata.  Returns None on error

    print "\n"  # separate logs from which test is running when printed during pytest

    # testdata to pre-load into db, indexed by db collection name containing json file with testdata
    # or directory to scan and insert into collection
    testdata = {}

    from app import create_app
    app = create_app("config.py")
    db = get_db()
    app.db = db
    app.client = app.test_client()

    # get all objects registred to rest API, drop db and create with proper keys
    for classname in registered_classes:
        c = registered_classes[classname]
        # drop existing collection
        logger.debug("dropping collection %s" % c._classname)
        db[c._classname].drop()
        # insert test if present
        if c._classname in testdata:
            load_testdata(c._classname, testdata[c._classname])

        # create unique indexes for collection
        indexes = []
        if not c._access["expose_id"]:
            for a in c._attributes:
                if c._attributes[a].get("key", False):
                    indexes.append((a, DESCENDING))
        if len(indexes) > 0:
            logger.debug("creating indexes for %s: %s", c._classname, indexes)
            db[c._classname].create_index(indexes, unique=True)

    # if uni is enabled then required before any other object is created
    uni = Universe.load()
    uni.save()
    logger.debug("db initialized")

    # create local and admin users required to be present for all tests
    logger.debug("creating default local users")
    u1 = User(username="******", password="******", role=Role.FULL_ADMIN)
    u2 = User(username="******", password="******", role=Role.FULL_ADMIN)
    assert (u1.save() and u2.save())

    # teardown called after all tests in session have completed
    def teardown():
        pass

    request.addfinalizer(teardown)

    logger.debug("app setup completed")
    return app
Ejemplo n.º 3
0
def db_exists():
    """ check if database exists by verifying presents of any of the following
        collections:
            users, settings, aci.settings
    """
    logger.debug("checking if db exists")
    collections = get_db().collection_names()
    logger.debug("current collections: %s" % collections)
    if len(collections)>0 and ("users" in collections and "settings" in collections):
        return True
    return False
def app(request, app):
    # module level setup executed before any 'user' test in current file
    logger.debug("%s module-prep start", "-"*80)

    app.config["LOGIN_ENABLED"] = False
    t_app = create_app("config.py")
    t_app.db = get_db()
    t_app.client = t_app.test_client()
    t_app.config["LOGIN_ENABLED"] = False
    t_app.config["PROXY_URL"] = "http://127.0.0.1:%s" % PROXY_PORT
    t_app.config["TMP_DIR"] = os.path.realpath("%s/test" % t_app.config["TMP_DIR"])

    # create test directory if not present
    if not os.path.isdir(t_app.config["TMP_DIR"]):
        os.makedirs(t_app.config["TMP_DIR"])

    # copy testdata files to /tmp/ directory before tests start
    testfiles = [
        "%s/download.txt" % testdata,
    ]
    logger.debug("copying testdata files to tmp directory")
    for tf in testfiles:
        dst = re.sub("//", "/", "%s/%s" % (t_app.config["TMP_DIR"], tf.split("/")[-1]))
        logger.debug("copying testfile from %s to %s", tf, dst)
        if not os.path.exists(dst): shutil.copy(tf, dst)

    logger.debug("setting up proxy server")
    proxy_server = Process(target=run_proxy_app, args=(t_app,))
    proxy_server.start()
    # give the server a second to start
    logger.debug("waiting for proxy server to start")
    time.sleep(2)

    # ensure uni exists
    uni = Universe()
    assert uni.save()

    # teardown called after all tests in session have completed
    def teardown(): 
        logger.debug("%s module-prep teardown", "-"*80)
        if proxy_server.is_alive(): 
            logger.debug("terminating proxy server (%s)", proxy_server.pid)
            kill_group(proxy_server.pid)
            terminate_process(proxy_server)
        shutil.rmtree(t_app.config["TMP_DIR"])

    request.addfinalizer(teardown)

    logger.debug("(proxy) module level app setup completed")
    return t_app
Ejemplo n.º 5
0
def dummy_work(subnets, count):
    # perform dummy_event lookup against subnets 'count' times and print final
    logger.debug("staring worker!")
    db = get_db(uniq=True, overwrite_global=True)
    history = db[eptHistory._classname]
    ts = time.time()
    for i in xrange(0, count):
        (vrf, ip) = subnets.get_next_ip()
        dummy_event(history, fabric, vrf, ip)

    total_time = time.time() - ts
    logger.debug(
        "completed %s lookups, time: %0.3f, avg: %0.6f, jobs/sec: %0.6f",
        count, total_time, total_time / count, count / total_time)
Ejemplo n.º 6
0
def app(request, app):
    # module level setup executed before any 'settings' test in current file

    from app import create_app
    app = create_app("config.py")
    app.db = get_db()
    app.config["LOGIN_ENABLED"] = False

    # create settings object with default attributes
    s = Settings.load()
    s.save()

    # set client to authenticated user before starting any tests
    app.client = app.test_client()

    # teardown called after all tests in session have completed
    def teardown():
        pass

    request.addfinalizer(teardown)

    logger.debug("(settings) module level app setup completed")
    return app
    logger.addHandler(logger_handler)
    return logger


logger = test_logging(logging.getLogger("app"))
test_logging(logging.getLogger("tests"))

from app.models.rest.db import db_setup
from app.models.utils import get_db
from app.models.utils import get_redis

# instance relative config - config.py implies instance/config.py
from tests.api.test_rest import Rest_TestClass

# test connectivity to database before starting any tests
db = get_db()
try:
    db.collection_names()
except Exception as e:
    sys.exit("failed to connect to database")

# flush redis db
r = get_redis()
r.flushall()


@pytest.fixture(scope="session")
def app(request):
    # setup test database with corresponding testdata.  Returns None on error

    print "\n"  # separate logs from which test is running when printed during pytest
Ejemplo n.º 8
0
def db_setup(args):
    """ delete any existing database and setup all tables for new database 
        returns boolean success
    """
    force = args.force
    logger.debug("setting up new database (force:%r)", force)
    
    if not force and db_exists():
        logger.debug("db already exists")
        return True

    # get password from user if not set
    pwd = args.password
    while pwd is None:
        pwd = getpass.getpass( "Enter admin password: "******"Re-enter password   : "******"Passwords do not match"
            pwd = None
        elif " " in pwd:
            print "No spaces allowed in password"
            pwd = None
    
    db = get_db()
    # get all objects registred to rest API, drop db and create with 
    # proper keys
    for classname in registered_classes:
        c = registered_classes[classname]
        # drop existing collection
        logger.debug("dropping collection %s" % c._classname)
        db[c._classname].drop()
        # create unique indexes for collection if expose_id is disabled
        # (else mongo _id is only unique key required)
        indexes = []
        if not c._access["expose_id"]:
            for a in c._attributes:
                if c._attributes[a].get("key", False): 
                    indexes.append((a,DESCENDING))
        if len(indexes)>0:
            logger.debug("creating indexes for %s: %s",c._classname,indexes)
            db[c._classname].create_index(indexes, unique=True)

    # if uni is enabled then required before any other object is created
    uni = Universe.load()
    uni.save()
        
    # insert settings with user provided values 
    lpass = "******"%uuid.uuid4()
    s = Settings(
        app_name=args.app_name,
        force_https= not args.no_https,
        lpass = lpass
    )
    try: s.save()
    except BadRequest as e:
        logger.error("failed to create settings: %s. Using all defaults"%e)
        s = Settings(lpass=lpass)
        s.save()
    
    # create admin user with provided password and local user
    u = User(username="******", password=lpass, role=Role.FULL_ADMIN)
    if not u.save(): 
        logger.error("failed to create local user")
        return False
    u = User(username=args.username, password=pwd, role=Role.FULL_ADMIN)
    if not u.save():
        logger.error("failed to create username: %s" % args.username)
        return False

    # create new definition for each object within definitions file
    # create a default definition named 'full' with all objects
    classnames = []
    for o in STATIC_MANAGED_OBJECTS:
        mo = ManagedObjects(**o)
        if not mo.save():
            logger.warn("failed to save static managed object: %s" % o)
        else:
            classnames.append(mo.classname)
    d = Definitions(definition="Full", managed_objects=classnames, template=True)
    d.description="default definition which includes all objects"
    if not d.save():
        logger.warn("failed to save default definition...")

    # create additional template definitions as defined in TEMPLATES
    for tname in TEMPLATES:
        template = TEMPLATES[tname]
        d = Definitions(definition=tname, managed_objects=template["objects"], template=True)
        d.description = template["description"]
        if not d.save():
            logger.warn("failed to save template %s", tname)

    #successful setup
    return True