Statistics
| Branch: | Tag: | Revision:

root / qa / qa_rapi.py @ 8201b996

History | View | Annotate | Download (11.5 kB)

1 a47f574c Oleksiy Mishchenko
#
2 a47f574c Oleksiy Mishchenko
3 677e16eb Iustin Pop
# Copyright (C) 2007, 2008, 2009, 2010 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 a47f574c Oleksiy Mishchenko
from ganeti import serializer
31 2771835c Michael Hanselmann
from ganeti import cli
32 2771835c Michael Hanselmann
from ganeti import rapi
33 2771835c Michael Hanselmann
34 2771835c Michael Hanselmann
import ganeti.rapi.client
35 2771835c Michael Hanselmann
import ganeti.rapi.client_utils
36 a47f574c Oleksiy Mishchenko
37 a47f574c Oleksiy Mishchenko
import qa_config
38 a47f574c Oleksiy Mishchenko
import qa_utils
39 a47f574c Oleksiy Mishchenko
import qa_error
40 a47f574c Oleksiy Mishchenko
41 e6ce18ac René Nussbaumer
from qa_utils import (AssertEqual, AssertNotEqual, AssertIn, AssertMatch,
42 5d831182 Michael Hanselmann
                      StartLocalCommand)
43 a47f574c Oleksiy Mishchenko
44 a47f574c Oleksiy Mishchenko
45 2771835c Michael Hanselmann
_rapi_ca = None
46 2771835c Michael Hanselmann
_rapi_client = None
47 5d831182 Michael Hanselmann
_rapi_username = None
48 5d831182 Michael Hanselmann
_rapi_password = None
49 e6ce18ac René Nussbaumer
50 e6ce18ac René Nussbaumer
51 2771835c Michael Hanselmann
def Setup(username, password):
52 2771835c Michael Hanselmann
  """Configures the RAPI client.
53 725ec2f1 René Nussbaumer

54 2771835c Michael Hanselmann
  """
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 94e63ca1 Michael Hanselmann
JOB_FIELDS = frozenset([
98 94e63ca1 Michael Hanselmann
  "id", "ops", "status", "summary",
99 94e63ca1 Michael Hanselmann
  "opstatus", "opresult", "oplog",
100 94e63ca1 Michael Hanselmann
  "received_ts", "start_ts", "end_ts",
101 94e63ca1 Michael Hanselmann
  ])
102 94e63ca1 Michael Hanselmann
103 68289c75 Iustin Pop
LIST_FIELDS = ("id", "uri")
104 a47f574c Oleksiy Mishchenko
105 a47f574c Oleksiy Mishchenko
106 a47f574c Oleksiy Mishchenko
def Enabled():
107 a47f574c Oleksiy Mishchenko
  """Return whether remote API tests should be run.
108 a47f574c Oleksiy Mishchenko

109 a47f574c Oleksiy Mishchenko
  """
110 e1876432 Guido Trotter
  return qa_config.TestEnabled('rapi')
111 a47f574c Oleksiy Mishchenko
112 a47f574c Oleksiy Mishchenko
113 a47f574c Oleksiy Mishchenko
def _DoTests(uris):
114 94e63ca1 Michael Hanselmann
  results = []
115 a47f574c Oleksiy Mishchenko
116 94e63ca1 Michael Hanselmann
  for uri, verify, method, body in uris:
117 a47f574c Oleksiy Mishchenko
    assert uri.startswith("/")
118 a47f574c Oleksiy Mishchenko
119 c326b4ef Michael Hanselmann
    print "%s %s" % (method, uri)
120 2771835c Michael Hanselmann
    data = _rapi_client._SendRequest(method, uri, None, body)
121 a47f574c Oleksiy Mishchenko
122 a47f574c Oleksiy Mishchenko
    if verify is not None:
123 a47f574c Oleksiy Mishchenko
      if callable(verify):
124 a47f574c Oleksiy Mishchenko
        verify(data)
125 a47f574c Oleksiy Mishchenko
      else:
126 a47f574c Oleksiy Mishchenko
        AssertEqual(data, verify)
