Skip to content

Commit e7e0eeb

Browse files
test: 100% unit test coverage for generators (#1861)
**Issue number:** ADDON-79015 ### PR Type **What kind of change does this PR introduce?** * [ ] Feature * [ ] Bug Fix * [x] Refactoring (no functional or API changes) * [ ] Documentation Update * [ ] Maintenance (dependency updates, CI, etc.) ## Summary ### Changes Added unit test cases for the generator's module to achieve `100% test coverage`. ### User experience No change in user experience. ## Checklist If an item doesn't apply to your changes, leave it unchecked. ### Review * [x] self-review - I have performed a self-review of this change according to the [development guidelines](https://splunk.github.io/addonfactory-ucc-generator/contributing/#development-guidelines) * [ ] Changes are documented. The documentation is understandable, examples work [(more info)](https://splunk.github.io/addonfactory-ucc-generator/contributing/#documentation-guidelines) * [x] PR title and description follows the [contributing principles](https://splunk.github.io/addonfactory-ucc-generator/contributing/#pull-requests) * [ ] meeting - I have scheduled a meeting or recorded a demo to explain these changes (if there is a video, put a link below and in the ticket) ### Tests See [the testing doc](https://splunk.github.io/addonfactory-ucc-generator/contributing/#build-and-test). * [x] Unit - tests have been added/modified to cover the changes * [ ] Smoke - tests have been added/modified to cover the changes * [ ] UI - tests have been added/modified to cover the changes * [x] coverage - I have checked the code coverage of my changes [(see more)](https://splunk.github.io/addonfactory-ucc-generator/contributing/#checking-the-code-coverage) **Demo/meeting:** *Reviewers are encouraged to request meetings or demos if any part of the change is unclear*
1 parent 58f62c3 commit e7e0eeb

File tree

9 files changed

+161
-62
lines changed

9 files changed

+161
-62
lines changed

splunk_add_on_ucc_framework/generators/conf_files/create_alert_actions_conf.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ def __init__(
3535
super().__init__(global_config, input_dir, output_dir)
3636
self.conf_file = "alert_actions.conf"
3737
self.conf_spec_file = f"{self.conf_file}.spec"
38-
if global_config is None:
39-
return
4038

4139
envs = normalize.normalize(
4240
global_config.alerts,

splunk_add_on_ucc_framework/generators/python_files/create_custom_command_python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def argument_generator(
7272
if args
7373
else f", validate=validators.{validate_type}()"
7474
)
75-
elif validate_type:
75+
else:
7676
validate_str = f", validate=validators.{validate_type}()"
7777

7878
if arg["default"] is None:

tests/unit/generators/conf_files/test_create_alert_actions_conf.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
from textwrap import dedent
55

66

7+
@patch.object(shutil, "copy")
8+
def test_custom_icon_file_name(
9+
mock_copy, global_config_for_alerts, input_dir, output_dir
10+
):
11+
alert_action_conf = AlertActionsConf(
12+
global_config_for_alerts, input_dir, output_dir
13+
)
14+
15+
assert "icon_path = dev_icon.png" in alert_action_conf.alerts["test_alert_default"]
16+
17+
718
def test_init_global_config_with_empty_alerts(
819
global_config_only_configuration,
920
input_dir,
@@ -55,7 +66,7 @@ def test_generate_conf(mock_copy, global_config_for_alerts, input_dir, output_di
5566
"is_custom = 1",
5667
"payload_format = json",
5768
"[test_alert_default]",
58-
"icon_path = alerticon.png",
69+
"icon_path = dev_icon.png",
5970
"label = Test Alert Default",
6071
"description = Description for test Alert Action",
6172
(

tests/unit/generators/conf_files/test_create_inputs_conf.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
11
import json
22
from pathlib import Path
33
from textwrap import dedent
4-
from unittest.mock import MagicMock
54

65
from splunk_add_on_ucc_framework.generators.conf_files import InputsConf
76
from splunk_add_on_ucc_framework.global_config import GlobalConfig
87
from tests.unit.helpers import get_testdata_file_path
98

109

11-
def test_init_no_inputs_in_global_config(
12-
global_config_only_configuration,
13-
input_dir,
14-
output_dir,
15-
):
16-
"""Test when _global_config is provided but has no inputs."""
17-
inputs_conf = InputsConf(
18-
global_config_only_configuration,
19-
input_dir,
20-
output_dir,
21-
)
22-
inputs_conf._global_config = MagicMock()
23-
inputs_conf._global_config.inputs = []
24-
25-
assert not inputs_conf.generate_conf()
26-
assert not inputs_conf.generate_conf_spec()
27-
28-
2910
def test_generate_conf(
3011
global_config_all_json,
3112
input_dir,
@@ -227,6 +208,7 @@ def test_inputs_conf_content_input_with_conf(input_dir, output_dir, ta_name, tmp
227208
"type": "text",
228209
"label": "Required field",
229210
"field": "required_field",
211+
"defaultValue": "test_field",
230212
"required": True,
231213
},
232214
{
@@ -266,7 +248,7 @@ def test_inputs_conf_content_input_with_conf(input_dir, output_dir, ta_name, tmp
266248
assert specs[0]["content"] == "\n".join(
267249
[
268250
"[<name>]",
269-
"required_field =",
251+
"required_field = (Default: test_field)",
270252
"optional_field =",
271253
"field_desc = Some description",
272254
]

tests/unit/generators/conf_files/test_create_settings_conf.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ def test_init_no_settings_key(global_config_for_alerts, input_dir, output_dir):
3838
assert settings_conf.default_content == ""
3939

4040

41+
def test_init_tab_without_entity(global_config_only_custom_tab, input_dir, output_dir):
42+
settings_conf = SettingsConf(global_config_only_custom_tab, input_dir, output_dir)
43+
assert settings_conf.default_content == "[custom_tab]\n\n"
44+
45+
4146
def test_generate_conf(global_config_all_json, input_dir, output_dir):
4247
ta_name = global_config_all_json.product
4348
exp_fname = f"{global_config_all_json.namespace.lower()}_settings.conf"

tests/unit/generators/python_files/test_create_custom_command_python.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ def custom_search_commands():
2424
"required": True,
2525
"validate": {"type": "Integer", "minimum": 5, "maximum": 10},
2626
},
27+
{
28+
"name": "max_word",
29+
"validate": {"type": "Integer", "maximum": 100},
30+
},
2731
{
2832
"name": "age",
2933
"validate": {"type": "Integer", "minimum": 18},
@@ -72,6 +76,7 @@ def test_init(
7276
"list_arg": [
7377
"count = Option(name='count', require=True, "
7478
"validate=validators.Integer(minimum=5, maximum=10))",
79+
"max_word = Option(name='max_word', require=False, validate=validators.Integer(maximum=100))",
7580
"age = Option(name='age', require=False, validate=validators.Integer(minimum=18))",
7681
"text = Option(name='text', require=True, default='test_text')",
7782
"contains = Option(name='contains', require=False)",

tests/unit/generators/test_file_generator.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,38 @@ def test_set_template_and_render_invalid_file_name(
105105
file_gen.set_template_and_render(["dir1"], "test.invalid")
106106

107107

108+
@patch(
109+
"splunk_add_on_ucc_framework.generators.conf_files.create_app_conf.get_app_manifest"
110+
)
111+
def test_begin_no_inputs(
112+
dummy_app_manifest, global_config_only_configuration, input_dir, output_dir
113+
):
114+
dummy_app_manifest.return_value = dummy_app_manifest
115+
result = begin(global_config_only_configuration, input_dir, output_dir)
116+
ta_name = global_config_only_configuration.product
117+
assert result == [
118+
{"app.conf": f"{output_dir}/{ta_name}/default/app.conf"},
119+
{"server.conf": f"{output_dir}/{ta_name}/default/server.conf"},
120+
{"restmap.conf": f"{output_dir}/{ta_name}/default/restmap.conf"},
121+
{"web.conf": f"{output_dir}/{ta_name}/default/web.conf"},
122+
{
123+
"splunk_ta_uccexample_account.conf.spec": f"{output_dir}/{ta_name}/"
124+
"README/splunk_ta_uccexample_account.conf.spec"
125+
},
126+
{
127+
"splunk_ta_uccexample_settings.conf": f"{output_dir}/{ta_name}/default/splunk_ta_uccexample_settings.conf"
128+
},
129+
{
130+
"splunk_ta_uccexample_settings.conf.spec": f"{output_dir}/{ta_name}/"
131+
"README/splunk_ta_uccexample_settings.conf.spec"
132+
},
133+
{
134+
"configuration.xml": f"{output_dir}/{ta_name}/default/data/ui/views/configuration.xml"
135+
},
136+
{"default.xml": f"{output_dir}/{ta_name}/default/data/ui/nav/default.xml"},
137+
]
138+
139+
108140
@patch(
109141
"splunk_add_on_ucc_framework.generators.conf_files.create_app_conf.get_app_manifest"
110142
)

tests/unit/generators/xml_files/test_create_default_xml.py

Lines changed: 103 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from splunk_add_on_ucc_framework.generators.xml_files import DefaultXml
33
from tests.unit.helpers import compare_xml_content
4+
from unittest.mock import patch
45

56

67
@pytest.fixture
@@ -18,6 +19,22 @@ def default_xml_object(global_config_all_json, input_dir, output_dir):
1819
return default_xml
1920

2021

22+
@patch("os.path.exists", return_value=True)
23+
def test_default_xml_already_exsits(
24+
mock_copy, global_config_all_json, input_dir, output_dir, caplog
25+
):
26+
expected_msg = (
27+
"Skipping generating data/ui/nav/default.xml because file already exists."
28+
)
29+
default_xml = DefaultXml(
30+
global_config_all_json,
31+
input_dir,
32+
output_dir,
33+
)
34+
assert expected_msg in caplog.text
35+
assert not hasattr(default_xml, "default_xml_content")
36+
37+
2138
def test_init_with_error(
2239
global_config_all_json,
2340
input_dir,
@@ -33,22 +50,78 @@ def test_init_with_error(
3350
)
3451

3552

36-
def test_generate_nav_default_xml(default_xml_object):
53+
@pytest.mark.parametrize(
54+
(
55+
"has_input",
56+
"has_dashboard",
57+
"has_configuration",
58+
"default_view",
59+
"expected_result",
60+
),
61+
[
62+
(
63+
False,
64+
False,
65+
True,
66+
None,
67+
"""<?xml version="1.0" ?>
68+
<nav>
69+
<view default="true" name="configuration"/>
70+
<view name="search"/>
71+
</nav>
72+
""",
73+
),
74+
(
75+
True,
76+
False,
77+
False,
78+
None,
79+
"""<?xml version="1.0" ?>
80+
<nav>
81+
<view default="true" name="inputs"/>
82+
<view name="search"/>
83+
</nav>
84+
""",
85+
),
86+
(
87+
False,
88+
True,
89+
False,
90+
None,
91+
"""<?xml version="1.0" ?>
92+
<nav>
93+
<view default="true" name="dashboard"/>
94+
<view name="search"/>
95+
</nav>
96+
""",
97+
),
98+
(
99+
False,
100+
False,
101+
False,
102+
None,
103+
"""<?xml version="1.0" ?>
104+
<nav>
105+
<view default="true" name="search"/>
106+
</nav>
107+
""",
108+
),
109+
],
110+
)
111+
def test_generate_nav_default_view_is_none(
112+
has_input,
113+
has_dashboard,
114+
has_configuration,
115+
default_view,
116+
expected_result,
117+
default_xml_object,
118+
):
37119
result = default_xml_object.generate_nav_default_xml(
38-
include_inputs=True,
39-
include_dashboard=True,
40-
include_configuration=True,
41-
default_view="configuration",
120+
include_inputs=has_input,
121+
include_dashboard=has_dashboard,
122+
include_configuration=has_configuration,
123+
default_view=default_view,
42124
)
43-
44-
expected_result = """<?xml version="1.0" ?>
45-
<nav>
46-
<view name="inputs"/>
47-
<view default="true" name="configuration"/>
48-
<view name="dashboard"/>
49-
<view name="search"/>
50-
</nav>
51-
"""
52125
diff = compare_xml_content(result, expected_result)
53126
assert diff == ""
54127

@@ -90,26 +163,6 @@ def test_generate_nav_default_xml_with_default_inputs_page(default_xml_object):
90163
assert diff == ""
91164

92165

93-
def test_generate_nav_default_xml_with_default_dashboard_page(default_xml_object):
94-
result = default_xml_object.generate_nav_default_xml(
95-
include_inputs=True,
96-
include_dashboard=True,
97-
include_configuration=True,
98-
default_view="dashboard",
99-
)
100-
101-
expected_result = """<?xml version="1.0" ?>
102-
<nav>
103-
<view name="inputs"/>
104-
<view name="configuration"/>
105-
<view default="true" name="dashboard"/>
106-
<view name="search"/>
107-
</nav>
108-
"""
109-
diff = compare_xml_content(result, expected_result)
110-
assert diff == ""
111-
112-
113166
def test_generate_nav_default_xml_with_search_view_default(default_xml_object):
114167
result = default_xml_object.generate_nav_default_xml(
115168
include_inputs=False,
@@ -158,7 +211,7 @@ def test_generate_nav_default_xml_with_no_configuration(default_xml_object):
158211
<view name="dashboard"/>
159212
<view name="search"/>
160213
</nav>
161-
""",
214+
""",
162215
),
163216
(
164217
"inputs",
@@ -169,7 +222,7 @@ def test_generate_nav_default_xml_with_no_configuration(default_xml_object):
169222
<view name="dashboard"/>
170223
<view name="search"/>
171224
</nav>
172-
""",
225+
""",
173226
),
174227
(
175228
"dashboard",
@@ -180,7 +233,7 @@ def test_generate_nav_default_xml_with_no_configuration(default_xml_object):
180233
<view default="true" name="dashboard"/>
181234
<view name="search"/>
182235
</nav>
183-
""",
236+
""",
184237
),
185238
(
186239
"search",
@@ -191,7 +244,7 @@ def test_generate_nav_default_xml_with_no_configuration(default_xml_object):
191244
<view name="dashboard"/>
192245
<view default="true" name="search"/>
193246
</nav>
194-
""",
247+
""",
195248
),
196249
],
197250
)
@@ -255,3 +308,15 @@ def test_generate_xml(
255308
output[0]["file_path"]
256309
== f"{output_dir}/{ta_name}/default/data/ui/nav/{exp_fname}"
257310
)
311+
312+
313+
def test_generate_xml_without_pages(
314+
global_config_for_conf_only_TA, input_dir, output_dir
315+
):
316+
default_xml = DefaultXml(
317+
global_config_for_conf_only_TA,
318+
input_dir,
319+
output_dir,
320+
)
321+
output = default_xml.generate()
322+
assert output is None

tests/unit/testdata/valid_config_all_alerts.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
{
122122
"name": "test_alert_default",
123123
"label": "Test Alert Default",
124+
"iconFileName": "dev_icon.png",
124125
"description": "Description for test Alert Action",
125126
"adaptiveResponse": {
126127
"task": [

0 commit comments

Comments
 (0)