From 081e4e9eedf3dd15879eec13d3c05822ab060e64 Mon Sep 17 00:00:00 2001 From: Ruby Loo Date: Thu, 10 Dec 2015 02:10:14 +0000 Subject: [PATCH] Add FSM.is_stable() method This adds an FSM.is_stable(state) method which returns True if the state is stable and False otherwise. Invalid states will cause an exception to be raised. Change-Id: Ic6a3c937be9370477fdb1fe2c462f6672497f838 --- ironic/common/fsm.py | 14 +++++++++++++- ironic/tests/unit/common/test_fsm.py | 9 +++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ironic/common/fsm.py b/ironic/common/fsm.py index d105081431..52b9c4d921 100644 --- a/ironic/common/fsm.py +++ b/ironic/common/fsm.py @@ -65,6 +65,18 @@ class FSM(machines.FiniteMachine): def target_state(self): return self._target_state + def is_stable(self, state): + """Is the state stable? + + :param state: the state of interest + :raises: InvalidState if the state is invalid + :returns True if it is a stable state; False otherwise + """ + try: + return self._states[state]['stable'] + except KeyError: + raise excp.InvalidState(_("State '%s' does not exist") % state) + @_translate_excp def add_state(self, state, on_enter=None, on_exit=None, target=None, terminal=None, stable=False): @@ -110,7 +122,7 @@ class FSM(machines.FiniteMachine): if target not in self._states: raise excp.InvalidState( _("Target state '%s' does not exist") % target) - if not self._states[target]['stable']: + if not self.is_stable(target): raise excp.InvalidState( _("Target state '%s' is not a 'stable' state") % target) diff --git a/ironic/tests/unit/common/test_fsm.py b/ironic/tests/unit/common/test_fsm.py index 2d523544a2..e1dc416f9e 100644 --- a/ironic/tests/unit/common/test_fsm.py +++ b/ironic/tests/unit/common/test_fsm.py @@ -30,6 +30,15 @@ class FSMTest(base.TestCase): m.add_transition('wakeup', 'working', 'walk') self.fsm = m + def test_is_stable(self): + self.assertTrue(self.fsm.is_stable('working')) + + def test_is_stable_not(self): + self.assertFalse(self.fsm.is_stable('daydream')) + + def test_is_stable_invalid_state(self): + self.assertRaises(excp.InvalidState, self.fsm.is_stable, 'foo') + def test_target_state_stable(self): # Test to verify that adding a new state with a 'target' state pointing # to a 'stable' state does not raise an exception