Statistics
| Branch: | Tag: | Revision:

root / qa / qa_cluster.py @ c6953b6e

History | View | Annotate | Download (11.5 kB)

1
#
2
#
3

    
4
# Copyright (C) 2007, 2010, 2011 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
"""Cluster related QA tests.
23

24
"""
25

    
26
import tempfile
27
import os.path
28

    
29
from ganeti import constants
30
from ganeti import utils
31

    
32
import qa_config
33
import qa_utils
34
import qa_error
35

    
36
from qa_utils import AssertEqual, AssertCommand
37

    
38

    
39
def _RemoveFileFromAllNodes(filename):
40
  """Removes a file from all nodes.
41

42
  """
43
  for node in qa_config.get("nodes"):
44
    AssertCommand(["rm", "-f", filename], node=node)
45

    
46

    
47
def _CheckFileOnAllNodes(filename, content):
48
  """Verifies the content of the given file on all nodes.
49

50
  """
51
  cmd = utils.ShellQuoteArgs(["cat", filename])
52
  for node in qa_config.get("nodes"):
53
    AssertEqual(qa_utils.GetCommandOutput(node["primary"], cmd), content)
54

    
55

    
56
def TestClusterInit(rapi_user, rapi_secret):
57
  """gnt-cluster init"""
58
  master = qa_config.GetMasterNode()
59

    
60
  rapi_dir = os.path.dirname(constants.RAPI_USERS_FILE)
61

    
62
  # First create the RAPI credentials
63
  fh = tempfile.NamedTemporaryFile()
64
  try:
65
    fh.write("%s %s write\n" % (rapi_user, rapi_secret))
66
    fh.flush()
67

    
68
    tmpru = qa_utils.UploadFile(master["primary"], fh.name)
69
    try:
70
      AssertCommand(["mkdir", "-p", rapi_dir])
71
      AssertCommand(["mv", tmpru, constants.RAPI_USERS_FILE])
72
    finally:
73
      AssertCommand(["rm", "-f", tmpru])
74
  finally:
75
    fh.close()
76

    
77
  # Initialize cluster
78
  cmd = ['gnt-cluster', 'init']
79

    
80
  cmd.append("--primary-ip-version=%d" %
81
             qa_config.get("primary_ip_version", 4))
82

    
83
  if master.get('secondary', None):
84
    cmd.append('--secondary-ip=%s' % master['secondary'])
85

    
86
  bridge = qa_config.get('bridge', None)
87
  if bridge:
88
    cmd.append('--bridge=%s' % bridge)
89
    cmd.append('--master-netdev=%s' % bridge)
90

    
91
  htype = qa_config.get('enabled-hypervisors', None)
92
  if htype:
93
    cmd.append('--enabled-hypervisors=%s' % htype)
94

    
95
  cmd.append(qa_config.get('name'))
96

    
97
  AssertCommand(cmd)
98

    
99

    
100
def TestClusterRename():
101
  """gnt-cluster rename"""
102
  cmd = ['gnt-cluster', 'rename', '-f']
103

    
104
  original_name = qa_config.get('name')
105
  rename_target = qa_config.get('rename', None)
106
  if rename_target is None:
107
    print qa_utils.FormatError('"rename" entry is missing')
108
    return
109

    
110
  cmd_verify = ['gnt-cluster', 'verify']
111

    
112
  for data in [
113
    cmd + [rename_target],
114
    cmd_verify,
115
    cmd + [original_name],
116
    cmd_verify,
117
    ]:
118
    AssertCommand(data)
119

    
120

    
121
def TestClusterOob():
122
  """out-of-band framework"""
123
  oob_path_exists = "/tmp/ganeti-qa-oob-does-exist-%s" % utils.NewUUID()
124

    
125
  AssertCommand(["gnt-cluster", "verify"])
126
  AssertCommand(["gnt-cluster", "modify", "--node-parameters",
127
                 "oob_program=/tmp/ganeti-qa-oob-does-not-exist-%s" %
128
                 utils.NewUUID()])
