From f05c064134e79a8a77ed6a3d267c5891781b4ecd Mon Sep 17 00:00:00 2001 From: eric-forte-elastic Date: Wed, 15 May 2024 20:38:42 -0400 Subject: [PATCH 1/3] Add flag to enable all prebuilt tests --- detection_rules/custom_rules.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/detection_rules/custom_rules.py b/detection_rules/custom_rules.py index 91ee5e6758a..5f96d20fd34 100644 --- a/detection_rules/custom_rules.py +++ b/detection_rules/custom_rules.py @@ -43,23 +43,34 @@ def create_config_content() -> str: return yaml.safe_dump(config_content, default_flow_style=False) -def create_test_config_content() -> str: +def create_test_config_content(enable_prebuilt_tests: bool) -> str: """Generate the content for the test_config.yaml with special content and references.""" + comment_char = '#' if enable_prebuilt_tests else '' example_test_config_path = DEFAULT_CONFIG_PATH.parent.joinpath("example_test_config.yaml") - content = f"# For more details, refer to the example configuration:\n# {example_test_config_path}\n" \ - "# Define tests to explicitly bypass, with all others being run.\n" \ - "# To run all tests, set bypass to empty or leave this file commented out.\n\n" \ - "unit_tests:\n bypass:\n# - tests.test_all_rules.TestValidRules.test_schema_and_dupes\n" \ - "# - tests.test_packages.TestRegistryPackage.test_registry_package_config\n" + + lines = [ + "# For more details, refer to the example configuration:", + f"# {example_test_config_path}", + "# Define tests to explicitly bypass, with all others being run.", + "# To run all tests, set bypass to empty or leave this file commented out.", + "", + "unit_tests:", + " bypass:", + f"{comment_char} - tests.test_gh_workflows.TestWorkflows.test_matrix_to_lock_version_defaults", + f"{comment_char} - tests.test_schemas.TestVersionLockSchema.test_version_lock_has_nested_previous", + ] - return content + return '\n'.join(lines) @custom_rules.command('setup-config') @click.argument('directory', type=Path) @click.argument('kibana-version', type=str, default=load_etc_dump('packages.yaml')['package']['name']) @click.option('--overwrite', is_flag=True, help="Overwrite the existing _config.yaml file.") -def setup_config(directory: Path, kibana_version: str, overwrite: bool): +@click.option( + "--enable-prebuilt-tests", "-e", is_flag=True, help="Enable all prebuilt tests instead of default subset." +) +def setup_config(directory: Path, kibana_version: str, overwrite: bool, enable_prebuilt_tests: bool): """Setup the custom rules configuration directory and files with defaults.""" config = directory / '_config.yaml' @@ -111,7 +122,7 @@ def setup_config(directory: Path, kibana_version: str, overwrite: bool): package_config.write_text(yaml.safe_dump(package_content, default_flow_style=False)) # Create and configure test_config.yaml - test_config.write_text(create_test_config_content()) + test_config.write_text(create_test_config_content(enable_prebuilt_tests)) # Create and configure _config.yaml config.write_text(create_config_content()) From c495a629d2a5cb7765392f33d8ae6faa61e45b1f Mon Sep 17 00:00:00 2001 From: eric-forte-elastic Date: Wed, 15 May 2024 23:25:07 -0400 Subject: [PATCH 2/3] Add additional bypasses --- detection_rules/custom_rules.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/detection_rules/custom_rules.py b/detection_rules/custom_rules.py index 5f96d20fd34..ccdeca3a150 100644 --- a/detection_rules/custom_rules.py +++ b/detection_rules/custom_rules.py @@ -47,7 +47,7 @@ def create_test_config_content(enable_prebuilt_tests: bool) -> str: """Generate the content for the test_config.yaml with special content and references.""" comment_char = '#' if enable_prebuilt_tests else '' example_test_config_path = DEFAULT_CONFIG_PATH.parent.joinpath("example_test_config.yaml") - + lines = [ "# For more details, refer to the example configuration:", f"# {example_test_config_path}", @@ -58,6 +58,8 @@ def create_test_config_content(enable_prebuilt_tests: bool) -> str: " bypass:", f"{comment_char} - tests.test_gh_workflows.TestWorkflows.test_matrix_to_lock_version_defaults", f"{comment_char} - tests.test_schemas.TestVersionLockSchema.test_version_lock_has_nested_previous", + f"{comment_char} - tests.test_packages.TestRegistryPackage.test_registry_package_config", + f"{comment_char} - tests.test_all_rules.TestValidRules.test_schema_and_dupes", ] return '\n'.join(lines) From c137b0e52baebf214c8a72cfc2d8d2596c4de6c8 Mon Sep 17 00:00:00 2001 From: eric-forte-elastic Date: Thu, 16 May 2024 13:28:26 -0400 Subject: [PATCH 3/3] Added format_test_string function --- detection_rules/custom_rules.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/detection_rules/custom_rules.py b/detection_rules/custom_rules.py index ccdeca3a150..3b74be98790 100644 --- a/detection_rules/custom_rules.py +++ b/detection_rules/custom_rules.py @@ -45,7 +45,12 @@ def create_config_content() -> str: def create_test_config_content(enable_prebuilt_tests: bool) -> str: """Generate the content for the test_config.yaml with special content and references.""" - comment_char = '#' if enable_prebuilt_tests else '' + + def format_test_string(test_string: str, comment_char: str) -> str: + """Generate a yaml formatted string with a comment character.""" + return f"{comment_char} - {test_string}" + + comment_char = "#" if enable_prebuilt_tests else "" example_test_config_path = DEFAULT_CONFIG_PATH.parent.joinpath("example_test_config.yaml") lines = [ @@ -56,13 +61,15 @@ def create_test_config_content(enable_prebuilt_tests: bool) -> str: "", "unit_tests:", " bypass:", - f"{comment_char} - tests.test_gh_workflows.TestWorkflows.test_matrix_to_lock_version_defaults", - f"{comment_char} - tests.test_schemas.TestVersionLockSchema.test_version_lock_has_nested_previous", - f"{comment_char} - tests.test_packages.TestRegistryPackage.test_registry_package_config", - f"{comment_char} - tests.test_all_rules.TestValidRules.test_schema_and_dupes", + format_test_string("tests.test_gh_workflows.TestWorkflows.test_matrix_to_lock_version_defaults", comment_char), + format_test_string( + "tests.test_schemas.TestVersionLockSchema.test_version_lock_has_nested_previous", comment_char + ), + format_test_string("tests.test_packages.TestRegistryPackage.test_registry_package_config", comment_char), + format_test_string("tests.test_all_rules.TestValidRules.test_schema_and_dupes", comment_char), ] - return '\n'.join(lines) + return "\n".join(lines) @custom_rules.command('setup-config')