summaryrefslogtreecommitdiff
path: root/heatclient/tests/unit/test_events.py
blob: b63aebe32d3e6499813a870535bfb5c088393fde (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
142
143
144
145
146
147
148
149
150
151
152
153
# Copyright 2013 IBM Corp.
#
#    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.

import mock
import testtools

from heatclient.common import utils
from heatclient.v1 import events


class EventManagerTest(testtools.TestCase):

    def test_list_event(self):
        stack_id = 'teststack',
        resource_name = 'testresource'
        manager = events.EventManager(None)
        with mock.patch('heatclient.v1.events.EventManager._resolve_stack_id')\
                as mock_re:
            mock_re.return_value = 'teststack/abcd1234'

            manager._list = mock.MagicMock()
            manager.list(stack_id, resource_name)
            # Make sure url is correct.
            manager._list.assert_called_once_with(
                '/stacks/teststack/abcd1234/'
                'resources/testresource/events',
                "events")
            mock_re.assert_called_once_with(stack_id)

    def test_list_event_with_unicode_resource_name(self):
        stack_id = 'teststack',
        resource_name = u'\u5de5\u4f5c'
        manager = events.EventManager(None)
        with mock.patch('heatclient.v1.events.EventManager._resolve_stack_id')\
                as mock_re:
            mock_re.return_value = 'teststack/abcd1234'

            manager._list = mock.MagicMock()
            manager.list(stack_id, resource_name)
            # Make sure url is correct.
            manager._list.assert_called_once_with(
                '/stacks/teststack/abcd1234/'
                'resources/%E5%B7%A5%E4%BD%9C/'
                'events', "events")
            mock_re.assert_called_once_with(stack_id)

    def test_list_event_with_none_resource_name(self):
        stack_id = 'teststack',
        manager = events.EventManager(None)
        manager._list = mock.MagicMock()
        manager.list(stack_id)
        # Make sure url is correct.
        manager._list.assert_called_once_with('/stacks/teststack/'
                                              'events', "events")

    def test_list_event_with_kwargs(self):
        stack_id = 'teststack',
        resource_name = 'testresource'
        kwargs = {'limit': 2,
                  'marker': '6d6935f4-0ae5',
                  'filters': {
                      'resource_action': 'CREATE',
                      'resource_status': 'COMPLETE'
                  }}
        manager = events.EventManager(None)
        manager = events.EventManager(None)
        with mock.patch('heatclient.v1.events.EventManager._resolve_stack_id')\
                as mock_re:
            mock_re.return_value = 'teststack/abcd1234'

            manager._list = mock.MagicMock()
            manager.list(stack_id, resource_name, **kwargs)
            # Make sure url is correct.
            self.assertEqual(1, manager._list.call_count)
            args = manager._list.call_args
            self.assertEqual(2, len(args[0]))
            url, param = args[0]
            self.assertEqual("events", param)
            base_url, query_params = utils.parse_query_url(url)
            expected_base_url = ('/stacks/teststack/abcd1234/'
                                 'resources/testresource/events')
            self.assertEqual(expected_base_url, base_url)
            expected_query_dict = {'marker': ['6d6935f4-0ae5'],
                                   'limit': ['2'],
                                   'resource_action': ['CREATE'],
                                   'resource_status': ['COMPLETE']}
            self.assertEqual(expected_query_dict, query_params)
            mock_re.assert_called_once_with(stack_id)

    @mock.patch('heatclient.v1.events.EventManager._resolve_stack_id')
    @mock.patch('heatclient.common.utils.get_response_body')
    def test_get_event(self, mock_utils, mock_re):
        fields = {'stack_id': 'teststack',
                  'resource_name': 'testresource',
                  'event_id': '1'}

        class FakeAPI(object):
            """Fake API and ensure request url is correct."""

            def json_request(self, *args, **kwargs):
                expect = ('GET',
                          '/stacks/teststack/abcd1234/resources'
                          '/testresource/events/1')
                assert args == expect
                return {}, {'event': []}

            def get(self, *args, **kwargs):
                pass

        manager = events.EventManager(FakeAPI())
        with mock.patch('heatclient.v1.events.Event'):
            mock_utils.return_value = {'event': []}
            mock_re.return_value = 'teststack/abcd1234'
            manager.get(**fields)
            mock_re.assert_called_once_with('teststack')

    @mock.patch('heatclient.v1.events.EventManager._resolve_stack_id')
    @mock.patch('heatclient.common.utils.get_response_body')
    def test_get_event_with_unicode_resource_name(self, mock_utils, mock_re):
        fields = {'stack_id': 'teststack',
                  'resource_name': u'\u5de5\u4f5c',
                  'event_id': '1'}

        class FakeAPI(object):
            """Fake API and ensure request url is correct."""

            def json_request(self, *args, **kwargs):
                expect = ('GET',
                          '/stacks/teststack/abcd1234/resources'
                          '/%E5%B7%A5%E4%BD%9C/events/1')
                assert args == expect
                return {}, {'event': []}

            def get(self, *args, **kwargs):
                pass

        manager = events.EventManager(FakeAPI())
        with mock.patch('heatclient.v1.events.Event'):
            mock_utils.return_value = {'event': []}
            mock_re.return_value = 'teststack/abcd1234'
            manager.get(**fields)
            mock_re.assert_called_once_with('teststack')