129

    
130
  AssertCommand(["gnt-cluster", "verify"], fail=True)
131

    
132
  AssertCommand(["touch", oob_path_exists])
133
  AssertCommand(["chmod", "0400", oob_path_exists])
134
  AssertCommand(["gnt-cluster", "copyfile", oob_path_exists])
135

    
136
  try:
137
    AssertCommand(["gnt-cluster", "modify", "--node-parameters",
138
                   "oob_program=%s" % oob_path_exists])
139

    
140
    AssertCommand(["gnt-cluster", "verify"], fail=True)
141

    
142
    AssertCommand(["chmod", "0500", oob_path_exists])
143
    AssertCommand(["gnt-cluster", "copyfile", oob_path_exists])
144

    
145
    AssertCommand(["gnt-cluster", "verify"])
146
  finally:
147
    AssertCommand(["gnt-cluster", "command", "rm", oob_path_exists])
148

    
149
  AssertCommand(["gnt-cluster", "modify", "--node-parameters",
150
                 "oob_program="])
151

    
152

    
153
def TestClusterVerify():
154
  """gnt-cluster verify"""
155
  AssertCommand(["gnt-cluster", "verify"])
156
  AssertCommand(["gnt-cluster", "verify-disks"])
157

    
158

    
159
def TestJobqueue():
160
  """gnt-debug test-jobqueue"""
161
  AssertCommand(["gnt-debug", "test-jobqueue"])
162

    
163

    
164
def TestClusterReservedLvs():
165
  """gnt-cluster reserved lvs"""
166
  CVERIFY = ["gnt-cluster", "verify"]
167
  for fail, cmd in [
168
    (False, CVERIFY),
169
    (False, ["gnt-cluster", "modify", "--reserved-lvs", ""]),
170
    (False, ["lvcreate", "-L1G", "-nqa-test", "xenvg"]),
171
    (True,  CVERIFY),
172
    (False, ["gnt-cluster", "modify", "--reserved-lvs",
173
             "xenvg/qa-test,.*/other-test"]),
174
    (False, CVERIFY),
175
    (False, ["gnt-cluster", "modify", "--reserved-lvs", ".*/qa-.*"]),
176
    (False, CVERIFY),
177
    (False, ["gnt-cluster", "modify", "--reserved-lvs", ""]),
178
    (True,  CVERIFY),
179
    (False, ["lvremove", "-f", "xenvg/qa-test"]),
180
    (False, CVERIFY),
181
    ]:
182
    AssertCommand(cmd, fail=fail)
183

    
184

    
185
def TestClusterModifyBe():
186
  """gnt-cluster modify -B"""
187
  for fail, cmd in [
188
    # mem
189
    (False, ["gnt-cluster", "modify", "-B", "memory=256"]),
190
    (False, ["sh", "-c", "gnt-cluster info|grep '^ *memory: 256$'"]),
191
    (True,  ["gnt-cluster", "modify", "-B", "memory=a"]),
192
    (False, ["gnt-cluster", "modify", "-B", "memory=128"]),
193
    (False, ["sh", "-c", "gnt-cluster info|grep '^ *memory: 128$'"]),
194
    # vcpus
195
    (False, ["gnt-cluster", "modify", "-B", "vcpus=4"]),
196
    (False, ["sh", "-c", "gnt-cluster info|grep '^ *vcpus: 4$'"]),
197
    (True,  ["gnt-cluster", "modify", "-B", "vcpus=a"]),
198
    (False, ["gnt-cluster", "modify", "-B", "vcpus=1"]),
199
    (False, ["sh", "-c", "gnt-cluster info|grep '^ *vcpus: 1$'"]),
200
    # auto_balance
201
    (False, ["gnt-cluster", "modify", "-B", "auto_balance=False"]),
202
    (False, ["sh", "-c", "gnt-cluster info|grep '^ *auto_balance: False$'"]),
203
    (True,  ["gnt-cluster", "modify", "-B", "auto_balance=1"]),
204
    (False, ["gnt-cluster", "modify", "-B", "auto_balance=True"]),
205
    (False, ["sh", "-c", "gnt-cluster info|grep '^ *auto_balance: True$'"]),
206
    ]:
