summaryrefslogtreecommitdiff
path: root/ironic/tests/unit/common/test_fsm.py
blob: 31c2f9e9d424b22e9c0c947b176c25fad019e71f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#    Copyright (C) 2014 Yahoo! Inc. All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from ironic.common import exception as excp
from ironic.common import fsm
from ironic.tests import base


class FSMTest(base.TestCase):
    def setUp(self):
        super(FSMTest, self).setUp()
        m = fsm.FSM()
        m.add_state('working', stable=True)
        m.add_state('daydream')
        m.add_state('wakeup', target='working')
        m.add_state('play', stable=True)
        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
        self.fsm.add_state('foo', target='working')
        self.fsm.default_start_state = 'working'
        self.fsm.initialize()

    def test__validate_target_state(self):
        # valid
        self.fsm._validate_target_state('working')

        # target doesn't exist
        self.assertRaisesRegex(excp.InvalidState, "does not exist",
                               self.fsm._validate_target_state, 'new state')

        # target isn't a stable state
        self.assertRaisesRegex(excp.InvalidState, "stable",
                               self.fsm._validate_target_state, 'daydream')

    def test_initialize(self):
        # no start state
        self.assertRaises(excp.InvalidState, self.fsm.initialize)

        # no target state
        self.fsm.initialize('working')
        self.assertEqual('working', self.fsm.current_state)
        self.assertIsNone(self.fsm.target_state)

        # default target state
        self.fsm.initialize('wakeup')
        self.assertEqual('wakeup', self.fsm.current_state)
        self.assertEqual('working', self.fsm.target_state)

        # specify (it overrides default) target state
        self.fsm.initialize('wakeup', 'play')
        self.assertEqual('wakeup', self.fsm.current_state)
        self.assertEqual('play', self.fsm.target_state)

        # specify an invalid target state
        self.assertRaises(excp.InvalidState, self.fsm.initialize,
                          'wakeup', 'daydream')

    def test_process_event(self):
        # default target state
        self.fsm.initialize('wakeup')
        self.fsm.process_event('walk')
        self.assertEqual('working', self.fsm.current_state)
        self.assertIsNone(self.fsm.target_state)

        # specify (it overrides default) target state
        self.fsm.initialize('wakeup')
        self.fsm.process_event('walk', 'play')
        self.assertEqual('working', self.fsm.current_state)
        self.assertEqual('play', self.fsm.target_state)

        # specify an invalid target state
        self.fsm.initialize('wakeup')
        self.assertRaises(excp.InvalidState, self.fsm.process_event,
                          'walk', 'daydream')