def test_reload_package_w_playbook( fx_copy_fn_main_mock_integration_w_playbooks, fx_get_sub_parser, fx_cmd_line_args_codegen_reload): output_path = os.path.join(mock_paths.TEST_TEMP_DIR, "mock_path", "fn_main_mock_integration-1.1.0") mock_integration_name = fx_copy_fn_main_mock_integration_w_playbooks[0] shutil.move(fx_copy_fn_main_mock_integration_w_playbooks[1], output_path) # Replace cmd line arg "fn_main_mock_integration" with path to temp dir location sys.argv[sys.argv.index(mock_integration_name)] = output_path # Add path to a mock export.res file sys.argv.extend(["-e", mock_paths.MOCK_RELOAD_EXPORT_RES_W_PLAYBOOK]) cmd_codegen = CmdCodegen(fx_get_sub_parser) args = cmd_codegen.parser.parse_known_args()[0] path_package_reloaded = cmd_codegen._reload_package(args) import_definition = package_helpers.get_import_definition_from_customize_py( os.path.join(path_package_reloaded, mock_integration_name, package_helpers.PATH_CUSTOMIZE_PY)) res_objs = sdk_helpers.get_from_export( import_definition, rules=["Additional Mock Rule", "Mock Manual Rule"], playbooks=["main_mock_playbook"]) general_test_package_structure(mock_integration_name, path_package_reloaded) assert helpers.verify_expected_list( ["Additional Mock Rule", "Mock Manual Rule"], [o.get("x_api_name") for o in res_objs.get("rules")]) assert helpers.verify_expected_list( ["main_mock_playbook"], [o.get("x_api_name") for o in res_objs.get("playbooks")])
def test_execute_command_with_validate_enabled( fx_copy_and_pip_install_fn_main_mock_integration, fx_get_sub_parser, fx_cmd_line_args_package, fx_add_dev_env_var): mock_integration_name = fx_copy_and_pip_install_fn_main_mock_integration[0] path_fn_main_mock_integration = fx_copy_and_pip_install_fn_main_mock_integration[ 1] # Replace cmd line arg "fn_main_mock_integration" with path to temp dir location sys.argv[sys.argv.index( mock_integration_name)] = path_fn_main_mock_integration sys.argv.append("--no-samples") sys.argv.append("--validate") with patch("resilient_sdk.cmds.validate.sdk_helpers.run_subprocess" ) as mock_process: mock_process.return_value = (0, "Done!") # Package the app cmd_validate = CmdValidate(fx_get_sub_parser) cmd_package = CmdPackage(fx_get_sub_parser, cmd_validate) args = cmd_package.parser.parse_known_args()[0] path_the_app_zip = cmd_package.execute_command(args) # Test app.zip contents assert zipfile.is_zipfile(path_the_app_zip) with zipfile.ZipFile((path_the_app_zip), 'r') as app_zip: assert helpers.verify_expected_list(EXPECTED_FILES_APP_ZIP, app_zip.namelist()) # Test app.zip/validate_report.md contents validate_report_contents = sdk_helpers.read_zip_file( path_the_app_zip, "validate_report.md") assert "## App Details" in validate_report_contents assert "## `setup.py` file validation" in validate_report_contents assert "## Package files validation" in validate_report_contents
def test_execute_command_no_samples(fx_copy_fn_main_mock_integration, fx_get_sub_parser, fx_cmd_line_args_package): mock_integration_name = fx_copy_fn_main_mock_integration[0] path_fn_main_mock_integration = fx_copy_fn_main_mock_integration[1] # Replace cmd line arg "fn_main_mock_integration" with path to temp dir location sys.argv[sys.argv.index( mock_integration_name)] = path_fn_main_mock_integration # Append --no-samples command line arg sys.argv.append("--no-samples") # Package the app cmd_package = CmdPackage(fx_get_sub_parser) args = cmd_package.parser.parse_known_args()[0] path_the_app_zip = cmd_package.execute_command(args) # Test app.zip contents assert zipfile.is_zipfile(path_the_app_zip) with zipfile.ZipFile((path_the_app_zip), 'r') as app_zip: assert helpers.verify_expected_list(EXPECTED_FILES_APP_ZIP[:-1], app_zip.namelist()) # Test app.zip/app.json contents app_json_contents = sdk_helpers.read_zip_file(path_the_app_zip, "app.json") mock_app_json_contents = sdk_helpers.read_file( mock_paths.MOCK_APP_ZIP_APP_JSON)[0] assert app_json_contents == mock_app_json_contents # Test app.zip/export.res contents export_res_contents = sdk_helpers.read_zip_file(path_the_app_zip, "export.res") mock_export_res_contents = sdk_helpers.read_file( mock_paths.MOCK_APP_ZIP_EXPORT_RES)[0] assert json.loads(export_res_contents) == json.loads( mock_export_res_contents)
def general_test_package_structure(package_name, package_path): """ This is a general function that the tests for gen_package and reload_package call to make sure that the expected files are created in each directory """ assert helpers.verify_expected_list(EXPECTED_FILES_ROOT_DIR, os.listdir(package_path)) assert helpers.verify_expected_list( EXPECTED_FILES_DATA_DIR, os.listdir(os.path.join(package_path, "data"))) assert helpers.verify_expected_list( EXPECTED_FILES_DOC_DIR, os.listdir(os.path.join(package_path, "doc"))) assert helpers.verify_expected_list( EXPECTED_FILES_DOC_SCREENSHOTS_DIR, os.listdir(os.path.join(package_path, "doc", "screenshots"))) assert helpers.verify_expected_list( EXPECTED_FILES_PACKAGE_DIR, os.listdir(os.path.join(package_path, package_name))) assert helpers.verify_expected_list( EXPECTED_FILES_PACKAGE_COMPONENTS_DIR, os.listdir(os.path.join(package_path, package_name, "components"))) assert helpers.verify_expected_list( EXPECTED_FILES_PACKAGE_UTIL_DIR, os.listdir(os.path.join(package_path, package_name, "util"))) assert helpers.verify_expected_list( EXPECTED_FILES_PACKAGE_UTIL_DATA_DIR, os.listdir(os.path.join(package_path, package_name, "util", "data"))) assert helpers.verify_expected_list( EXPECTED_FILES_ICONS_DIR, os.listdir(os.path.join(package_path, "icons"))) assert helpers.verify_expected_list( EXPECTED_FILES_TESTS_DIR, os.listdir(os.path.join(package_path, "tests"))) # Test payload_samples were generated for each fn files_in_payload_samples = sorted( os.listdir(os.path.join(package_path, "payload_samples"))) assert helpers.verify_expected_list(EXPECTED_FILES_PAYLOAD_SAMPLES_DIR, files_in_payload_samples) for file_name in files_in_payload_samples: if sdk_helpers.is_env_var_set(constants.ENV_VAR_DEV): assert helpers.verify_expected_list( EXPECTED_FILES_PAYLOAD_SAMPLES_FN_NAME_DIR_DEV, os.listdir( os.path.join(package_path, "payload_samples", file_name))) else: assert helpers.verify_expected_list( EXPECTED_FILES_PAYLOAD_SAMPLES_FN_NAME_DIR, os.listdir( os.path.join(package_path, "payload_samples", file_name)))
def test_reload_package(fx_copy_fn_main_mock_integration, fx_get_sub_parser, fx_cmd_line_args_codegen_reload): """ This tests that when a package is reloaded with codegen --reload that each of the EXPECTED_FILES exist and also the additional 'Additional Mock Rule' and its related Workflow which has a Function is also added to the package """ output_path = os.path.join(mock_paths.TEST_TEMP_DIR, "mock_path", "fn_main_mock_integration-1.1.0") mock_integration_name = fx_copy_fn_main_mock_integration[0] shutil.move(fx_copy_fn_main_mock_integration[1], output_path) # Replace cmd line arg "fn_main_mock_integration" with path to temp dir location sys.argv[sys.argv.index(mock_integration_name)] = output_path # Add path to a mock export.res file sys.argv.extend(["-e", mock_paths.MOCK_RELOAD_EXPORT_RES]) cmd_codegen = CmdCodegen(fx_get_sub_parser) args = cmd_codegen.parser.parse_known_args()[0] path_package_reloaded = cmd_codegen._reload_package(args) # This is really getting the import definition from the new data/export.res file, so tests that as well import_definition = package_helpers.get_import_definition_from_customize_py( os.path.join(path_package_reloaded, mock_integration_name, package_helpers.PATH_CUSTOMIZE_PY)) res_objs = sdk_helpers.get_from_export( import_definition, rules=["Additional Mock Rule", "Mock Manual Rule"], functions=[ "additional_mock_function", "mock_function_one", "funct_new_mock_function", "func_new_mock_function" ], workflows=[ "additional_mock_workflow", "mock_workflow_one", "wf_new_mock_workflow" ]) # Assert the general structure of the reloaded package general_test_package_structure(mock_integration_name, path_package_reloaded) # Assert the additional rule, function and workflow were added assert helpers.verify_expected_list( ["Additional Mock Rule", "Mock Manual Rule"], [o.get("x_api_name") for o in res_objs.get("rules")]) assert helpers.verify_expected_list([ "additional_mock_function", "mock_function_one", "funct_new_mock_function", "func_new_mock_function" ], [o.get("x_api_name") for o in res_objs.get("functions")]) assert helpers.verify_expected_list([ "additional_mock_workflow", "mock_workflow_one", "wf_new_mock_workflow" ], [o.get("x_api_name") for o in res_objs.get("workflows")]) # Assert a new components file is created expected_component_files = EXPECTED_FILES_PACKAGE_COMPONENTS_DIR + [ "funct_additional_mock_function.py" ] assert helpers.verify_expected_list( expected_component_files, os.listdir( os.path.join(path_package_reloaded, mock_integration_name, "components"))) # Assert a new components file with prefix 'funct_' is created expected_component_files = ["funct_new_mock_function.py"] assert helpers.verify_expected_list( expected_component_files, os.listdir( os.path.join(path_package_reloaded, mock_integration_name, "components"))) # Assert a new components file with prefix 'func_' is created expected_component_files = ["func_new_mock_function.py"] assert helpers.verify_expected_list( expected_component_files, os.listdir( os.path.join(path_package_reloaded, mock_integration_name, "components"))) # Assert a new tests file is created expected_test_files = EXPECTED_FILES_TESTS_DIR + [ "test_funct_additional_mock_function.py" ] assert helpers.verify_expected_list( expected_test_files, os.listdir(os.path.join(path_package_reloaded, "tests"))) # Assert a new tests file including 'func_' is created. expected_test_files = ["test_func_new_mock_function.py"] assert helpers.verify_expected_list( expected_test_files, os.listdir(os.path.join(path_package_reloaded, "tests"))) # Assert a new md file is created in data dir expected_workflow_files = EXPECTED_FILES_DATA_DIR + [ "wf_additional_mock_workflow.md" ] assert helpers.verify_expected_list( expected_workflow_files, os.listdir(os.path.join(path_package_reloaded, "data"))) # Assert a new md file with 'wf_' is created in data dir expected_workflow_files = ["wf_new_mock_workflow.md"] assert helpers.verify_expected_list( expected_workflow_files, os.listdir(os.path.join(path_package_reloaded, "data"))) # Remove files from generated package path and recreate without prefix or substring of 'funct_' or 'wf_'. os.remove( os.path.join(path_package_reloaded, mock_integration_name, "components", "funct_additional_mock_function.py")) Path( os.path.join(path_package_reloaded, mock_integration_name, "components", "additional_mock_function.py")).touch() os.remove( os.path.join(path_package_reloaded, "tests", "test_funct_additional_mock_function.py")) Path( os.path.join(path_package_reloaded, "tests", "test_additional_mock_function.py")).touch() os.remove( os.path.join(path_package_reloaded, "data", "wf_additional_mock_workflow.md")) Path( os.path.join(path_package_reloaded, "data", "additional_mock_workflow.md")).touch() # Get modification time for workflow file "wf_mock_workflow_one.md" in seconds since the epoch.' wf_modified_time = os.path.getmtime( os.path.join(path_package_reloaded, "data", "wf_mock_workflow_one.md")) # Perform another test reload. cmd_codegen = CmdCodegen(fx_get_sub_parser) args = cmd_codegen.parser.parse_known_args()[0] path_package_reloaded = cmd_codegen._reload_package(args) # This is really getting the import definition from the new data/export.res file, so tests that as well import_definition = package_helpers.get_import_definition_from_customize_py( os.path.join(path_package_reloaded, mock_integration_name, package_helpers.PATH_CUSTOMIZE_PY)) res_objs = sdk_helpers.get_from_export( import_definition, rules=["Additional Mock Rule", "Mock Manual Rule"], functions=[ "additional_mock_function", "mock_function_one", "funct_new_mock_function", "func_new_mock_function" ], workflows=[ "additional_mock_workflow", "mock_workflow_one", "wf_new_mock_workflow" ]) # Assert the general structure of the reloaded package general_test_package_structure(mock_integration_name, path_package_reloaded) # Assert a new components file with 'funct_' prefix is not created expected_component_files = ["funct_additional_mock_function.py"] assert not helpers.verify_expected_list( expected_component_files, os.listdir( os.path.join(path_package_reloaded, mock_integration_name, "components"))) # Assert a new workflow file with 'md_' prefix is not created in data dir expected_workflow_files = ["wf_additional_mock_workflow.md"] assert not helpers.verify_expected_list( expected_workflow_files, os.listdir(os.path.join(path_package_reloaded, "data"))) # Assert a new tests file with "funct_" substring is not created expected_test_files = ["test_func_additional_mock_function.py"] assert not helpers.verify_expected_list( expected_test_files, os.listdir(os.path.join(path_package_reloaded, "tests"))) # Get new modification time for test workflow file. new_wf_modified_time = os.path.getmtime( os.path.join(path_package_reloaded, "data", "wf_mock_workflow_one.md")) # Assert modification time of workflow has been updated. assert new_wf_modified_time > wf_modified_time