127 a47f574c Oleksiy Mishchenko
128 fd837171 Michael Hanselmann
    results.append(data)
129 94e63ca1 Michael Hanselmann
130 94e63ca1 Michael Hanselmann
  return results
131 94e63ca1 Michael Hanselmann
132 94e63ca1 Michael Hanselmann
133 94e63ca1 Michael Hanselmann
def _VerifyReturnsJob(data):
134 94e63ca1 Michael Hanselmann
  AssertMatch(data, r'^\d+$')
135 94e63ca1 Michael Hanselmann
136 a47f574c Oleksiy Mishchenko
137 a47f574c Oleksiy Mishchenko
def TestVersion():
138 a47f574c Oleksiy Mishchenko
  """Testing remote API version.
139 a47f574c Oleksiy Mishchenko

140 a47f574c Oleksiy Mishchenko
  """
141 a47f574c Oleksiy Mishchenko
  _DoTests([
142 94e63ca1 Michael Hanselmann
    ("/version", constants.RAPI_VERSION, 'GET', None),
143 a47f574c Oleksiy Mishchenko
    ])
144 a47f574c Oleksiy Mishchenko
145 a47f574c Oleksiy Mishchenko
146 a47f574c Oleksiy Mishchenko
def TestEmptyCluster():
147 a47f574c Oleksiy Mishchenko
  """Testing remote API on an empty cluster.
148 a47f574c Oleksiy Mishchenko

149 a47f574c Oleksiy Mishchenko
  """
150 94e63ca1 Michael Hanselmann
  master = qa_config.GetMasterNode()
151 94e63ca1 Michael Hanselmann
  master_full = qa_utils.ResolveNodeName(master)
152 a47f574c Oleksiy Mishchenko
153 a47f574c Oleksiy Mishchenko
  def _VerifyInfo(data):
154 a47f574c Oleksiy Mishchenko
    AssertIn("name", data)
155 a47f574c Oleksiy Mishchenko
    AssertIn("master", data)
156 94e63ca1 Michael Hanselmann
    AssertEqual(data["master"], master_full)
157 a47f574c Oleksiy Mishchenko
158 a47f574c Oleksiy Mishchenko
  def _VerifyNodes(data):
159 a47f574c Oleksiy Mishchenko
    master_entry = {
160 94e63ca1 Michael Hanselmann
      "id": master_full,
161 94e63ca1 Michael Hanselmann
      "uri": "/2/nodes/%s" % master_full,
162 a47f574c Oleksiy Mishchenko
      }
163 a47f574c Oleksiy Mishchenko
    AssertIn(master_entry, data)
164 a47f574c Oleksiy Mishchenko
165 a47f574c Oleksiy Mishchenko
  def _VerifyNodesBulk(data):
166 a47f574c Oleksiy Mishchenko
    for node in data:
167 a47f574c Oleksiy Mishchenko
      for entry in NODE_FIELDS:
168 a47f574c Oleksiy Mishchenko
        AssertIn(entry, node)
169 a47f574c Oleksiy Mishchenko
170 a47f574c Oleksiy Mishchenko
  _DoTests([
171 94e63ca1 Michael Hanselmann
    ("/", None, 'GET', None),
172 94e63ca1 Michael Hanselmann
    ("/2/info", _VerifyInfo, 'GET', None),
173 94e63ca1 Michael Hanselmann
    ("/2/tags", None, 'GET', None),
174 94e63ca1 Michael Hanselmann
    ("/2/nodes", _VerifyNodes, 'GET', None),
175 94e63ca1 Michael Hanselmann
    ("/2/nodes?bulk=1", _VerifyNodesBulk, 'GET', None),
176 94e63ca1 Michael Hanselmann
    ("/2/instances", [], 'GET', None),
177 94e63ca1 Michael Hanselmann
    ("/2/instances?bulk=1", [], 'GET', None),
178 94e63ca1 Michael Hanselmann
    ("/2/os", None, 'GET', None),
179 a47f574c Oleksiy Mishchenko
    ])
