summaryrefslogtreecommitdiff
path: root/taskflow/tests/unit/test_check_transition.py
blob: d8bb59466bb878ba68b7eb6ae06b59ec4b49b20f (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: utf-8 -*-

#    Copyright (C) 2013 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 taskflow import exceptions as exc
from taskflow import states
from taskflow import test


class TransitionTest(test.TestCase):

    _DISALLOWED_TPL = "Transition from '%s' to '%s' was found to be disallowed"
    _NOT_IGNORED_TPL = "Transition from '%s' to '%s' was not ignored"

    def assertTransitionAllowed(self, from_state, to_state):
        msg = self._DISALLOWED_TPL % (from_state, to_state)
        self.assertTrue(self.check_transition(from_state, to_state), msg=msg)

    def assertTransitionIgnored(self, from_state, to_state):
        msg = self._NOT_IGNORED_TPL % (from_state, to_state)
        self.assertFalse(self.check_transition(from_state, to_state), msg=msg)

    def assertTransitionForbidden(self, from_state, to_state):
        self.assertRaisesRegex(exc.InvalidState,
                               self.transition_exc_regexp,
                               self.check_transition, from_state, to_state)

    def assertTransitions(self, from_state, allowed=None, ignored=None,
                          forbidden=None):
        for a in allowed or []:
            self.assertTransitionAllowed(from_state, a)
        for i in ignored or []:
            self.assertTransitionIgnored(from_state, i)
        for f in forbidden or []:
            self.assertTransitionForbidden(from_state, f)


class CheckFlowTransitionTest(TransitionTest):

    def setUp(self):
        super(CheckFlowTransitionTest, self).setUp()
        self.check_transition = states.check_flow_transition
        self.transition_exc_regexp = '^Flow transition.*not allowed'

    def test_to_same_state(self):
        self.assertTransitionIgnored(states.SUCCESS, states.SUCCESS)

    def test_rerunning_allowed(self):
        self.assertTransitionAllowed(states.SUCCESS, states.RUNNING)

    def test_no_resuming_from_pending(self):
        self.assertTransitionIgnored(states.PENDING, states.RESUMING)

    def test_resuming_from_running(self):
        self.assertTransitionAllowed(states.RUNNING, states.RESUMING)

    def test_bad_transition_raises(self):
        self.assertTransitionForbidden(states.FAILURE, states.SUCCESS)


class CheckTaskTransitionTest(TransitionTest):

    def setUp(self):
        super(CheckTaskTransitionTest, self).setUp()
        self.check_transition = states.check_task_transition
        self.transition_exc_regexp = '^Task transition.*not allowed'

    def test_from_pending_state(self):
        self.assertTransitions(from_state=states.PENDING,
                               allowed=(states.RUNNING,),
                               ignored=(states.PENDING, states.REVERTING,
                                        states.SUCCESS, states.FAILURE,
                                        states.REVERTED))

    def test_from_running_state(self):
        self.assertTransitions(from_state=states.RUNNING,
                               allowed=(states.SUCCESS, states.FAILURE,),
                               ignored=(states.REVERTING, states.RUNNING,
                                        states.PENDING, states.REVERTED))

    def test_from_success_state(self):
        self.assertTransitions(from_state=states.SUCCESS,
                               allowed=(states.REVERTING,),
                               ignored=(states.RUNNING, states.SUCCESS,
                                        states.PENDING, states.FAILURE,
                                        states.REVERTED))

    def test_from_failure_state(self):
        self.assertTransitions(from_state=states.FAILURE,
                               allowed=(states.REVERTING,),
                               ignored=(states.FAILURE, states.RUNNING,
                                        states.PENDING,
                                        states.SUCCESS, states.REVERTED))

    def test_from_reverting_state(self):
        self.assertTransitions(from_state=states.REVERTING,
                               allowed=(states.REVERT_FAILURE,
                                        states.REVERTED),
                               ignored=(states.RUNNING, states.REVERTING,
                                        states.PENDING, states.SUCCESS))

    def test_from_reverted_state(self):
        self.assertTransitions(from_state=states.REVERTED,
                               allowed=(states.PENDING,),
                               ignored=(states.REVERTING, states.REVERTED,
                                        states.RUNNING,
                                        states.SUCCESS, states.FAILURE))


class CheckRetryTransitionTest(CheckTaskTransitionTest):

    def setUp(self):
        super(CheckRetryTransitionTest, self).setUp()
        self.check_transition = states.check_retry_transition
        self.transition_exc_regexp = '^Retry transition.*not allowed'

    def test_from_success_state(self):
        self.assertTransitions(from_state=states.SUCCESS,
                               allowed=(states.REVERTING, states.RETRYING),
                               ignored=(states.RUNNING, states.SUCCESS,
                                        states.PENDING, states.FAILURE,
                                        states.REVERTED))

    def test_from_retrying_state(self):
        self.assertTransitions(from_state=states.RETRYING,
                               allowed=(states.RUNNING,),
                               ignored=(states.RETRYING, states.SUCCESS,
                                        states.PENDING, states.FAILURE,
                                        states.REVERTED))