qa_rapi.py 8.6 KB
Newer Older
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#

# Copyright (C) 2007, 2008 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


"""Remote API QA tests.

"""

Michael Hanselmann's avatar
Michael Hanselmann committed
25
import tempfile
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
26
27
28
29
30

from ganeti import utils
from ganeti import constants
from ganeti import errors
from ganeti import serializer
Michael Hanselmann's avatar
Michael Hanselmann committed
31
32
33
34
35
from ganeti import cli
from ganeti import rapi

import ganeti.rapi.client
import ganeti.rapi.client_utils
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
36
37
38
39
40

import qa_config
import qa_utils
import qa_error

41
42
from qa_utils import (AssertEqual, AssertNotEqual, AssertIn, AssertMatch,
                      StartSSH)
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
43
44


Michael Hanselmann's avatar
Michael Hanselmann committed
45
46
_rapi_ca = None
_rapi_client = None
47
48


Michael Hanselmann's avatar
Michael Hanselmann committed
49
50
def Setup(username, password):
  """Configures the RAPI client.
51

Michael Hanselmann's avatar
Michael Hanselmann committed
52
53
54
  """
  global _rapi_ca
  global _rapi_client
55

Michael Hanselmann's avatar
Michael Hanselmann committed
56
  master = qa_config.GetMasterNode()
57

Michael Hanselmann's avatar
Michael Hanselmann committed
58
59
  # Load RAPI certificate from master node
  cmd = ["cat", constants.RAPI_CERT_FILE]
60

Michael Hanselmann's avatar
Michael Hanselmann committed
61
62
63
64
65
  # Write to temporary file
  _rapi_ca = tempfile.NamedTemporaryFile()
  _rapi_ca.write(qa_utils.GetCommandOutput(master["primary"],
                                           utils.ShellQuoteArgs(cmd)))
  _rapi_ca.flush()
66

Michael Hanselmann's avatar
Michael Hanselmann committed
67
68
  port = qa_config.get("rapi-port", default=constants.DEFAULT_RAPI_PORT)
  cfg_ssl = rapi.client.CertAuthorityVerify(cafile=_rapi_ca.name)
69

Michael Hanselmann's avatar
Michael Hanselmann committed
70
71
72
73
74
  _rapi_client = rapi.client.GanetiRapiClient(master["primary"], port=port,
                                              username=username,
                                              password=password,
                                              config_ssl_verification=cfg_ssl,
                                              ignore_proxy=True)
75

Michael Hanselmann's avatar
Michael Hanselmann committed
76
  print "RAPI protocol version: %s" % _rapi_client.GetVersion()
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
77
78
79


INSTANCE_FIELDS = ("name", "os", "pnode", "snodes",
80
81
                   "admin_state",
                   "disk_template", "disk.sizes",
82
                   "nic.ips", "nic.macs", "nic.modes", "nic.links",
83
84
                   "beparams", "hvparams",
                   "oper_state", "oper_ram", "status", "tags")
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
85
86
87
88
89

NODE_FIELDS = ("name", "dtotal", "dfree",
               "mtotal", "mnode", "mfree",
               "pinst_cnt", "sinst_cnt", "tags")

90
91
92
93
94
95
JOB_FIELDS = frozenset([
  "id", "ops", "status", "summary",
  "opstatus", "opresult", "oplog",
  "received_ts", "start_ts", "end_ts",
  ])

Iustin Pop's avatar
Iustin Pop committed
96
LIST_FIELDS = ("id", "uri")
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
97
98
99
100
101
102


def Enabled():
  """Return whether remote API tests should be run.

  """
103
  return qa_config.TestEnabled('rapi')
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
104
105
106


def _DoTests(uris):
107
  results = []
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
108

109
  for uri, verify, method, body in uris:
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
110
111
    assert uri.startswith("/")

Michael Hanselmann's avatar
Michael Hanselmann committed
112
    data = _rapi_client._SendRequest(method, uri, None, body)
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
113
114
115
116
117
118
119

    if verify is not None:
      if callable(verify):
        verify(data)
      else:
        AssertEqual(data, verify)

120
121
122
123
124
125
126
127
      results.append(data)

  return results


def _VerifyReturnsJob(data):
  AssertMatch(data, r'^\d+$')

Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
128
129
130
131
132
133

def TestVersion():
  """Testing remote API version.

  """
  _DoTests([
134
    ("/version", constants.RAPI_VERSION, 'GET', None),
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
135
136
137
138
139
140
141
    ])


def TestEmptyCluster():
  """Testing remote API on an empty cluster.

  """
142
143
  master = qa_config.GetMasterNode()
  master_full = qa_utils.ResolveNodeName(master)
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
144
145
146
147

  def _VerifyInfo(data):
    AssertIn("name", data)
    AssertIn("master", data)
148
    AssertEqual(data["master"], master_full)
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
149
150
151

  def _VerifyNodes(data):
    master_entry = {
152
153
      "id": master_full,
      "uri": "/2/nodes/%s" % master_full,
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
154
155
156
157
158
159
160
161
162
      }
    AssertIn(master_entry, data)

  def _VerifyNodesBulk(data):
    for node in data:
      for entry in NODE_FIELDS:
        AssertIn(entry, node)

  _DoTests([
163
164
165
166
167
168
169
170
    ("/", None, 'GET', None),
    ("/2/info", _VerifyInfo, 'GET', None),
    ("/2/tags", None, 'GET', None),
    ("/2/nodes", _VerifyNodes, 'GET', None),
    ("/2/nodes?bulk=1", _VerifyNodesBulk, 'GET', None),
    ("/2/instances", [], 'GET', None),
    ("/2/instances?bulk=1", [], 'GET', None),
    ("/2/os", None, 'GET', None),
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
171
172
173
174
175
176
177
178
179
180
    ])


