|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from click.testing import CliRunner |
| 4 | +from swsscommon.swsscommon import SonicV2Connector |
| 5 | +from utilities_common.db import Db |
| 6 | + |
| 7 | +import show.main as show |
| 8 | + |
| 9 | +test_path = os.path.dirname(os.path.abspath(__file__)) |
| 10 | +mock_db_path = os.path.join(test_path, "bfd_input") |
| 11 | + |
| 12 | +class TestShowBfd(object): |
| 13 | + @classmethod |
| 14 | + def setup_class(cls): |
| 15 | + print("SETUP") |
| 16 | + os.environ["UTILITIES_UNIT_TESTING"] = "1" |
| 17 | + |
| 18 | + def set_db_values(self, db, key, kvs): |
| 19 | + for field, value in kvs.items(): |
| 20 | + db.set(db.STATE_DB, key, field, value) |
| 21 | + |
| 22 | + def test_bfd_show(self): |
| 23 | + runner = CliRunner() |
| 24 | + db = Db() |
| 25 | + dbconnector = db.db |
| 26 | + |
| 27 | + self.set_db_values(dbconnector, "BFD_SESSION_TABLE|default|default|10.0.1.1", |
| 28 | + {"state": "DOWN", "type": "async_active", "local_addr" : "10.0.0.1", |
| 29 | + "tx_interval" :"300", "rx_interval" : "500", "multiplier" : "3", "multihop": "true"}) |
| 30 | + self.set_db_values(dbconnector, "BFD_SESSION_TABLE|default|Ethernet12|10.0.2.1", |
| 31 | + {"state": "UP", "type": "async_active", "local_addr" : "10.0.0.1", |
| 32 | + "tx_interval" :"200", "rx_interval" : "600", "multiplier" : "3", "multihop": "false"}) |
| 33 | + self.set_db_values(dbconnector, "BFD_SESSION_TABLE|default|default|2000::10:1", |
| 34 | + {"state": "UP", "type": "async_active", "local_addr" : "2000::1", |
| 35 | + "tx_interval" :"100", "rx_interval" : "700", "multiplier" : "3", "multihop": "false"}) |
| 36 | + self.set_db_values(dbconnector, "BFD_SESSION_TABLE|VrfRed|default|10.0.1.1", |
| 37 | + {"state": "UP", "type": "async_active", "local_addr" : "10.0.0.1", |
| 38 | + "tx_interval" :"400", "rx_interval" : "500", "multiplier" : "5", "multihop": "false"}) |
| 39 | + |
| 40 | + expected_output = """\ |
| 41 | +Total number of BFD sessions: 4 |
| 42 | +Peer Addr Interface Vrf State Type Local Addr TX Interval RX Interval Multiplier Multihop |
| 43 | +----------- ----------- ------- ------- ------------ ------------ ------------- ------------- ------------ ---------- |
| 44 | +10.0.1.1 default default DOWN async_active 10.0.0.1 300 500 3 true |
| 45 | +10.0.2.1 Ethernet12 default UP async_active 10.0.0.1 200 600 3 false |
| 46 | +2000::10:1 default default UP async_active 2000::1 100 700 3 false |
| 47 | +10.0.1.1 default VrfRed UP async_active 10.0.0.1 400 500 5 false |
| 48 | +""" |
| 49 | + |
| 50 | + result = runner.invoke(show.cli.commands['bfd'].commands['summary'], [], obj=db) |
| 51 | + assert result.exit_code == 0 |
| 52 | + assert result.output == expected_output |
| 53 | + |
| 54 | + expected_output = """\ |
| 55 | +Total number of BFD sessions for peer IP 10.0.1.1: 2 |
| 56 | +Peer Addr Interface Vrf State Type Local Addr TX Interval RX Interval Multiplier Multihop |
| 57 | +----------- ----------- ------- ------- ------------ ------------ ------------- ------------- ------------ ---------- |
| 58 | +10.0.1.1 default default DOWN async_active 10.0.0.1 300 500 3 true |
| 59 | +10.0.1.1 default VrfRed UP async_active 10.0.0.1 400 500 5 false |
| 60 | +""" |
| 61 | + |
| 62 | + result = runner.invoke(show.cli.commands['bfd'].commands['peer'], ['10.0.1.1'], obj=db) |
| 63 | + assert result.exit_code == 0 |
| 64 | + assert result.output == expected_output |
| 65 | + |
| 66 | + expected_output = """\ |
| 67 | +Total number of BFD sessions for peer IP 10.0.2.1: 1 |
| 68 | +Peer Addr Interface Vrf State Type Local Addr TX Interval RX Interval Multiplier Multihop |
| 69 | +----------- ----------- ------- ------- ------------ ------------ ------------- ------------- ------------ ---------- |
| 70 | +10.0.2.1 Ethernet12 default UP async_active 10.0.0.1 200 600 3 false |
| 71 | +""" |
| 72 | + |
| 73 | + result = runner.invoke(show.cli.commands['bfd'].commands['peer'], ['10.0.2.1'], obj=db) |
| 74 | + assert result.exit_code == 0 |
| 75 | + assert result.output == expected_output |
| 76 | + |
| 77 | + expected_output = """\ |
| 78 | +No BFD sessions found for peer IP 10.0.3.1 |
| 79 | +""" |
| 80 | + |
| 81 | + result = runner.invoke(show.cli.commands['bfd'].commands['peer'], ['10.0.3.1'], obj=db) |
| 82 | + assert result.exit_code == 0 |
| 83 | + assert result.output == expected_output |
| 84 | + |
| 85 | + |
| 86 | + def test_bfd_show_no_session(self): |
| 87 | + runner = CliRunner() |
| 88 | + db = Db() |
| 89 | + |
| 90 | + expected_output = """\ |
| 91 | +Total number of BFD sessions: 0 |
| 92 | +Peer Addr Interface Vrf State Type Local Addr TX Interval RX Interval Multiplier Multihop |
| 93 | +----------- ----------- ----- ------- ------ ------------ ------------- ------------- ------------ ---------- |
| 94 | +""" |
| 95 | + |
| 96 | + result = runner.invoke(show.cli.commands['bfd'].commands['summary'], [], obj=db) |
| 97 | + assert result.exit_code == 0 |
| 98 | + assert result.output == expected_output |
0 commit comments