-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_new_command.py
186 lines (160 loc) · 7.42 KB
/
test_new_command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
from unittest.mock import patch, Mock
from opnsense_cli.commands.new.command import command
from opnsense_cli.commands.test_base import CommandTestCase
from click.testing import CliRunner
import textwrap
class TestNewCommandCommands(CommandTestCase):
def setUp(self):
self._setup_fakefs()
self._core_model_fixture = self._read_fixture_file(
"fixtures/opn_cli/core_model.xml", base_dir=os.path.dirname(__file__)
)
self._core_form_fixture = self._read_fixture_file("fixtures/opn_cli/core_form.xml", base_dir=os.path.dirname(__file__))
self._plugin_model_fixture = self._read_fixture_file(
"fixtures/opn_cli/plugin_model.xml", base_dir=os.path.dirname(__file__)
)
self._plugin_form_fixture = self._read_fixture_file(
"fixtures/opn_cli/plugin_form.xml", base_dir=os.path.dirname(__file__)
)
self._mock_model_core_resp = self._mock_response(content=self._core_model_fixture)
self._mock_form_core_resp = self._mock_response(content=self._core_form_fixture)
self._mock_model_plugin_resp = self._mock_response(content=self._plugin_model_fixture)
self._mock_form_plugin_resp = self._mock_response(content=self._plugin_form_fixture)
self._command_template = self._read_template_file("code_generators/opn_cli/command/template.py.j2")
self._service_template = self._read_template_file("code_generators/opn_cli/service/template.py.j2")
self._test_template = self._read_template_file("code_generators/opn_cli/unit_test/template.py.j2")
self._output_dir = self._get_output_path()
self._generated_plugin_command_path = f"{self._output_dir}/commands/plugin/haproxy/frontend.py"
self._generated_plugin_service_path = (
f"{self._output_dir}/commands/plugin/haproxy/services/haproxy_frontend_service.py"
)
self._generated_plugin_test_path = f"{self._output_dir}/commands/plugin/haproxy/tests/test_haproxy_frontend.py"
self._generated_core_command_path = f"{self._output_dir}/commands/core/firewall/category.py"
self._generated_core_service_path = f"{self._output_dir}/commands/core/firewall/services/firewall_category_service.py"
self._generated_core_test_path = f"{self._output_dir}/commands/core/firewall/tests/test_firewall_category.py"
@patch("opnsense_cli.parser.xml_parser.requests.get")
def test_plugin(self, mock_model_get: Mock):
mock_model_get.side_effect = [self._mock_model_plugin_resp, self._mock_form_plugin_resp]
runner = CliRunner()
result = runner.invoke(
command,
[
"plugin",
"haproxy",
"frontend",
"--tag",
"frontends",
"--model-url",
"https://fake.githubusercontent.com/model.xml",
"--form-url",
"https://fake.githubusercontent.com/form.xml",
],
catch_exceptions=False,
)
self.assertIn(
f"generate new code: {self._generated_plugin_command_path}\n"
f"generate new code: {self._generated_plugin_service_path}\n"
f"generate new code: {self._generated_plugin_test_path}\n",
result.output,
)
self.assertTrue(os.path.exists(self._generated_plugin_command_path))
self.assertTrue(os.path.exists(self._generated_plugin_service_path))
self.assertTrue(os.path.exists(self._generated_plugin_test_path))
command_file_content = self._read_file(self._generated_plugin_command_path)
facade_file_content = self._read_file(self._generated_plugin_service_path)
test_file_content = self._read_file(self._generated_plugin_test_path)
self.assertIn(
"enabled,name,description,bind,bindOptions,mode,defaultBackend,ssl_enabled,ssl_certificates", command_file_content
)
self.assertIn(
textwrap.dedent(
"""
@click.option(
'--linkedCpuAffinityRules',
help=('Choose CPU affinity rules that should be applied to this Public Service.'),
callback=resolve_linked_names_to_uuids,
type=CSV,
show_default=True,
default=None
)
"""
),
command_file_content,
)
self.assertIn(
textwrap.dedent(
"""
@click.option(
'--linkedCpuAffinityRules',
help=('Choose CPU affinity rules that should be applied to this Public Service.'),
callback=resolve_linked_names_to_uuids,
type=CSV,
show_default=True,
default=None,
required=False,
)
"""
),
command_file_content,
)
self.assertIn("class HaproxyFrontendService(CommandService):", facade_file_content)
self.assertIn(
"'defaultBackend': {'template': '$.haproxy.backends.backend[{uuids}].name', 'insert_as_key': 'Backend'},",
facade_file_content,
)
self.assertIn("class TestHaproxyFrontendCommands(CommandTestCase):", test_file_content)
self.assertIn("@patch('opnsense_cli.commands.plugin.haproxy.frontend.ApiClient.execute')", test_file_content)
@patch("opnsense_cli.parser.xml_parser.requests.get")
def test_plugin_without_form_url(self, mock_model_get: Mock):
mock_model_get.side_effect = [
self._mock_model_plugin_resp,
]
runner = CliRunner()
result = runner.invoke(
command,
[
"plugin",
"haproxy",
"frontend",
"--tag",
"frontends",
"--model-url",
"https://fake.githubusercontent.com/model.xml",
],
catch_exceptions=False,
)
self.assertIn(
f"generate new code: {self._generated_plugin_command_path}\n"
f"generate new code: {self._generated_plugin_service_path}\n"
f"generate new code: {self._generated_plugin_test_path}\n",
result.output,
)
self.assertTrue(os.path.exists(self._generated_plugin_command_path))
self.assertTrue(os.path.exists(self._generated_plugin_service_path))
self.assertTrue(os.path.exists(self._generated_plugin_test_path))
@patch("opnsense_cli.parser.xml_parser.requests.get")
def test_core(self, mock_model_get: Mock):
mock_model_get.side_effect = [self._mock_model_core_resp, self._mock_form_core_resp]
runner = CliRunner()
result = runner.invoke(
command,
[
"core",
"firewall",
"category",
"--tag",
"categories",
"--model-url",
"https://fake.githubusercontent.com/model.xml",
"--form-url",
"https://fake.githubusercontent.com/form.xml",
],
catch_exceptions=False,
)
self.assertIn(
f"generate new code: {self._generated_core_command_path}\n"
f"generate new code: {self._generated_core_service_path}\n"
f"generate new code: {self._generated_core_test_path}\n",
result.output,
)