Test Failed
Push — master ( e380d0...f5671d )
by W
02:58
created

tests/unit/test_announcementrunner.py (1 issue)

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
from __future__ import absolute_import
17
import mock
18
19
from st2common.constants.action import LIVEACTION_STATUS_SUCCEEDED
20
from st2common.models.api.trace import TraceContext
21
from st2tests.base import RunnerTestCase
22
import st2tests.config as tests_config
23
from announcement_runner import announcement_runner
24
25
26
mock_dispatcher = mock.Mock()
27
28
29
@mock.patch('st2common.transport.announcement.AnnouncementDispatcher.dispatch')
30
class AnnouncementRunnerTestCase(RunnerTestCase):
31
32
    @classmethod
33
    def setUpClass(cls):
34
        tests_config.parse_args()
35
36
    def test_runner_creation(self, dispatch):
37
        runner = announcement_runner.get_runner()
38
        self.assertTrue(runner is not None, 'Creation failed. No instance.')
39
        self.assertEqual(type(runner), announcement_runner.AnnouncementRunner,
40
                         'Creation failed. No instance.')
41
        self.assertEqual(runner._dispatcher.dispatch, dispatch)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _dispatcher was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
42
43
    def test_announcement(self, dispatch):
44
        runner = announcement_runner.get_runner()
45
        runner.runner_parameters = {
46
            'experimental': True,
47
            'route': 'general'
48
        }
49
        runner.liveaction = mock.Mock(context={})
50
51
        runner.pre_run()
52
        (status, result, _) = runner.run({'test': 'passed'})
53
54
        self.assertEqual(status, LIVEACTION_STATUS_SUCCEEDED)
55
        self.assertTrue(result is not None)
56
        self.assertEqual(result['test'], 'passed')
57
        dispatch.assert_called_once_with('general', payload={'test': 'passed'},
58
                                         trace_context=None)
59
60
    def test_announcement_no_experimental(self, dispatch):
61
        runner = announcement_runner.get_runner()
62
        runner.action = mock.Mock(ref='some.thing')
63
        runner.runner_parameters = {
64
            'route': 'general'
65
        }
66
        runner.liveaction = mock.Mock(context={})
67
68
        expected_msg = 'Experimental flag is missing for action some.thing'
69
        self.assertRaisesRegexp(Exception, expected_msg, runner.pre_run)
70
71
    @mock.patch('st2common.models.api.trace.TraceContext.__new__')
72
    def test_announcement_with_trace(self, context, dispatch):
73
        runner = announcement_runner.get_runner()
74
        runner.runner_parameters = {
75
            'experimental': True,
76
            'route': 'general'
77
        }
78
        runner.liveaction = mock.Mock(context={
79
            'trace_context': {
80
                'id_': 'a',
81
                'trace_tag': 'b'
82
            }
83
        })
84
85
        runner.pre_run()
86
        (status, result, _) = runner.run({'test': 'passed'})
87
88
        self.assertEqual(status, LIVEACTION_STATUS_SUCCEEDED)
89
        self.assertTrue(result is not None)
90
        self.assertEqual(result['test'], 'passed')
91
        context.assert_called_once_with(TraceContext,
92
                                        **runner.liveaction.context['trace_context'])
93
        dispatch.assert_called_once_with('general', payload={'test': 'passed'},
94
                                         trace_context=context.return_value)
95