From 36c5ac09942eef9991ce1c7891e9ff178aeba4ed Mon Sep 17 00:00:00 2001 From: "kanza.latif" Date: Fri, 17 Jan 2025 12:00:43 +0500 Subject: [PATCH] testing the global max age fucntion --- config/stp.py | 8 +++++--- tests/test_config_stp.py | 39 ++++++++++++++++++++------------------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/config/stp.py b/config/stp.py index 512d122fe0..48bb4cc29c 100644 --- a/config/stp.py +++ b/config/stp.py @@ -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") diff --git a/tests/test_config_stp.py b/tests/test_config_stp.py index 17ed12b27b..d2c5ef692a 100644 --- a/tests/test_config_stp.py +++ b/tests/test_config_stp.py @@ -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")