Esempio n. 1
0
def mem_db(do_echo=False):
    from anchore_engine.db.entities.common import (
        get_engine,
        initialize,
        do_disconnect,
        init_thread_session,
        end_session,
    )
    from anchore_engine.db.entities.upgrade import do_create_tables

    conn_str = "sqllite://:memory:"

    config = {"credentials": {"database": {"db_connect": conn_str, "db_echo": do_echo}}}

    try:
        logger.info("Initializing connection: {}".format(config))
        ret = initialize(localconfig=config)
        init_thread_session(force_new=True)

        engine = get_engine()
        logger.info("Dropping db if found")
        engine.execute("DROP SCHEMA public CASCADE")
        engine.execute("CREATE SCHEMA public")
        engine.execute("GRANT ALL ON SCHEMA public TO postgres")
        engine.execute("GRANT ALL ON SCHEMA public TO public")

        # Now ready for anchore init (tables etc)
        logger.info("Creating tables")
        do_create_tables()

        yield ret
    finally:
        logger.info("Cleaning up/disconnect")
        end_session()
        do_disconnect()
Esempio n. 2
0
 def _teardown_and_recreate_tables(tablenames: list):
     tablenames_joined = ", ".join(map(str, tablenames))
     engine = get_engine()
     with engine.connect() as connection:
         with connection.begin():
             connection.execute(f"DROP TABLE {tablenames_joined} CASCADE")
     do_create_tables()
Esempio n. 3
0
def _teardown_vuln_data():
    """
    Drops all vulnerability related data
    """
    tablenames = [cls.__tablename__ for cls in DB_TABLES]
    tablenames_joined = ", ".join(map(str, tablenames))
    engine = get_engine()
    with engine.connect() as connection:
        with connection.begin():
            connection.execute(f"DROP TABLE {tablenames_joined} CASCADE")
    do_create_tables()
Esempio n. 4
0
def cls_anchore_db(connection_str=None, do_echo=False):
    """
    Sets up a db connection to an existing db, and fails if not found/present.

    This is for use in legacy unittest frameworks where it is set once at the class level, not on each function.
    :return:
    """

    from anchore_engine.db.entities.common import (
        get_engine,
        initialize,
        do_disconnect,
        init_thread_session,
        end_session,
    )
    from anchore_engine.db.entities.upgrade import do_create_tables

    conn_str = connection_str if connection_str else os.getenv(
        "ANCHORE_TEST_DB_URL")

    config = {
        "credentials": {
            "database": {
                "db_connect": conn_str,
                "db_echo": do_echo
            }
        }
    }

    try:
        logger.info("Initializing connection: {}".format(config))
        ret = initialize(localconfig=config)
        init_thread_session(force_new=True)

        engine = get_engine()
        logger.info("Dropping db if found")
        engine.execute("DROP SCHEMA public CASCADE")
        engine.execute("CREATE SCHEMA public")
        engine.execute("GRANT ALL ON SCHEMA public TO postgres")
        engine.execute("GRANT ALL ON SCHEMA public TO public")

        # Now ready for anchore init (tables etc)
        logger.info("Creating tables")
        do_create_tables()

        yield ret
    finally:
        logger.info("Cleaning up/disconnect")
        end_session()
        do_disconnect()