Skip to content

Commit

Permalink
more test and fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Arvindsrinivasan Lakshmi Narasimhan <arlakshm@microsoft.com>
  • Loading branch information
arlakshm committed Aug 21, 2020
1 parent d740d14 commit 05a87fb
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 97 deletions.
2 changes: 1 addition & 1 deletion show/bgp_quagga_v4.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import click
from show.main import AliasedGroup, ip, run_command,
from show.main import AliasedGroup, ip, run_command
from utilities_common.bgp_util import get_bgp_summary_extended


Expand Down
2 changes: 1 addition & 1 deletion show/bgp_quagga_v6.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import click
from show.main import AliasedGroup, ipv6, run_command,
from show.main import AliasedGroup, ipv6, run_command
from utilities_common.bgp_util import get_bgp_summary_extended


Expand Down
2 changes: 1 addition & 1 deletion show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ def protocol(verbose):
ip.add_command(bgp)
from .bgp_quagga_v6 import bgp
ipv6.add_command(bgp)
else:
elif routing_stack == "frr":
from .bgp_frr_v4 import bgp
ip.add_command(bgp)
from .bgp_frr_v6 import bgp
Expand Down
68 changes: 53 additions & 15 deletions tests/bgp_commands_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import os

import pytest

from click.testing import CliRunner
import show.main as show

show_bgp_summary_v4="""\
show_bgp_summary_v4 = """\
IPv4 Unicast Summary:
BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0
BGP table version 12811
BGP table version 12811
RIB entries 12817, using 2358328 bytes of memory
Peers 24, using 502080 KiB of memory
Peer groups 4, using 256 bytes of memory
Expand Down Expand Up @@ -40,11 +42,11 @@
10.0.0.63 4 64016 0 0 0 0 0 never Active ARISTA16T0
"""

show_bgp_summary_v6="""\
show_bgp_summary_v6 = """\
IPv6 Unicast Summary:
BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0
BGP table version 8972
BGP table version 8972
RIB entries 12817, using 2358328 bytes of memory
Peers 24, using 502080 KiB of memory
Peer groups 4, using 256 bytes of memory
Expand Down Expand Up @@ -77,23 +79,59 @@
fc00::76 4 64014 0 0 0 0 0 never Active ARISTA14T0
fc00::a 4 65200 6665 6671 0 0 0 2d09h38m 6402 ARISTA03T2
"""

show_error_invalid_json = """\
Usage: summary [OPTIONS]
Try 'summary --help' for help.
Error: bgp summary from bgp container not in json format
"""


class TestBgpCommands(object):
@classmethod
def setup_class(cls):
print("SETUP")
import mock_tables.dbconnector

@pytest.mark.parametrize('setup_single_bgp_instance', ['v4'], indirect=['setup_single_bgp_instance'])
def test_bgp_summary_v4(self, setup_single_bgp_instance):
#reload(show)
@pytest.mark.parametrize('setup_single_bgp_instance',
['v4'], indirect=['setup_single_bgp_instance'])
def test_bgp_summary_v4(
self,
setup_bgp_commands,
setup_single_bgp_instance):
show = setup_bgp_commands
runner = CliRunner()
result = runner.invoke(show.cli.commands["ip"].commands["bgp"].commands["summary"])
print("Result : {}".format(result.output))
result = runner.invoke(
show.cli.commands["ip"].commands["bgp"].commands["summary"], [])
print("{}".format(result.output))
assert result.exit_code == 0
assert result.output == show_bgp_summary_v4

@pytest.mark.parametrize('setup_single_bgp_instance', ['v6'], indirect=['setup_single_bgp_instance'])
def test_bgp_summary_v6(self, setup_single_bgp_instance):
@pytest.mark.parametrize('setup_single_bgp_instance',
['v6'], indirect=['setup_single_bgp_instance'])
def test_bgp_summary_v6(
self,
setup_bgp_commands,
setup_single_bgp_instance):
show = setup_bgp_commands
runner = CliRunner()
result = runner.invoke(
show.cli.commands["ipv6"].commands["bgp"].commands["summary"], [])
print("{}".format(result.output))
assert result.exit_code == 0
assert result.output == show_bgp_summary_v6

@pytest.mark.parametrize('setup_single_bgp_instance',
[' '], indirect=['setup_single_bgp_instance'])
def test_bgp_summary_error(
self,
setup_bgp_commands,
setup_single_bgp_instance):
show = setup_bgp_commands
runner = CliRunner()
result = runner.invoke(show.cli.commands["ipv6"].commands["bgp"].commands["summary"])
print("Result : {}".format(result.output))
assert result.output == show_bgp_summary_v6
result = runner.invoke(
show.cli.commands["ipv6"].commands["bgp"].commands["summary"], [])
print("{}".format(result.output))
assert result.exit_code == 2
assert result.output == show_error_invalid_json
46 changes: 31 additions & 15 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,43 +49,59 @@
'snmp.timer',
'telemetry.timer']


@pytest.fixture
def get_cmd_module():
import config.main as config
import show.main as show

return (config, show)

@pytest.fixture
def get_show_module():
import show.main as show
return show

@pytest.fixture
def setup_single_broacom_asic():
import config.main as config
import show.main as show

device_info.get_num_npus = mock.MagicMock(return_value = 1)
device_info.get_num_npus = mock.MagicMock(return_value=1)
config._get_sonic_generated_services = \
mock.MagicMock(return_value = (generated_services_list, []))
mock.MagicMock(return_value=(generated_services_list, []))

config.asic_type = mock.MagicMock(return_value="broadcom")
config._get_device_type = mock.MagicMock(return_value="ToRRouter")

config.asic_type = mock.MagicMock(return_value = "broadcom")
config._get_device_type = mock.MagicMock(return_value = "ToRRouter")

@pytest.fixture
def setup_single_bgp_instance(monkeypatch, request):
def setup_single_bgp_instance(request):
import utilities_common.bgp_util as bgp_util

if request.param == 'v4':
bgp_summary_json = 'mock_tables/ipv4_bgp_summary.json'
bgp_summary_json = os.path.join(
test_path, 'mock_tables', 'ipv4_bgp_summary.json')
elif request.param == 'v6':
bgp_summary_json = os.path.join(
test_path, 'mock_tables', 'ipv6_bgp_summary.json')
else:
bgp_summary_json = 'mock_tables/ipv6_bgp_summary.json'
bgp_summary_json = os.path.join(
test_path, 'mock_tables', 'dummy.json')

def mock_run_bgp_command(vtysh_cmd, bgp_namespace):
with open(bgp_summary_json) as json_data:
mock_frr_data = json_data.read()
return mock_frr_data
if os.path.isfile(bgp_summary_json):
with open(bgp_summary_json) as json_data:
mock_frr_data = json_data.read()
return mock_frr_data
return ""

monkeypatch.setattr(bgp_util, "run_bgp_command", mock_run_bgp_command)
bgp_util.run_bgp_command = mock.MagicMock(
return_value=mock_run_bgp_command("", ""))


@pytest.fixture
def setup_bgp_commands():
import show.main as show
from show.bgp_frr_v4 import bgp as bgpv4
from show.bgp_frr_v6 import bgp as bgpv6

show.ip.add_command(bgpv4)
show.ipv6.add_command(bgpv6)
return show
Loading

0 comments on commit 05a87fb

Please sign in to comment.