Statistics
| Branch: | Tag: | Revision:

root / qa / qa_rapi.py @ 2237687b

History | View | Annotate | Download (13.8 kB)

1 a47f574c Oleksiy Mishchenko
#
2 a47f574c Oleksiy Mishchenko
3 3582eef6 Iustin Pop
# Copyright (C) 2007, 2008, 2009, 2010, 2011 Google Inc.
4 a47f574c Oleksiy Mishchenko
#
5 a47f574c Oleksiy Mishchenko
# This program is free software; you can redistribute it and/or modify
6 a47f574c Oleksiy Mishchenko
# it under the terms of the GNU General Public License as published by
7 a47f574c Oleksiy Mishchenko
# the Free Software Foundation; either version 2 of the License, or
8 a47f574c Oleksiy Mishchenko
# (at your option) any later version.
9 a47f574c Oleksiy Mishchenko
#
10 a47f574c Oleksiy Mishchenko
# This program is distributed in the hope that it will be useful, but
11 a47f574c Oleksiy Mishchenko
# WITHOUT ANY WARRANTY; without even the implied warranty of
12 a47f574c Oleksiy Mishchenko
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 a47f574c Oleksiy Mishchenko
# General Public License for more details.
14 a47f574c Oleksiy Mishchenko
#
15 a47f574c Oleksiy Mishchenko
# You should have received a copy of the GNU General Public License
16 a47f574c Oleksiy Mishchenko
# along with this program; if not, write to the Free Software
17 a47f574c Oleksiy Mishchenko
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 a47f574c Oleksiy Mishchenko
# 02110-1301, USA.
19 a47f574c Oleksiy Mishchenko
20 a47f574c Oleksiy Mishchenko
21 a47f574c Oleksiy Mishchenko
"""Remote API QA tests.
22 a47f574c Oleksiy Mishchenko

23 a47f574c Oleksiy Mishchenko
"""
24 a47f574c Oleksiy Mishchenko
25 2771835c Michael Hanselmann
import tempfile
26 a47f574c Oleksiy Mishchenko
27 a47f574c Oleksiy Mishchenko
from ganeti import utils
28 a47f574c Oleksiy Mishchenko
from ganeti import constants
29 a47f574c Oleksiy Mishchenko
from ganeti import errors
30 2771835c Michael Hanselmann
from ganeti import cli
31 2771835c Michael Hanselmann
from ganeti import rapi
32 2771835c Michael Hanselmann
33 3582eef6 Iustin Pop
import ganeti.rapi.client        # pylint: disable-msg=W0611
34 2771835c Michael Hanselmann
import ganeti.rapi.client_utils
35 a47f574c Oleksiy Mishchenko
36 a47f574c Oleksiy Mishchenko
import qa_config
37 a47f574c Oleksiy Mishchenko
import qa_utils
38 a47f574c Oleksiy Mishchenko
import qa_error
39 a47f574c Oleksiy Mishchenko
40 3582eef6 Iustin Pop
from qa_utils import (AssertEqual, AssertIn, AssertMatch, StartLocalCommand)
41 a47f574c Oleksiy Mishchenko
42 a47f574c Oleksiy Mishchenko
43 2771835c Michael Hanselmann
_rapi_ca = None
44 2771835c Michael Hanselmann
_rapi_client = None
45 5d831182 Michael Hanselmann
_rapi_username = None
46 5d831182 Michael Hanselmann
_rapi_password = None
47 e6ce18ac René Nussbaumer
48 e6ce18ac René Nussbaumer
49 2771835c Michael Hanselmann
def Setup(username, password):
50 2771835c Michael Hanselmann
  """Configures the RAPI client.
51 725ec2f1 René Nussbaumer

52 2771835c Michael Hanselmann
  """
53 3582eef6 Iustin Pop
  # pylint: disable-msg=W0603
54 3582eef6 Iustin Pop
  # due to global usage
55 2771835c Michael Hanselmann
  global _rapi_ca
56 2771835c Michael Hanselmann
  global _rapi_client
57 5d831182 Michael Hanselmann
  global _rapi_username
58 5d831182 Michael Hanselmann
  global _rapi_password
59 5d831182 Michael Hanselmann
60 5d831182 Michael Hanselmann
  _rapi_username = username
61 5d831182 Michael Hanselmann
  _rapi_password = password
62 e6ce18ac René Nussbaumer
63 2771835c Michael Hanselmann
  master = qa_config.GetMasterNode()
64 e6ce18ac René Nussbaumer
65 2771835c Michael Hanselmann
  # Load RAPI certificate from master node
66 2771835c Michael Hanselmann
  cmd = ["cat", constants.RAPI_CERT_FILE]
67 e6ce18ac René Nussbaumer
68 2771835c Michael Hanselmann
  # Write to temporary file
69 2771835c Michael Hanselmann
  _rapi_ca = tempfile.NamedTemporaryFile()
70 2771835c Michael Hanselmann
  _rapi_ca.write(qa_utils.GetCommandOutput(master["primary"],
71 2771835c Michael Hanselmann
                                           utils.ShellQuoteArgs(cmd)))
