Skip to content

test: increase coverage of unit tests generator classes #1659

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

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class AlertActionsConf(ConfGenerator):
def _set_attributes(self, **kwargs: Any) -> None:
self.conf_file = "alert_actions.conf"
self.conf_spec_file = f"{self.conf_file}.spec"
if self._global_config is None:
return

envs = normalize.normalize(
self._global_config.alerts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2090,7 +2090,7 @@
"meta": {
"name": "Splunk_TA_UCCExample",
"restRoot": "splunk_ta_uccexample",
"version": "5.60.0+34ad0cb7a",
"version": "5.60.0+19f2276ad",
"displayName": "Splunk UCC test Add-on",
"schemaVersion": "0.0.9",
"supportedThemes": [
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ def global_config_single_authentication() -> global_config_lib.GlobalConfig:
return global_config


@pytest.fixture
def global_config_with_oauth_account() -> global_config_lib.GlobalConfig:
global_config_path = helpers.get_testdata_file_path("valid_config_expand.json")
global_config = global_config_lib.GlobalConfig.from_file(global_config_path)
return global_config


@pytest.fixture
def global_config_with_with_one_entity_per_input() -> global_config_lib.GlobalConfig:
global_config_path = helpers.get_testdata_file_path(
"valid_config_with_one_entity_per_input.json"
)
global_config = global_config_lib.GlobalConfig.from_file(global_config_path)
return global_config


@pytest.fixture()
def os_dependent_library_config():
return lambda name="lib1", python_version="37", target="t", os="os": OSDependentLibraryConfig(
Expand Down
90 changes: 36 additions & 54 deletions tests/unit/generators/conf_files/test_create_account_conf_spec.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,32 @@
from pytest import fixture
from unittest.mock import patch, MagicMock
from splunk_add_on_ucc_framework.generators.conf_files import AccountConf
from splunk_add_on_ucc_framework.global_config import GlobalConfig
from tests.unit.helpers import get_testdata_file_path

TA_NAME = "test_addon"


@fixture
def global_config():
gc = GlobalConfig.from_file(get_testdata_file_path("valid_config.json"))
gc._content["meta"]["restRoot"] = TA_NAME
return gc


def test_set_attributes(global_config, input_dir, output_dir, ucc_dir, ta_name):
def test_set_attributes(
global_config_with_with_one_entity_per_input,
input_dir,
output_dir,
ucc_dir,
ta_name,
):
"""Test when _global_config has mixed accounts (some 'oauth', some not)."""
account_spec = AccountConf(
global_config, input_dir, output_dir, ucc_dir=ucc_dir, addon_name=ta_name
)
account_spec._global_config = MagicMock()
account_spec._gc_schema = MagicMock()

# Mock the global config and schema behaviors
account_spec._global_config.configs = [
{"name": "oauth", "entity": "entity1"},
{"name": "non_oauth", "entity": "entity2"},
]
account_spec._global_config.namespace = TA_NAME
account_spec._gc_schema._get_oauth_enitities.return_value = "mocked_content"
account_spec._gc_schema._parse_fields.return_value = (
[MagicMock(_name="field2")],
[MagicMock(_name="field3")],
global_config_with_with_one_entity_per_input,
input_dir,
output_dir,
ucc_dir=ucc_dir,
addon_name=ta_name,
)

account_spec._set_attributes()

# Only the non-oauth account should be processed
assert account_spec.account_fields == [("<name>", ["field2 = "])]
assert account_spec.account_fields == [
("<name>", ["custom_endpoint = ", "endpoint = ", "account_checkbox = "])
]
assert (
account_spec.conf_spec_file
== f"{global_config.namespace.lower()}_account.conf.spec"
== f"{global_config_with_with_one_entity_per_input.namespace.lower()}_account.conf.spec"
)


Expand All @@ -56,23 +42,22 @@ def test_set_attributes_conf_only_TA(
addon_name=ta_name,
)

account_spec._set_attributes()

assert account_spec.account_fields == []


def test_set_attributes_with_oauth_account(
global_config, input_dir, output_dir, ucc_dir, ta_name
global_config_with_oauth_account, input_dir, output_dir, ucc_dir, ta_name
):
"""Test when _global_config has an account with name 'oauth'."""
# need to expand the global config for logging tab
global_config_with_oauth_account.expand()
account_spec = AccountConf(
global_config, input_dir, output_dir, ucc_dir=ucc_dir, addon_name=ta_name
global_config_with_oauth_account,
input_dir,
output_dir,
ucc_dir=ucc_dir,
addon_name=ta_name,
)
account_spec._global_config = MagicMock()

account_spec._global_config.configs = [{"name": "oauth", "entity": "entity1"}]

account_spec._set_attributes()

# Since 'oauth' should be skipped, account_fields should remain empty
assert account_spec.account_fields == []
Expand All @@ -81,34 +66,31 @@ def test_set_attributes_with_oauth_account(
@patch(
"splunk_add_on_ucc_framework.generators.conf_files.AccountConf.set_template_and_render"
)
@patch(
"splunk_add_on_ucc_framework.generators.conf_files.AccountConf.get_file_output_path"
)
def test_generate_conf_spec(
mock_op_path, mock_template, global_config, input_dir, output_dir, ucc_dir, ta_name
mock_template,
global_config_with_with_one_entity_per_input,
input_dir,
output_dir,
ucc_dir,
ta_name,
):
content = "content"
exp_fname = f"{ta_name}_account.conf.spec"
file_path = "output_path/ta_name_account.conf.spec"
mock_op_path.return_value = file_path
mock_template_render = MagicMock()
mock_template_render.render.return_value = content

account_spec = AccountConf(
global_config, input_dir, output_dir, ucc_dir=ucc_dir, addon_name=ta_name
global_config_with_with_one_entity_per_input,
input_dir,
output_dir,
ucc_dir=ucc_dir,
addon_name=ta_name,
)
account_spec.writer = MagicMock()
account_spec._template = mock_template_render

file_paths = account_spec.generate_conf_spec()
assert mock_op_path.call_count == 1
assert mock_template.call_count == 1
account_spec.writer.assert_called_once_with(
file_name=exp_fname,
file_path=file_path,
content=content,
)
assert file_paths == {exp_fname: file_path}
assert file_paths == {exp_fname: f"{output_dir}/{ta_name}/README/{exp_fname}"}


def test_generate_conf_spec_no_configuration(
Expand Down
Loading
Loading