Skip to content

Commit

Permalink
testing the global max age fucntion
Browse files Browse the repository at this point in the history
  • Loading branch information
kanza-latif committed Jan 17, 2025
1 parent bb5790b commit 36c5ac0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
8 changes: 5 additions & 3 deletions config/stp.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,26 +692,28 @@ def stp_global_hello_interval(_db, hello_interval):
@clicommon.pass_db
def stp_global_max_age(_db, max_age):
"""Configure STP global max_age"""
ctx = click.get_current_context()
ctx = click.get_current_context() # Ensure we are getting the correct context
db = _db.cfgdb

# Check if global STP is enabled
check_if_global_stp_enabled(db, ctx)

current_mode = get_global_stp_mode(db)

if current_mode == "pvst":
# Validate max_age for PVST mode
is_valid_max_age(ctx, max_age)
is_valid_stp_global_parameters(ctx, db, "max_age", max_age)
update_stp_vlan_parameter(ctx, db, "max_age", max_age)
db.mod_entry('STP', "GLOBAL", {'max_age': max_age})

elif current_mode == "mst":
# Validate max_age for MST mode
is_valid_max_age(ctx, max_age)
db.mod_entry('STP_MST', "GLOBAL", {'max_age': max_age})
# db.mod_entry('STP_MST', "STP_MST|GLOBAL", {'max_age': max_age})
# update_mst_instance_parameters(ctx, db, 'max_age', max_age)

else:
# If the mode is invalid, fail with an error message
ctx.fail("Invalid STP mode configuration, no mode is enabled")


Expand Down
39 changes: 20 additions & 19 deletions tests/test_config_stp.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,38 +564,39 @@ def test_stp_global_max_hops_invalid_mode(mock_db):


def test_stp_global_max_age_mst_mode(mock_db, mock_ctx):
"""Test for the 'mst' mode scenario."""
"""Test for MST mode configuration."""
mock_ctx.fail = MagicMock() # Mock the fail method of the context
mock_db.cfgdb.get_entry.return_value = {'mode': 'mst'} # Simulate MST mode

# Mocking the behavior of the database and context
mock_ctx.fail = MagicMock()
mock_db.cfgdb.get_global_stp_mode.return_value = "mst" # Simulate MST mode

# Mock the helper functions
# Mock helper functions
with patch('config.stp.check_if_global_stp_enabled') as check_if_global_stp_enabled, \
patch('config.stp.is_valid_max_age') as is_valid_max_age:

# Create the CliRunner instance
# Create a runner instance
runner = CliRunner()

# Use invoke to pass the argument to the command
result = runner.invoke(stp_global_max_age, ['25'], obj=mock_db) # Simulate command-line input
# Invoke the command with max_age argument
result = runner.invoke(stp_global_max_age, ['25'], obj=mock_db)

# Assert the command ran successfully
# Check the command ran successfully
assert result.exit_code == 0

# Assert that the helper functions are called correctly
# Check that helper functions were called
check_if_global_stp_enabled.assert_called_once()
is_valid_max_age.assert_called_once_with(mock_ctx, 25)
mock_db.cfgdb.mod_entry.assert_called_once_with('STP_MST', "GLOBAL", {'max_age': 25})

# Check that database entries were modified for MST mode
mock_db.cfgdb.mod_entry.assert_called_once_with('STP_MST', 'GLOBAL', {'max_age': 25})

def test_stp_global_max_age_invalid_mode(mock_db, mock_ctx):
"""Test for the case when no valid STP mode is enabled (invalid mode)."""

# Mocking the behavior of the database and context
def test_stp_global_max_age_invalid_mode(mock_db, mock_ctx):
"""Test for invalid mode configuration."""
mock_ctx.fail = MagicMock()
mock_db.cfgdb.get_global_stp_mode.return_value = "invalid_mode" # Simulate an invalid mode
mock_db.cfgdb.get_entry.return_value = {'mode': 'invalid_mode'} # Simulate invalid mode

# Expect SystemExit due to invalid mode
with pytest.raises(SystemExit):
stp_global_max_age(mock_db, 30)

# Expecting SystemExit due to the failure in the function
with pytest.raises(SystemExit): # We expect a failure (SystemExit) due to invalid mode
stp_global_max_age(mock_db, 30) # Test with max_age = 30
# Check that fail method was called on invalid mode
mock_ctx.fail.assert_called_once_with("Invalid STP mode configuration, no mode is enabled")

0 comments on commit 36c5ac0

Please sign in to comment.