72 2771835c Michael Hanselmann
  _rapi_ca.flush()
73 e6ce18ac René Nussbaumer
74 2771835c Michael Hanselmann
  port = qa_config.get("rapi-port", default=constants.DEFAULT_RAPI_PORT)
75 2a7c3583 Michael Hanselmann
  cfg_curl = rapi.client.GenericCurlConfig(cafile=_rapi_ca.name,
76 2a7c3583 Michael Hanselmann
                                           proxy="")
77 e6ce18ac René Nussbaumer
78 2771835c Michael Hanselmann
  _rapi_client = rapi.client.GanetiRapiClient(master["primary"], port=port,
79 2771835c Michael Hanselmann
                                              username=username,
80 2771835c Michael Hanselmann
                                              password=password,
81 2a7c3583 Michael Hanselmann
                                              curl_config_fn=cfg_curl)
82 e6ce18ac René Nussbaumer
83 2771835c Michael Hanselmann
  print "RAPI protocol version: %s" % _rapi_client.GetVersion()
84 a47f574c Oleksiy Mishchenko
85 a47f574c Oleksiy Mishchenko
86 a47f574c Oleksiy Mishchenko
INSTANCE_FIELDS = ("name", "os", "pnode", "snodes",
87 a5b9d725 Iustin Pop
                   "admin_state",
88 a5b9d725 Iustin Pop
                   "disk_template", "disk.sizes",
89 22482387 Iustin Pop
                   "nic.ips", "nic.macs", "nic.modes", "nic.links",
90 a5b9d725 Iustin Pop
                   "beparams", "hvparams",
91 4ea3de4e Balazs Lecz
                   "oper_state", "oper_ram", "oper_vcpus", "status", "tags")
92 a47f574c Oleksiy Mishchenko
93 a47f574c Oleksiy Mishchenko
NODE_FIELDS = ("name", "dtotal", "dfree",
94 a47f574c Oleksiy Mishchenko
               "mtotal", "mnode", "mfree",
95 a47f574c Oleksiy Mishchenko
               "pinst_cnt", "sinst_cnt", "tags")
96 a47f574c Oleksiy Mishchenko
97 30131294 Adeodato Simo
GROUP_FIELDS = frozenset([
98 30131294 Adeodato Simo
  "name", "uuid",
99 90e99856 Adeodato Simo
  "alloc_policy",
100 30131294 Adeodato Simo
  "node_cnt", "node_list",
101 30131294 Adeodato Simo
  ])
102 30131294 Adeodato Simo
103 94e63ca1 Michael Hanselmann
JOB_FIELDS = frozenset([
104 94e63ca1 Michael Hanselmann
  "id", "ops", "status", "summary",
105 94e63ca1 Michael Hanselmann
  "opstatus", "opresult", "oplog",
106 94e63ca1 Michael Hanselmann
  "received_ts", "start_ts", "end_ts",
107 94e63ca1 Michael Hanselmann
  ])
108 94e63ca1 Michael Hanselmann
109 68289c75 Iustin Pop
LIST_FIELDS = ("id", "uri")
110 a47f574c Oleksiy Mishchenko
111 a47f574c Oleksiy Mishchenko
112 a47f574c Oleksiy Mishchenko
def Enabled():
113 a47f574c Oleksiy Mishchenko
  """Return whether remote API tests should be run.
114 a47f574c Oleksiy Mishchenko

115 a47f574c Oleksiy Mishchenko
  """
116 e1876432 Guido Trotter
  return qa_config.TestEnabled('rapi')
117 a47f574c Oleksiy Mishchenko
118 a47f574c Oleksiy Mishchenko
119 a47f574c Oleksiy Mishchenko
def _DoTests(uris):
120 3582eef6 Iustin Pop
  # pylint: disable-msg=W0212
121 3582eef6 Iustin Pop
  # due to _SendRequest usage
122 94e63ca1 Michael Hanselmann
  results = []
123 a47f574c Oleksiy Mishchenko
124 94e63ca1 Michael Hanselmann
  for uri, verify, method, body in uris:
125 a47f574c Oleksiy Mishchenko
    assert uri.startswith("/")
126 a47f574c Oleksiy Mishchenko
127 c326b4ef Michael Hanselmann
    print "%s %s" % (method, uri)
128 2771835c Michael Hanselmann
    data = _rapi_client._SendRequest(method, uri, None, body)
129 a47f574c Oleksiy Mishchenko
130 a47f574c Oleksiy Mishchenko
    if verify is not None:
131 a47f574c Oleksiy Mishchenko
      if callable(verify):
132 a47f574c Oleksiy Mishchenko
        verify(data)
133 a47f574c Oleksiy Mishchenko
      else:
134 a47f574c Oleksiy Mishchenko
        AssertEqual(data, verify)
135 a47f574c Oleksiy Mishchenko
136 fd837171 Michael Hanselmann
    results.append(data)
137 94e63ca1 Michael Hanselmann
138 94e63ca1 Michael Hanselmann
  return results
