def test_list(modules_mock): sys.path.insert(0, "attacks") module = __import__("terminate_ec2_instance") modules_store = ModulesStore(Attack) modules_mock.return_value = [module] assert len(modules_store.list()) == 1
def test_load_path_with_valid_modules(validate_path_mock, get_module_names_mock): module_list = ["sys", "os"] get_module_names_mock.return_value = module_list validate_path_mock.return_value = True modules_store = ModulesStore(Attack) modules_store.load("/tmp") assert len(modules_store.modules) == len(module_list)
def test_get_module_names(): with patch("chaosmonkey.modules.module_store.listdir") as listdir_mock, \ patch("chaosmonkey.modules.module_store.isfile") as isfile_mock: listdir_mock.return_value = ["test.py", "tost.py"] isfile_mock.return_value = True modules_store = ModulesStore(Attack) module_names = modules_store._get_module_names("") assert len(module_names) == 2 assert module_names == ["test", "tost"]
def test_remove(): sys.path.insert(0, "attacks") module = __import__("terminate_ec2_instance") modules_store = ModulesStore(Attack) modules_store.add(module) modules_store.remove("terminate_ec2_instance") assert len(modules_store.list()) == 0
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 test_ref_to_obj(): modules_store = ModulesStore(Attack) modules_store.add(module_attack) obj = modules_store._ref_to_obj("chaosmonkey.attacks.attack:Attack") assert obj is Attack
def test_load_path_with_no_modules(get_module_names_mock): get_module_names_mock.return_value = ["InvalidModule"] modules = ModulesStore(Attack) with pytest.raises(ValueError): modules.load("/tmp")
def test_load_raise_exception_for_invalid_path(): modules = ModulesStore(Attack) with pytest.raises(ValueError): modules.load("")