def test_mw_runner_pre_run_check(tmpdir) -> int: """ Purpose: Do a pre run check with the mw_locust-apachekill.json Args: tmpdir: Location of tempdir made by pytest Returns: (Int): 0 if file passed run, -1 if not """ # Create new runner class log_level = logging.INFO try: os.chdir(tmpdir) init_utils.init("test_folder") os.chdir("test_folder") mw_runner = MwRunner(log_level) config = mw_runner.pre_run_check("configs/mw_locust-apachekill.json", "test_run") # test if data_version already exists config2 = mw_runner.pre_run_check("configs/mw_locust-apachekill.json", "test_run") except Exception as error: logging.error(error) pytest.fail("Pre Run check failed") return -1 return 0
def test_mw_runner_pre_run_check_fail(tmpdir) -> int: """ Purpose: Do a pre run check with the mw_locust-apachekill.json Args: tmpdir: Location of tempdir made by pytest Returns: (Int): 0 if file passed run, -1 if not """ # Create new runner class log_level = logging.INFO try: os.chdir(tmpdir) init_utils.init("test_folder") os.chdir("test_folder") mw_runner = MwRunner(log_level) try: config = mw_runner.pre_run_check(None, "test_run") except TypeError as type_error: print(type_error) except Exception as error: pytest.fail("None check failed?") return -1 except Exception as error: logging.error(error) pytest.fail("Pre Run check failed") return -1 return 0
def test_mw_runner_build_docker_compose(tmpdir) -> int: """ Purpose: Do a build_docker_compose with the mw_locust-apachekill.json Args: tmpdir: Location of tempdir made by pytest Returns: (Int): 0 if file passed run, -1 if not """ # Create new runner class log_level = logging.INFO try: os.chdir(tmpdir) init_utils.init("test_folder") os.chdir("test_folder") mw_runner = MwRunner(log_level) config = mw_runner.pre_run_check("configs/mw_locust-apachekill.json", "test_run") run_json, compoents = mw_runner.build_docker_compose(config, log_level) except Exception as error: logging.error(error) pytest.fail("Build docker compose check failed") return -1 return 0
def test_init_folder(tmpdir) -> int: """ Purpose: Test to make sure init creates the required files """ os.chdir(tmpdir) init_utils.init("test_folder") # check if test_foler is empty # Check to make sure all files were created if os.path.exists("test_folder/mw_data_viewer.py"): pytest.fail("File should not be there") return -1 if not os.path.exists("test_folder/data_runs"): pytest.fail("Required file not found") return -1 if not os.path.exists("test_folder/configs"): pytest.fail("Required file not found") return -1 if not os.path.exists("test_folder/.mw_proj"): pytest.fail("Required file not found") return -1 return 0
def test_calibrate_apachekill_weak(tmpdir) -> int: """ Purpose: Test a appachekill calibration run Args: tmpdir: Location of tmpdir made by pytest Returns: (Int): 0 if file passed run, -1 if not """ # Create new runner class log_level = logging.INFO attack = "apachekill" try: os.chdir(tmpdir) init_utils.init("test_folder") os.chdir("test_folder") data_version = "test_run" # Edit the ak config file make_weak_ak() mw_calibrate.calibrate(attack, log_level) except Exception as error: pytest.fail(error) return -1 return 0
def magicwand_run(tmpdir, attack: str, run_config: str) -> int: """ Purpose: Do a test run using passed in config Args: tmpdir: the test tmpdir attack (String): Name of attack run_config (String): Run configuartion Returns: (Int): 0 if file passed run, -1 if not """ # Create new runner class log_level = logging.INFO try: # int magicwand folder os.chdir(tmpdir) logging.info(f"Using {tmpdir}") init_utils.init("test_folder") os.chdir("test_folder") data_version = "test_run" num_runs = 1 make_30_second_test() status = start_runs.run(run_config, num_runs, data_version, log_level) if status != 0: pytest.fail("Start Runs Failed") # check output folder has required csv and json, and pcap files files = glob.glob("data_runs/test_runs/*/*.csv") files_json = glob.glob("data_runs/test_runs/*/*.json") files_pcap = glob.glob("data_runs/test_runs/*/*.pcap") files.extend(files_json) files.extend(files_pcap) for file_path in files: if not is_valid_file(file_path, attack): pytest.fail(file_path + " is not valid file") return -1 return 0 except Exception as error: logging.error(error) pytest.fail("Docker run check failed") return -1
def test_init_folder_exists(tmpdir) -> int: """ Purpose: Test to make sure init exits when folder already exists """ # TODO, use mocks instead of actually creating real dirs. # https://docs.pytest.org/en/stable/tmpdir.html if necessary os.chdir(tmpdir) os.mkdir("test_folder") init_utils.init("test_folder") # check if test_foler is empty if os.path.exists("test_folder/mw_data_viewer.py"): # TODO, are we still copying python files? We should not be copying pytest.fail("File was created in folder that already existed") return -1 return 0
def test_mw_runner_start_stop_docker_containers(tmpdir) -> int: """ Purpose: Do a run with with the mw_locust-apachekill.json Args: tmpdir: Location of tempdir made by pytest Returns: (Int): 0 if file passed run, -1 if not """ # Create new runner class log_level = logging.INFO try: # int magicwand folder os.chdir(tmpdir) init_utils.init("test_folder") os.chdir("test_folder") data_version = "test_run" config_file = "configs/mw_locust-apachekill.json" make_30_second_test() # start magicwand run mw_runner = MwRunner(log_level) config = mw_runner.pre_run_check(config_file, data_version) run_json, compoents = mw_runner.build_docker_compose(config, log_level) counter = 1 mw_runner.start_docker_containers(run_json, config, counter) mw_runner.stop_docker_containers(run_json, counter, data_version) except Exception as error: logging.error(error) pytest.fail("Docker run check failed") return -1 return 0
def init_command(project: str) -> None: """Create and initialize magicwand folder""" """ Purpose: Create and initialize magicwand folder Args: folder (String) - Folder to create Returns: N/A """ # already pevents duplicate inits # No force let them delete their data if init_utils.init(project) != 0: sys.exit(1)