139 94e63ca1 Michael Hanselmann
140 94e63ca1 Michael Hanselmann
141 94e63ca1 Michael Hanselmann
def _VerifyReturnsJob(data):
142 94e63ca1 Michael Hanselmann
  AssertMatch(data, r'^\d+$')
143 94e63ca1 Michael Hanselmann
144 a47f574c Oleksiy Mishchenko
145 a47f574c Oleksiy Mishchenko
def TestVersion():
146 a47f574c Oleksiy Mishchenko
  """Testing remote API version.
147 a47f574c Oleksiy Mishchenko

148 a47f574c Oleksiy Mishchenko
  """
149 a47f574c Oleksiy Mishchenko
  _DoTests([
150 94e63ca1 Michael Hanselmann
    ("/version", constants.RAPI_VERSION, 'GET', None),
151 a47f574c Oleksiy Mishchenko
    ])
152 a47f574c Oleksiy Mishchenko
153 a47f574c Oleksiy Mishchenko
154 a47f574c Oleksiy Mishchenko
def TestEmptyCluster():
155 a47f574c Oleksiy Mishchenko
  """Testing remote API on an empty cluster.
156 a47f574c Oleksiy Mishchenko

157 a47f574c Oleksiy Mishchenko
  """
158 94e63ca1 Michael Hanselmann
  master = qa_config.GetMasterNode()
159 94e63ca1 Michael Hanselmann
  master_full = qa_utils.ResolveNodeName(master)
160 a47f574c Oleksiy Mishchenko
161 a47f574c Oleksiy Mishchenko
  def _VerifyInfo(data):
162 a47f574c Oleksiy Mishchenko
    AssertIn("name", data)
163 a47f574c Oleksiy Mishchenko
    AssertIn("master", data)
164 94e63ca1 Michael Hanselmann
    AssertEqual(data["master"], master_full)
165 a47f574c Oleksiy Mishchenko
166 a47f574c Oleksiy Mishchenko
  def _VerifyNodes(data):
167 a47f574c Oleksiy Mishchenko
    master_entry = {
168 94e63ca1 Michael Hanselmann
      "id": master_full,
169 94e63ca1 Michael Hanselmann
      "uri": "/2/nodes/%s" % master_full,
170 a47f574c Oleksiy Mishchenko
      }
171 a47f574c Oleksiy Mishchenko
    AssertIn(master_entry, data)
172 a47f574c Oleksiy Mishchenko
173 a47f574c Oleksiy Mishchenko
  def _VerifyNodesBulk(data):
174 a47f574c Oleksiy Mishchenko
    for node in data:
175 a47f574c Oleksiy Mishchenko
      for entry in NODE_FIELDS:
176 a47f574c Oleksiy Mishchenko
        AssertIn(entry, node)
177 a47f574c Oleksiy Mishchenko
178 30131294 Adeodato Simo
  def _VerifyGroups(data):
179 30131294 Adeodato Simo
    default_group = {
180 75cf411a Adeodato Simo
      "name": constants.INITIAL_NODE_GROUP_NAME,
181 75cf411a Adeodato Simo
      "uri": "/2/groups/" + constants.INITIAL_NODE_GROUP_NAME,
182 30131294 Adeodato Simo
      }
183 30131294 Adeodato Simo
    AssertIn(default_group, data)
184 30131294 Adeodato Simo
185 30131294 Adeodato Simo
  def _VerifyGroupsBulk(data):
186 30131294 Adeodato Simo
    for group in data:
187 30131294 Adeodato Simo
      for field in GROUP_FIELDS:
188 30131294 Adeodato Simo
        AssertIn(field, group)
189 30131294 Adeodato Simo
190 a47f574c Oleksiy Mishchenko
  _DoTests([
191 94e63ca1 Michael Hanselmann
    ("/", None, 'GET', None),
192 94e63ca1 Michael Hanselmann
    ("/2/info", _VerifyInfo, 'GET', None),
193 94e63ca1 Michael Hanselmann
    ("/2/tags", None, 'GET', None),
194 94e63ca1 Michael Hanselmann
    ("/2/nodes", _VerifyNodes, 'GET', None),
195 94e63ca1 Michael Hanselmann
    ("/2/nodes?bulk=1", _VerifyNodesBulk, 'GET', None),
196 30131294 Adeodato Simo
    ("/2/groups", _VerifyGroups, 'GET', None),
197 30131294 Adeodato Simo
    ("/2/groups?bulk=1", _VerifyGroupsBulk, 'GET', None),
198 94e63ca1 Michael Hanselmann
    ("/2/instances", [], 'GET', None),
199 94e63ca1 Michael Hanselmann
    ("/2/instances?bulk=1", [], 'GET', None),
200 94e63ca1 Michael Hanselmann
    ("/2/os", None, 'GET', None),
201 a47f574c Oleksiy Mishchenko
    ])
202 a47f574c Oleksiy Mishchenko
203 4d2bd00a Michael Hanselmann
  # Test HTTP Not Found
204 4d2bd00a Michael Hanselmann
  for method in ["GET", "PUT", "POST", "DELETE"]:
