Statistics
| Branch: | Tag: | Revision:

root / qa / qa_cluster.py @ e51ca051

History | View | Annotate | Download (11.1 kB)

1
#
2
#
3

    
4
# Copyright (C) 2007 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

    
28
from ganeti import constants
29
from ganeti import utils
30

    
31
import qa_config
32
import qa_utils
33
import qa_error
34

    
35
from qa_utils import AssertEqual, AssertNotEqual, StartSSH
36

    
37

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

41
  """
42
  for node in qa_config.get('nodes'):
43
    cmd = ['rm', '-f', filename]
44
    AssertEqual(StartSSH(node['primary'],
45
                         utils.ShellQuoteArgs(cmd)).wait(), 0)
46

    
47

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

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

    
57

    
58
def TestClusterInit(rapi_user, rapi_secret):
59
  """gnt-cluster init"""
60
  master = qa_config.GetMasterNode()
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
      cmd = ["mv", tmpru, constants.RAPI_USERS_FILE]
71
      AssertEqual(StartSSH(master["primary"],
72
                           utils.ShellQuoteArgs(cmd)).wait(), 0)
73
    finally:
74
      cmd = ["rm", "-f", tmpru]
75
      AssertEqual(StartSSH(master["primary"],
76
                           utils.ShellQuoteArgs(cmd)).wait(), 0)
77
  finally:
78
    fh.close()
79

    
80
  # Initialize cluster
81
  cmd = ['gnt-cluster', 'init']
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
  AssertEqual(StartSSH(master['primary'],
98
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
99

    
100

    
101
def TestClusterRename():
102
  """gnt-cluster rename"""
103
  master = qa_config.GetMasterNode()
104

    
105
  cmd = ['gnt-cluster', 'rename', '-f']
106

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

    
113
  cmd_1 = cmd + [rename_target]
114
  cmd_2 = cmd + [original_name]
115

    
116
  cmd_verify = ['gnt-cluster', 'verify']
117

    
118
  AssertEqual(StartSSH(master['primary'],
119
                       utils.ShellQuoteArgs(cmd_1)).wait(), 0)
120

    
121
  AssertEqual(StartSSH(master['primary'],
122
                       utils.ShellQuoteArgs(cmd_verify)).wait(), 0)
123

    
124
  AssertEqual(StartSSH(master['primary'],
125
                       utils.ShellQuoteArgs(cmd_2)).wait(), 0)
126

    
127
  AssertEqual(StartSSH(master['primary'],
128
                       utils.ShellQuoteArgs(cmd_verify)).wait(), 0)
129

    
130

    
131
def TestClusterVerify():
132
  """gnt-cluster verify"""
133
  master = qa_config.GetMasterNode()
134

    
135
  cmd = ['gnt-cluster', 'verify']
136
  AssertEqual(StartSSH(master['primary'],
137
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
138

    
139

    
140
def TestClusterInfo():
141
  """gnt-cluster info"""
142
  master = qa_config.GetMasterNode()
143

    
144
  cmd = ['gnt-cluster', 'info']
145
  AssertEqual(StartSSH(master['primary'],
146
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
147

    
148

    
149
def TestClusterGetmaster():
150
  """gnt-cluster getmaster"""
151
  master = qa_config.GetMasterNode()
152

    
153
  cmd = ['gnt-cluster', 'getmaster']
154
  AssertEqual(StartSSH(master['primary'],
155
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
156

    
157

    
158
def TestClusterVersion():
159
  """gnt-cluster version"""
160
  master = qa_config.GetMasterNode()
161

    
162
  cmd = ['gnt-cluster', 'version']
163
  AssertEqual(StartSSH(master['primary'],
164
                       utils.ShellQuoteArgs(cmd)).wait(), 0)
165

    
166

    
167
def TestClusterRenewCrypto():
168
  """gnt-cluster renew-crypto"""
169
  master = qa_config.GetMasterNode()
170

    
171
  # Conflicting options
172
  cmd = ["gnt-cluster", "renew-crypto", "--force",
173
         "--new-cluster-certificate", "--new-confd-hmac-key"]
174
  conflicting = [
175
    ["--new-rapi-certificate", "--rapi-certificate=/dev/null"],
176
    ["--new-cluster-domain-secret", "--cluster-domain-secret=/dev/null"],
177
    ]
178
  for i in conflicting:
179
    AssertNotEqual(StartSSH(master["primary"],
180
                            utils.ShellQuoteArgs(cmd + i)).wait(), 0)
181

    
182
  # Invalid RAPI certificate
183
  cmd = ["gnt-cluster", "renew-crypto", "--force",
184
         "--rapi-certificate=/dev/null"]
185
  AssertNotEqual(StartSSH(master["primary"],
186
                          utils.ShellQuoteArgs(cmd)).wait(), 0)
187

    
188
  rapi_cert_backup = qa_utils.BackupFile(master["primary"],
189
                                         constants.RAPI_CERT_FILE)
190
  try:
191
    # Custom RAPI certificate
192
    fh = tempfile.NamedTemporaryFile()
193

    
194
    # Ensure certificate doesn't cause "gnt-cluster verify" to complain
195
    validity = constants.SSL_CERT_EXPIRATION_WARN * 3
196

    
197
    utils.GenerateSelfSignedSslCert(fh.name, validity=validity)
198

    
199
    tmpcert = qa_utils.UploadFile(master["primary"], fh.name)
200
    try:
201
      cmd = ["gnt-cluster", "renew-crypto", "--force",
202
             "--rapi-certificate=%s" % tmpcert]
203
      AssertEqual(StartSSH(master["primary"],
204
                           utils.ShellQuoteArgs(cmd)).wait(), 0)
205
    finally:
206
      cmd = ["rm", "-f", tmpcert]
207
      AssertEqual(StartSSH(master["primary"],
208
                           utils.ShellQuoteArgs(cmd)).wait(), 0)
209

    
210
    # Custom cluster domain secret
211
    cds_fh = tempfile.NamedTemporaryFile()
212
    cds_fh.write(utils.GenerateSecret())
213
    cds_fh.write("\n")
214
    cds_fh.flush()
215

    
216
    tmpcds = qa_utils.UploadFile(master["primary"], cds_fh.name)
217
    try:
218
      cmd = ["gnt-cluster", "renew-crypto", "--force",
219
             "--cluster-domain-secret=%s" % tmpcds]
220
      AssertEqual(StartSSH(master["primary"],
221
                           utils.ShellQuoteArgs(cmd)).wait(), 0)
222
    finally:
223
      cmd = ["rm", "-f", tmpcds]
224
      AssertEqual(StartSSH(master["primary"],
225
                           utils.ShellQuoteArgs(cmd)).wait(), 0)
226

    
227
    # Normal case
228
    cmd = ["gnt-cluster", "renew-crypto", "--force",
229
           "--new-cluster-certificate", "--new-confd-hmac-key",
230
           "--new-rapi-certificate", "--new-cluster-domain-secret"]
231
    AssertEqual(StartSSH(master["primary"],
232
                         utils.ShellQuoteArgs(cmd)).wait(), 0)
233

    
234
    # Restore RAPI certificate
235
    cmd = ["gnt-cluster", "renew-crypto", "--force",
236
           "--rapi-certificate=%s" % rapi_cert_backup]
237
    AssertEqual(StartSSH(master["primary"],
238
                         utils.ShellQuoteArgs(cmd)).wait(), 0)
239
  finally:
240
    cmd = ["rm", "-f", rapi_cert_backup]
241
    AssertEqual(StartSSH(master["primary"],
242
                         utils.ShellQuoteArgs(cmd)).wait(), 0)
243

    
244

    
245
def TestClusterBurnin():
246
  """Burnin"""
247
  master = qa_config.GetMasterNode()
248

    
249
  options = qa_config.get('options', {})
250
  disk_template = options.get('burnin-disk-template', 'drbd')
251
  parallel = options.get('burnin-in-parallel', False)
252
  check_inst = options.get('burnin-check-instances', False)
253
  do_rename = options.get('burnin-rename', '')
254
  do_reboot = options.get('burnin-reboot', True)
255
  reboot_types = options.get("reboot-types", constants.REBOOT_TYPES)
256

    
257
  # Get as many instances as we need
258
  instances = []
259
  try:
260
    try:
261
      num = qa_config.get('options', {}).get('burnin-instances', 1)
262
      for _ in range(0, num):
263
        instances.append(qa_config.AcquireInstance())
264
    except qa_error.OutOfInstancesError:
265
      print "Not enough instances, continuing anyway."
266

    
267
    if len(instances) < 1:
268
      raise qa_error.Error("Burnin needs at least one instance")
269

    
270
    script = qa_utils.UploadFile(master['primary'], '../tools/burnin')
271
    try:
272
      # Run burnin
273
      cmd = [script,
274
             '--os=%s' % qa_config.get('os'),
275
             '--disk-size=%s' % ",".join(qa_config.get('disk')),
276
             '--disk-growth=%s' % ",".join(qa_config.get('disk-growth')),
277
             '--disk-template=%s' % disk_template]
278
      if parallel:
279
        cmd.append('--parallel')
280
        cmd.append('--early-release')
281
      if check_inst:
282
        cmd.append('--http-check')
283
      if do_rename:
284
        cmd.append('--rename=%s' % do_rename)
285
      if not do_reboot:
286
        cmd.append('--no-reboot')
287
      else:
288
        cmd.append('--reboot-types=%s' % ",".join(reboot_types))
289
      cmd += [inst['name'] for inst in instances]
290
      AssertEqual(StartSSH(master['primary'],
291
                           utils.ShellQuoteArgs(cmd)).wait(), 0)
292
    finally:
293
      cmd = ['rm', '-f', script]
294
      AssertEqual(StartSSH(master['primary'],
295
                           utils.ShellQuoteArgs(cmd)).wait(), 0)
296
  finally:
297
    for inst in instances:
298
      qa_config.ReleaseInstance(inst)
299

    
300

    
301
def TestClusterMasterFailover():
302
  """gnt-cluster masterfailover"""
303
  master = qa_config.GetMasterNode()
304

    
305
  failovermaster = qa_config.AcquireNode(exclude=master)
306
  try:
307
    cmd = ['gnt-cluster', 'masterfailover']
308
    AssertEqual(StartSSH(failovermaster['primary'],
309
                         utils.ShellQuoteArgs(cmd)).wait(), 0)
310

    
311
    cmd = ['gnt-cluster', 'masterfailover']
312
    AssertEqual(StartSSH(master['primary'],
313
                         utils.ShellQuoteArgs(cmd)).wait(), 0)
314
  finally:
315
    qa_config.ReleaseNode(failovermaster)
316

    
317

    
318
def TestClusterCopyfile():
319
  """gnt-cluster copyfile"""
320
  master = qa_config.GetMasterNode()
321

    
322
  uniqueid = utils.NewUUID()
323

    
324
  # Create temporary file
325
  f = tempfile.NamedTemporaryFile()
326
  f.write(uniqueid)
327
  f.flush()
328
  f.seek(0)
329

    
330
  # Upload file to master node
331
  testname = qa_utils.UploadFile(master['primary'], f.name)
332
  try:
333
    # Copy file to all nodes
334
    cmd = ['gnt-cluster', 'copyfile', testname]
335
    AssertEqual(StartSSH(master['primary'],
336
                         utils.ShellQuoteArgs(cmd)).wait(), 0)
337
    _CheckFileOnAllNodes(testname, uniqueid)
338
  finally:
339
    _RemoveFileFromAllNodes(testname)
340

    
341

    
342
def TestClusterCommand():
343
  """gnt-cluster command"""
344
  master = qa_config.GetMasterNode()
345

    
346
  uniqueid = utils.NewUUID()
347
  rfile = "/tmp/gnt%s" % utils.NewUUID()
348
  rcmd = utils.ShellQuoteArgs(['echo', '-n', uniqueid])
349
  cmd = utils.ShellQuoteArgs(['gnt-cluster', 'command',
350
                              "%s >%s" % (rcmd, rfile)])
351

    
352
  try:
353
    AssertEqual(StartSSH(master['primary'], cmd).wait(), 0)
354
    _CheckFileOnAllNodes(rfile, uniqueid)
355
  finally:
356
    _RemoveFileFromAllNodes(rfile)
357

    
358

    
359
def TestClusterDestroy():
360
  """gnt-cluster destroy"""
361
  master = qa_config.GetMasterNode()
362

    
363
  cmd = ['gnt-cluster', 'destroy', '--yes-do-it']
364
  AssertEqual(StartSSH(master['primary'],
365
                       utils.ShellQuoteArgs(cmd)).wait(), 0)