Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generated_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The following table describes the files generated by UCC framework.
| alert_actions.conf | output/<YOUR_ADDON_NAME>/default | Generates `alert_actions.conf` and `alert_actions.conf.spec` file for the custom alert actions defined in globalConfig |
| eventtypes.conf | output/<YOUR_ADDON_NAME>/default | Generates `eventtypes.conf` file if the sourcetype is mentioned in Adaptive Response of custom alert action in globalConfig |
| tags.conf | output/<YOUR_ADDON_NAME>/default | Generates `tags.conf` file based on the `eventtypes.conf` created for custom alert actions. |
| searchbnf.conf | output/<YOUR_ADDON_NAME>/default | Generates `searchbnf.conf` for custom search commands provided in the globalConfig. |
| _account.conf | output/&lt;YOUR_ADDON_NAME&gt;/README | Generates `<YOUR_ADDON_NAME>_account.conf.spec` file for the configuration mentioned in globalConfig |
| _settings.conf | output/&lt;YOUR_ADDON_NAME&gt;/README | Generates `<YOUR_ADDON_NAME>_settings.conf.spec` file for the Proxy, Logging or Custom Tab mentioned in globalConfig |
| configuration.xml | output/&lt;YOUR_ADDON_NAME&gt;/default/data/ui/views | Generates configuration.xml file in `default/data/ui/views/` folder if configuration is defined in globalConfig. |
Expand Down
2 changes: 2 additions & 0 deletions splunk_add_on_ucc_framework/generators/conf_files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from .create_web_conf import WebConf
from .create_account_conf import AccountConf
from .create_settings_conf import SettingsConf
from .create_searchbnf_conf import SearchbnfConf

__all__ = [
"FileGenerator",
Expand All @@ -37,4 +38,5 @@
"InputsConf",
"AccountConf",
"SettingsConf",
"SearchbnfConf",
]
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}
2 changes: 2 additions & 0 deletions splunk_add_on_ucc_framework/generators/file_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
WebConf,
AccountConf,
SettingsConf,
SearchbnfConf,
)

__all__ = ["FileClass", "GEN_FILE_LIST"]
Expand All @@ -59,6 +60,7 @@ class FileClass(NamedTuple):
),
FileClass("eventtypes.conf", EventtypesConf, ["default"]),
FileClass("tags.conf", TagsConf, ["default"]),
FileClass("searchbnf.conf", SearchbnfConf, ["default"]),
FileClass("_account.conf", AccountConf, ["README"]),
FileClass("_settings.conf", SettingsConf, ["README"]),
FileClass(
Expand Down
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 -%}
1 change: 1 addition & 0 deletions tests/smoke/test_ucc_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def test_ucc_generate_with_everything(caplog):
("default", "splunk_ta_uccexample_settings.conf"),
("default", "web.conf"),
("default", "server.conf"),
("default", "searchbnf.conf"),
("default", "data", "ui", "alerts", "test_alert.html"),
("default", "data", "ui", "nav", "default.xml"),
("default", "data", "ui", "views", "configuration.xml"),
Expand Down
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
2 changes: 2 additions & 0 deletions tests/unit/generators/conf_files/test_conf_files_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def test___init__conf():
"InputsConf",
"AccountConf",
"SettingsConf",
"SearchbnfConf",
]
expected_modules = [
"file_generator",
Expand All @@ -24,6 +25,7 @@ def test___init__conf():
"create_web_conf",
"create_account_conf",
"create_settings_conf",
"create_searchbnf_conf",
]
import splunk_add_on_ucc_framework.generators.conf_files as conf

Expand Down
117 changes: 117 additions & 0 deletions tests/unit/generators/conf_files/test_create_searchbnf_conf.py
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 == []
Copy link
Contributor

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

Copy link
Contributor Author

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.



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
1 change: 1 addition & 0 deletions tests/unit/generators/test_doc_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_generate_docs():
| alert_actions.conf | output/&lt;YOUR_ADDON_NAME&gt;/default | Generates `alert_actions.conf` and `alert_actions.conf.spec` file for the custom alert actions defined in globalConfig |
| eventtypes.conf | output/&lt;YOUR_ADDON_NAME&gt;/default | Generates `eventtypes.conf` file if the sourcetype is mentioned in Adaptive Response of custom alert action in globalConfig |
| tags.conf | output/&lt;YOUR_ADDON_NAME&gt;/default | Generates `tags.conf` file based on the `eventtypes.conf` created for custom alert actions. |
| searchbnf.conf | output/&lt;YOUR_ADDON_NAME&gt;/default | Generates `searchbnf.conf` for custom search commands provided in the globalConfig. |
| _account.conf | output/&lt;YOUR_ADDON_NAME&gt;/README | Generates `<YOUR_ADDON_NAME>_account.conf.spec` file for the configuration mentioned in globalConfig |
| _settings.conf | output/&lt;YOUR_ADDON_NAME&gt;/README | Generates `<YOUR_ADDON_NAME>_settings.conf.spec` file for the Proxy, Logging or Custom Tab mentioned in globalConfig |
| configuration.xml | output/&lt;YOUR_ADDON_NAME&gt;/default/data/ui/views | Generates configuration.xml file in `default/data/ui/views/` folder if configuration is defined in globalConfig. |
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def test_get_j2_env():
"conf_files/settings_conf.template",
"conf_files/tags_conf.template",
"web_conf.template",
"conf_files/searchbnf_conf.template",
]
assert sorted(expected_list_of_templates) == sorted(list_of_templates)

Expand Down
Loading