205 4d2bd00a Michael Hanselmann
    try:
206 4d2bd00a Michael Hanselmann
      _DoTests([("/99/resource/not/here/99", None, method, None)])
207 4d2bd00a Michael Hanselmann
    except rapi.client.GanetiApiError, err:
208 4d2bd00a Michael Hanselmann
      AssertEqual(err.code, 404)
209 4d2bd00a Michael Hanselmann
    else:
210 4d2bd00a Michael Hanselmann
      raise qa_error.Error("Non-existent resource didn't return HTTP 404")
211 4d2bd00a Michael Hanselmann
212 4d2bd00a Michael Hanselmann
  # Test HTTP Not Implemented
213 4d2bd00a Michael Hanselmann
  for method in ["PUT", "POST", "DELETE"]:
214 4d2bd00a Michael Hanselmann
    try:
215 4d2bd00a Michael Hanselmann
      _DoTests([("/version", None, method, None)])
216 4d2bd00a Michael Hanselmann
    except rapi.client.GanetiApiError, err:
217 4d2bd00a Michael Hanselmann
      AssertEqual(err.code, 501)
218 4d2bd00a Michael Hanselmann
    else:
219 4d2bd00a Michael Hanselmann
      raise qa_error.Error("Non-implemented method didn't fail")
220 4d2bd00a Michael Hanselmann
221 a47f574c Oleksiy Mishchenko
222 a47f574c Oleksiy Mishchenko
def TestInstance(instance):
223 a47f574c Oleksiy Mishchenko
  """Testing getting instance(s) info via remote API.
224 a47f574c Oleksiy Mishchenko

225 a47f574c Oleksiy Mishchenko
  """
226 a47f574c Oleksiy Mishchenko
  def _VerifyInstance(data):
227 a47f574c Oleksiy Mishchenko
    for entry in INSTANCE_FIELDS:
228 a47f574c Oleksiy Mishchenko
      AssertIn(entry, data)
229 c85d3b64 Michael Hanselmann
230 a47f574c Oleksiy Mishchenko
  def _VerifyInstancesList(data):
231 a47f574c Oleksiy Mishchenko
    for instance in data:
232 c85d3b64 Michael Hanselmann
      for entry in LIST_FIELDS:
233 a47f574c Oleksiy Mishchenko
        AssertIn(entry, instance)
234 c85d3b64 Michael Hanselmann
235 a47f574c Oleksiy Mishchenko
  def _VerifyInstancesBulk(data):
236 a47f574c Oleksiy Mishchenko
    for instance_data in data:
237 a47f574c Oleksiy Mishchenko
      _VerifyInstance(instance_data)
238 a47f574c Oleksiy Mishchenko
239 a47f574c Oleksiy Mishchenko
  _DoTests([
240 94e63ca1 Michael Hanselmann
    ("/2/instances/%s" % instance["name"], _VerifyInstance, 'GET', None),
241 94e63ca1 Michael Hanselmann
    ("/2/instances", _VerifyInstancesList, 'GET', None),
242 94e63ca1 Michael Hanselmann
    ("/2/instances?bulk=1", _VerifyInstancesBulk, 'GET', None),
243 94e63ca1 Michael Hanselmann
    ("/2/instances/%s/activate-disks" % instance["name"],
244 94e63ca1 Michael Hanselmann
     _VerifyReturnsJob, 'PUT', None),
245 94e63ca1 Michael Hanselmann
    ("/2/instances/%s/deactivate-disks" % instance["name"],
246 94e63ca1 Michael Hanselmann
     _VerifyReturnsJob, 'PUT', None),
247 a47f574c Oleksiy Mishchenko
    ])
248 a47f574c Oleksiy Mishchenko
249 71910715 Iustin Pop
  # Test OpBackupPrepare
250 ebeb600f Michael Hanselmann
  (job_id, ) = _DoTests([
251 ebeb600f Michael Hanselmann
    ("/2/instances/%s/prepare-export?mode=%s" %
252 ebeb600f Michael Hanselmann
     (instance["name"], constants.EXPORT_MODE_REMOTE),
253 ebeb600f Michael Hanselmann
     _VerifyReturnsJob, "PUT", None),
254 ebeb600f Michael Hanselmann
    ])
255 ebeb600f Michael Hanselmann
256 ebeb600f Michael Hanselmann
  result = _WaitForRapiJob(job_id)[0]
257 ebeb600f Michael Hanselmann
  AssertEqual(len(result["handshake"]), 3)
258 ebeb600f Michael Hanselmann
  AssertEqual(result["handshake"][0], constants.RIE_VERSION)
259 ebeb600f Michael Hanselmann
  AssertEqual(len(result["x509_key_name"]), 3)
260 ebeb600f Michael Hanselmann
  AssertIn("-----BEGIN CERTIFICATE-----", result["x509_ca"])
261 ebeb600f Michael Hanselmann
262 a47f574c Oleksiy Mishchenko
263 a47f574c Oleksiy Mishchenko
def TestNode(node):
264 a47f574c Oleksiy Mishchenko
  """Testing getting node(s) info via remote API.
265 a47f574c Oleksiy Mishchenko

266 a47f574c Oleksiy Mishchenko
  """