180 a47f574c Oleksiy Mishchenko
181 a47f574c Oleksiy Mishchenko
182 a47f574c Oleksiy Mishchenko
def TestInstance(instance):
183 a47f574c Oleksiy Mishchenko
  """Testing getting instance(s) info via remote API.
184 a47f574c Oleksiy Mishchenko

185 a47f574c Oleksiy Mishchenko
  """
186 a47f574c Oleksiy Mishchenko
  def _VerifyInstance(data):
187 a47f574c Oleksiy Mishchenko
    for entry in INSTANCE_FIELDS:
188 a47f574c Oleksiy Mishchenko
      AssertIn(entry, data)
189 c85d3b64 Michael Hanselmann
190 a47f574c Oleksiy Mishchenko
  def _VerifyInstancesList(data):
191 a47f574c Oleksiy Mishchenko
    for instance in data:
192 c85d3b64 Michael Hanselmann
      for entry in LIST_FIELDS:
193 a47f574c Oleksiy Mishchenko
        AssertIn(entry, instance)
194 c85d3b64 Michael Hanselmann
195 a47f574c Oleksiy Mishchenko
  def _VerifyInstancesBulk(data):
196 a47f574c Oleksiy Mishchenko
    for instance_data in data:
197 a47f574c Oleksiy Mishchenko
      _VerifyInstance(instance_data)
198 a47f574c Oleksiy Mishchenko
199 a47f574c Oleksiy Mishchenko
  _DoTests([
200 94e63ca1 Michael Hanselmann
    ("/2/instances/%s" % instance["name"], _VerifyInstance, 'GET', None),
201 94e63ca1 Michael Hanselmann
    ("/2/instances", _VerifyInstancesList, 'GET', None),
202 94e63ca1 Michael Hanselmann
    ("/2/instances?bulk=1", _VerifyInstancesBulk, 'GET', None),
203 94e63ca1 Michael Hanselmann
    ("/2/instances/%s/activate-disks" % instance["name"],
204 94e63ca1 Michael Hanselmann
     _VerifyReturnsJob, 'PUT', None),
205 94e63ca1 Michael Hanselmann
    ("/2/instances/%s/deactivate-disks" % instance["name"],
206 94e63ca1 Michael Hanselmann
     _VerifyReturnsJob, 'PUT', None),
207 a47f574c Oleksiy Mishchenko
    ])
208 a47f574c Oleksiy Mishchenko
209 ebeb600f Michael Hanselmann
  # Test OpPrepareExport
210 ebeb600f Michael Hanselmann
  (job_id, ) = _DoTests([
211 ebeb600f Michael Hanselmann
    ("/2/instances/%s/prepare-export?mode=%s" %
212 ebeb600f Michael Hanselmann
     (instance["name"], constants.EXPORT_MODE_REMOTE),
213 ebeb600f Michael Hanselmann
     _VerifyReturnsJob, "PUT", None),
214 ebeb600f Michael Hanselmann
    ])
215 ebeb600f Michael Hanselmann
216 ebeb600f Michael Hanselmann
  result = _WaitForRapiJob(job_id)[0]
217 ebeb600f Michael Hanselmann
  AssertEqual(len(result["handshake"]), 3)
218 ebeb600f Michael Hanselmann
  AssertEqual(result["handshake"][0], constants.RIE_VERSION)
219 ebeb600f Michael Hanselmann
  AssertEqual(len(result["x509_key_name"]), 3)
220 ebeb600f Michael Hanselmann
  AssertIn("-----BEGIN CERTIFICATE-----", result["x509_ca"])
221 ebeb600f Michael Hanselmann
222 a47f574c Oleksiy Mishchenko
223 a47f574c Oleksiy Mishchenko
def TestNode(node):
224 a47f574c Oleksiy Mishchenko
  """Testing getting node(s) info via remote API.
225 a47f574c Oleksiy Mishchenko

226 a47f574c Oleksiy Mishchenko
  """
227 a47f574c Oleksiy Mishchenko
  def _VerifyNode(data):
