Skip to content

Commit a8d236c

Browse files
[fast-reboot-filter-routes.py] Remove click and improve error reporting (#3030)
* [fast-reboot-filter-routes.py] Remove click and improve error reporting 1. Removed click usage from a script since there is no active click context therefore we saw these error messages: ``` ERR fast-reboot-filter-routes: Got an exception There is no active click context.: Traceback: Traceback (most recent call last):edge-core#12 File "/usr/local/bin/fast-reboot-filter-routes.py", line 18, in get_connected_routes#012 output, ret = clicommon.run_command(cmd, return_cmd=True)edge-core#12 File "/usr/local/lib/python3.9/dist-packages/utilities_common/cli.py", line 545, in run_command#012 proc = subprocess.Popen(command, shell=shell, text=True, stdout=subprocess.PIPE)edge-core#12 File "/usr/lib/python3.9/subprocess.py", line 951, in __init__#012 self._execute_child(args, executable, preexec_fn, close_fds,edge-core#12 File "/usr/lib/python3.9/subprocess.py", line 1823, in _execute_child#012 raise child_exception_type(errno_num, err_msg, err_filename)#012FileNotFoundError: [Errno 2] No such file or directory: 'sudo vtysh -c "show ip route connected json"'edge-core#12#012During handling of the above exception, another exception occurred:edge-core#12#012Traceback (most recent call last):edge-core#12 File "/usr/local/lib/python3.9/dist-packages/click/globals.py", line 23, in get_current_context#012 return getattr(_local, 'stack')[-1]#012AttributeError: '_thread._local' object has no attribute 'stack'edge-core#12#012During handling of the above exception, another exception occurred:edge-core#12#012Traceback (most recent call last):edge-core#12 File "/usr/local/bin/fast-reboot-filter-routes.py", line 79, in <module>edge-core#12 res = main()edge-core#12 File "/usr/local/bin/fast-reboot-filter-routes.py", line 70, in main#012 connected_routes = get_connected_routes()edge-core#12 File "/usr/local/bin/fast-reboot-filter-routes.py", line 27, in get_connected_routes#012 ctx = click.get_current_context()edge-core#12 File "/usr/local/lib/python3.9/dist-packages/click/globals.py", line 26, in get_current_context#012 raise RuntimeError('There is no active click context.')#012RuntimeError: There is no active click context. ``` 2. Improved error reporting so that when an error occurs when we run a command it will report it via terminal and syslog: stdout: ``` Failed to execute sudo vtysh -c show ip route connected jsson: % Unknown command: show ip route connected jsson ``` syslog: ``` Oct 17 11:53:10.620788 arc-switch1025 ERR fast-reboot-filter-routes: Got an exception: Failed to execute sudo vtysh -c show ip route connected jsson: % Unknown command: show ip route connected jsson: Traceback: Traceback (most recent call last):edge-core#12 File "/home/admin/fast-reboot-filter-routes.py", line 73, in <module>edge-core#12 res = main()edge-core#12 File "/home/admin/fast-reboot-filter-routes.py", line 64, in main#012 connected_routes = get_connected_routes()edge-core#12 File "/home/admin/fast-reboot-filter-routes.py", line 18, in get_connected_routes#012 raise Exception("Failed to execute {}: {}".format(" ".join(cmd), output.rstrip('\n')))#012Exception: Failed to execute sudo vtysh -c show ip route connected jsson: % Unknown command: show ip route connected jsson ``` Signed-off-by: Stepan Blyschak <stepanb@nvidia.com> * add a test for command failure Signed-off-by: Stepan Blyschak <stepanb@nvidia.com> * Increase coverage Signed-off-by: Stepan Blyschak <stepanb@nvidia.com> --------- Signed-off-by: Stepan Blyschak <stepanb@nvidia.com>
1 parent 75199c0 commit a8d236c

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

scripts/fast-reboot-filter-routes.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,21 @@
66
import utilities_common.cli as clicommon
77
import syslog
88
import traceback
9-
import click
109
from swsscommon.swsscommon import ConfigDBConnector
1110

1211
ROUTE_IDX = 1
1312

1413
def get_connected_routes():
1514
cmd = ['sudo', 'vtysh', '-c', "show ip route connected json"]
1615
connected_routes = []
17-
try:
18-
output, ret = clicommon.run_command(cmd, return_cmd=True)
19-
if ret != 0:
20-
click.echo(output.rstrip('\n'))
21-
sys.exit(ret)
22-
if output is not None:
23-
route_info = json.loads(output)
24-
for route in route_info.keys():
25-
connected_routes.append(route)
26-
except Exception:
27-
ctx = click.get_current_context()
28-
ctx.fail("Unable to get connected routes from bgp")
29-
16+
output, ret = clicommon.run_command(cmd, return_cmd=True)
17+
if ret != 0:
18+
raise Exception("Failed to execute {}: {}".format(" ".join(cmd), output.rstrip('\n')))
19+
if output is not None:
20+
route_info = json.loads(output)
21+
for route in route_info.keys():
22+
connected_routes.append(route)
23+
3024
return connected_routes
3125

3226
def get_route(db, route):
@@ -81,6 +75,7 @@ def main():
8175
syslog.syslog(syslog.LOG_NOTICE, "SIGINT received. Quitting")
8276
res = 1
8377
except Exception as e:
78+
print(e)
8479
syslog.syslog(syslog.LOG_ERR, "Got an exception %s: Traceback: %s" % (str(e), traceback.format_exc()))
8580
res = 2
8681
finally:

tests/fast_reboot_filter_routes_test.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ def setup(self):
1010

1111
@patch('utilities_common.cli.run_command')
1212
def test_get_connected_routes(self, mock_run_command):
13-
mock_run_command.return_value = (None, 0)
13+
mock_run_command.return_value = ('{"1.1.0.0/16": {}}', 0)
1414
output = fast_reboot_filter_routes.get_connected_routes()
1515
mock_run_command.assert_called_with(['sudo', 'vtysh', '-c', "show ip route connected json"], return_cmd=True)
16-
assert output == []
16+
assert output == ['1.1.0.0/16']
17+
18+
@patch('utilities_common.cli.run_command')
19+
def test_get_connected_routes_command_failed(self, mock_run_command):
20+
mock_run_command.return_value = ('{"1.1.0.0/16": {}}', 1)
21+
with pytest.raises(Exception):
22+
fast_reboot_filter_routes.get_connected_routes()
23+
mock_run_command.assert_called_with(['sudo', 'vtysh', '-c', "show ip route connected json"], return_cmd=True)
1724

1825
def teardown(self):
1926
print("TEAR DOWN")

0 commit comments

Comments
 (0)