267 a47f574c Oleksiy Mishchenko
  def _VerifyNode(data):
268 a47f574c Oleksiy Mishchenko
    for entry in NODE_FIELDS:
269 a47f574c Oleksiy Mishchenko
      AssertIn(entry, data)
270 c85d3b64 Michael Hanselmann
271 a47f574c Oleksiy Mishchenko
  def _VerifyNodesList(data):
272 a47f574c Oleksiy Mishchenko
    for node in data:
273 c85d3b64 Michael Hanselmann
      for entry in LIST_FIELDS:
274 a47f574c Oleksiy Mishchenko
        AssertIn(entry, node)
275 c85d3b64 Michael Hanselmann
276 a47f574c Oleksiy Mishchenko
  def _VerifyNodesBulk(data):
277 a47f574c Oleksiy Mishchenko
    for node_data in data:
278 a47f574c Oleksiy Mishchenko
      _VerifyNode(node_data)
279 a47f574c Oleksiy Mishchenko
280 a47f574c Oleksiy Mishchenko
  _DoTests([
281 94e63ca1 Michael Hanselmann
    ("/2/nodes/%s" % node["primary"], _VerifyNode, 'GET', None),
282 94e63ca1 Michael Hanselmann
    ("/2/nodes", _VerifyNodesList, 'GET', None),
283 94e63ca1 Michael Hanselmann
    ("/2/nodes?bulk=1", _VerifyNodesBulk, 'GET', None),
284 a47f574c Oleksiy Mishchenko
    ])
285 a47f574c Oleksiy Mishchenko
286 a47f574c Oleksiy Mishchenko
287 a47f574c Oleksiy Mishchenko
def TestTags(kind, name, tags):
288 a47f574c Oleksiy Mishchenko
  """Tests .../tags resources.
289 a47f574c Oleksiy Mishchenko

290 a47f574c Oleksiy Mishchenko
  """
291 a47f574c Oleksiy Mishchenko
  if kind == constants.TAG_CLUSTER:
292 a5b9d725 Iustin Pop
    uri = "/2/tags"
293 a47f574c Oleksiy Mishchenko
  elif kind == constants.TAG_NODE:
294 a5b9d725 Iustin Pop
    uri = "/2/nodes/%s/tags" % name
295 a47f574c Oleksiy Mishchenko
  elif kind == constants.TAG_INSTANCE:
296 a5b9d725 Iustin Pop
    uri = "/2/instances/%s/tags" % name
297 a47f574c Oleksiy Mishchenko
  else:
298 a47f574c Oleksiy Mishchenko
    raise errors.ProgrammerError("Unknown tag kind")
299 a47f574c Oleksiy Mishchenko
300 a47f574c Oleksiy Mishchenko
  def _VerifyTags(data):
301 94e63ca1 Michael Hanselmann
    AssertEqual(sorted(tags), sorted(data))
302 a47f574c Oleksiy Mishchenko
303 c326b4ef Michael Hanselmann
  query = "&".join("tag=%s" % i for i in tags)
304 c326b4ef Michael Hanselmann
305 c326b4ef Michael Hanselmann
  # Add tags
306 c326b4ef Michael Hanselmann
  (job_id, ) = _DoTests([
307 c326b4ef Michael Hanselmann
    ("%s?%s" % (uri, query), _VerifyReturnsJob, "PUT", None),
308 c326b4ef Michael Hanselmann
    ])
309 c326b4ef Michael Hanselmann
  _WaitForRapiJob(job_id)
310 c326b4ef Michael Hanselmann
311 c326b4ef Michael Hanselmann
  # Retrieve tags
312 a47f574c Oleksiy Mishchenko
  _DoTests([
313 94e63ca1 Michael Hanselmann
    (uri, _VerifyTags, 'GET', None),
314 a47f574c Oleksiy Mishchenko
    ])
315 8cb70e56 Michael Hanselmann
316 c326b4ef Michael Hanselmann
  # Remove tags
317 c326b4ef Michael Hanselmann
  (job_id, ) = _DoTests([
318 c326b4ef Michael Hanselmann
    ("%s?%s" % (uri, query), _VerifyReturnsJob, "DELETE", None),
319 c326b4ef Michael Hanselmann
    ])
320 c326b4ef Michael Hanselmann
  _WaitForRapiJob(job_id)
321 c326b4ef Michael Hanselmann
322 8cb70e56 Michael Hanselmann
323 8cb70e56 Michael Hanselmann
def _WaitForRapiJob(job_id):
324 8cb70e56 Michael Hanselmann
  """Waits for a job to finish.
325 8cb70e56 Michael Hanselmann

326 8cb70e56 Michael Hanselmann
  """
327 8cb70e56 Michael Hanselmann
  def _VerifyJob(data):
328 8cb70e56 Michael Hanselmann
    AssertEqual(data["id"], job_id)
329 8cb70e56 Michael Hanselmann
    for field in JOB_FIELDS:
330 8cb70e56 Michael Hanselmann
      AssertIn(field, data)