228 a47f574c Oleksiy Mishchenko
    for entry in NODE_FIELDS:
229 a47f574c Oleksiy Mishchenko
      AssertIn(entry, data)
230 c85d3b64 Michael Hanselmann
231 a47f574c Oleksiy Mishchenko
  def _VerifyNodesList(data):
232 a47f574c Oleksiy Mishchenko
    for node in data:
233 c85d3b64 Michael Hanselmann
      for entry in LIST_FIELDS:
234 a47f574c Oleksiy Mishchenko
        AssertIn(entry, node)
235 c85d3b64 Michael Hanselmann
236 a47f574c Oleksiy Mishchenko
  def _VerifyNodesBulk(data):
237 a47f574c Oleksiy Mishchenko
    for node_data in data:
238 a47f574c Oleksiy Mishchenko
      _VerifyNode(node_data)
239 a47f574c Oleksiy Mishchenko
240 a47f574c Oleksiy Mishchenko
  _DoTests([
241 94e63ca1 Michael Hanselmann
    ("/2/nodes/%s" % node["primary"], _VerifyNode, 'GET', None),
242 94e63ca1 Michael Hanselmann
    ("/2/nodes", _VerifyNodesList, 'GET', None),
243 94e63ca1 Michael Hanselmann
    ("/2/nodes?bulk=1", _VerifyNodesBulk, 'GET', None),
244 a47f574c Oleksiy Mishchenko
    ])
245 a47f574c Oleksiy Mishchenko
246 a47f574c Oleksiy Mishchenko
247 a47f574c Oleksiy Mishchenko
def TestTags(kind, name, tags):
248 a47f574c Oleksiy Mishchenko
  """Tests .../tags resources.
249 a47f574c Oleksiy Mishchenko

250 a47f574c Oleksiy Mishchenko
  """
251 a47f574c Oleksiy Mishchenko
  if kind == constants.TAG_CLUSTER:
252 a5b9d725 Iustin Pop
    uri = "/2/tags"
253 a47f574c Oleksiy Mishchenko
  elif kind == constants.TAG_NODE:
254 a5b9d725 Iustin Pop
    uri = "/2/nodes/%s/tags" % name
255 a47f574c Oleksiy Mishchenko
  elif kind == constants.TAG_INSTANCE:
256 a5b9d725 Iustin Pop
    uri = "/2/instances/%s/tags" % name
257 a47f574c Oleksiy Mishchenko
  else:
258 a47f574c Oleksiy Mishchenko
    raise errors.ProgrammerError("Unknown tag kind")
259 a47f574c Oleksiy Mishchenko
260 a47f574c Oleksiy Mishchenko
  def _VerifyTags(data):
261 94e63ca1 Michael Hanselmann
    AssertEqual(sorted(tags), sorted(data))
262 a47f574c Oleksiy Mishchenko
263 c326b4ef Michael Hanselmann
  query = "&".join("tag=%s" % i for i in tags)
264 c326b4ef Michael Hanselmann
265 c326b4ef Michael Hanselmann
  # Add tags
266 c326b4ef Michael Hanselmann
  (job_id, ) = _DoTests([
267 c326b4ef Michael Hanselmann
    ("%s?%s" % (uri, query), _VerifyReturnsJob, "PUT", None),
268 c326b4ef Michael Hanselmann
    ])
269 c326b4ef Michael Hanselmann
  _WaitForRapiJob(job_id)
270 c326b4ef Michael Hanselmann
271 c326b4ef Michael Hanselmann
  # Retrieve tags
272 a47f574c Oleksiy Mishchenko
  _DoTests([
273 94e63ca1 Michael Hanselmann
    (uri, _VerifyTags, 'GET', None),
274 a47f574c Oleksiy Mishchenko
    ])
275 8cb70e56 Michael Hanselmann
276 c326b4ef Michael Hanselmann
  # Remove tags
277 c326b4ef Michael Hanselmann
  (job_id, ) = _DoTests([
278 c326b4ef Michael Hanselmann
    ("%s?%s" % (uri, query), _VerifyReturnsJob, "DELETE", None),
279 c326b4ef Michael Hanselmann
    ])
