Rename masterfailover to master-failover
[ganeti-local] / qa / qa_cluster.py
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 def TestClusterReservedLvs():
140   """gnt-cluster reserved lvs"""
141   master = qa_config.GetMasterNode()
142   CVERIFY = ['gnt-cluster', 'verify']
143   for rcode, cmd in [
144     (0, CVERIFY),
145     (0, ['gnt-cluster', 'modify', '--reserved-lvs', '']),
146     (0, ['lvcreate', '-L1G', '-nqa-test', 'xenvg']),
147     (1, CVERIFY),
148     (0, ['gnt-cluster', 'modify', '--reserved-lvs', 'qa-test,other-test']),
149     (0, CVERIFY),
150     (0, ['gnt-cluster', 'modify', '--reserved-lvs', 'qa-.*']),
151     (0, CVERIFY),
152     (0, ['gnt-cluster', 'modify', '--reserved-lvs', '']),
153     (1, CVERIFY),
154     (0, ['lvremove', '-f', 'xenvg/qa-test']),
155     (0, CVERIFY),
156     ]:
157     AssertEqual(StartSSH(master['primary'],
158                          utils.ShellQuoteArgs(cmd)).wait(), rcode)
159
160
161 def TestClusterInfo():
162   """gnt-cluster info"""
163   master = qa_config.GetMasterNode()
164
165   cmd = ['gnt-cluster', 'info']
166   AssertEqual(StartSSH(master['primary'],
167                        utils.ShellQuoteArgs(cmd)).wait(), 0)
168
169
170 def TestClusterGetmaster():
171   """gnt-cluster getmaster"""
172   master = qa_config.GetMasterNode()
173
174   cmd = ['gnt-cluster', 'getmaster']
175   AssertEqual(StartSSH(master['primary'],
176                        utils.ShellQuoteArgs(cmd)).wait(), 0)
177
178
179 def TestClusterVersion():
180   """gnt-cluster version"""
181   master = qa_config.GetMasterNode()
182
183   cmd = ['gnt-cluster', 'version']
184   AssertEqual(StartSSH(master['primary'],
185                        utils.ShellQuoteArgs(cmd)).wait(), 0)
186
187
188 def TestClusterRenewCrypto():
189   """gnt-cluster renew-crypto"""
190   master = qa_config.GetMasterNode()
191
192   # Conflicting options
193   cmd = ["gnt-cluster", "renew-crypto", "--force",
194          "--new-cluster-certificate", "--new-confd-hmac-key"]
195   conflicting = [
196     ["--new-rapi-certificate", "--rapi-certificate=/dev/null"],
197     ["--new-cluster-domain-secret", "--cluster-domain-secret=/dev/null"],
198     ]
199   for i in conflicting:
200     AssertNotEqual(StartSSH(master["primary"],
201                             utils.ShellQuoteArgs(cmd + i)).wait(), 0)
202
203   # Invalid RAPI certificate
204   cmd = ["gnt-cluster", "renew-crypto", "--force",
205          "--rapi-certificate=/dev/null"]
206   AssertNotEqual(StartSSH(master["primary"],
207                           utils.ShellQuoteArgs(cmd)).wait(), 0)
208
209   rapi_cert_backup = qa_utils.BackupFile(master["primary"],
210                                          constants.RAPI_CERT_FILE)
211   try:
212     # Custom RAPI certificate
213     fh = tempfile.NamedTemporaryFile()
214
215     # Ensure certificate doesn't cause "gnt-cluster verify" to complain
216     validity = constants.SSL_CERT_EXPIRATION_WARN * 3
217
218     utils.GenerateSelfSignedSslCert(fh.name, validity=validity)
219
220     tmpcert = qa_utils.UploadFile(master["primary"], fh.name)
221     try:
222       cmd = ["gnt-cluster", "renew-crypto", "--force",
223              "--rapi-certificate=%s" % tmpcert]
224       AssertEqual(StartSSH(master["primary"],
225                            utils.ShellQuoteArgs(cmd)).wait(), 0)
226     finally:
227       cmd = ["rm", "-f", tmpcert]
228       AssertEqual(StartSSH(master["primary"],
229                            utils.ShellQuoteArgs(cmd)).wait(), 0)
230
231     # Custom cluster domain secret
232     cds_fh = tempfile.NamedTemporaryFile()
233     cds_fh.write(utils.GenerateSecret())
234     cds_fh.write("\n")
235     cds_fh.flush()
236
237     tmpcds = qa_utils.UploadFile(master["primary"], cds_fh.name)
238     try:
239       cmd = ["gnt-cluster", "renew-crypto", "--force",
240              "--cluster-domain-secret=%s" % tmpcds]
241       AssertEqual(StartSSH(master["primary"],
242                            utils.ShellQuoteArgs(cmd)).wait(), 0)
243     finally:
244       cmd = ["rm", "-f", tmpcds]
245       AssertEqual(StartSSH(master["primary"],
246                            utils.ShellQuoteArgs(cmd)).wait(), 0)
247
248     # Normal case
249     cmd = ["gnt-cluster", "renew-crypto", "--force",
250            "--new-cluster-certificate", "--new-confd-hmac-key",
251            "--new-rapi-certificate", "--new-cluster-domain-secret"]
252     AssertEqual(StartSSH(master["primary"],
253                          utils.ShellQuoteArgs(cmd)).wait(), 0)
254
255     # Restore RAPI certificate
256     cmd = ["gnt-cluster", "renew-crypto", "--force",
257            "--rapi-certificate=%s" % rapi_cert_backup]
258     AssertEqual(StartSSH(master["primary"],
259                          utils.ShellQuoteArgs(cmd)).wait(), 0)
260   finally:
261     cmd = ["rm", "-f", rapi_cert_backup]
262     AssertEqual(StartSSH(master["primary"],
263                          utils.ShellQuoteArgs(cmd)).wait(), 0)
264
265
266 def TestClusterBurnin():
267   """Burnin"""
268   master = qa_config.GetMasterNode()
269
270   options = qa_config.get('options', {})
271   disk_template = options.get('burnin-disk-template', 'drbd')
272   parallel = options.get('burnin-in-parallel', False)
273   check_inst = options.get('burnin-check-instances', False)
274   do_rename = options.get('burnin-rename', '')
275   do_reboot = options.get('burnin-reboot', True)
276   reboot_types = options.get("reboot-types", constants.REBOOT_TYPES)
277
278   # Get as many instances as we need
279   instances = []
280   try:
281     try:
282       num = qa_config.get('options', {}).get('burnin-instances', 1)
283       for _ in range(0, num):
284         instances.append(qa_config.AcquireInstance())
285     except qa_error.OutOfInstancesError:
286       print "Not enough instances, continuing anyway."
287
288     if len(instances) < 1:
289       raise qa_error.Error("Burnin needs at least one instance")
290
291     script = qa_utils.UploadFile(master['primary'], '../tools/burnin')
292     try:
293       # Run burnin
294       cmd = [script,
295              '--os=%s' % qa_config.get('os'),
296              '--disk-size=%s' % ",".join(qa_config.get('disk')),
297              '--disk-growth=%s' % ",".join(qa_config.get('disk-growth')),
298              '--disk-template=%s' % disk_template]
299       if parallel:
300         cmd.append('--parallel')
301         cmd.append('--early-release')
302       if check_inst:
303         cmd.append('--http-check')
304       if do_rename:
305         cmd.append('--rename=%s' % do_rename)
306       if not do_reboot:
307         cmd.append('--no-reboot')
308       else:
309         cmd.append('--reboot-types=%s' % ",".join(reboot_types))
310       cmd += [inst['name'] for inst in instances]
311       AssertEqual(StartSSH(master['primary'],
312                            utils.ShellQuoteArgs(cmd)).wait(), 0)
313     finally:
314       cmd = ['rm', '-f', script]
315       AssertEqual(StartSSH(master['primary'],
316                            utils.ShellQuoteArgs(cmd)).wait(), 0)
317   finally:
318     for inst in instances:
319       qa_config.ReleaseInstance(inst)
320
321
322 def TestClusterMasterFailover():
323   """gnt-cluster master-failover"""
324   master = qa_config.GetMasterNode()
325
326   failovermaster = qa_config.AcquireNode(exclude=master)
327   try:
328     cmd = ['gnt-cluster', 'master-failover']
329     AssertEqual(StartSSH(failovermaster['primary'],
330                          utils.ShellQuoteArgs(cmd)).wait(), 0)
331
332     cmd = ['gnt-cluster', 'master-failover']
333     AssertEqual(StartSSH(master['primary'],
334                          utils.ShellQuoteArgs(cmd)).wait(), 0)
335   finally:
336     qa_config.ReleaseNode(failovermaster)
337
338
339 def TestClusterCopyfile():
340   """gnt-cluster copyfile"""
341   master = qa_config.GetMasterNode()
342
343   uniqueid = utils.NewUUID()
344
345   # Create temporary file
346   f = tempfile.NamedTemporaryFile()
347   f.write(uniqueid)
348   f.flush()
349   f.seek(0)
350
351   # Upload file to master node
352   testname = qa_utils.UploadFile(master['primary'], f.name)
353   try:
354     # Copy file to all nodes
355     cmd = ['gnt-cluster', 'copyfile', testname]
356     AssertEqual(StartSSH(master['primary'],
357                          utils.ShellQuoteArgs(cmd)).wait(), 0)
358     _CheckFileOnAllNodes(testname, uniqueid)
359   finally:
360     _RemoveFileFromAllNodes(testname)
361
362
363 def TestClusterCommand():
364   """gnt-cluster command"""
365   master = qa_config.GetMasterNode()
366
367   uniqueid = utils.NewUUID()
368   rfile = "/tmp/gnt%s" % utils.NewUUID()
369   rcmd = utils.ShellQuoteArgs(['echo', '-n', uniqueid])
370   cmd = utils.ShellQuoteArgs(['gnt-cluster', 'command',
371                               "%s >%s" % (rcmd, rfile)])
372
373   try:
374     AssertEqual(StartSSH(master['primary'], cmd).wait(), 0)
375     _CheckFileOnAllNodes(rfile, uniqueid)
376   finally:
377     _RemoveFileFromAllNodes(rfile)
378
379
380 def TestClusterDestroy():
381   """gnt-cluster destroy"""
382   master = qa_config.GetMasterNode()
383
384   cmd = ['gnt-cluster', 'destroy', '--yes-do-it']
385   AssertEqual(StartSSH(master['primary'],
386                        utils.ShellQuoteArgs(cmd)).wait(), 0)