331 8cb70e56 Michael Hanselmann
332 8cb70e56 Michael Hanselmann
  _DoTests([
333 8cb70e56 Michael Hanselmann
    ("/2/jobs/%s" % job_id, _VerifyJob, "GET", None),
334 8cb70e56 Michael Hanselmann
    ])
335 8cb70e56 Michael Hanselmann
336 ebeb600f Michael Hanselmann
  return rapi.client_utils.PollJob(_rapi_client, job_id,
337 ebeb600f Michael Hanselmann
                                   cli.StdioJobPollReportCb())
338 8cb70e56 Michael Hanselmann
339 8cb70e56 Michael Hanselmann
340 4b10fb65 Adeodato Simo
def TestRapiNodeGroups():
341 4b10fb65 Adeodato Simo
  """Test several node group operations using RAPI.
342 4b10fb65 Adeodato Simo

343 4b10fb65 Adeodato Simo
  """
344 4b10fb65 Adeodato Simo
  groups = qa_config.get("groups", {})
345 4b10fb65 Adeodato Simo
  group1, group2, group3 = groups.get("inexistent-groups",
346 4b10fb65 Adeodato Simo
                                      ["group1", "group2", "group3"])[:3]
347 4b10fb65 Adeodato Simo
348 4b10fb65 Adeodato Simo
  # Create a group with no attributes
349 4b10fb65 Adeodato Simo
  body = {
350 4b10fb65 Adeodato Simo
    "name": group1,
351 4b10fb65 Adeodato Simo
    }
352 4b10fb65 Adeodato Simo
353 4b10fb65 Adeodato Simo
  (job_id, ) = _DoTests([
354 4b10fb65 Adeodato Simo
    ("/2/groups", _VerifyReturnsJob, "POST", body),
355 4b10fb65 Adeodato Simo
    ])
356 4b10fb65 Adeodato Simo
357 4b10fb65 Adeodato Simo
  _WaitForRapiJob(job_id)
358 4b10fb65 Adeodato Simo
359 4b10fb65 Adeodato Simo
  # Create a group specifying alloc_policy
360 4b10fb65 Adeodato Simo
  body = {
361 4b10fb65 Adeodato Simo
    "name": group2,
362 4b10fb65 Adeodato Simo
    "alloc_policy": constants.ALLOC_POLICY_UNALLOCABLE,
363 4b10fb65 Adeodato Simo
    }
364 4b10fb65 Adeodato Simo
365 4b10fb65 Adeodato Simo
  (job_id, ) = _DoTests([
366 4b10fb65 Adeodato Simo
    ("/2/groups", _VerifyReturnsJob, "POST", body),
367 4b10fb65 Adeodato Simo
    ])
368 4b10fb65 Adeodato Simo
369 4b10fb65 Adeodato Simo
  _WaitForRapiJob(job_id)
370 4b10fb65 Adeodato Simo
371 4b10fb65 Adeodato Simo
  # Modify alloc_policy
372 4b10fb65 Adeodato Simo
  body = {
373 4b10fb65 Adeodato Simo
    "alloc_policy": constants.ALLOC_POLICY_UNALLOCABLE,
374 4b10fb65 Adeodato Simo
    }
375 4b10fb65 Adeodato Simo
376 4b10fb65 Adeodato Simo
  (job_id, ) = _DoTests([
377 4b10fb65 Adeodato Simo
    ("/2/groups/%s/modify" % group1, _VerifyReturnsJob, "PUT", body),
378 4b10fb65 Adeodato Simo
    ])
379 4b10fb65 Adeodato Simo
380 4b10fb65 Adeodato Simo
  _WaitForRapiJob(job_id)
381 4b10fb65 Adeodato Simo
382 4b10fb65 Adeodato Simo
  # Rename a group
383 4b10fb65 Adeodato Simo
  body = {
384 4b10fb65 Adeodato Simo
    "new_name": group3,
385 4b10fb65 Adeodato Simo
    }
386 4b10fb65 Adeodato Simo
387 4b10fb65 Adeodato Simo
  (job_id, ) = _DoTests([
388 4b10fb65 Adeodato Simo
    ("/2/groups/%s/rename" % group2, _VerifyReturnsJob, "PUT", body),
389 4b10fb65 Adeodato Simo
    ])
390 4b10fb65 Adeodato Simo
391 4b10fb65 Adeodato Simo
  _WaitForRapiJob(job_id)
392 4b10fb65 Adeodato Simo
393 4b10fb65 Adeodato Simo
  # Delete groups
394 4b10fb65 Adeodato Simo
  for group in [group1, group3]:
395 4b10fb65 Adeodato Simo
    (job_id, ) = _DoTests([
396 4b10fb65 Adeodato Simo
      ("/2/groups/%s" % group, _VerifyReturnsJob, "DELETE", None),
397 4b10fb65 Adeodato Simo
      ])
398 4b10fb65 Adeodato Simo
399 4b10fb65 Adeodato Simo
    _WaitForRapiJob(job_id)