280 c326b4ef Michael Hanselmann
  _WaitForRapiJob(job_id)
281 c326b4ef Michael Hanselmann
282 8cb70e56 Michael Hanselmann
283 8cb70e56 Michael Hanselmann
def _WaitForRapiJob(job_id):
284 8cb70e56 Michael Hanselmann
  """Waits for a job to finish.
285 8cb70e56 Michael Hanselmann

286 8cb70e56 Michael Hanselmann
  """
287 8cb70e56 Michael Hanselmann
  master = qa_config.GetMasterNode()
288 8cb70e56 Michael Hanselmann
289 8cb70e56 Michael Hanselmann
  def _VerifyJob(data):
290 8cb70e56 Michael Hanselmann
    AssertEqual(data["id"], job_id)
291 8cb70e56 Michael Hanselmann
    for field in JOB_FIELDS:
292 8cb70e56 Michael Hanselmann
      AssertIn(field, data)
293 8cb70e56 Michael Hanselmann
294 8cb70e56 Michael Hanselmann
  _DoTests([
295 8cb70e56 Michael Hanselmann
    ("/2/jobs/%s" % job_id, _VerifyJob, "GET", None),
296 8cb70e56 Michael Hanselmann
    ])
297 8cb70e56 Michael Hanselmann
298 ebeb600f Michael Hanselmann
  return rapi.client_utils.PollJob(_rapi_client, job_id,
299 ebeb600f Michael Hanselmann
                                   cli.StdioJobPollReportCb())
300 8cb70e56 Michael Hanselmann
301 8cb70e56 Michael Hanselmann
302 924e95f9 Michael Hanselmann
def TestRapiInstanceAdd(node, use_client):
303 8cb70e56 Michael Hanselmann
  """Test adding a new instance via RAPI"""
304 8cb70e56 Michael Hanselmann
  instance = qa_config.AcquireInstance()
305 8cb70e56 Michael Hanselmann
  try:
306 924e95f9 Michael Hanselmann
    memory = utils.ParseUnit(qa_config.get("mem"))
307 924e95f9 Michael Hanselmann
    disk_sizes = [utils.ParseUnit(size) for size in qa_config.get("disk")]
308 924e95f9 Michael Hanselmann
309 924e95f9 Michael Hanselmann
    if use_client:
310 924e95f9 Michael Hanselmann
      disks = [{"size": size} for size in disk_sizes]
311 924e95f9 Michael Hanselmann
      nics = [{}]
312 924e95f9 Michael Hanselmann
313 924e95f9 Michael Hanselmann
      beparams = {
314 924e95f9 Michael Hanselmann
        constants.BE_MEMORY: memory,
315 924e95f9 Michael Hanselmann
        }
316 924e95f9 Michael Hanselmann
317 924e95f9 Michael Hanselmann
      job_id = _rapi_client.CreateInstance(constants.INSTANCE_CREATE,
318 924e95f9 Michael Hanselmann
                                           instance["name"],
319 924e95f9 Michael Hanselmann
                                           constants.DT_PLAIN,
320 924e95f9 Michael Hanselmann
                                           disks, nics,
321 924e95f9 Michael Hanselmann
                                           os=qa_config.get("os"),
322 924e95f9 Michael Hanselmann
                                           pnode=node["primary"],
323 924e95f9 Michael Hanselmann
                                           beparams=beparams)
324 924e95f9 Michael Hanselmann
    else:
325 924e95f9 Michael Hanselmann
      body = {
326 924e95f9 Michael Hanselmann
        "name": instance["name"],
327 924e95f9 Michael Hanselmann
        "os": qa_config.get("os"),
328 924e95f9 Michael Hanselmann
        "disk_template": constants.DT_PLAIN,
329 924e95f9 Michael Hanselmann
        "pnode": node["primary"],
330 924e95f9 Michael Hanselmann
        "memory": memory,
331 924e95f9 Michael Hanselmann
        "disks": disk_sizes,
332 924e95f9 Michael Hanselmann
        }