def TestInstance(instance):
  """Testing getting instance(s) info via remote API.

  """
  def _VerifyInstance(data):
    for entry in INSTANCE_FIELDS:
      AssertIn(entry, data)
181

Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
182
183
  def _VerifyInstancesList(data):
    for instance in data:
184
      for entry in LIST_FIELDS:
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
185
        AssertIn(entry, instance)
186

Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
187
188
189
190
191
  def _VerifyInstancesBulk(data):
    for instance_data in data:
      _VerifyInstance(instance_data)

  _DoTests([
192
193
194
195
196
197
198
    ("/2/instances/%s" % instance["name"], _VerifyInstance, 'GET', None),
    ("/2/instances", _VerifyInstancesList, 'GET', None),
    ("/2/instances?bulk=1", _VerifyInstancesBulk, 'GET', None),
    ("/2/instances/%s/activate-disks" % instance["name"],
     _VerifyReturnsJob, 'PUT', None),
    ("/2/instances/%s/deactivate-disks" % instance["name"],
     _VerifyReturnsJob, 'PUT', None),
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
199
200
    ])

201
202
203
204
205
206
207
208
209
210
211
212
213
  # Test OpPrepareExport
  (job_id, ) = _DoTests([
    ("/2/instances/%s/prepare-export?mode=%s" %
     (instance["name"], constants.EXPORT_MODE_REMOTE),
     _VerifyReturnsJob, "PUT", None),
    ])

  result = _WaitForRapiJob(job_id)[0]
  AssertEqual(len(result["handshake"]), 3)
  AssertEqual(result["handshake"][0], constants.RIE_VERSION)
  AssertEqual(len(result["x509_key_name"]), 3)
  AssertIn("-----BEGIN CERTIFICATE-----", result["x509_ca"])

Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
214
215
216
217
218
219
220
221

def TestNode(node):
  """Testing getting node(s) info via remote API.

  """
  def _VerifyNode(data):
    for entry in NODE_FIELDS:
      AssertIn(entry, data)
222

Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
223
224
  def _VerifyNodesList(data):
    for node in data:
225
      for entry in LIST_FIELDS:
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
226
        AssertIn(entry, node)
227

Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
228
229
230
231
232
  def _VerifyNodesBulk(data):
    for node_data in data:
      _VerifyNode(node_data)

  _DoTests([
233
234
235
    ("/2/nodes/%s" % node["primary"], _VerifyNode, 'GET', None),
    ("/2/nodes", _VerifyNodesList, 'GET', None),
    ("/2/nodes?bulk=1", _VerifyNodesBulk, 'GET', None),
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
236
237
238
239
240
241
242
243
    ])


def TestTags(kind, name, tags):
  """Tests .../tags resources.

  """
  if kind == constants.TAG_CLUSTER:
244
    uri = "/2/tags"
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
245
  elif kind == constants.TAG_NODE:
246
    uri = "/2/nodes/%s/tags" % name
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
247
  elif kind == constants.TAG_INSTANCE:
248
    uri = "/2/instances/%s/tags" % name
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
249
250
251
252
  else:
    raise errors.ProgrammerError("Unknown tag kind")

  def _VerifyTags(data):
253
    AssertEqual(sorted(tags), sorted(data))
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
254
255

  _DoTests([
256
    (uri, _VerifyTags, 'GET', None),
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
257
    ])
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274


def _WaitForRapiJob(job_id):
  """Waits for a job to finish.

  """
  master = qa_config.GetMasterNode()

  def _VerifyJob(data):
    AssertEqual(data["id"], job_id)
    for field in JOB_FIELDS:
      AssertIn(field, data)

  _DoTests([
    ("/2/jobs/%s" % job_id, _VerifyJob, "GET", None),
    ])

275
276
  return rapi.client_utils.PollJob(_rapi_client, job_id,
                                   cli.StdioJobPollReportCb())
277
278


279
def TestRapiInstanceAdd(node, use_client):
280
281
282
  """Test adding a new instance via RAPI"""
  instance = qa_config.AcquireInstance()
  try:
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
    memory = utils.ParseUnit(qa_config.get("mem"))
    disk_sizes = [utils.ParseUnit(size) for size in qa_config.get("disk")]

    if use_client:
      disks = [{"size": size} for size in disk_sizes]
      nics = [{}]

      beparams = {
        constants.BE_MEMORY: memory,
        }

      job_id = _rapi_client.CreateInstance(constants.INSTANCE_CREATE,
                                           instance["name"],
                                           constants.DT_PLAIN,
                                           disks, nics,
                                           os=qa_config.get("os"),
                                           pnode=node["primary"],
                                           beparams=beparams)
    else:
      body = {
        "name": instance["name"],
        "os": qa_config.get("os"),
        "disk_template": constants.DT_PLAIN,
        "pnode": node["primary"],
        "memory": memory,
        "disks": disk_sizes,
        }

      (job_id, ) = _DoTests([
        ("/2/instances", _VerifyReturnsJob, "POST", body),
        ])
314
315
316
317
318
319
320
321
322

    _WaitForRapiJob(job_id)

    return instance
  except:
    qa_config.ReleaseInstance(instance)
    raise


323
def TestRapiInstanceRemove(instance, use_client):
324
  """Test removing instance via RAPI"""
325
326
327
328
329
330
  if use_client:
    job_id = _rapi_client.DeleteInstance(instance["name"])
  else:
    (job_id, ) = _DoTests([
      ("/2/instances/%s" % instance["name"], _VerifyReturnsJob, "DELETE", None),
      ])
331
332
333
334

  _WaitForRapiJob(job_id)

  qa_config.ReleaseInstance(instance)