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