Skip to content

Commit

Permalink
added pytests
Browse files Browse the repository at this point in the history
  • Loading branch information
anilkpandey committed Dec 9, 2020
1 parent 38fd881 commit 2a9ab88
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 8 deletions.
7 changes: 3 additions & 4 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1939,12 +1939,11 @@ def mac(ctx, redis_unix_socket_path):

@mac.command('aging_time')
@click.argument('interval', metavar='<aging interval in sec [0-1000000], 0 to disable aging>', required=True, type=int)
@click.pass_context
def set_aging_time(ctx, interval):
db = ctx.obj['db']
@clicommon.pass_db
def set_aging_time(db, interval):
if interval not in range(0,1000001):
ctx.fail("Aging timer must be in range [0-1000000]")
db.set_entry('SWITCH', 'switch', {'fdb_aging_time': interval})
db.cfgdb.set_entry('SWITCH', 'switch', {'fdb_aging_time': interval})

def mac_address_is_valid(mac):
"""Check if the mac address is valid
Expand Down
8 changes: 4 additions & 4 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,13 +653,12 @@ def mac(ctx, vlan, port, verbose):
run_command(cmd, display_cmd=verbose)

@mac.command()
def aging_time():
@clicommon.pass_db
def aging_time(db):
"""Show MAC Aging-Time"""
config_db = ConfigDBConnector()
config_db.connect()

# Fetching data from config_db for SWITCH
switch_table = config_db.get_table('SWITCH')
switch_table = db.cfgdb.get_table('SWITCH')
switch_keys = switch_table.keys()

age_time = 0
Expand All @@ -669,6 +668,7 @@ def aging_time():
age_time = switch_table[key]['fdb_aging_time']
except KeyError:
age_time = '0'
pass
output = 'Mac Aging-Time : '
output += ('%s seconds\n' % (str(age_time)))
click.echo(output)
Expand Down
38 changes: 38 additions & 0 deletions tests/fdb_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import traceback
from unittest import mock

from click.testing import CliRunner

import config.main as config
import show.main as show
from utilities_common.db import Db

show_mac_aging_time="""\
Mac Aging-Time : 300 seconds
"""

class TestFdb(object):
@classmethod
def setup_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "1"
print("SETUP")

def test_fdb_aging_time(self):
runner = CliRunner()
db = Db()
result = runner.invoke(config.config.commands["mac"].commands["aging_time"], ["300"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
result = runner.invoke(show.cli.commands["mac"].commands["aging-time"], [], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == show_mac_aging_time

@classmethod
def teardown_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "0"
print("TEARDOWN")
38 changes: 38 additions & 0 deletions tests/vlan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@
Vlan2000 2000 Ethernet28 untagged
"""

show_vlan_config_output_range="""\
Name VID Member Mode
-------- ----- ---------- --------
Vlan1000 1000 Ethernet4 untagged
Vlan1000 1000 Ethernet8 untagged
Vlan1000 1000 Ethernet12 untagged
Vlan1000 1000 Ethernet16 untagged
Vlan2000 2000 Ethernet24 untagged
Vlan2000 2000 Ethernet28 untagged
Vlan3001 3001 Ethernet4 tagged
Vlan3001 3001 Ethernet8 tagged
Vlan3002 3002 Ethernet4 tagged
Vlan3002 3002 Ethernet8 tagged
Vlan3003 3003 Ethernet4 tagged
Vlan3003 3003 Ethernet8 tagged
"""

show_vlan_config_in_alias_mode_output="""\
Name VID Member Mode
-------- ----- -------- --------
Expand Down Expand Up @@ -519,6 +536,27 @@ def test_config_vlan_add_del_dhcp_relay_dest(self):
print(result.output)
assert result.output == show_vlan_brief_output

def test_vlan_config_range(self):
runner = CliRunner()
db = Db()
result = runner.invoke(config.config.commands["vlan"].commands["range"].commands["add"], ["3001", "3003"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["range"].commands["add"], ["3001", "3003", "Ethernet4"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["range"].commands["add"], ["3001", "3003", "Ethernet8"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
result = runner.invoke(show.cli.commands["vlan"].commands["config"], [], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == show_vlan_config_output_range

def test_config_vlan_remove_nonexist_dhcp_relay_dest(self):
runner = CliRunner()

Expand Down

0 comments on commit 2a9ab88

Please sign in to comment.