Skip to content

Commit

Permalink
add UT
Browse files Browse the repository at this point in the history
Signed-off-by: Stepan Blyschak <[email protected]>
  • Loading branch information
stepanblyschak committed Mar 28, 2022
1 parent cc89a02 commit 4fae05e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
18 changes: 6 additions & 12 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,16 @@
# location (configdb?), so that we prevent the continous execution of this
# bash oneliner. To be revisited once routing-stack info is tracked somewhere.
def get_routing_stack():
result = None
command = "sudo docker ps | grep bgp | awk '{print$2}' | cut -d'-' -f3 | cut -d':' -f1 | head -n 1"

try:
proc = subprocess.Popen(command,
stdout=subprocess.PIPE,
shell=True,
text=True)
proc.wait(timeout=COMMAND_TIMEOUT)
stdout = proc.communicate()[0]
stdout = subprocess.check_output(command, shell=True, timeout=COMMAND_TIMEOUT)
result = stdout.rstrip('\n')
except Exception as err:
click.echo('Failed to get routing stack: {}'.format(err), err=True)

except (OSError, subprocess.TimeoutExpired) as e:
click.echo('Failed to get routing stack', err=True)
return

return (result)
return result


# Global Routing-Stack variable
Expand Down Expand Up @@ -1167,7 +1161,7 @@ def techsupport(since, global_timeout, cmd_timeout, verbose, allow_process_stop,
cmd += " -s '{}'".format(since)

if debug_dump:
cmd += " -d "
cmd += " -d"

cmd += " -t {}".format(cmd_timeout)
if redirect_stderr:
Expand Down
24 changes: 24 additions & 0 deletions tests/techsupport_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest
import show.main
from unittest.mock import patch, Mock
from click.testing import CliRunner

EXPECTED_BASE_COMMAND = 'sudo timeout --kill-after=300s -s SIGTERM --foreground '

@patch("show.main.run_command")
@pytest.mark.parametrize(
"cli_arguments,expected",
[
([], '30m generate_dump -v -t 5'),
(['--since', '2 days ago'], "30m generate_dump -v -s '2 days ago' -t 5"),
(['-g', '50'], '50m generate_dump -v -t 5'),
(['--allow-process-stop'], '30m -a generate_dump -v -t 5'),
(['--silent'], '30m generate_dump -t 5'),
(['--debug-dump', '--redirect-stderr'], '30m generate_dump -v -d -t 5 -r'),
]
)
def test_techsupport(run_command, cli_arguments, expected):
runner = CliRunner()
result = runner.invoke(show.main.cli.commands['techsupport'], cli_arguments)
run_command.assert_called_with(EXPECTED_BASE_COMMAND + expected, display_cmd=False)

0 comments on commit 4fae05e

Please sign in to comment.