summaryrefslogtreecommitdiff
path: root/nova/tests/unit/api/openstack/compute/test_server_external_events.py
blob: 2ca97fc6d8645382bc22d4392950b0e1dbf103af (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#    Copyright 2014 Red Hat, Inc.
#
#    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 fixtures as fx
import mock
from oslo_utils.fixture import uuidsentinel as uuids

from nova.api.openstack.compute import server_external_events \
                                                 as server_external_events_v21
from nova import exception
from nova import objects
from nova.objects import instance as instance_obj
from nova import test
from nova.tests import fixtures
from nova.tests.unit.api.openstack import fakes


fake_instances = {
    '00000000-0000-0000-0000-000000000001': objects.Instance(id=1,
        uuid='00000000-0000-0000-0000-000000000001', host='host1'),
    '00000000-0000-0000-0000-000000000002': objects.Instance(id=2,
        uuid='00000000-0000-0000-0000-000000000002', host='host1'),
    '00000000-0000-0000-0000-000000000003': objects.Instance(id=3,
        uuid='00000000-0000-0000-0000-000000000003', host='host2'),
    '00000000-0000-0000-0000-000000000004': objects.Instance(id=4,
        uuid='00000000-0000-0000-0000-000000000004', host=None),
}
fake_instance_uuids = sorted(fake_instances.keys())
MISSING_UUID = '00000000-0000-0000-0000-000000000005'

fake_cells = [objects.CellMapping(uuid=uuids.cell1, database_connection="db1"),
              objects.CellMapping(uuid=uuids.cell2, database_connection="db2")]
fake_instance_mappings = [
        objects.InstanceMapping(cell_mapping=fake_cells[instance.id % 2],
                                instance_uuid=instance.uuid)
        for instance in fake_instances.values()]


@classmethod
def fake_get_by_filters(cls, context, filters, expected_attrs=None):
    if expected_attrs:
        # This is a regression check for bug 1645479.
        expected_attrs_set = set(expected_attrs)
        full_expected_attrs_set = set(instance_obj.INSTANCE_OPTIONAL_ATTRS)
        assert expected_attrs_set.issubset(full_expected_attrs_set), \
            ('%s is not a subset of %s' % (expected_attrs_set,
                                           full_expected_attrs_set))
    return objects.InstanceList(objects=[
        inst for inst in fake_instances.values()
        if inst.uuid in filters['uuid']])


@classmethod
def fake_get_by_instance_uuids(cls, context, uuids):
    mappings = [im for im in fake_instance_mappings
                if im.instance_uuid in uuids]
    return objects.InstanceMappingList(objects=mappings)


@mock.patch('nova.objects.InstanceMappingList.get_by_instance_uuids',
            fake_get_by_instance_uuids)
@mock.patch('nova.objects.InstanceList.get_by_filters',
            fake_get_by_filters)
class ServerExternalEventsTestV21(test.NoDBTestCase):
    server_external_events = server_external_events_v21
    invalid_error = exception.ValidationError
    wsgi_api_version = '2.1'

    def setUp(self):
        super(ServerExternalEventsTestV21, self).setUp()
        self.api = \
            self.server_external_events.ServerExternalEventsController()
        self.event_1 = {'name': 'network-vif-plugged',
                        'tag': 'foo',
                        'server_uuid': fake_instance_uuids[0],
                        'status': 'completed'}
        self.event_2 = {'name': 'network-changed',
                        'server_uuid': fake_instance_uuids[1]}
        self.default_body = {'events': [self.event_1, self.event_2]}
        self.resp_event_1 = dict(self.event_1)
        self.resp_event_1['code'] = 200
        self.resp_event_2 = dict(self.event_2)
        self.resp_event_2['code'] = 200
        self.resp_event_2['status'] = 'completed'
        self.default_resp_body = {'events': [self.resp_event_1,
                                             self.resp_event_2]}
        self.req = fakes.HTTPRequest.blank('', use_admin_context=True,
                                           version=self.wsgi_api_version)

    def _assert_call(self, body, expected_uuids, expected_events):
        with mock.patch.object(self.api.compute_api,
                               'external_instance_event') as api_method:
            response = self.api.create(self.req, body=body)

        result = response.obj
        code = response._code

        if expected_events:
            self.assertEqual(1, api_method.call_count)
            call = api_method.call_args_list[0]
            args = call[0]

            call_instances = args[1]
            call_events = args[2]
        else:
            self.assertEqual(0, api_method.call_count)
            call_instances = []
            call_events = []

        self.assertEqual(set(expected_uuids),
                         set([instance.uuid for instance in call_instances]))
        self.assertEqual(len(expected_uuids), len(call_instances))

        self.assertEqual(set(expected_events),
                         set([event.name for event in call_events]))
        self.assertEqual(len(expected_events),
                         len(call_events))

        return result, code

    def test_create(self):
        result, code = self._assert_call(self.default_body,
                                         fake_instance_uuids[:2],
                                         ['network-vif-plugged',
                                          'network-changed'])
        self.assertEqual(self.default_resp_body, result)
        self.assertEqual(200, code)

    def test_create_one_bad_instance(self):
        body = self.default_body
        body['events'][1]['server_uuid'] = MISSING_UUID
        result, code = self._assert_call(body, [fake_instance_uuids[0]],
                                         ['network-vif-plugged'])
        self.assertEqual('failed', result['events'][1]['status'])
        self.assertEqual(200, result['events'][0]['code'])
        self.assertEqual(404, result['events'][1]['code'])
        self.assertEqual(207, code)

    def test_create_event_instance_has_no_host(self):
        body = self.default_body
        body['events'][0]['server_uuid'] = fake_instance_uuids[-1]
        # the instance without host should not be passed to the compute layer
        result, code = self._assert_call(body,
                                         [fake_instance_uuids[1]],
                                         ['network-changed'])
        self.assertEqual(422, result['events'][0]['code'])
        self.assertEqual('failed', result['events'][0]['status'])
        self.assertEqual(200, result['events'][1]['code'])
        self.assertEqual(207, code)

    def test_create_no_good_instances(self):
        """Always 207 with granular codes even if all fail; see bug 1855752."""
        body = self.default_body
        body['events'][0]['server_uuid'] = MISSING_UUID
        body['events'][1]['server_uuid'] = fake_instance_uuids[-1]
        result, code = self._assert_call(body, [], [])
        self.assertEqual(404, result['events'][0]['code'])
        self.assertEqual(422, result['events'][1]['code'])
        self.assertEqual(207, code)

    def test_create_bad_status(self):
        body = self.default_body
        body['events'][1]['status'] = 'foo'
        self.assertRaises(self.invalid_error,
                          self.api.create, self.req, body=body)

    def test_create_extra_gorp(self):
        body = self.default_body
        body['events'][0]['foobar'] = 'bad stuff'
        self.assertRaises(self.invalid_error,
                          self.api.create, self.req, body=body)

    def test_create_bad_events(self):
        body = {'events': 'foo'}
        self.assertRaises(self.invalid_error,
                          self.api.create, self.req, body=body)

    def test_create_bad_body(self):
        body = {'foo': 'bar'}
        self.assertRaises(self.invalid_error,
                          self.api.create, self.req, body=body)

    def test_create_unknown_events(self):
        self.event_1['name'] = 'unkown_event'
        body = {'events': self.event_1}
        self.assertRaises(self.invalid_error,
                          self.api.create, self.req, body=body)


@mock.patch('nova.objects.InstanceMappingList.get_by_instance_uuids',
            fake_get_by_instance_uuids)
@mock.patch('nova.objects.InstanceList.get_by_filters',
            fake_get_by_filters)
class ServerExternalEventsTestV251(ServerExternalEventsTestV21):
    wsgi_api_version = '2.51'

    def test_create_with_missing_tag(self):
        body = self.default_body
        body['events'][1]['name'] = 'volume-extended'
        result, code = self._assert_call(body,
                                         [fake_instance_uuids[0]],
                                         ['network-vif-plugged'])
        self.assertEqual(200, result['events'][0]['code'])
        self.assertEqual('completed', result['events'][0]['status'])
        self.assertEqual(400, result['events'][1]['code'])
        self.assertEqual('failed', result['events'][1]['status'])
        self.assertEqual(207, code)


@mock.patch('nova.objects.InstanceMappingList.get_by_instance_uuids',
            fake_get_by_instance_uuids)
@mock.patch('nova.objects.InstanceList.get_by_filters',
            fake_get_by_filters)
class ServerExternalEventsTestV276(ServerExternalEventsTestV21):
    wsgi_api_version = '2.76'

    def setUp(self):
        super(ServerExternalEventsTestV276, self).setUp()
        self.useFixture(fx.EnvironmentVariable('OS_DEBUG', '1'))

        self.stdlog = self.useFixture(fixtures.StandardLogging())

    def test_create_with_missing_tag(self):
        body = self.default_body
        body['events'][0]['name'] = 'power-update'
        body['events'][1]['name'] = 'power-update'
        result, code = self._assert_call(body,
                                         [fake_instance_uuids[0]],
                                         ['power-update'])
        msg = "Event tag is missing for instance"
        self.assertIn(msg, self.stdlog.logger.output)
        self.assertEqual(200, result['events'][0]['code'])
        self.assertEqual('completed', result['events'][0]['status'])
        self.assertEqual(400, result['events'][1]['code'])
        self.assertEqual('failed', result['events'][1]['status'])
        self.assertEqual(207, code)

    def test_create_event_auth_pre_2_76_fails(self):
        # Negative test to make sure you can't create 'power-update'
        # before 2.76.
        body = self.default_body
        body['events'][0]['name'] = 'power-update'
        body['events'][1]['name'] = 'power-update'
        req = fakes.HTTPRequestV21.blank(
            '/os-server-external-events', version='2.75')
        exp = self.assertRaises(
                exception.ValidationError,
                self.api.create,
                req,
                body=body)
        self.assertIn('Invalid input for field/attribute name.', str(exp))


@mock.patch('nova.objects.InstanceMappingList.get_by_instance_uuids',
            fake_get_by_instance_uuids)
@mock.patch('nova.objects.InstanceList.get_by_filters',
            fake_get_by_filters)
class ServerExternalEventsTestV282(ServerExternalEventsTestV21):
    wsgi_api_version = '2.82'

    def setUp(self):
        super(ServerExternalEventsTestV282, self).setUp()
        self.useFixture(fx.EnvironmentVariable('OS_DEBUG', '1'))
        self.stdlog = self.useFixture(fixtures.StandardLogging())

    def test_accelerator_request_bound_event(self):
        body = self.default_body
        event_name = 'accelerator-request-bound'
        body['events'][0]['name'] = event_name  # event 0 has a tag
        body['events'][1]['name'] = event_name  # event 1 has no tag

        result, code = self._assert_call(
            body, [fake_instance_uuids[0]], [event_name])

        self.assertEqual(200, result['events'][0]['code'])
        self.assertEqual('completed', result['events'][0]['status'])

        msg = "Event tag is missing for instance"
        self.assertIn(msg, self.stdlog.logger.output)
        self.assertEqual(400, result['events'][1]['code'])
        self.assertEqual('failed', result['events'][1]['status'])
        self.assertEqual(207, code)