Completed
Pull Request — master (#2304)
by Arma
07:07
created

st2tests.mocks.MockActionRunner   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %
Metric Value
wmc 5
dl 0
loc 36
rs 10
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import json
17
18
from st2actions.runners import ActionRunner
19
from st2common.constants.action import (LIVEACTION_STATUS_SUCCEEDED)
20
21
__all__ = [
22
    'get_runner',
23
    'MockActionRunner'
24
]
25
26
27
def get_runner():
28
    return MockActionRunner()
29
30
31
class MockActionRunner(ActionRunner):
32
    def __init__(self):
33
        super(MockActionRunner, self).__init__(runner_id='1')
34
35
        self.pre_run_called = False
36
        self.run_called = False
37
        self.post_run_called = False
38
39
    def pre_run(self):
40
        self.pre_run_called = True
41
42
    def run(self, action_params):
43
        self.run_called = True
44
        result = {}
45
46
        if self.runner_parameters.get('raise', False):
47
            raise Exception('Raise required.')
48
49
        default_result = {
50
            'ran': True,
51
            'action_params': action_params
52
        }
53
        default_context = {
54
            'third_party_system': {
55
                'ref_id': '1234'
56
            }
57
        }
58
59
        status = self.runner_parameters.get('mock_status', LIVEACTION_STATUS_SUCCEEDED)
60
        result = self.runner_parameters.get('mock_result', default_result)
61
        context = self.runner_parameters.get('mock_context', default_context)
62
63
        return (status, json.dumps(result), context)
64
65
    def post_run(self, status, result):
66
        self.post_run_called = True
67