# Path: chaosmonkey/engine/app.py
# def configure_engine(database_uri, attacks_folder, planners_folder, cme_timezone):
#     """
#     Create a Flask App and all the configuration needed to run the CMEEngine
# 
#     * Init and configure the SQLAlchemy store (create db and tables if don't exists)
#     * Init ModuleStores (attacks and planners)
#     * Configure the timezone and jobstore for the scheduler
#     * Configure the CMEManager
# 
#     TODO:
#         The scheduler start is not made until the first request is made. This is due to
#         the way the SQLAlchemy store is created, because it needs the app.context to work
#         properly
# 
#     :param database_uri:    SQLAlchemy SQLALCHEMY_DATABASE_URI
#     :param attacks_folder:  folder to load the attacks modules
#     :param planners_folder: folder to load the planners modules
#     :param cme_timezone:    timezone to set in the scheduler
#     """
# 
#     # configure and init FlaskSQLAlchemy
#     if database_uri != ":memory:":
#         # check for memory database for tests if not, make sure the path exists and create it
#         make_sure_path_exists(os.path.dirname(database_uri))
# 
#     flask_app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///%s" % database_uri
#     flask_app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
#     db.init_app(flask_app)
#     with flask_app.app_context():
#         db.create_all()
#         db.app = flask_app
# 
#     # init stores
#     sql_store = CMESQLAlchemyStore()
#     planners_store = ModulesStore(Planner)
#     attacks_store = ModulesStore(Attack)
# 
#     # configure the scheduler
#     tz = timezone(cme_timezone)
#     jobstores = {
#         "default": sql_store
#     }
#     scheduler.configure(jobstores=jobstores, timezone=tz)
# 
#     # configure module stores
#     attacks_store.load(attacks_folder)
#     planners_store.load(planners_folder)
# 
#     # configure CMEManager
#     manager.configure(scheduler, sql_store, planners_store, attacks_store)
# 
# def shutdown_engine():
#     """
#     Shutdown the scheduler
#     """
#     if scheduler.running:
#         scheduler.shutdown()
# 
# Path: chaosmonkey/engine/cme_manager.py
# class CMEManager:
#     def __init__(self):
#     def configure(self, scheduler, sql_store, planners_store, attacks_store):
#     def scheduler(self):
#     def attacks_store(self):
#     def planners_store(self):
#     def sql_store(self):
#     def get_executors(self, executed=False):
#     def get_executor(self, executor_id):
#     def get_executors_for_plan(self, plan_id):
#     def update_executor_trigger(self, executor_id, trigger):
#     def remove_executor(self, executor_id):
#     def add_executor(self, date, name, attack_config, plan_id):
#     def get_attack_list(self):
#     def get_planner_list(self):
#     def execute_plan(self, name, planner_config, attack_config):
#     def add_plan(self, name):
#     def get_plan(self, plan_id):
#     def get_plans(self, show_all=None):
#     def delete_plan(self, plan_id):
#     def _job_to_executor(job):
# 
# Path: chaosmonkey/api/app.py
# def handle_invalid_usage(error):


import pytest
from chaosmonkey.engine.app import configure_engine, shutdown_engine
from chaosmonkey.engine.cme_manager import manager as cme_manager
from chaosmonkey.api.app import flask_app
    """Options to configure attacks and planners root"""
    parser.addoption("--root", action="store", default="./",
                     help="src root folder")


@pytest.fixture(scope="session")
def root_src(request):
    return request.config.getoption("--root")


@pytest.yield_fixture(scope="session")
def app(root_src):
    """Crete app for test with context."""
    database_uri = ":memory:"
    attacks_folder = root_src + "attacks"
    planners_folder = root_src + "planners"
    cme_timezone = "Europe/Madrid"

    configure_engine(database_uri, attacks_folder, planners_folder, cme_timezone)
    cme_manager.scheduler.start()
    yield flask_app
    tear_down()


def tear_down():
    shutdown_engine()


@pytest.fixture
def manager():

