mirror of
https://github.com/krkn-chaos/krkn.git
synced 2026-04-03 17:27:17 +00:00
fix: bind exception variable in except handlers to prevent NameError (#1198)
Signed-off-by: 1PoPTRoN <vrxn.arp1traj@gmail.com> Co-authored-by: Paige Patton <64206430+paigerube14@users.noreply.github.com>
This commit is contained in:
@@ -36,7 +36,7 @@ class TimeActionsScenarioPlugin(AbstractScenarioPlugin):
|
||||
)
|
||||
if len(not_reset) > 0:
|
||||
logging.info("Object times were not reset")
|
||||
except (RuntimeError, Exception):
|
||||
except (RuntimeError, Exception) as e:
|
||||
logging.error(
|
||||
f"TimeActionsScenarioPlugin scenario {scenario} failed with exception: {e}"
|
||||
)
|
||||
|
||||
@@ -10,7 +10,7 @@ Assisted By: Claude Code
|
||||
"""
|
||||
|
||||
import unittest
|
||||
from unittest.mock import MagicMock
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from krkn_lib.k8s import KrknKubernetes
|
||||
from krkn_lib.telemetry.ocp import KrknTelemetryOpenshift
|
||||
@@ -35,6 +35,22 @@ class TestTimeActionsScenarioPlugin(unittest.TestCase):
|
||||
self.assertEqual(result, ["time_scenarios"])
|
||||
self.assertEqual(len(result), 1)
|
||||
|
||||
@patch("krkn.scenario_plugins.time_actions.time_actions_scenario_plugin.logging")
|
||||
@patch("builtins.open", side_effect=RuntimeError("disk quota exceeded"))
|
||||
def test_exception_variable_bound_in_except_handler(self, mock_open, mock_logging):
|
||||
"""run() must bind exception variable so logging shows actual error, not NameError"""
|
||||
result = self.plugin.run(
|
||||
run_uuid="test-uuid",
|
||||
scenario="fake_scenario.yaml",
|
||||
lib_telemetry=MagicMock(),
|
||||
scenario_telemetry=MagicMock(),
|
||||
)
|
||||
|
||||
self.assertEqual(result, 1)
|
||||
logged_msg = mock_logging.error.call_args[0][0]
|
||||
self.assertIn("disk quota exceeded", logged_msg)
|
||||
self.assertNotIn("NameError", logged_msg)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user