207
    AssertCommand(cmd, fail=fail)
208

    
209

    
210
def TestClusterInfo():
211
  """gnt-cluster info"""
212
  AssertCommand(["gnt-cluster", "info"])
213

    
214

    
215
def TestClusterGetmaster():
216
  """gnt-cluster getmaster"""
217
  AssertCommand(["gnt-cluster", "getmaster"])
218

    
219

    
220
def TestClusterVersion():
221
  """gnt-cluster version"""
222
  AssertCommand(["gnt-cluster", "version"])
223

    
224

    
225
def TestClusterRenewCrypto():
226
  """gnt-cluster renew-crypto"""
227
  master = qa_config.GetMasterNode()
228

    
229
  # Conflicting options
230
  cmd = ["gnt-cluster", "renew-crypto", "--force",
231
         "--new-cluster-certificate", "--new-confd-hmac-key"]
232
  conflicting = [
233
    ["--new-rapi-certificate", "--rapi-certificate=/dev/null"],
234
    ["--new-cluster-domain-secret", "--cluster-domain-secret=/dev/null"],
235
    ]
236
  for i in conflicting:
237
    AssertCommand(cmd+i, fail=True)
238

    
239
  # Invalid RAPI certificate
240
  cmd = ["gnt-cluster", "renew-crypto", "--force",
241
         "--rapi-certificate=/dev/null"]
242
  AssertCommand(cmd, fail=True)
243

    
244
  rapi_cert_backup = qa_utils.BackupFile(master["primary"],
245
                                         constants.RAPI_CERT_FILE)
246
  try:
247
    # Custom RAPI certificate
248
    fh = tempfile.NamedTemporaryFile()
249

    
250
    # Ensure certificate doesn't cause "gnt-cluster verify" to complain
251
    validity = constants.SSL_CERT_EXPIRATION_WARN * 3
252

    
253
    utils.GenerateSelfSignedSslCert(fh.name, validity=validity)
254

    
255
    tmpcert = qa_utils.UploadFile(master["primary"], fh.name)
256
    try:
257
      AssertCommand(["gnt-cluster", "renew-crypto", "--force",
258
                     "--rapi-certificate=%s" % tmpcert])
259
    finally:
260
      AssertCommand(["rm", "-f", tmpcert])
261

    
262
    # Custom cluster domain secret
263
    cds_fh = tempfile.NamedTemporaryFile()
264
    cds_fh.write(utils.GenerateSecret())
265
    cds_fh.write("\n")
266
    cds_fh.flush()
267

    
268
    tmpcds = qa_utils.UploadFile(master["primary"], cds_fh.name)
269
    try:
270
      AssertCommand(["gnt-cluster", "renew-crypto", "--force",
271
                     "--cluster-domain-secret=%s" % tmpcds])
272
    finally:
273
      AssertCommand(["rm", "-f", tmpcds])
274

    
275
    # Normal case
276
    AssertCommand(["gnt-cluster", "renew-crypto", "--force",
277
                   "--new-cluster-certificate", "--new-confd-hmac-key",
278
                   "--new-rapi-certificate", "--new-cluster-domain-secret"])
279

    
280
    # Restore RAPI certificate
281
    AssertCommand(["gnt-cluster", "renew-crypto", "--force",
282
                   "--rapi-certificate=%s" % rapi_cert_backup])
283
  finally:
284
    AssertCommand(["rm", "-f", rapi_cert_backup])
285

    
286

    
287
def TestClusterBurnin():
288
  """Burnin"""
289
  master = qa_config.GetMasterNode()
290

    
291
  options = qa_config.get('options', {})
292
  disk_template = options.get('burnin-disk-template', 'drbd')
293
  parallel = options.get('burnin-in-parallel', False)