333 924e95f9 Michael Hanselmann
334 924e95f9 Michael Hanselmann
      (job_id, ) = _DoTests([
335 924e95f9 Michael Hanselmann
        ("/2/instances", _VerifyReturnsJob, "POST", body),
336 924e95f9 Michael Hanselmann
        ])
337 8cb70e56 Michael Hanselmann
338 8cb70e56 Michael Hanselmann
    _WaitForRapiJob(job_id)
339 8cb70e56 Michael Hanselmann
340 8cb70e56 Michael Hanselmann
    return instance
341 8cb70e56 Michael Hanselmann
  except:
342 8cb70e56 Michael Hanselmann
    qa_config.ReleaseInstance(instance)
343 8cb70e56 Michael Hanselmann
    raise
344 8cb70e56 Michael Hanselmann
345 8cb70e56 Michael Hanselmann
346 924e95f9 Michael Hanselmann
def TestRapiInstanceRemove(instance, use_client):
347 8cb70e56 Michael Hanselmann
  """Test removing instance via RAPI"""
348 924e95f9 Michael Hanselmann
  if use_client:
349 924e95f9 Michael Hanselmann
    job_id = _rapi_client.DeleteInstance(instance["name"])
350 924e95f9 Michael Hanselmann
  else:
351 924e95f9 Michael Hanselmann
    (job_id, ) = _DoTests([
352 924e95f9 Michael Hanselmann
      ("/2/instances/%s" % instance["name"], _VerifyReturnsJob, "DELETE", None),
353 924e95f9 Michael Hanselmann
      ])
354 8cb70e56 Michael Hanselmann
355 8cb70e56 Michael Hanselmann
  _WaitForRapiJob(job_id)
356 8cb70e56 Michael Hanselmann
357 8cb70e56 Michael Hanselmann
  qa_config.ReleaseInstance(instance)
358 5d831182 Michael Hanselmann
359 5d831182 Michael Hanselmann
360 938bde86 Michael Hanselmann
def TestRapiInstanceMigrate(instance):
361 938bde86 Michael Hanselmann
  """Test migrating instance via RAPI"""
362 938bde86 Michael Hanselmann
  # Move to secondary node
363 938bde86 Michael Hanselmann
  _WaitForRapiJob(_rapi_client.MigrateInstance(instance["name"]))
364 938bde86 Michael Hanselmann
  # And back to previous primary
365 938bde86 Michael Hanselmann
  _WaitForRapiJob(_rapi_client.MigrateInstance(instance["name"]))
366 938bde86 Michael Hanselmann
367 938bde86 Michael Hanselmann
368 7fb50870 Michael Hanselmann
def TestRapiInstanceRename(instance, rename_target):
369 7fb50870 Michael Hanselmann
  """Test renaming instance via RAPI"""
370 7fb50870 Michael Hanselmann
  rename_source = instance["name"]
371 7fb50870 Michael Hanselmann
372 7fb50870 Michael Hanselmann
  for name1, name2 in [(rename_source, rename_target),
373 7fb50870 Michael Hanselmann
                       (rename_target, rename_source)]:
374 7fb50870 Michael Hanselmann
    _WaitForRapiJob(_rapi_client.RenameInstance(name1, name2))
375 7fb50870 Michael Hanselmann
376 7fb50870 Michael Hanselmann
377 3b7158ef Michael Hanselmann
def TestRapiInstanceModify(instance):
378 3b7158ef Michael Hanselmann
  """Test modifying instance via RAPI"""
379 3b7158ef Michael Hanselmann
  def _ModifyInstance(**kwargs):
380 3b7158ef Michael Hanselmann
    _WaitForRapiJob(_rapi_client.ModifyInstance(instance["name"], **kwargs))
381 3b7158ef Michael Hanselmann
382 3b7158ef Michael Hanselmann
  _ModifyInstance(hvparams={
383 3b7158ef Michael Hanselmann
    constants.HV_KERNEL_ARGS: "single",
384 3b7158ef Michael Hanselmann
    })