400 4b10fb65 Adeodato Simo
401 4b10fb65 Adeodato Simo
402 924e95f9 Michael Hanselmann
def TestRapiInstanceAdd(node, use_client):
403 8cb70e56 Michael Hanselmann
  """Test adding a new instance via RAPI"""
404 8cb70e56 Michael Hanselmann
  instance = qa_config.AcquireInstance()
405 8cb70e56 Michael Hanselmann
  try:
406 924e95f9 Michael Hanselmann
    memory = utils.ParseUnit(qa_config.get("mem"))
407 924e95f9 Michael Hanselmann
    disk_sizes = [utils.ParseUnit(size) for size in qa_config.get("disk")]
408 924e95f9 Michael Hanselmann
409 924e95f9 Michael Hanselmann
    if use_client:
410 924e95f9 Michael Hanselmann
      disks = [{"size": size} for size in disk_sizes]
411 924e95f9 Michael Hanselmann
      nics = [{}]
412 924e95f9 Michael Hanselmann
413 924e95f9 Michael Hanselmann
      beparams = {
414 924e95f9 Michael Hanselmann
        constants.BE_MEMORY: memory,
415 924e95f9 Michael Hanselmann
        }
416 924e95f9 Michael Hanselmann
417 924e95f9 Michael Hanselmann
      job_id = _rapi_client.CreateInstance(constants.INSTANCE_CREATE,
418 924e95f9 Michael Hanselmann
                                           instance["name"],
419 924e95f9 Michael Hanselmann
                                           constants.DT_PLAIN,
420 924e95f9 Michael Hanselmann
                                           disks, nics,
421 924e95f9 Michael Hanselmann
                                           os=qa_config.get("os"),
422 924e95f9 Michael Hanselmann
                                           pnode=node["primary"],
423 924e95f9 Michael Hanselmann
                                           beparams=beparams)
424 924e95f9 Michael Hanselmann
    else:
425 924e95f9 Michael Hanselmann
      body = {
426 924e95f9 Michael Hanselmann
        "name": instance["name"],
427 924e95f9 Michael Hanselmann
        "os": qa_config.get("os"),
428 924e95f9 Michael Hanselmann
        "disk_template": constants.DT_PLAIN,
429 924e95f9 Michael Hanselmann
        "pnode": node["primary"],
430 924e95f9 Michael Hanselmann
        "memory": memory,
431 924e95f9 Michael Hanselmann
        "disks": disk_sizes,
432 924e95f9 Michael Hanselmann
        }
433 924e95f9 Michael Hanselmann
434 924e95f9 Michael Hanselmann
      (job_id, ) = _DoTests([
435 924e95f9 Michael Hanselmann
        ("/2/instances", _VerifyReturnsJob, "POST", body),
436 924e95f9 Michael Hanselmann
        ])
437 8cb70e56 Michael Hanselmann
438 8cb70e56 Michael Hanselmann
    _WaitForRapiJob(job_id)
439 8cb70e56 Michael Hanselmann
440 8cb70e56 Michael Hanselmann
    return instance
441 8cb70e56 Michael Hanselmann
  except:
442 8cb70e56 Michael Hanselmann
    qa_config.ReleaseInstance(instance)
443 8cb70e56 Michael Hanselmann
    raise
444 8cb70e56 Michael Hanselmann
445 8cb70e56 Michael Hanselmann
446 924e95f9 Michael Hanselmann
def TestRapiInstanceRemove(instance, use_client):
447 8cb70e56 Michael Hanselmann
  """Test removing instance via RAPI"""
448 924e95f9 Michael Hanselmann
  if use_client:
449 924e95f9 Michael Hanselmann
    job_id = _rapi_client.DeleteInstance(instance["name"])
450 924e95f9 Michael Hanselmann
  else:
451 924e95f9 Michael Hanselmann
    (job_id, ) = _DoTests([
452 924e95f9 Michael Hanselmann
      ("/2/instances/%s" % instance["name"], _VerifyReturnsJob, "DELETE", None),
453 924e95f9 Michael Hanselmann
      ])
454 8cb70e56 Michael Hanselmann
455 8cb70e56 Michael Hanselmann
  _WaitForRapiJob(job_id)
456 8cb70e56 Michael Hanselmann
457 8cb70e56 Michael Hanselmann
  qa_config.ReleaseInstance(instance)
458 5d831182 Michael Hanselmann
459 5d831182 Michael Hanselmann
460 938bde86 Michael Hanselmann
def TestRapiInstanceMigrate(instance):
461 938bde86 Michael Hanselmann
  """Test migrating instance via RAPI"""
462 938bde86 Michael Hanselmann
  # Move to secondary node
463 938bde86 Michael Hanselmann
  _WaitForRapiJob(_rapi_client.MigrateInstance(instance["name"]))
464 938bde86 Michael Hanselmann
  # And back to previous primary
465 938bde86 Michael Hanselmann
  _WaitForRapiJob(_rapi_client.MigrateInstance(instance["name"]))
