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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
# Copyright 2012 Nebula, Inc.
# 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 nova.conf
from nova import objects
from nova.tests.functional.api_sample_tests import test_servers
from oslo_utils.fixture import uuidsentinel
from unittest import mock
CONF = nova.conf.CONF
fake_aggregate = {
'deleted': 0,
'deleted_at': None,
'created_at': None,
'updated_at': None,
'id': 123,
'uuid': uuidsentinel.fake_aggregate,
'name': 'us-west',
'hosts': ['host01'],
'metadetails': {'availability_zone': 'us-west'},
}
class ShelveJsonTest(test_servers.ServersSampleBase):
# The 'os_compute_api:os-shelve:shelve_offload' policy is admin-only
ADMIN_API = True
sample_dir = "os-shelve"
def setUp(self):
super(ShelveJsonTest, self).setUp()
# Don't offload instance, so we can test the offload call.
CONF.set_override('shelved_offload_time', -1)
def _test_server_action(self, uuid, template, action, subs=None):
subs = subs or {}
subs.update({'action': action})
response = self._do_post('servers/%s/action' % uuid,
template, subs)
self.assertEqual(202, response.status_code)
self.assertEqual("", response.text)
def test_shelve(self):
uuid = self._post_server()
self._test_server_action(uuid, 'os-shelve', 'shelve')
def test_shelve_offload(self):
uuid = self._post_server()
self._test_server_action(uuid, 'os-shelve', 'shelve')
self._test_server_action(uuid, 'os-shelve-offload', 'shelveOffload')
def test_unshelve(self):
uuid = self._post_server()
self._test_server_action(uuid, 'os-shelve', 'shelve')
self._test_server_action(uuid, 'os-unshelve', 'unshelve')
class UnshelveJson277Test(ShelveJsonTest):
ADMIN_API = False
sample_dir = "os-shelve"
microversion = '2.77'
scenarios = [('v2_77', {'api_major_version': 'v2.1'})]
def setUp(self):
super(UnshelveJson277Test, self).setUp()
# Almost all next tests require the instance to be shelve offloaded.
# So shelve offload the instance and skip the shelve_offload_test
# below.
CONF.set_override('shelved_offload_time', 0)
def test_shelve_offload(self):
# Skip this test as the instance is already shelve offloaded.
pass
def test_unshelve_with_az(self):
uuid = self._post_server()
self._test_server_action(uuid, 'os-shelve', 'shelve')
self._test_server_action(
uuid,
'os-unshelve-az',
'unshelve',
subs={"availability_zone": "us-west"}
)
class UnshelveJson291Test(UnshelveJson277Test):
ADMIN_API = True
sample_dir = "os-shelve"
microversion = '2.91'
scenarios = [('v2_91', {'api_major_version': 'v2.1'})]
def _test_server_action_invalid(
self, uuid, template, action, subs=None, msg=None):
subs = subs or {}
subs.update({'action': action})
response = self._do_post('servers/%s/action' % uuid,
template, subs)
self.assertEqual(400, response.status_code)
self.assertIn(msg, response.text)
def test_unshelve_with_non_valid_host(self):
"""Ensure an exception rise if host is invalid and
a http 400 error
"""
uuid = self._post_server()
self._test_server_action(uuid, 'os-shelve', 'shelve')
self._test_server_action_invalid(
uuid, 'os-unshelve-host',
'unshelve',
subs={'host': 'host01'},
msg='Compute host host01 could not be found.')
@mock.patch('nova.objects.aggregate._get_by_host_from_db')
@mock.patch('nova.objects.ComputeNodeList.get_all_by_host')
def test_unshelve_with_valid_host(
self, compute_node_get_all_by_host, mock_api_get_by_host):
"""Ensure we can unshelve to a host
"""
# Put compute in the correct az
mock_api_get_by_host.return_value = [fake_aggregate]
uuid = self._post_server()
self._test_server_action(uuid, 'os-shelve', 'shelve')
fake_computes = objects.ComputeNodeList(
objects=[
objects.ComputeNode(
host='host01',
uuid=uuidsentinel.host1,
hypervisor_hostname='host01')
]
)
compute_node_get_all_by_host.return_value = fake_computes
self._test_server_action(
uuid,
'os-unshelve-host',
'unshelve',
subs={'host': 'host01'}
)
@mock.patch('nova.objects.aggregate._get_by_host_from_db')
@mock.patch('nova.objects.ComputeNodeList.get_all_by_host')
def test_unshelve_with_az_and_host(
self, compute_node_get_all_by_host, mock_api_get_by_host):
"""Ensure we can unshelve to a host and az
"""
# Put compute in the correct az
mock_api_get_by_host.return_value = [fake_aggregate]
uuid = self._post_server()
self._test_server_action(uuid, 'os-shelve', 'shelve')
fake_computes = objects.ComputeNodeList(
objects=[
objects.ComputeNode(
host='host01',
uuid=uuidsentinel.host1,
hypervisor_hostname='host01')
]
)
compute_node_get_all_by_host.return_value = fake_computes
self._test_server_action(
uuid,
'os-unshelve-host',
'unshelve',
subs={'host': 'host01', 'availability_zone': 'us-west'},
)
@mock.patch('nova.objects.aggregate._get_by_host_from_db')
@mock.patch('nova.objects.ComputeNodeList.get_all_by_host')
def test_unshelve_with_unpin_az_and_host(
self, compute_node_get_all_by_host, mock_api_get_by_host):
"""Ensure we can unshelve to a host and az
"""
# Put compute in the correct az
mock_api_get_by_host.return_value = [fake_aggregate]
uuid = self._post_server()
self._test_server_action(uuid, 'os-shelve', 'shelve')
fake_computes = objects.ComputeNodeList(
objects=[
objects.ComputeNode(
host='host01',
uuid=uuidsentinel.host1,
hypervisor_hostname='host01')
]
)
compute_node_get_all_by_host.return_value = fake_computes
self._test_server_action(
uuid,
'os-unshelve-host-and-unpin-az',
'unshelve',
subs={'host': 'host01'},
)
@mock.patch('nova.objects.aggregate._get_by_host_from_db')
@mock.patch('nova.objects.ComputeNodeList.get_all_by_host')
def test_unshelve_with_unpin_az(
self, compute_node_get_all_by_host, mock_api_get_by_host):
"""Ensure we can unpin an az
"""
# Put compute in the correct az
mock_api_get_by_host.return_value = [fake_aggregate]
uuid = self._post_server()
self._test_server_action(uuid, 'os-shelve', 'shelve')
fake_computes = objects.ComputeNodeList(
objects=[
objects.ComputeNode(
host='host01',
uuid=uuidsentinel.host1,
hypervisor_hostname='host01')
]
)
compute_node_get_all_by_host.return_value = fake_computes
self._test_server_action(
uuid,
'os-unshelve-unpin-az',
'unshelve',
subs={'host': 'host01'},
)
class UnshelveJson291NonAdminTest(UnshelveJson291Test):
# Use non admin api credentials.
ADMIN_API = False
sample_dir = "os-shelve"
microversion = '2.91'
scenarios = [('v2_91', {'api_major_version': 'v2.1'})]
def _test_server_action_invalid(self, uuid, template, action, subs=None):
subs = subs or {}
subs.update({'action': action})
response = self._do_post('servers/%s/action' % uuid,
template, subs)
self.assertEqual(403, response.status_code)
self.assertIn(
"Policy doesn\'t allow os_compute_api:os-shelve:unshelve_to_host" +
" to be performed.", response.text)
def _test_server_action(self, uuid, template, action, subs=None):
subs = subs or {}
subs.update({'action': action})
response = self._do_post('servers/%s/action' % uuid,
template, subs)
self.assertEqual(202, response.status_code)
self.assertEqual('', response.text)
def test_unshelve_with_non_valid_host(self):
"""Ensure an exception rise if user is not admin.
a http 403 error
"""
uuid = self._post_server()
self._test_server_action(uuid, 'os-shelve', 'shelve')
self._test_server_action_invalid(
uuid,
'os-unshelve-host',
'unshelve',
subs={'host': 'host01'}
)
@mock.patch('nova.objects.aggregate._get_by_host_from_db')
@mock.patch('nova.objects.ComputeNodeList.get_all_by_host')
def test_unshelve_with_unpin_az_and_host(
self, compute_node_get_all_by_host, mock_api_get_by_host):
# Put compute in the correct az
mock_api_get_by_host.return_value = [fake_aggregate]
uuid = self._post_server()
self._test_server_action(uuid, 'os-shelve', 'shelve')
fake_computes = objects.ComputeNodeList(
objects=[
objects.ComputeNode(
host='host01',
uuid=uuidsentinel.host1,
hypervisor_hostname='host01')
]
)
compute_node_get_all_by_host.return_value = fake_computes
self._test_server_action_invalid(
uuid,
'os-unshelve-host-and-unpin-az',
'unshelve',
subs={'host': 'host01'},
)
@mock.patch('nova.objects.aggregate._get_by_host_from_db')
@mock.patch('nova.objects.ComputeNodeList.get_all_by_host')
def test_unshelve_with_valid_host(
self, compute_node_get_all_by_host, mock_api_get_by_host):
# Put compute in the correct az
mock_api_get_by_host.return_value = [fake_aggregate]
uuid = self._post_server()
self._test_server_action(uuid, 'os-shelve', 'shelve')
fake_computes = objects.ComputeNodeList(
objects=[
objects.ComputeNode(
host='host01',
uuid=uuidsentinel.host1,
hypervisor_hostname='host01')
]
)
compute_node_get_all_by_host.return_value = fake_computes
self._test_server_action_invalid(
uuid,
'os-unshelve-host',
'unshelve',
subs={'host': 'host01'}
)
@mock.patch('nova.objects.aggregate._get_by_host_from_db')
@mock.patch('nova.objects.ComputeNodeList.get_all_by_host')
def test_unshelve_with_az_and_host(
self, compute_node_get_all_by_host, mock_api_get_by_host):
"""Ensure we can unshelve to a host and az
"""
# Put compute in the correct az
mock_api_get_by_host.return_value = [fake_aggregate]
uuid = self._post_server()
self._test_server_action(uuid, 'os-shelve', 'shelve')
fake_computes = objects.ComputeNodeList(
objects=[
objects.ComputeNode(
host='host01',
uuid=uuidsentinel.host1,
hypervisor_hostname='host01')
]
)
compute_node_get_all_by_host.return_value = fake_computes
self._test_server_action_invalid(
uuid,
'os-unshelve-host',
'unshelve',
subs={'host': 'host01', 'availability_zone': 'us-west'},
)
|