385 3b7158ef Michael Hanselmann
386 3b7158ef Michael Hanselmann
  _ModifyInstance(beparams={
387 3b7158ef Michael Hanselmann
    constants.BE_VCPUS: 3,
388 3b7158ef Michael Hanselmann
    })
389 3b7158ef Michael Hanselmann
390 3b7158ef Michael Hanselmann
  _ModifyInstance(beparams={
391 3b7158ef Michael Hanselmann
    constants.BE_VCPUS: constants.VALUE_DEFAULT,
392 3b7158ef Michael Hanselmann
    })
393 3b7158ef Michael Hanselmann
394 3b7158ef Michael Hanselmann
  _ModifyInstance(hvparams={
395 3b7158ef Michael Hanselmann
    constants.HV_KERNEL_ARGS: constants.VALUE_DEFAULT,
396 3b7158ef Michael Hanselmann
    })
397 3b7158ef Michael Hanselmann
398 3b7158ef Michael Hanselmann
399 638a7266 Iustin Pop
def TestInterClusterInstanceMove(src_instance, dest_instance,
400 638a7266 Iustin Pop
                                 pnode, snode, tnode):
401 5d831182 Michael Hanselmann
  """Test tools/move-instance"""
402 5d831182 Michael Hanselmann
  master = qa_config.GetMasterNode()
403 5d831182 Michael Hanselmann
404 5d831182 Michael Hanselmann
  rapi_pw_file = tempfile.NamedTemporaryFile()
405 5d831182 Michael Hanselmann
  rapi_pw_file.write(_rapi_password)
406 5d831182 Michael Hanselmann
  rapi_pw_file.flush()
407 5d831182 Michael Hanselmann
408 5d831182 Michael Hanselmann
  # TODO: Run some instance tests before moving back
409 677e16eb Iustin Pop
410 638a7266 Iustin Pop
  if snode is None:
411 638a7266 Iustin Pop
    # instance is not redundant, but we still need to pass a node
412 638a7266 Iustin Pop
    # (which will be ignored)
413 638a7266 Iustin Pop
    fsec = tnode
414 638a7266 Iustin Pop
  else:
415 638a7266 Iustin Pop
    fsec = snode
416 638a7266 Iustin Pop
  # note: pnode:snode are the *current* nodes, so we move it first to
417 638a7266 Iustin Pop
  # tnode:pnode, then back to pnode:snode
418 677e16eb Iustin Pop
  for si, di, pn, sn in [(src_instance["name"], dest_instance["name"],
419 638a7266 Iustin Pop
                          tnode["primary"], pnode["primary"]),
420 677e16eb Iustin Pop
                         (dest_instance["name"], src_instance["name"],
421 638a7266 Iustin Pop
                          pnode["primary"], fsec["primary"])]:
422 5d831182 Michael Hanselmann
    cmd = [
423 5d831182 Michael Hanselmann
      "../tools/move-instance",
424 5d831182 Michael Hanselmann
      "--verbose",
425 5d831182 Michael Hanselmann
      "--src-ca-file=%s" % _rapi_ca.name,
426 5d831182 Michael Hanselmann
      "--src-username=%s" % _rapi_username,
427 5d831182 Michael Hanselmann
      "--src-password-file=%s" % rapi_pw_file.name,
428 677e16eb Iustin Pop
      "--dest-instance-name=%s" % di,
429 677e16eb Iustin Pop
      "--dest-primary-node=%s" % pn,
430 677e16eb Iustin Pop
      "--dest-secondary-node=%s" % sn,
431 a889c536 Michael Hanselmann
      "--net=0:mac=%s" % constants.VALUE_GENERATE,
432 5d831182 Michael Hanselmann
      master["primary"],
433 5d831182 Michael Hanselmann
      master["primary"],
434 677e16eb Iustin Pop
      si,
435 5d831182 Michael Hanselmann
      ]
436 5d831182 Michael Hanselmann
437 5d831182 Michael Hanselmann
    AssertEqual(StartLocalCommand(cmd).wait(), 0)