# Path: chaosmonkey/engine/app.py
# def configure_engine(database_uri, attacks_folder, planners_folder, cme_timezone):
#     """
#     Create a Flask App and all the configuration needed to run the CMEEngine
# 
#     * Init and configure the SQLAlchemy store (create db and tables if don't exists)
#     * Init ModuleStores (attacks and planners)
#     * Configure the timezone and jobstore for the scheduler
#     * Configure the CMEManager
# 
#     TODO:
#         The scheduler start is not made until the first request is made. This is due to
#         the way the SQLAlchemy store is created, because it needs the app.context to work
#         properly
# 
#     :param database_uri:    SQLAlchemy SQLALCHEMY_DATABASE_URI
#     :param attacks_folder:  folder to load the attacks modules
#     :param planners_folder: folder to load the planners modules
#     :param cme_timezone:    timezone to set in the scheduler
#     """
# 
#     # configure and init FlaskSQLAlchemy
#     if database_uri != ":memory:":
#         # check for memory database for tests if not, make sure the path exists and create it
#         make_sure_path_exists(os.path.dirname(database_uri))
# 
#     flask_app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///%s" % database_uri
#     flask_app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
#     db.init_app(flask_app)
#     with flask_app.app_context():
#         db.create_all()
#         db.app = flask_app
# 
#     # init stores
#     sql_store = CMESQLAlchemyStore()
#     planners_store = ModulesStore(Planner)
#     attacks_store = ModulesStore(Attack)
# 
#     # configure the scheduler
#     tz = timezone(cme_timezone)
#     jobstores = {
#         "default": sql_store
#     }
#     scheduler.configure(jobstores=jobstores, timezone=tz)
# 
#     # configure module stores
#     attacks_store.load(attacks_folder)
#     planners_store.load(planners_folder)
# 
#     # configure CMEManager
#     manager.configure(scheduler, sql_store, planners_store, attacks_store)
# 
# def shutdown_engine():
#     """
#     Shutdown the scheduler
#     """
#     if scheduler.running:
#         scheduler.shutdown()
# 
# Path: chaosmonkey/engine/cme_manager.py
# class CMEManager:
#     def __init__(self):
#     def configure(self, scheduler, sql_store, planners_store, attacks_store):
#     def scheduler(self):
#     def attacks_store(self):
#     def planners_store(self):
#     def sql_store(self):
#     def get_executors(self, executed=False):
#     def get_executor(self, executor_id):
#     def get_executors_for_plan(self, plan_id):
#     def update_executor_trigger(self, executor_id, trigger):
#     def remove_executor(self, executor_id):
#     def add_executor(self, date, name, attack_config, plan_id):
#     def get_attack_list(self):
#     def get_planner_list(self):
#     def execute_plan(self, name, planner_config, attack_config):
#     def add_plan(self, name):
#     def get_plan(self, plan_id):
#     def get_plans(self, show_all=None):
#     def delete_plan(self, plan_id):
#     def _job_to_executor(job):
# 
# Path: chaosmonkey/api/app.py
# def handle_invalid_usage(error):


# Path: test/unit/conftest.py
import pytest
from chaosmonkey.engine.app import configure_engine, shutdown_engine
from chaosmonkey.engine.cme_manager import manager as cme_manager
from chaosmonkey.api.app import flask_app

    """Options to configure attacks and planners root"""
    parser.addoption("--root", action="store", default="./",
                     help="src root folder")


@pytest.fixture(scope="session")
def root_src(request):
    return request.config.getoption("--root")


@pytest.yield_fixture(scope="session")
def app(root_src):
    """Crete app for test with context."""
    database_uri = ":memory:"
    attacks_folder = root_src + "attacks"
    planners_folder = root_src + "planners"
    cme_timezone = "Europe/Madrid"

    configure_engine(database_uri, attacks_folder, planners_folder, cme_timezone)
    cme_manager.scheduler.start()
    yield flask_app
    tear_down()


def tear_down():
    shutdown_engine()


@pytest.fixture
def manager():

    return cme_manager