294
  check_inst = options.get('burnin-check-instances', False)
295
  do_rename = options.get('burnin-rename', '')
296
  do_reboot = options.get('burnin-reboot', True)
297
  reboot_types = options.get("reboot-types", constants.REBOOT_TYPES)
298

    
299
  # Get as many instances as we need
300
  instances = []
301
  try:
302
    try:
303
      num = qa_config.get('options', {}).get('burnin-instances', 1)
304
      for _ in range(0, num):
305
        instances.append(qa_config.AcquireInstance())
306
    except qa_error.OutOfInstancesError:
307
      print "Not enough instances, continuing anyway."
308

    
309
    if len(instances) < 1:
310
      raise qa_error.Error("Burnin needs at least one instance")
311

    
312
    script = qa_utils.UploadFile(master['primary'], '../tools/burnin')
313
    try:
314
      # Run burnin
315
      cmd = [script,
316
             '--os=%s' % qa_config.get('os'),
317
             '--disk-size=%s' % ",".join(qa_config.get('disk')),
318
             '--disk-growth=%s' % ",".join(qa_config.get('disk-growth')),
319
             '--disk-template=%s' % disk_template]
320
      if parallel:
321
        cmd.append('--parallel')
322
        cmd.append('--early-release')
323
      if check_inst:
324
        cmd.append('--http-check')
325
      if do_rename:
326
        cmd.append('--rename=%s' % do_rename)
327
      if not do_reboot:
328
        cmd.append('--no-reboot')
329
      else:
330
        cmd.append('--reboot-types=%s' % ",".join(reboot_types))
331
      cmd += [inst['name'] for inst in instances]
332
      AssertCommand(cmd)
333
    finally:
334
      AssertCommand(["rm", "-f", script])
335

    
336
  finally:
337
    for inst in instances:
338
      qa_config.ReleaseInstance(inst)
339

    
340

    
341
def TestClusterMasterFailover():
342
  """gnt-cluster master-failover"""
343
  master = qa_config.GetMasterNode()
344
  failovermaster = qa_config.AcquireNode(exclude=master)
345

    
346
  cmd = ["gnt-cluster", "master-failover"]
347
  try:
348
    AssertCommand(cmd, node=failovermaster)
349
    AssertCommand(cmd, node=master)
350
  finally:
351
    qa_config.ReleaseNode(failovermaster)
352

    
353

    
354
def TestClusterCopyfile():
355
  """gnt-cluster copyfile"""
356
  master = qa_config.GetMasterNode()
357

    
358
  uniqueid = utils.NewUUID()
359

    
360
  # Create temporary file
361
  f = tempfile.NamedTemporaryFile()
362
  f.write(uniqueid)
363
  f.flush()
364
  f.seek(0)
365

    
366
  # Upload file to master node
367
  testname = qa_utils.UploadFile(master['primary'], f.name)
368
  try:
369
    # Copy file to all nodes
370
    AssertCommand(["gnt-cluster", "copyfile", testname])
371
    _CheckFileOnAllNodes(testname, uniqueid)
372
  finally:
373
    _RemoveFileFromAllNodes(testname)
374

    
375

    
376
def TestClusterCommand():
377
  """gnt-cluster command"""
378
  uniqueid = utils.NewUUID()
379
  rfile = "/tmp/gnt%s" % utils.NewUUID()
380
  rcmd = utils.ShellQuoteArgs(['echo', '-n', uniqueid])
381
  cmd = utils.ShellQuoteArgs(['gnt-cluster', 'command',
382
                              "%s >%s" % (rcmd, rfile)])
383

    
384
  try:
385
    AssertCommand(cmd)
386
    _CheckFileOnAllNodes(rfile, uniqueid)
387
  finally:
388
    _RemoveFileFromAllNodes(rfile)
389

    
390

    
391
def TestClusterDestroy():
392
  """gnt-cluster destroy"""
393
  AssertCommand(["gnt-cluster", "destroy", "--yes-do-it"])