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 mock |
17
|
|
|
|
18
|
|
|
from st2common.constants.action import LIVEACTION_STATUS_SUCCEEDED |
19
|
|
|
from st2common.models.api.trace import TraceContext |
20
|
|
|
from base import RunnerTestCase |
21
|
|
|
import st2tests.config as tests_config |
22
|
|
|
import announcementrunner |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
mock_dispatcher = mock.Mock() |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
@mock.patch('st2common.transport.announcement.AnnouncementDispatcher.dispatch') |
29
|
|
|
class AnnouncementRunnerTestCase(RunnerTestCase): |
30
|
|
|
|
31
|
|
|
@classmethod |
32
|
|
|
def setUpClass(cls): |
33
|
|
|
tests_config.parse_args() |
34
|
|
|
|
35
|
|
|
def test_runner_creation(self, dispatch): |
36
|
|
|
runner = announcementrunner.get_runner() |
37
|
|
|
self.assertTrue(runner is not None, 'Creation failed. No instance.') |
38
|
|
|
self.assertEqual(type(runner), announcementrunner.AnnouncementRunner, |
39
|
|
|
'Creation failed. No instance.') |
40
|
|
|
self.assertEqual(runner._dispatcher.dispatch, dispatch) |
41
|
|
|
|
42
|
|
|
def test_announcement(self, dispatch): |
43
|
|
|
runner = announcementrunner.get_runner() |
44
|
|
|
runner.runner_parameters = { |
45
|
|
|
'experimental': True, |
46
|
|
|
'route': 'general' |
47
|
|
|
} |
48
|
|
|
runner.liveaction = mock.Mock(context={}) |
49
|
|
|
|
50
|
|
|
runner.pre_run() |
51
|
|
|
(status, result, _) = runner.run({'test': 'passed'}) |
52
|
|
|
|
53
|
|
|
self.assertEqual(status, LIVEACTION_STATUS_SUCCEEDED) |
54
|
|
|
self.assertTrue(result is not None) |
55
|
|
|
self.assertEqual(result['test'], 'passed') |
56
|
|
|
dispatch.assert_called_once_with('general', payload={'test': 'passed'}, |
57
|
|
|
trace_context=None) |
58
|
|
|
|
59
|
|
|
def test_announcement_no_experimental(self, dispatch): |
60
|
|
|
runner = announcementrunner.get_runner() |
61
|
|
|
runner.action = mock.Mock(ref='some.thing') |
62
|
|
|
runner.runner_parameters = { |
63
|
|
|
'route': 'general' |
64
|
|
|
} |
65
|
|
|
runner.liveaction = mock.Mock(context={}) |
66
|
|
|
|
67
|
|
|
expected_msg = 'Experimental flag is missing for action some.thing' |
68
|
|
|
self.assertRaisesRegexp(Exception, expected_msg, runner.pre_run) |
69
|
|
|
|
70
|
|
|
@mock.patch('st2common.models.api.trace.TraceContext.__new__') |
71
|
|
|
def test_announcement_with_trace(self, context, dispatch): |
72
|
|
|
runner = announcementrunner.get_runner() |
73
|
|
|
runner.runner_parameters = { |
74
|
|
|
'experimental': True, |
75
|
|
|
'route': 'general' |
76
|
|
|
} |
77
|
|
|
runner.liveaction = mock.Mock(context={ |
78
|
|
|
'trace_context': { |
79
|
|
|
'id_': 'a', |
80
|
|
|
'trace_tag': 'b' |
81
|
|
|
} |
82
|
|
|
}) |
83
|
|
|
|
84
|
|
|
runner.pre_run() |
85
|
|
|
(status, result, _) = runner.run({'test': 'passed'}) |
86
|
|
|
|
87
|
|
|
self.assertEqual(status, LIVEACTION_STATUS_SUCCEEDED) |
88
|
|
|
self.assertTrue(result is not None) |
89
|
|
|
self.assertEqual(result['test'], 'passed') |
90
|
|
|
context.assert_called_once_with(TraceContext, |
91
|
|
|
**runner.liveaction.context['trace_context']) |
92
|
|
|
dispatch.assert_called_once_with('general', payload={'test': 'passed'}, |
93
|
|
|
trace_context=context.return_value) |
94
|
|
|
|