466 938bde86 Michael Hanselmann
467 938bde86 Michael Hanselmann
468 e5c2accd Guido Trotter
def TestRapiInstanceRename(rename_source, rename_target):
469 7fb50870 Michael Hanselmann
  """Test renaming instance via RAPI"""
470 e5c2accd Guido Trotter
  _WaitForRapiJob(_rapi_client.RenameInstance(rename_source, rename_target))
471 7fb50870 Michael Hanselmann
472 7fb50870 Michael Hanselmann
473 3b7158ef Michael Hanselmann
def TestRapiInstanceModify(instance):
474 3b7158ef Michael Hanselmann
  """Test modifying instance via RAPI"""
475 3b7158ef Michael Hanselmann
  def _ModifyInstance(**kwargs):
476 3b7158ef Michael Hanselmann
    _WaitForRapiJob(_rapi_client.ModifyInstance(instance["name"], **kwargs))
477 3b7158ef Michael Hanselmann
478 3b7158ef Michael Hanselmann
  _ModifyInstance(hvparams={
479 3b7158ef Michael Hanselmann
    constants.HV_KERNEL_ARGS: "single",
480 3b7158ef Michael Hanselmann
    })
481 3b7158ef Michael Hanselmann
482 3b7158ef Michael Hanselmann
  _ModifyInstance(beparams={
483 3b7158ef Michael Hanselmann
    constants.BE_VCPUS: 3,
484 3b7158ef Michael Hanselmann
    })
485 3b7158ef Michael Hanselmann
486 3b7158ef Michael Hanselmann
  _ModifyInstance(beparams={
487 3b7158ef Michael Hanselmann
    constants.BE_VCPUS: constants.VALUE_DEFAULT,
488 3b7158ef Michael Hanselmann
    })
489 3b7158ef Michael Hanselmann
490 3b7158ef Michael Hanselmann
  _ModifyInstance(hvparams={
491 3b7158ef Michael Hanselmann
    constants.HV_KERNEL_ARGS: constants.VALUE_DEFAULT,
492 3b7158ef Michael Hanselmann
    })
493 3b7158ef Michael Hanselmann
494 3b7158ef Michael Hanselmann
495 638a7266 Iustin Pop
def TestInterClusterInstanceMove(src_instance, dest_instance,
496 638a7266 Iustin Pop
                                 pnode, snode, tnode):
497 5d831182 Michael Hanselmann
  """Test tools/move-instance"""
498 5d831182 Michael Hanselmann
  master = qa_config.GetMasterNode()
499 5d831182 Michael Hanselmann
500 5d831182 Michael Hanselmann
  rapi_pw_file = tempfile.NamedTemporaryFile()
501 5d831182 Michael Hanselmann
  rapi_pw_file.write(_rapi_password)
502 5d831182 Michael Hanselmann
  rapi_pw_file.flush()
503 5d831182 Michael Hanselmann
504 5d831182 Michael Hanselmann
  # TODO: Run some instance tests before moving back
505 677e16eb Iustin Pop
506 638a7266 Iustin Pop
  if snode is None:
507 638a7266 Iustin Pop
    # instance is not redundant, but we still need to pass a node
508 638a7266 Iustin Pop
    # (which will be ignored)
509 638a7266 Iustin Pop
    fsec = tnode
510 638a7266 Iustin Pop
  else:
511 638a7266 Iustin Pop
    fsec = snode
512 638a7266 Iustin Pop
  # note: pnode:snode are the *current* nodes, so we move it first to
513 638a7266 Iustin Pop
  # tnode:pnode, then back to pnode:snode
514 677e16eb Iustin Pop
  for si, di, pn, sn in [(src_instance["name"], dest_instance["name"],
515 638a7266 Iustin Pop
                          tnode["primary"], pnode["primary"]),
516 677e16eb Iustin Pop
                         (dest_instance["name"], src_instance["name"],
517 638a7266 Iustin Pop
                          pnode["primary"], fsec["primary"])]:
518 5d831182 Michael Hanselmann
    cmd = [
519 5d831182 Michael Hanselmann
      "../tools/move-instance",
520 5d831182 Michael Hanselmann
      "--verbose",
521 5d831182 Michael Hanselmann
      "--src-ca-file=%s" % _rapi_ca.name,
522 5d831182 Michael Hanselmann
      "--src-username=%s" % _rapi_username,
523 5d831182 Michael Hanselmann
      "--src-password-file=%s" % rapi_pw_file.name,
524 677e16eb Iustin Pop
      "--dest-instance-name=%s" % di,
525 677e16eb Iustin Pop
      "--dest-primary-node=%s" % pn,
526 677e16eb Iustin Pop
      "--dest-secondary-node=%s" % sn,
527 a889c536 Michael Hanselmann
      "--net=0:mac=%s" % constants.VALUE_GENERATE,
528 5d831182 Michael Hanselmann
      master["primary"],
529 5d831182 Michael Hanselmann
      master["primary"],
530 677e16eb Iustin Pop
      si,
531 5d831182 Michael Hanselmann
      ]
532 5d831182 Michael Hanselmann
533 5d831182 Michael Hanselmann
    AssertEqual(StartLocalCommand(cmd).wait(), 0)