root / qa / qa_rapi.py @ a07ae57f
History | View | Annotate | Download (28.9 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Remote API QA tests.
|
23 |
|
24 |
"""
|
25 |
|
26 |
import itertools |
27 |
import functools |
28 |
import random |
29 |
import re |
30 |
import tempfile |
31 |
|
32 |
from ganeti import cli |
33 |
from ganeti import compat |
34 |
from ganeti import constants |
35 |
from ganeti import errors |
36 |
from ganeti import locking |
37 |
from ganeti import pathutils |
38 |
from ganeti import objects |
39 |
from ganeti import qlang |
40 |
from ganeti import query |
41 |
from ganeti import rapi |
42 |
from ganeti import utils |
43 |
|
44 |
from ganeti.http.auth import ParsePasswordFile |
45 |
import ganeti.rapi.client # pylint: disable=W0611 |
46 |
import ganeti.rapi.client_utils |
47 |
|
48 |
import qa_config |
49 |
import qa_error |
50 |
import qa_utils |
51 |
|
52 |
from qa_instance import IsDiskReplacingSupported |
53 |
from qa_instance import IsFailoverSupported |
54 |
from qa_instance import IsMigrationSupported |
55 |
from qa_job_utils import RunWithLocks |
56 |
from qa_utils import (AssertEqual, AssertIn, AssertMatch, StartLocalCommand) |
57 |
from qa_utils import InstanceCheck, INST_DOWN, INST_UP, FIRST_ARG |
58 |
|
59 |
|
60 |
_rapi_ca = None
|
61 |
_rapi_client = None
|
62 |
_rapi_username = None
|
63 |
_rapi_password = None
|
64 |
|
65 |
|
66 |
def Setup(username, password): |
67 |
"""Configures the RAPI client.
|
68 |
|
69 |
"""
|
70 |
# pylint: disable=W0603
|
71 |
# due to global usage
|
72 |
global _rapi_ca
|
73 |
global _rapi_client
|
74 |
global _rapi_username
|
75 |
global _rapi_password
|
76 |
|
77 |
_rapi_username = username |
78 |
_rapi_password = password |
79 |
|
80 |
master = qa_config.GetMasterNode() |
81 |
|
82 |
# Load RAPI certificate from master node
|
83 |
cmd = ["cat", qa_utils.MakeNodePath(master, pathutils.RAPI_CERT_FILE)]
|
84 |
|
85 |
# Write to temporary file
|
86 |
_rapi_ca = tempfile.NamedTemporaryFile() |
87 |
_rapi_ca.write(qa_utils.GetCommandOutput(master.primary, |
88 |
utils.ShellQuoteArgs(cmd))) |
89 |
_rapi_ca.flush() |
90 |
|
91 |
port = qa_config.get("rapi-port", default=constants.DEFAULT_RAPI_PORT)
|
92 |
cfg_curl = rapi.client.GenericCurlConfig(cafile=_rapi_ca.name, |
93 |
proxy="")
|
94 |
|
95 |
if qa_config.UseVirtualCluster():
|
96 |
# TODO: Implement full support for RAPI on virtual clusters
|
97 |
print qa_utils.FormatWarning("RAPI tests are not yet supported on" |
98 |
" virtual clusters and will be disabled")
|
99 |
|
100 |
assert _rapi_client is None |
101 |
else:
|
102 |
_rapi_client = rapi.client.GanetiRapiClient(master.primary, port=port, |
103 |
username=username, |
104 |
password=password, |
105 |
curl_config_fn=cfg_curl) |
106 |
|
107 |
print "RAPI protocol version: %s" % _rapi_client.GetVersion() |
108 |
|
109 |
return _rapi_client
|
110 |
|
111 |
|
112 |
def LookupRapiSecret(rapi_user): |
113 |
"""Find the RAPI secret for the given user.
|
114 |
|
115 |
@param rapi_user: Login user
|
116 |
@return: Login secret for the user
|
117 |
|
118 |
"""
|
119 |
CTEXT = "{CLEARTEXT}"
|
120 |
master = qa_config.GetMasterNode() |
121 |
cmd = ["cat", qa_utils.MakeNodePath(master, pathutils.RAPI_USERS_FILE)]
|
122 |
file_content = qa_utils.GetCommandOutput(master.primary, |
123 |
utils.ShellQuoteArgs(cmd)) |
124 |
users = ParsePasswordFile(file_content) |
125 |
entry = users.get(rapi_user) |
126 |
if not entry: |
127 |
raise qa_error.Error("User %s not found in RAPI users file" % rapi_user) |
128 |
secret = entry.password |
129 |
if secret.upper().startswith(CTEXT):
|
130 |
secret = secret[len(CTEXT):]
|
131 |
elif secret.startswith("{"): |
132 |
raise qa_error.Error("Unsupported password schema for RAPI user %s:" |
133 |
" not a clear text password" % rapi_user)
|
134 |
return secret
|
135 |
|
136 |
|
137 |
INSTANCE_FIELDS = ("name", "os", "pnode", "snodes", |
138 |
"admin_state",
|
139 |
"disk_template", "disk.sizes", "disk.spindles", |
140 |
"nic.ips", "nic.macs", "nic.modes", "nic.links", |
141 |
"beparams", "hvparams", |
142 |
"oper_state", "oper_ram", "oper_vcpus", "status", "tags") |
143 |
|
144 |
NODE_FIELDS = ("name", "dtotal", "dfree", "sptotal", "spfree", |
145 |
"mtotal", "mnode", "mfree", |
146 |
"pinst_cnt", "sinst_cnt", "tags") |
147 |
|
148 |
GROUP_FIELDS = compat.UniqueFrozenset([ |
149 |
"name", "uuid", |
150 |
"alloc_policy",
|
151 |
"node_cnt", "node_list", |
152 |
]) |
153 |
|
154 |
JOB_FIELDS = compat.UniqueFrozenset([ |
155 |
"id", "ops", "status", "summary", |
156 |
"opstatus", "opresult", "oplog", |
157 |
"received_ts", "start_ts", "end_ts", |
158 |
]) |
159 |
|
160 |
LIST_FIELDS = ("id", "uri") |
161 |
|
162 |
|
163 |
def Enabled(): |
164 |
"""Return whether remote API tests should be run.
|
165 |
|
166 |
"""
|
167 |
# TODO: Implement RAPI tests for virtual clusters
|
168 |
return (qa_config.TestEnabled("rapi") and |
169 |
not qa_config.UseVirtualCluster())
|
170 |
|
171 |
|
172 |
def _DoTests(uris): |
173 |
# pylint: disable=W0212
|
174 |
# due to _SendRequest usage
|
175 |
results = [] |
176 |
|
177 |
for uri, verify, method, body in uris: |
178 |
assert uri.startswith("/") |
179 |
|
180 |
print "%s %s" % (method, uri) |
181 |
data = _rapi_client._SendRequest(method, uri, None, body)
|
182 |
|
183 |
if verify is not None: |
184 |
if callable(verify): |
185 |
verify(data) |
186 |
else:
|
187 |
AssertEqual(data, verify) |
188 |
|
189 |
results.append(data) |
190 |
|
191 |
return results
|
192 |
|
193 |
|
194 |
def _VerifyReturnsJob(data): |
195 |
if not isinstance(data, int): |
196 |
AssertMatch(data, r"^\d+$")
|
197 |
|
198 |
|
199 |
def TestVersion(): |
200 |
"""Testing remote API version.
|
201 |
|
202 |
"""
|
203 |
_DoTests([ |
204 |
("/version", constants.RAPI_VERSION, "GET", None), |
205 |
]) |
206 |
|
207 |
|
208 |
def TestEmptyCluster(): |
209 |
"""Testing remote API on an empty cluster.
|
210 |
|
211 |
"""
|
212 |
master = qa_config.GetMasterNode() |
213 |
master_full = qa_utils.ResolveNodeName(master) |
214 |
|
215 |
def _VerifyInfo(data): |
216 |
AssertIn("name", data)
|
217 |
AssertIn("master", data)
|
218 |
AssertEqual(data["master"], master_full)
|
219 |
|
220 |
def _VerifyNodes(data): |
221 |
master_entry = { |
222 |
"id": master_full,
|
223 |
"uri": "/2/nodes/%s" % master_full, |
224 |
} |
225 |
AssertIn(master_entry, data) |
226 |
|
227 |
def _VerifyNodesBulk(data): |
228 |
for node in data: |
229 |
for entry in NODE_FIELDS: |
230 |
AssertIn(entry, node) |
231 |
|
232 |
def _VerifyGroups(data): |
233 |
default_group = { |
234 |
"name": constants.INITIAL_NODE_GROUP_NAME,
|
235 |
"uri": "/2/groups/" + constants.INITIAL_NODE_GROUP_NAME, |
236 |
} |
237 |
AssertIn(default_group, data) |
238 |
|
239 |
def _VerifyGroupsBulk(data): |
240 |
for group in data: |
241 |
for field in GROUP_FIELDS: |
242 |
AssertIn(field, group) |
243 |
|
244 |
_DoTests([ |
245 |
("/", None, "GET", None), |
246 |
("/2/info", _VerifyInfo, "GET", None), |
247 |
("/2/tags", None, "GET", None), |
248 |
("/2/nodes", _VerifyNodes, "GET", None), |
249 |
("/2/nodes?bulk=1", _VerifyNodesBulk, "GET", None), |
250 |
("/2/groups", _VerifyGroups, "GET", None), |
251 |
("/2/groups?bulk=1", _VerifyGroupsBulk, "GET", None), |
252 |
("/2/instances", [], "GET", None), |
253 |
("/2/instances?bulk=1", [], "GET", None), |
254 |
("/2/os", None, "GET", None), |
255 |
]) |
256 |
|
257 |
# Test HTTP Not Found
|
258 |
for method in ["GET", "PUT", "POST", "DELETE"]: |
259 |
try:
|
260 |
_DoTests([("/99/resource/not/here/99", None, method, None)]) |
261 |
except rapi.client.GanetiApiError, err:
|
262 |
AssertEqual(err.code, 404)
|
263 |
else:
|
264 |
raise qa_error.Error("Non-existent resource didn't return HTTP 404") |
265 |
|
266 |
# Test HTTP Not Implemented
|
267 |
for method in ["PUT", "POST", "DELETE"]: |
268 |
try:
|
269 |
_DoTests([("/version", None, method, None)]) |
270 |
except rapi.client.GanetiApiError, err:
|
271 |
AssertEqual(err.code, 501)
|
272 |
else:
|
273 |
raise qa_error.Error("Non-implemented method didn't fail") |
274 |
|
275 |
|
276 |
def TestRapiQuery(): |
277 |
"""Testing resource queries via remote API.
|
278 |
|
279 |
"""
|
280 |
# FIXME: the tests are failing if no LVM is enabled, investigate
|
281 |
# if it is a bug in the QA or in the code
|
282 |
if not qa_config.IsStorageTypeSupported(constants.ST_LVM_VG): |
283 |
return
|
284 |
|
285 |
master_name = qa_utils.ResolveNodeName(qa_config.GetMasterNode()) |
286 |
rnd = random.Random(7818)
|
287 |
|
288 |
for what in constants.QR_VIA_RAPI: |
289 |
if what == constants.QR_JOB:
|
290 |
namefield = "id"
|
291 |
trivial_filter = [qlang.OP_GE, namefield, 0]
|
292 |
elif what == constants.QR_EXPORT:
|
293 |
namefield = "export"
|
294 |
trivial_filter = [qlang.OP_REGEXP, ".*", namefield]
|
295 |
else:
|
296 |
namefield = "name"
|
297 |
trivial_filter = [qlang.OP_REGEXP, ".*", namefield]
|
298 |
|
299 |
all_fields = query.ALL_FIELDS[what].keys() |
300 |
rnd.shuffle(all_fields) |
301 |
|
302 |
# No fields, should return everything
|
303 |
result = _rapi_client.QueryFields(what) |
304 |
qresult = objects.QueryFieldsResponse.FromDict(result) |
305 |
AssertEqual(len(qresult.fields), len(all_fields)) |
306 |
|
307 |
# One field
|
308 |
result = _rapi_client.QueryFields(what, fields=[namefield]) |
309 |
qresult = objects.QueryFieldsResponse.FromDict(result) |
310 |
AssertEqual(len(qresult.fields), 1) |
311 |
|
312 |
# Specify all fields, order must be correct
|
313 |
result = _rapi_client.QueryFields(what, fields=all_fields) |
314 |
qresult = objects.QueryFieldsResponse.FromDict(result) |
315 |
AssertEqual(len(qresult.fields), len(all_fields)) |
316 |
AssertEqual([fdef.name for fdef in qresult.fields], all_fields) |
317 |
|
318 |
# Unknown field
|
319 |
result = _rapi_client.QueryFields(what, fields=["_unknown!"])
|
320 |
qresult = objects.QueryFieldsResponse.FromDict(result) |
321 |
AssertEqual(len(qresult.fields), 1) |
322 |
AssertEqual(qresult.fields[0].name, "_unknown!") |
323 |
AssertEqual(qresult.fields[0].kind, constants.QFT_UNKNOWN)
|
324 |
|
325 |
# Try once more, this time without the client
|
326 |
_DoTests([ |
327 |
("/2/query/%s/fields" % what, None, "GET", None), |
328 |
("/2/query/%s/fields?fields=name,name,%s" % (what, all_fields[0]), |
329 |
None, "GET", None), |
330 |
]) |
331 |
|
332 |
# Try missing query argument
|
333 |
try:
|
334 |
_DoTests([ |
335 |
("/2/query/%s" % what, None, "GET", None), |
336 |
]) |
337 |
except rapi.client.GanetiApiError, err:
|
338 |
AssertEqual(err.code, 400)
|
339 |
else:
|
340 |
raise qa_error.Error("Request missing 'fields' parameter didn't fail") |
341 |
|
342 |
def _Check(exp_fields, data): |
343 |
qresult = objects.QueryResponse.FromDict(data) |
344 |
AssertEqual([fdef.name for fdef in qresult.fields], exp_fields) |
345 |
if not isinstance(qresult.data, list): |
346 |
raise qa_error.Error("Query did not return a list") |
347 |
|
348 |
_DoTests([ |
349 |
# Specify fields in query
|
350 |
("/2/query/%s?fields=%s" % (what, ",".join(all_fields)), |
351 |
compat.partial(_Check, all_fields), "GET", None), |
352 |
|
353 |
("/2/query/%s?fields=%s" % (what, namefield),
|
354 |
compat.partial(_Check, [namefield]), "GET", None), |
355 |
|
356 |
# Note the spaces
|
357 |
("/2/query/%s?fields=%s,%%20%s%%09,%s%%20" %
|
358 |
(what, namefield, namefield, namefield), |
359 |
compat.partial(_Check, [namefield] * 3), "GET", None)]) |
360 |
|
361 |
if what in constants.QR_VIA_RAPI_PUT: |
362 |
_DoTests([ |
363 |
# PUT with fields in query
|
364 |
("/2/query/%s?fields=%s" % (what, namefield),
|
365 |
compat.partial(_Check, [namefield]), "PUT", {}),
|
366 |
|
367 |
("/2/query/%s" % what, compat.partial(_Check, [namefield] * 4), "PUT", { |
368 |
"fields": [namefield] * 4, |
369 |
}), |
370 |
|
371 |
("/2/query/%s" % what, compat.partial(_Check, all_fields), "PUT", { |
372 |
"fields": all_fields,
|
373 |
}), |
374 |
|
375 |
("/2/query/%s" % what, compat.partial(_Check, [namefield] * 4), "PUT", { |
376 |
"fields": [namefield] * 4 |
377 |
})]) |
378 |
|
379 |
def _CheckFilter(): |
380 |
_DoTests([ |
381 |
# With filter
|
382 |
("/2/query/%s" % what, compat.partial(_Check, all_fields), "PUT", { |
383 |
"fields": all_fields,
|
384 |
"filter": trivial_filter
|
385 |
}), |
386 |
]) |
387 |
|
388 |
if what == constants.QR_LOCK:
|
389 |
# Locks can't be filtered
|
390 |
try:
|
391 |
_CheckFilter() |
392 |
except rapi.client.GanetiApiError, err:
|
393 |
AssertEqual(err.code, 500)
|
394 |
else:
|
395 |
raise qa_error.Error("Filtering locks didn't fail") |
396 |
else:
|
397 |
if what in constants.QR_VIA_RAPI_PUT: |
398 |
_CheckFilter() |
399 |
|
400 |
if what == constants.QR_NODE:
|
401 |
# Test with filter
|
402 |
(nodes, ) = _DoTests( |
403 |
[("/2/query/%s" % what,
|
404 |
compat.partial(_Check, ["name", "master"]), "PUT", |
405 |
{"fields": ["name", "master"], |
406 |
"filter": [qlang.OP_TRUE, "master"], |
407 |
})]) |
408 |
qresult = objects.QueryResponse.FromDict(nodes) |
409 |
AssertEqual(qresult.data, [ |
410 |
[[constants.RS_NORMAL, master_name], [constants.RS_NORMAL, True]],
|
411 |
]) |
412 |
|
413 |
|
414 |
@InstanceCheck(INST_UP, INST_UP, FIRST_ARG)
|
415 |
def TestInstance(instance): |
416 |
"""Testing getting instance(s) info via remote API.
|
417 |
|
418 |
"""
|
419 |
def _VerifyInstance(data): |
420 |
for entry in INSTANCE_FIELDS: |
421 |
AssertIn(entry, data) |
422 |
|
423 |
def _VerifyInstancesList(data): |
424 |
for instance in data: |
425 |
for entry in LIST_FIELDS: |
426 |
AssertIn(entry, instance) |
427 |
|
428 |
def _VerifyInstancesBulk(data): |
429 |
for instance_data in data: |
430 |
_VerifyInstance(instance_data) |
431 |
|
432 |
_DoTests([ |
433 |
("/2/instances/%s" % instance.name, _VerifyInstance, "GET", None), |
434 |
("/2/instances", _VerifyInstancesList, "GET", None), |
435 |
("/2/instances?bulk=1", _VerifyInstancesBulk, "GET", None), |
436 |
("/2/instances/%s/activate-disks" % instance.name,
|
437 |
_VerifyReturnsJob, "PUT", None), |
438 |
("/2/instances/%s/deactivate-disks" % instance.name,
|
439 |
_VerifyReturnsJob, "PUT", None), |
440 |
]) |
441 |
|
442 |
# Test OpBackupPrepare
|
443 |
(job_id, ) = _DoTests([ |
444 |
("/2/instances/%s/prepare-export?mode=%s" %
|
445 |
(instance.name, constants.EXPORT_MODE_REMOTE), |
446 |
_VerifyReturnsJob, "PUT", None), |
447 |
]) |
448 |
|
449 |
result = _WaitForRapiJob(job_id)[0]
|
450 |
AssertEqual(len(result["handshake"]), 3) |
451 |
AssertEqual(result["handshake"][0], constants.RIE_VERSION) |
452 |
AssertEqual(len(result["x509_key_name"]), 3) |
453 |
AssertIn("-----BEGIN CERTIFICATE-----", result["x509_ca"]) |
454 |
|
455 |
|
456 |
def TestNode(node): |
457 |
"""Testing getting node(s) info via remote API.
|
458 |
|
459 |
"""
|
460 |
def _VerifyNode(data): |
461 |
for entry in NODE_FIELDS: |
462 |
AssertIn(entry, data) |
463 |
|
464 |
def _VerifyNodesList(data): |
465 |
for node in data: |
466 |
for entry in LIST_FIELDS: |
467 |
AssertIn(entry, node) |
468 |
|
469 |
def _VerifyNodesBulk(data): |
470 |
for node_data in data: |
471 |
_VerifyNode(node_data) |
472 |
|
473 |
_DoTests([ |
474 |
("/2/nodes/%s" % node.primary, _VerifyNode, "GET", None), |
475 |
("/2/nodes", _VerifyNodesList, "GET", None), |
476 |
("/2/nodes?bulk=1", _VerifyNodesBulk, "GET", None), |
477 |
]) |
478 |
|
479 |
|
480 |
def _FilterTags(seq): |
481 |
"""Removes unwanted tags from a sequence.
|
482 |
|
483 |
"""
|
484 |
ignore_re = qa_config.get("ignore-tags-re", None) |
485 |
|
486 |
if ignore_re:
|
487 |
return itertools.ifilterfalse(re.compile(ignore_re).match, seq)
|
488 |
else:
|
489 |
return seq
|
490 |
|
491 |
|
492 |
def TestTags(kind, name, tags): |
493 |
"""Tests .../tags resources.
|
494 |
|
495 |
"""
|
496 |
if kind == constants.TAG_CLUSTER:
|
497 |
uri = "/2/tags"
|
498 |
elif kind == constants.TAG_NODE:
|
499 |
uri = "/2/nodes/%s/tags" % name
|
500 |
elif kind == constants.TAG_INSTANCE:
|
501 |
uri = "/2/instances/%s/tags" % name
|
502 |
elif kind == constants.TAG_NODEGROUP:
|
503 |
uri = "/2/groups/%s/tags" % name
|
504 |
elif kind == constants.TAG_NETWORK:
|
505 |
uri = "/2/networks/%s/tags" % name
|
506 |
else:
|
507 |
raise errors.ProgrammerError("Unknown tag kind") |
508 |
|
509 |
def _VerifyTags(data): |
510 |
AssertEqual(sorted(tags), sorted(_FilterTags(data))) |
511 |
|
512 |
queryargs = "&".join("tag=%s" % i for i in tags) |
513 |
|
514 |
# Add tags
|
515 |
(job_id, ) = _DoTests([ |
516 |
("%s?%s" % (uri, queryargs), _VerifyReturnsJob, "PUT", None), |
517 |
]) |
518 |
_WaitForRapiJob(job_id) |
519 |
|
520 |
# Retrieve tags
|
521 |
_DoTests([ |
522 |
(uri, _VerifyTags, "GET", None), |
523 |
]) |
524 |
|
525 |
# Remove tags
|
526 |
(job_id, ) = _DoTests([ |
527 |
("%s?%s" % (uri, queryargs), _VerifyReturnsJob, "DELETE", None), |
528 |
]) |
529 |
_WaitForRapiJob(job_id) |
530 |
|
531 |
|
532 |
def _WaitForRapiJob(job_id): |
533 |
"""Waits for a job to finish.
|
534 |
|
535 |
"""
|
536 |
def _VerifyJob(data): |
537 |
AssertEqual(data["id"], job_id)
|
538 |
for field in JOB_FIELDS: |
539 |
AssertIn(field, data) |
540 |
|
541 |
_DoTests([ |
542 |
("/2/jobs/%s" % job_id, _VerifyJob, "GET", None), |
543 |
]) |
544 |
|
545 |
return rapi.client_utils.PollJob(_rapi_client, job_id,
|
546 |
cli.StdioJobPollReportCb()) |
547 |
|
548 |
|
549 |
def TestRapiNodeGroups(): |
550 |
"""Test several node group operations using RAPI.
|
551 |
|
552 |
"""
|
553 |
(group1, group2, group3) = qa_utils.GetNonexistentGroups(3)
|
554 |
|
555 |
# Create a group with no attributes
|
556 |
body = { |
557 |
"name": group1,
|
558 |
} |
559 |
|
560 |
(job_id, ) = _DoTests([ |
561 |
("/2/groups", _VerifyReturnsJob, "POST", body), |
562 |
]) |
563 |
|
564 |
_WaitForRapiJob(job_id) |
565 |
|
566 |
# Create a group specifying alloc_policy
|
567 |
body = { |
568 |
"name": group2,
|
569 |
"alloc_policy": constants.ALLOC_POLICY_UNALLOCABLE,
|
570 |
} |
571 |
|
572 |
(job_id, ) = _DoTests([ |
573 |
("/2/groups", _VerifyReturnsJob, "POST", body), |
574 |
]) |
575 |
|
576 |
_WaitForRapiJob(job_id) |
577 |
|
578 |
# Modify alloc_policy
|
579 |
body = { |
580 |
"alloc_policy": constants.ALLOC_POLICY_UNALLOCABLE,
|
581 |
} |
582 |
|
583 |
(job_id, ) = _DoTests([ |
584 |
("/2/groups/%s/modify" % group1, _VerifyReturnsJob, "PUT", body), |
585 |
]) |
586 |
|
587 |
_WaitForRapiJob(job_id) |
588 |
|
589 |
# Rename a group
|
590 |
body = { |
591 |
"new_name": group3,
|
592 |
} |
593 |
|
594 |
(job_id, ) = _DoTests([ |
595 |
("/2/groups/%s/rename" % group2, _VerifyReturnsJob, "PUT", body), |
596 |
]) |
597 |
|
598 |
_WaitForRapiJob(job_id) |
599 |
|
600 |
# Delete groups
|
601 |
for group in [group1, group3]: |
602 |
(job_id, ) = _DoTests([ |
603 |
("/2/groups/%s" % group, _VerifyReturnsJob, "DELETE", None), |
604 |
]) |
605 |
|
606 |
_WaitForRapiJob(job_id) |
607 |
|
608 |
|
609 |
def TestRapiInstanceAdd(node, use_client): |
610 |
"""Test adding a new instance via RAPI"""
|
611 |
if not qa_config.IsTemplateSupported(constants.DT_PLAIN): |
612 |
return
|
613 |
instance = qa_config.AcquireInstance() |
614 |
instance.SetDiskTemplate(constants.DT_PLAIN) |
615 |
try:
|
616 |
disks = [{"size": utils.ParseUnit(d.get("size")), |
617 |
"name": str(d.get("name"))} |
618 |
for d in qa_config.GetDiskOptions()] |
619 |
nic0_mac = instance.GetNicMacAddr(0, constants.VALUE_GENERATE)
|
620 |
nics = [{ |
621 |
constants.INIC_MAC: nic0_mac, |
622 |
}] |
623 |
|
624 |
beparams = { |
625 |
constants.BE_MAXMEM: utils.ParseUnit(qa_config.get(constants.BE_MAXMEM)), |
626 |
constants.BE_MINMEM: utils.ParseUnit(qa_config.get(constants.BE_MINMEM)), |
627 |
} |
628 |
|
629 |
if use_client:
|
630 |
job_id = _rapi_client.CreateInstance(constants.INSTANCE_CREATE, |
631 |
instance.name, |
632 |
constants.DT_PLAIN, |
633 |
disks, nics, |
634 |
os=qa_config.get("os"),
|
635 |
pnode=node.primary, |
636 |
beparams=beparams) |
637 |
else:
|
638 |
body = { |
639 |
"__version__": 1, |
640 |
"mode": constants.INSTANCE_CREATE,
|
641 |
"name": instance.name,
|
642 |
"os_type": qa_config.get("os"), |
643 |
"disk_template": constants.DT_PLAIN,
|
644 |
"pnode": node.primary,
|
645 |
"beparams": beparams,
|
646 |
"disks": disks,
|
647 |
"nics": nics,
|
648 |
} |
649 |
|
650 |
(job_id, ) = _DoTests([ |
651 |
("/2/instances", _VerifyReturnsJob, "POST", body), |
652 |
]) |
653 |
|
654 |
_WaitForRapiJob(job_id) |
655 |
|
656 |
return instance
|
657 |
except:
|
658 |
instance.Release() |
659 |
raise
|
660 |
|
661 |
|
662 |
def _GenInstanceAllocationDict(node, instance): |
663 |
"""Creates an instance allocation dict to be used with the RAPI"""
|
664 |
instance.SetDiskTemplate(constants.DT_PLAIN) |
665 |
|
666 |
disks = [{"size": utils.ParseUnit(d.get("size")), |
667 |
"name": str(d.get("name"))} |
668 |
for d in qa_config.GetDiskOptions()] |
669 |
|
670 |
nic0_mac = instance.GetNicMacAddr(0, constants.VALUE_GENERATE)
|
671 |
nics = [{ |
672 |
constants.INIC_MAC: nic0_mac, |
673 |
}] |
674 |
|
675 |
beparams = { |
676 |
constants.BE_MAXMEM: utils.ParseUnit(qa_config.get(constants.BE_MAXMEM)), |
677 |
constants.BE_MINMEM: utils.ParseUnit(qa_config.get(constants.BE_MINMEM)), |
678 |
} |
679 |
|
680 |
return _rapi_client.InstanceAllocation(constants.INSTANCE_CREATE,
|
681 |
instance.name, |
682 |
constants.DT_PLAIN, |
683 |
disks, nics, |
684 |
os=qa_config.get("os"),
|
685 |
pnode=node.primary, |
686 |
beparams=beparams) |
687 |
|
688 |
|
689 |
def TestRapiInstanceMultiAlloc(node): |
690 |
"""Test adding two new instances via the RAPI instance-multi-alloc method"""
|
691 |
if not qa_config.IsTemplateSupported(constants.DT_PLAIN): |
692 |
return
|
693 |
|
694 |
JOBS_KEY = "jobs"
|
695 |
|
696 |
instance_one = qa_config.AcquireInstance() |
697 |
instance_two = qa_config.AcquireInstance() |
698 |
instance_list = [instance_one, instance_two] |
699 |
try:
|
700 |
rapi_dicts = map(functools.partial(_GenInstanceAllocationDict, node),
|
701 |
instance_list) |
702 |
|
703 |
job_id = _rapi_client.InstancesMultiAlloc(rapi_dicts) |
704 |
|
705 |
results, = _WaitForRapiJob(job_id) |
706 |
|
707 |
if JOBS_KEY not in results: |
708 |
raise qa_error.Error("RAPI instance-multi-alloc did not deliver " |
709 |
"information about created jobs")
|
710 |
|
711 |
if len(results[JOBS_KEY]) != len(instance_list): |
712 |
raise qa_error.Error("RAPI instance-multi-alloc failed to return the " |
713 |
"desired number of jobs!")
|
714 |
|
715 |
for success, job in results[JOBS_KEY]: |
716 |
if success:
|
717 |
_WaitForRapiJob(job) |
718 |
else:
|
719 |
raise qa_error.Error("Failed to create instance in " |
720 |
"instance-multi-alloc call")
|
721 |
except:
|
722 |
# Note that although released, it may be that some of the instance creations
|
723 |
# have in fact succeeded. Handling this in a better way may be possible, but
|
724 |
# is not necessary as the QA has already failed at this point.
|
725 |
for instance in instance_list: |
726 |
instance.Release() |
727 |
raise
|
728 |
|
729 |
return (instance_one, instance_two)
|
730 |
|
731 |
|
732 |
@InstanceCheck(None, INST_DOWN, FIRST_ARG) |
733 |
def TestRapiInstanceRemove(instance, use_client): |
734 |
"""Test removing instance via RAPI"""
|
735 |
# FIXME: this does not work if LVM is not enabled. Find out if this is a bug
|
736 |
# in RAPI or in the test
|
737 |
if not qa_config.IsStorageTypeSupported(constants.ST_LVM_VG): |
738 |
return
|
739 |
|
740 |
if use_client:
|
741 |
job_id = _rapi_client.DeleteInstance(instance.name) |
742 |
else:
|
743 |
(job_id, ) = _DoTests([ |
744 |
("/2/instances/%s" % instance.name, _VerifyReturnsJob, "DELETE", None), |
745 |
]) |
746 |
|
747 |
_WaitForRapiJob(job_id) |
748 |
|
749 |
|
750 |
@InstanceCheck(INST_UP, INST_UP, FIRST_ARG)
|
751 |
def TestRapiInstanceMigrate(instance): |
752 |
"""Test migrating instance via RAPI"""
|
753 |
if not IsMigrationSupported(instance): |
754 |
print qa_utils.FormatInfo("Instance doesn't support migration, skipping" |
755 |
" test")
|
756 |
return
|
757 |
# Move to secondary node
|
758 |
_WaitForRapiJob(_rapi_client.MigrateInstance(instance.name)) |
759 |
qa_utils.RunInstanceCheck(instance, True)
|
760 |
# And back to previous primary
|
761 |
_WaitForRapiJob(_rapi_client.MigrateInstance(instance.name)) |
762 |
|
763 |
|
764 |
@InstanceCheck(INST_UP, INST_UP, FIRST_ARG)
|
765 |
def TestRapiInstanceFailover(instance): |
766 |
"""Test failing over instance via RAPI"""
|
767 |
if not IsFailoverSupported(instance): |
768 |
print qa_utils.FormatInfo("Instance doesn't support failover, skipping" |
769 |
" test")
|
770 |
return
|
771 |
# Move to secondary node
|
772 |
_WaitForRapiJob(_rapi_client.FailoverInstance(instance.name)) |
773 |
qa_utils.RunInstanceCheck(instance, True)
|
774 |
# And back to previous primary
|
775 |
_WaitForRapiJob(_rapi_client.FailoverInstance(instance.name)) |
776 |
|
777 |
|
778 |
@InstanceCheck(INST_UP, INST_DOWN, FIRST_ARG)
|
779 |
def TestRapiInstanceShutdown(instance): |
780 |
"""Test stopping an instance via RAPI"""
|
781 |
_WaitForRapiJob(_rapi_client.ShutdownInstance(instance.name)) |
782 |
|
783 |
|
784 |
@InstanceCheck(INST_DOWN, INST_UP, FIRST_ARG)
|
785 |
def TestRapiInstanceStartup(instance): |
786 |
"""Test starting an instance via RAPI"""
|
787 |
_WaitForRapiJob(_rapi_client.StartupInstance(instance.name)) |
788 |
|
789 |
|
790 |
@InstanceCheck(INST_DOWN, INST_DOWN, FIRST_ARG)
|
791 |
def TestRapiInstanceRenameAndBack(rename_source, rename_target): |
792 |
"""Test renaming instance via RAPI
|
793 |
|
794 |
This must leave the instance with the original name (in the
|
795 |
non-failure case).
|
796 |
|
797 |
"""
|
798 |
_WaitForRapiJob(_rapi_client.RenameInstance(rename_source, rename_target)) |
799 |
qa_utils.RunInstanceCheck(rename_source, False)
|
800 |
qa_utils.RunInstanceCheck(rename_target, False)
|
801 |
_WaitForRapiJob(_rapi_client.RenameInstance(rename_target, rename_source)) |
802 |
qa_utils.RunInstanceCheck(rename_target, False)
|
803 |
|
804 |
|
805 |
@InstanceCheck(INST_DOWN, INST_DOWN, FIRST_ARG)
|
806 |
def TestRapiInstanceReinstall(instance): |
807 |
"""Test reinstalling an instance via RAPI"""
|
808 |
if instance.disk_template == constants.DT_DISKLESS:
|
809 |
print qa_utils.FormatInfo("Test not supported for diskless instances") |
810 |
return
|
811 |
|
812 |
_WaitForRapiJob(_rapi_client.ReinstallInstance(instance.name)) |
813 |
# By default, the instance is started again
|
814 |
qa_utils.RunInstanceCheck(instance, True)
|
815 |
|
816 |
# Reinstall again without starting
|
817 |
_WaitForRapiJob(_rapi_client.ReinstallInstance(instance.name, |
818 |
no_startup=True))
|
819 |
|
820 |
|
821 |
@InstanceCheck(INST_UP, INST_UP, FIRST_ARG)
|
822 |
def TestRapiInstanceReplaceDisks(instance): |
823 |
"""Test replacing instance disks via RAPI"""
|
824 |
if not IsDiskReplacingSupported(instance): |
825 |
print qa_utils.FormatInfo("Instance doesn't support disk replacing," |
826 |
" skipping test")
|
827 |
return
|
828 |
fn = _rapi_client.ReplaceInstanceDisks |
829 |
_WaitForRapiJob(fn(instance.name, |
830 |
mode=constants.REPLACE_DISK_AUTO, disks=[])) |
831 |
_WaitForRapiJob(fn(instance.name, |
832 |
mode=constants.REPLACE_DISK_SEC, disks="0"))
|
833 |
|
834 |
|
835 |
@InstanceCheck(INST_UP, INST_UP, FIRST_ARG)
|
836 |
def TestRapiInstanceModify(instance): |
837 |
"""Test modifying instance via RAPI"""
|
838 |
default_hv = qa_config.GetDefaultHypervisor() |
839 |
|
840 |
def _ModifyInstance(**kwargs): |
841 |
_WaitForRapiJob(_rapi_client.ModifyInstance(instance.name, **kwargs)) |
842 |
|
843 |
_ModifyInstance(beparams={ |
844 |
constants.BE_VCPUS: 3,
|
845 |
}) |
846 |
|
847 |
_ModifyInstance(beparams={ |
848 |
constants.BE_VCPUS: constants.VALUE_DEFAULT, |
849 |
}) |
850 |
|
851 |
if default_hv == constants.HT_XEN_PVM:
|
852 |
_ModifyInstance(hvparams={ |
853 |
constants.HV_KERNEL_ARGS: "single",
|
854 |
}) |
855 |
_ModifyInstance(hvparams={ |
856 |
constants.HV_KERNEL_ARGS: constants.VALUE_DEFAULT, |
857 |
}) |
858 |
elif default_hv == constants.HT_XEN_HVM:
|
859 |
_ModifyInstance(hvparams={ |
860 |
constants.HV_BOOT_ORDER: "acn",
|
861 |
}) |
862 |
_ModifyInstance(hvparams={ |
863 |
constants.HV_BOOT_ORDER: constants.VALUE_DEFAULT, |
864 |
}) |
865 |
|
866 |
|
867 |
@InstanceCheck(INST_UP, INST_UP, FIRST_ARG)
|
868 |
def TestRapiInstanceConsole(instance): |
869 |
"""Test getting instance console information via RAPI"""
|
870 |
result = _rapi_client.GetInstanceConsole(instance.name) |
871 |
console = objects.InstanceConsole.FromDict(result) |
872 |
AssertEqual(console.Validate(), None)
|
873 |
AssertEqual(console.instance, qa_utils.ResolveInstanceName(instance.name)) |
874 |
|
875 |
|
876 |
@InstanceCheck(INST_DOWN, INST_DOWN, FIRST_ARG)
|
877 |
def TestRapiStoppedInstanceConsole(instance): |
878 |
"""Test getting stopped instance's console information via RAPI"""
|
879 |
try:
|
880 |
_rapi_client.GetInstanceConsole(instance.name) |
881 |
except rapi.client.GanetiApiError, err:
|
882 |
AssertEqual(err.code, 503)
|
883 |
else:
|
884 |
raise qa_error.Error("Getting console for stopped instance didn't" |
885 |
" return HTTP 503")
|
886 |
|
887 |
|
888 |
def GetOperatingSystems(): |
889 |
"""Retrieves a list of all available operating systems.
|
890 |
|
891 |
"""
|
892 |
return _rapi_client.GetOperatingSystems()
|
893 |
|
894 |
|
895 |
def _InvokeMoveInstance(current_dest_inst, current_src_inst, rapi_pw_filename, |
896 |
joint_master, perform_checks, target_nodes=None):
|
897 |
""" Invokes the move-instance tool for testing purposes.
|
898 |
|
899 |
"""
|
900 |
# Some uses of this test might require that RAPI-only commands are used,
|
901 |
# and the checks are command-line based.
|
902 |
if perform_checks:
|
903 |
qa_utils.RunInstanceCheck(current_dest_inst, False)
|
904 |
|
905 |
cmd = [ |
906 |
"../tools/move-instance",
|
907 |
"--verbose",
|
908 |
"--src-ca-file=%s" % _rapi_ca.name,
|
909 |
"--src-username=%s" % _rapi_username,
|
910 |
"--src-password-file=%s" % rapi_pw_filename,
|
911 |
"--dest-instance-name=%s" % current_dest_inst,
|
912 |
] |
913 |
|
914 |
if target_nodes:
|
915 |
pnode, snode = target_nodes |
916 |
cmd.extend([ |
917 |
"--dest-primary-node=%s" % pnode,
|
918 |
"--dest-secondary-node=%s" % snode,
|
919 |
]) |
920 |
else:
|
921 |
cmd.extend([ |
922 |
"--iallocator=%s" % constants.IALLOC_HAIL,
|
923 |
"--opportunistic-tries=1",
|
924 |
]) |
925 |
|
926 |
cmd.extend([ |
927 |
"--net=0:mac=%s" % constants.VALUE_GENERATE,
|
928 |
joint_master, |
929 |
joint_master, |
930 |
current_src_inst, |
931 |
]) |
932 |
|
933 |
AssertEqual(StartLocalCommand(cmd).wait(), 0)
|
934 |
|
935 |
if perform_checks:
|
936 |
qa_utils.RunInstanceCheck(current_src_inst, False)
|
937 |
qa_utils.RunInstanceCheck(current_dest_inst, True)
|
938 |
|
939 |
|
940 |
def TestInterClusterInstanceMove(src_instance, dest_instance, |
941 |
inodes, tnode, perform_checks=True):
|
942 |
"""Test tools/move-instance"""
|
943 |
master = qa_config.GetMasterNode() |
944 |
|
945 |
rapi_pw_file = tempfile.NamedTemporaryFile() |
946 |
rapi_pw_file.write(_rapi_password) |
947 |
rapi_pw_file.flush() |
948 |
|
949 |
# Needed only if checks are to be performed
|
950 |
if perform_checks:
|
951 |
dest_instance.SetDiskTemplate(src_instance.disk_template) |
952 |
|
953 |
# TODO: Run some instance tests before moving back
|
954 |
|
955 |
if len(inodes) > 1: |
956 |
# No disk template currently requires more than 1 secondary node. If this
|
957 |
# changes, either this test must be skipped or the script must be updated.
|
958 |
assert len(inodes) == 2 |
959 |
snode = inodes[1]
|
960 |
else:
|
961 |
# Instance is not redundant, but we still need to pass a node
|
962 |
# (which will be ignored)
|
963 |
snode = tnode |
964 |
pnode = inodes[0]
|
965 |
|
966 |
# pnode:snode are the *current* nodes, and the first move is an
|
967 |
# iallocator-guided move outside of pnode. The node lock for the pnode
|
968 |
# assures that this happens, and while we cannot be sure where the instance
|
969 |
# will land, it is a real move.
|
970 |
locks = {locking.LEVEL_NODE: [pnode.primary]} |
971 |
RunWithLocks(_InvokeMoveInstance, locks, 600.0, False, |
972 |
dest_instance.name, src_instance.name, rapi_pw_file.name, |
973 |
master.primary, perform_checks) |
974 |
|
975 |
# And then back to pnode:snode
|
976 |
_InvokeMoveInstance(src_instance.name, dest_instance.name, rapi_pw_file.name, |
977 |
master.primary, perform_checks, |
978 |
target_nodes=(pnode.primary, snode.primary)) |