Skip to content

Commit 73665b9

Browse files
committed
[ssip]: Add CLI UTs.
Signed-off-by: Nazarii Hnydyn <[email protected]>
1 parent 1598cff commit 73665b9

File tree

4 files changed

+291
-0
lines changed

4 files changed

+291
-0
lines changed
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Module holding the correct values for show CLI command outputs for the syslog_test.py
3+
"""
4+
5+
show_syslog_empty="""\
6+
SERVER IP SOURCE IP PORT VRF
7+
----------- ----------- ------ -----
8+
"""
9+
10+
11+
show_syslog="""\
12+
SERVER IP SOURCE IP PORT VRF
13+
----------- ----------- ------ --------
14+
2.2.2.2 1.1.1.1 514 default
15+
3.3.3.3 1.1.1.1 514 mgmt
16+
2222::2222 1111::1111 514 Vrf-Data
17+
"""

tests/syslog_input/syslog_cdb.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"VRF|Vrf-Data": {
3+
"NULL": "NULL"
4+
},
5+
"SYSLOG_SERVER|2.2.2.2": {
6+
"source": "1.1.1.1",
7+
"port": "514",
8+
"vrf": "default"
9+
},
10+
"SYSLOG_SERVER|3.3.3.3": {
11+
"source": "1.1.1.1",
12+
"port": "514",
13+
"vrf": "mgmt"
14+
},
15+
"SYSLOG_SERVER|2222::2222": {
16+
"source": "1111::1111",
17+
"port": "514",
18+
"vrf": "Vrf-Data"
19+
}
20+
}

tests/syslog_input/vrf_cdb.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"MGMT_VRF_CONFIG|vrf_global": {
3+
"mgmtVrfEnabled": "true"
4+
},
5+
"VRF|Vrf-Data": {
6+
"NULL": "NULL"
7+
}
8+
}

tests/syslog_test.py

+246
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
#!/usr/bin/env python
2+
3+
import pytest
4+
5+
import os
6+
import mock
7+
import logging
8+
9+
import show.main as show
10+
import config.main as config
11+
12+
from click.testing import CliRunner
13+
from utilities_common.db import Db
14+
15+
from .mock_tables import dbconnector
16+
from .syslog_input import assert_show_output
17+
18+
19+
ERROR_PATTERN_INVALID_IP = "does not appear to be an IPv4 or IPv6 address"
20+
ERROR_PATTERN_PROHIBITED_IP = "is a loopback/multicast IP address"
21+
ERROR_PATTERN_IP_FAMILY_MISMATCH = "IP address family mismatch"
22+
23+
ERROR_PATTERN_INVALID_PORT = "is not a valid integer"
24+
ERROR_PATTERN_INVALID_PORT_RANGE = "is not in the valid range of 0 to 65535"
25+
26+
ERROR_PATTERN_INVALID_VRF = "invalid choice"
27+
ERROR_PATTERN_NONEXISTENT_VRF = "VRF doesn't exist in Linux"
28+
29+
VRF_LIST = ["mgmt", "Vrf-Data"]
30+
VRF_MEMBER_DICT = {"mgmt": ["eth0"], "Vrf-Data": ["Ethernet0"]}
31+
IP_ADDR_DICT = {"Ethernet0": ["1111::1111"], "Loopback0": ["1.1.1.1"], "eth0": ["3.3.3.3"]}
32+
33+
SUCCESS = 0
34+
ERROR2 = 2
35+
36+
37+
test_path = os.path.dirname(os.path.abspath(__file__))
38+
mock_db_path = os.path.join(test_path, "syslog_input")
39+
logger = logging.getLogger(__name__)
40+
41+
42+
class TestSyslog:
43+
@classmethod
44+
def setup_class(cls):
45+
logger.info("SETUP")
46+
os.environ["UTILITIES_UNIT_TESTING"] = "1"
47+
48+
@classmethod
49+
def teardown_class(cls):
50+
logger.info("TEARDOWN")
51+
os.environ["UTILITIES_UNIT_TESTING"] = "0"
52+
os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] = ""
53+
dbconnector.dedicated_dbs["CONFIG_DB"] = None
54+
55+
########## CONFIG SYSLOG ##########
56+
57+
@mock.patch("utilities_common.cli.run_command", mock.MagicMock(return_value=None))
58+
@pytest.mark.parametrize("server_ip", ["2.2.2.2", "2222::2222"])
59+
def test_config_syslog_basic(self, server_ip):
60+
db = Db()
61+
runner = CliRunner()
62+
63+
result = runner.invoke(
64+
config.config.commands["syslog"].commands["add"], [server_ip], obj=db
65+
)
66+
67+
logger.debug("\n" + result.output)
68+
logger.debug(result.exit_code)
69+
assert result.exit_code == SUCCESS
70+
71+
result = runner.invoke(
72+
config.config.commands["syslog"].commands["del"], [server_ip], obj=db
73+
)
74+
75+
logger.debug("\n" + result.output)
76+
logger.debug(result.exit_code)
77+
assert result.exit_code == SUCCESS
78+
79+
@mock.patch("utilities_common.cli.run_command", mock.MagicMock(return_value=None))
80+
@mock.patch("config.syslog.get_vrf_list", mock.MagicMock(return_value=VRF_LIST))
81+
@mock.patch("config.syslog.get_vrf_member_dict", mock.MagicMock(return_value=VRF_MEMBER_DICT))
82+
@mock.patch("config.syslog.get_ip_addr_dict", mock.MagicMock(return_value=IP_ADDR_DICT))
83+
@pytest.mark.parametrize("server_ip,source_ip,port,vrf", [
84+
("2.2.2.2", "1.1.1.1", "514", "default"),
85+
("4.4.4.4", "3.3.3.3", "514", "mgmt"),
86+
("2222::2222", "1111::1111", "514", "Vrf-Data")
87+
])
88+
def test_config_syslog_extended(self, server_ip, source_ip, port, vrf):
89+
dbconnector.dedicated_dbs["CONFIG_DB"] = os.path.join(mock_db_path, "vrf_cdb")
90+
91+
db = Db()
92+
runner = CliRunner()
93+
94+
result = runner.invoke(
95+
config.config.commands["syslog"].commands["add"],
96+
[server_ip, "--source", source_ip, "--port", port, "--vrf", vrf], obj=db
97+
)
98+
99+
logger.debug("\n" + result.output)
100+
logger.debug(result.exit_code)
101+
assert result.exit_code == SUCCESS
102+
103+
result = runner.invoke(
104+
config.config.commands["syslog"].commands["del"], [server_ip], obj=db
105+
)
106+
107+
logger.debug("\n" + result.output)
108+
logger.debug(result.exit_code)
109+
assert result.exit_code == SUCCESS
110+
111+
@pytest.mark.parametrize("server_ip,source_ip", [
112+
("2.2.2.2", "1.1.1.1111"),
113+
("4.4.4.4444", "3.3.3.3")
114+
])
115+
def test_config_syslog_invalid_ip(self, server_ip, source_ip):
116+
db = Db()
117+
runner = CliRunner()
118+
119+
result = runner.invoke(
120+
config.config.commands["syslog"].commands["add"],
121+
[server_ip, "--source", source_ip], obj=db
122+
)
123+
124+
logger.debug("\n" + result.output)
125+
logger.debug(result.exit_code)
126+
assert ERROR_PATTERN_INVALID_IP in result.output
127+
assert result.exit_code == ERROR2
128+
129+
@pytest.mark.parametrize("source_ip", ["127.0.0.1", "224.0.0.1"])
130+
def test_config_syslog_prohibited_sip(self, source_ip):
131+
db = Db()
132+
runner = CliRunner()
133+
134+
result = runner.invoke(
135+
config.config.commands["syslog"].commands["add"],
136+
["2.2.2.2", "--source", source_ip], obj=db
137+
)
138+
139+
logger.debug("\n" + result.output)
140+
logger.debug(result.exit_code)
141+
assert ERROR_PATTERN_PROHIBITED_IP in result.output
142+
assert result.exit_code == ERROR2
143+
144+
def test_config_syslog_ip_family_mismatch(self):
145+
db = Db()
146+
runner = CliRunner()
147+
148+
result = runner.invoke(
149+
config.config.commands["syslog"].commands["add"],
150+
["2.2.2.2", "--source", "1111::1111"], obj=db
151+
)
152+
153+
logger.debug("\n" + result.output)
154+
logger.debug(result.exit_code)
155+
assert ERROR_PATTERN_IP_FAMILY_MISMATCH in result.output
156+
assert result.exit_code == ERROR2
157+
158+
def test_config_syslog_invalid_port(self):
159+
db = Db()
160+
runner = CliRunner()
161+
162+
result = runner.invoke(
163+
config.config.commands["syslog"].commands["add"],
164+
["2.2.2.2", "--port", "514p"], obj=db
165+
)
166+
167+
logger.debug("\n" + result.output)
168+
logger.debug(result.exit_code)
169+
assert ERROR_PATTERN_INVALID_PORT in result.output
170+
assert result.exit_code == ERROR2
171+
172+
@pytest.mark.parametrize("port", ["-1", "65536"])
173+
def test_config_syslog_invalid_port_range(self, port):
174+
db = Db()
175+
runner = CliRunner()
176+
177+
result = runner.invoke(
178+
config.config.commands["syslog"].commands["add"],
179+
["2.2.2.2", "--port", port], obj=db
180+
)
181+
182+
logger.debug("\n" + result.output)
183+
logger.debug(result.exit_code)
184+
assert ERROR_PATTERN_INVALID_PORT_RANGE in result.output
185+
assert result.exit_code == ERROR2
186+
187+
def test_config_syslog_invalid_vrf(self):
188+
db = Db()
189+
runner = CliRunner()
190+
191+
result = runner.invoke(
192+
config.config.commands["syslog"].commands["add"],
193+
["2.2.2.2", "--vrf", "default1"], obj=db
194+
)
195+
196+
logger.debug("\n" + result.output)
197+
logger.debug(result.exit_code)
198+
assert ERROR_PATTERN_INVALID_VRF in result.output
199+
assert result.exit_code == ERROR2
200+
201+
@pytest.mark.parametrize("vrf", ["mgmt", "Vrf-Data"])
202+
def test_config_syslog_nonexistent_vrf(self, vrf):
203+
dbconnector.dedicated_dbs["CONFIG_DB"] = os.path.join(mock_db_path, "vrf_cdb")
204+
205+
db = Db()
206+
runner = CliRunner()
207+
208+
result = runner.invoke(
209+
config.config.commands["syslog"].commands["add"],
210+
["2.2.2.2", "--vrf", vrf], obj=db
211+
)
212+
213+
logger.debug("\n" + result.output)
214+
logger.debug(result.exit_code)
215+
assert ERROR_PATTERN_NONEXISTENT_VRF in result.output
216+
assert result.exit_code == ERROR2
217+
218+
########## SHOW SYSLOG ##########
219+
220+
def test_show_syslog_empty(self):
221+
db = Db()
222+
runner = CliRunner()
223+
224+
result = runner.invoke(
225+
show.cli.commands["syslog"], [], obj=db
226+
)
227+
228+
logger.debug("\n" + result.output)
229+
logger.debug(result.exit_code)
230+
assert result.exit_code == SUCCESS
231+
assert result.output == assert_show_output.show_syslog_empty
232+
233+
def test_show_syslog(self):
234+
dbconnector.dedicated_dbs["CONFIG_DB"] = os.path.join(mock_db_path, "syslog_cdb")
235+
236+
db = Db()
237+
runner = CliRunner()
238+
239+
result = runner.invoke(
240+
show.cli.commands["syslog"], [], obj=db
241+
)
242+
243+
logger.debug("\n" + result.output)
244+
logger.debug(result.exit_code)
245+
assert result.exit_code == SUCCESS
246+
assert result.output == assert_show_output.show_syslog

0 commit comments

Comments
 (0)