-
Notifications
You must be signed in to change notification settings - Fork 35
feat: generation of searchbnf.conf
#1695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hetangmodi-crest
merged 6 commits into
feat/add-validations-for-csc
from
feat/generation-of-searchbnf.conf
May 9, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fa731fc
feat: generation of searchbnf.conf
hetangmodi-crest f13fde5
tests: added respective unit and smoke test case
hetangmodi-crest 5770fa0
chore: updated syntax for csc in valid_config.json
hetangmodi-crest 3639353
Merge branch 'feat/add-validations-for-csc' into feat/generation-of-s…
hetangmodi-crest 14be2cd
chore: return empty dict
hetangmodi-crest 36c7a6f
Merge branch 'feat/add-validations-for-csc' into feat/generation-of-s…
hetangmodi-crest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
splunk_add_on_ucc_framework/generators/conf_files/create_searchbnf_conf.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # | ||
| # Copyright 2025 Splunk Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| from typing import Any, Dict | ||
|
|
||
| from splunk_add_on_ucc_framework.generators.file_generator import FileGenerator | ||
|
|
||
|
|
||
| class SearchbnfConf(FileGenerator): | ||
| __description__ = "Generates `searchbnf.conf` for custom search commands provided in the globalConfig." | ||
|
|
||
| def _set_attributes(self, **kwargs: Any) -> None: | ||
| self.conf_file = "searchbnf.conf" | ||
| self.searchbnf_info = [] | ||
| if self._global_config.has_custom_search_commands(): | ||
| for command in self._global_config.custom_search_commands: | ||
| if command.get("requiredSearchAssistant", False): | ||
| searchbnf_dict = { | ||
| "command_name": command["commandName"], | ||
| "description": command["description"], | ||
| "syntax": command["syntax"], | ||
| "usage": command["usage"], | ||
| } | ||
| self.searchbnf_info.append(searchbnf_dict) | ||
|
|
||
| def generate(self) -> Dict[str, str]: | ||
| if not self.searchbnf_info: | ||
| return {} | ||
|
|
||
| file_path = self.get_file_output_path(["default", self.conf_file]) | ||
| self.set_template_and_render( | ||
| template_file_path=["conf_files"], file_name="searchbnf_conf.template" | ||
| ) | ||
| rendered_content = self._template.render( | ||
| searchbnf_info=self.searchbnf_info, | ||
| ) | ||
| self.writer( | ||
| file_name=self.conf_file, | ||
| file_path=file_path, | ||
| content=rendered_content, | ||
| ) | ||
| return {self.conf_file: file_path} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
splunk_add_on_ucc_framework/templates/conf_files/searchbnf_conf.template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| {% for info in searchbnf_info -%} | ||
| [{{info["command_name"]}}] | ||
| syntax = {{info["syntax"]}} | ||
| description = {{info["description"]}} | ||
| usage = {{info["usage"]}} | ||
| {% endfor -%} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...dons/expected_output_global_config_everything/Splunk_TA_UCCExample/default/searchbnf.conf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| [generatetextcommand] | ||
| syntax = generatetextcommand count=<event_count> text=<string> | ||
| description = This command generates COUNT occurrences of a TEXT string. | ||
| usage = public | ||
|
|
||
| [filtercommand] | ||
| syntax = | filtercommand contains='value1' replace='value to be replaced,value to replace with' | ||
| description = It filters records from the events stream returning only those which has :code:`contains` in them and replaces :code:`replace_array[0]` with :code:`replace_array[1]`. | ||
| usage = public |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
tests/unit/generators/conf_files/test_create_searchbnf_conf.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| from pytest import fixture | ||
| from splunk_add_on_ucc_framework.generators.conf_files import SearchbnfConf | ||
| from splunk_add_on_ucc_framework import __file__ as ucc_framework_file | ||
| import os.path | ||
| from textwrap import dedent | ||
|
|
||
| UCC_DIR = os.path.dirname(ucc_framework_file) | ||
|
|
||
|
|
||
| @fixture | ||
| def custom_search_command_without_search_assistance(): | ||
| return [ | ||
| { | ||
| "commandName": "testcommand2", | ||
| "commandType": "streaming", | ||
| "fileName": "test2.py", | ||
| } | ||
| ] | ||
|
|
||
|
|
||
| def test_set_attributes_without_custom_command( | ||
| global_config_only_configuration, input_dir, output_dir, ucc_dir, ta_name | ||
| ): | ||
| searchbnf_conf = SearchbnfConf( | ||
| global_config_only_configuration, | ||
| input_dir, | ||
| output_dir, | ||
| ucc_dir=ucc_dir, | ||
| addon_name=ta_name, | ||
| ) | ||
| assert searchbnf_conf.searchbnf_info == [] | ||
|
|
||
|
|
||
| def test_set_attributes( | ||
| global_config_all_json, input_dir, output_dir, ucc_dir, ta_name | ||
| ): | ||
| searchbnf_conf = SearchbnfConf( | ||
| global_config_all_json, | ||
| input_dir, | ||
| output_dir, | ||
| ucc_dir=ucc_dir, | ||
| addon_name=ta_name, | ||
| ) | ||
| assert searchbnf_conf.conf_file == "searchbnf.conf" | ||
| assert searchbnf_conf.searchbnf_info == [ | ||
| { | ||
| "command_name": "generatetextcommand", | ||
| "description": "This command generates COUNT occurrences of a TEXT string.", | ||
| "syntax": "generatetextcommand count=<event_count> text=<string>", | ||
| "usage": "public", | ||
| } | ||
| ] | ||
|
|
||
|
|
||
| def test_set_attributes_without_search_assistance( | ||
| global_config_all_json, | ||
| input_dir, | ||
| output_dir, | ||
| ucc_dir, | ||
| ta_name, | ||
| custom_search_command_without_search_assistance, | ||
| ): | ||
| global_config_all_json._content[ | ||
| "customSearchCommand" | ||
| ] = custom_search_command_without_search_assistance | ||
| searchbnf_conf = SearchbnfConf( | ||
| global_config_all_json, | ||
| input_dir, | ||
| output_dir, | ||
| ucc_dir=ucc_dir, | ||
| addon_name=ta_name, | ||
| ) | ||
| assert searchbnf_conf.searchbnf_info == [] | ||
|
|
||
|
|
||
| def test_generate_conf_without_custom_command( | ||
| global_config_only_configuration, input_dir, output_dir, ucc_dir, ta_name | ||
| ): | ||
| searchbnf_conf = SearchbnfConf( | ||
| global_config_only_configuration, | ||
| input_dir, | ||
| output_dir, | ||
| ucc_dir=ucc_dir, | ||
| addon_name=ta_name, | ||
| ) | ||
| file_paths = searchbnf_conf.generate() | ||
|
|
||
| # Assert that no files are returned since no custom command is configured | ||
| assert file_paths == {} | ||
|
|
||
|
|
||
| def test_generate_conf(global_config_all_json, input_dir, output_dir, ta_name): | ||
| searchbnf_conf = SearchbnfConf( | ||
| global_config_all_json, | ||
| input_dir, | ||
| output_dir, | ||
| ucc_dir=UCC_DIR, | ||
| addon_name=ta_name, | ||
| ) | ||
| file_paths = searchbnf_conf.generate() | ||
| exp_fname = "searchbnf.conf" | ||
|
|
||
| assert file_paths == {exp_fname: f"{output_dir}/{ta_name}/default/{exp_fname}"} | ||
|
|
||
| with open(file_paths["searchbnf.conf"]) as fp: | ||
| content = fp.read() | ||
|
|
||
| expected_content = dedent( | ||
| """ | ||
| [generatetextcommand] | ||
| syntax = generatetextcommand count=<event_count> text=<string> | ||
| description = This command generates COUNT occurrences of a TEXT string. | ||
| usage = public | ||
| """ | ||
| ).lstrip() | ||
|
|
||
| assert content == expected_content | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the last test but TBH I am not sure a test like that is needed. I mean, the last test probably covers the whole class and on the other hand a test like this one here, will fail when the parameter is renamed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case when the searchAssistant isn't provided in globalConfig, we wouldn't be able to test for the file content as it won't be present as the file itself wouldn't be created, and hence, in the test case, I didn't make the
generate()call as we know the file won't be generated. I'll take it up in the other PR for updating tests.