LUExportInstance: Accept instance already shut down
[ganeti-local] / qa / ganeti-qa.py
1 #!/usr/bin/python -u
2 #
3
4 # Copyright (C) 2007, 2008, 2009, 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 """Script for doing QA on Ganeti.
23
24 """
25
26 import sys
27 import datetime
28 import optparse
29
30 import qa_cluster
31 import qa_config
32 import qa_daemon
33 import qa_env
34 import qa_instance
35 import qa_node
36 import qa_os
37 import qa_rapi
38 import qa_tags
39 import qa_utils
40
41 from ganeti import utils
42 from ganeti import rapi
43
44 import ganeti.rapi.client
45
46
47 def _FormatHeader(line, end=72, char="-"):
48   """Fill a line up to the end column.
49
50   """
51   line = "---- " + line + " "
52   line += "-" * (end-len(line))
53   line = line.rstrip()
54   return line
55
56
57 def RunTest(fn, *args):
58   """Runs a test after printing a header.
59
60   """
61   if fn.__doc__:
62     desc = fn.__doc__.splitlines()[0].strip()
63   else:
64     desc = "%r" % fn
65
66   desc = desc.rstrip(".")
67
68   tstart = datetime.datetime.now()
69
70   print
71   print _FormatHeader("%s start %s" % (tstart, desc))
72
73   try:
74     retval = fn(*args)
75     return retval
76   finally:
77     tstop = datetime.datetime.now()
78     tdelta = tstop - tstart
79     print _FormatHeader("%s time=%s %s" % (tstop, tdelta, desc))
80
81
82 def RunEnvTests():
83   """Run several environment tests.
84
85   """
86   if not qa_config.TestEnabled('env'):
87     return
88
89   RunTest(qa_env.TestSshConnection)
90   RunTest(qa_env.TestIcmpPing)
91   RunTest(qa_env.TestGanetiCommands)
92
93
94 def SetupCluster(rapi_user, rapi_secret):
95   """Initializes the cluster.
96
97   @param rapi_user: Login user for RAPI
98   @param rapi_secret: Login secret for RAPI
99
100   """
101   if qa_config.TestEnabled('create-cluster'):
102     RunTest(qa_cluster.TestClusterInit, rapi_user, rapi_secret)
103     RunTest(qa_node.TestNodeAddAll)
104   else:
105     # consider the nodes are already there
106     qa_node.MarkNodeAddedAll()
107
108   if qa_config.TestEnabled("test-jobqueue"):
109     RunTest(qa_cluster.TestJobqueue)
110
111   # enable the watcher (unconditionally)
112   RunTest(qa_daemon.TestResumeWatcher)
113
114   if qa_config.TestEnabled('node-info'):
115     RunTest(qa_node.TestNodeInfo)
116
117
118 def RunClusterTests():
119   """Runs tests related to gnt-cluster.
120
121   """
122   if qa_config.TestEnabled("cluster-renew-crypto"):
123     RunTest(qa_cluster.TestClusterRenewCrypto)
124
125   if qa_config.TestEnabled('cluster-verify'):
126     RunTest(qa_cluster.TestClusterVerify)
127
128   if qa_config.TestEnabled('cluster-reserved-lvs'):
129     RunTest(qa_cluster.TestClusterReservedLvs)
130
131   if qa_config.TestEnabled('cluster-rename'):
132     RunTest(qa_cluster.TestClusterRename)
133
134   if qa_config.TestEnabled('cluster-info'):
135     RunTest(qa_cluster.TestClusterVersion)
136     RunTest(qa_cluster.TestClusterInfo)
137     RunTest(qa_cluster.TestClusterGetmaster)
138
139   if qa_config.TestEnabled('cluster-copyfile'):
140     RunTest(qa_cluster.TestClusterCopyfile)
141
142   if qa_config.TestEnabled('cluster-command'):
143     RunTest(qa_cluster.TestClusterCommand)
144
145   if qa_config.TestEnabled('cluster-burnin'):
146     RunTest(qa_cluster.TestClusterBurnin)
147
148   if qa_config.TestEnabled('cluster-master-failover'):
149     RunTest(qa_cluster.TestClusterMasterFailover)
150
151   if qa_rapi.Enabled():
152     RunTest(qa_rapi.TestVersion)
153     RunTest(qa_rapi.TestEmptyCluster)
154
155
156 def RunOsTests():
157   """Runs all tests related to gnt-os.
158
159   """
160   if not qa_config.TestEnabled('os'):
161     return
162
163   RunTest(qa_os.TestOsList)
164   RunTest(qa_os.TestOsDiagnose)
165   RunTest(qa_os.TestOsValid)
166   RunTest(qa_os.TestOsInvalid)
167   RunTest(qa_os.TestOsPartiallyValid)
168   RunTest(qa_os.TestOsModifyValid)
169   RunTest(qa_os.TestOsModifyInvalid)
170   RunTest(qa_os.TestOsStates)
171
172
173 def RunCommonInstanceTests(instance):
174   """Runs a few tests that are common to all disk types.
175
176   """
177   if qa_config.TestEnabled('instance-shutdown'):
178     RunTest(qa_instance.TestInstanceShutdown, instance)
179     RunTest(qa_instance.TestInstanceStartup, instance)
180
181   if qa_config.TestEnabled('instance-list'):
182     RunTest(qa_instance.TestInstanceList)
183
184   if qa_config.TestEnabled('instance-info'):
185     RunTest(qa_instance.TestInstanceInfo, instance)
186
187   if qa_config.TestEnabled('instance-modify'):
188     RunTest(qa_instance.TestInstanceModify, instance)
189     if qa_rapi.Enabled():
190       RunTest(qa_rapi.TestRapiInstanceModify, instance)
191
192   if qa_config.TestEnabled('instance-console'):
193     RunTest(qa_instance.TestInstanceConsole, instance)
194
195   if qa_config.TestEnabled('instance-reinstall'):
196     RunTest(qa_instance.TestInstanceShutdown, instance)
197     RunTest(qa_instance.TestInstanceReinstall, instance)
198     RunTest(qa_instance.TestInstanceStartup, instance)
199
200   if qa_config.TestEnabled('instance-reboot'):
201     RunTest(qa_instance.TestInstanceReboot, instance)
202
203   if qa_config.TestEnabled('instance-rename'):
204     rename_target = qa_config.get("rename", None)
205     if rename_target is None:
206       print qa_utils.FormatError("Can rename instance, 'rename' entry is"
207                                  " missing from configuration")
208     else:
209       RunTest(qa_instance.TestInstanceShutdown, instance)
210       RunTest(qa_instance.TestInstanceRename, instance, rename_target)
211       if qa_rapi.Enabled():
212         RunTest(qa_rapi.TestRapiInstanceRename, instance, rename_target)
213       RunTest(qa_instance.TestInstanceStartup, instance)
214
215   if qa_config.TestEnabled('tags'):
216     RunTest(qa_tags.TestInstanceTags, instance)
217
218   if qa_rapi.Enabled():
219     RunTest(qa_rapi.TestInstance, instance)
220
221
222 def RunCommonNodeTests():
223   """Run a few common node tests.
224
225   """
226   if qa_config.TestEnabled('node-volumes'):
227     RunTest(qa_node.TestNodeVolumes)
228
229   if qa_config.TestEnabled("node-storage"):
230     RunTest(qa_node.TestNodeStorage)
231
232
233 def RunExportImportTests(instance, pnode, snode):
234   """Tries to export and import the instance.
235
236   @param pnode: current primary node of the instance
237   @param snode: current secondary node of the instance, if any,
238       otherwise None
239
240   """
241   if qa_config.TestEnabled('instance-export'):
242     RunTest(qa_instance.TestInstanceExportNoTarget, instance)
243
244     expnode = qa_config.AcquireNode(exclude=pnode)
245     try:
246       name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
247
248       RunTest(qa_instance.TestBackupList, expnode)
249
250       if qa_config.TestEnabled('instance-import'):
251         newinst = qa_config.AcquireInstance()
252         try:
253           RunTest(qa_instance.TestInstanceImport, pnode, newinst,
254                   expnode, name)
255           RunTest(qa_instance.TestInstanceRemove, newinst)
256         finally:
257           qa_config.ReleaseInstance(newinst)
258     finally:
259       qa_config.ReleaseNode(expnode)
260
261   if (qa_rapi.Enabled() and
262       qa_config.TestEnabled("inter-cluster-instance-move")):
263     newinst = qa_config.AcquireInstance()
264     try:
265       if snode is None:
266         excl = [pnode]
267       else:
268         excl = [pnode, snode]
269       tnode = qa_config.AcquireNode(exclude=excl)
270       try:
271         RunTest(qa_rapi.TestInterClusterInstanceMove, instance, newinst,
272                 pnode, snode, tnode)
273       finally:
274         qa_config.ReleaseNode(tnode)
275     finally:
276       qa_config.ReleaseInstance(newinst)
277
278
279 def RunDaemonTests(instance, pnode):
280   """Test the ganeti-watcher script.
281
282   """
283   automatic_restart = \
284     qa_config.TestEnabled('instance-automatic-restart')
285   consecutive_failures = \
286     qa_config.TestEnabled('instance-consecutive-failures')
287
288   RunTest(qa_daemon.TestPauseWatcher)
289   if automatic_restart or consecutive_failures:
290
291     if automatic_restart:
292       RunTest(qa_daemon.TestInstanceAutomaticRestart, pnode, instance)
293
294     if consecutive_failures:
295       RunTest(qa_daemon.TestInstanceConsecutiveFailures, pnode, instance)
296
297   RunTest(qa_daemon.TestResumeWatcher)
298
299
300 def RunHardwareFailureTests(instance, pnode, snode):
301   """Test cluster internal hardware failure recovery.
302
303   """
304   if qa_config.TestEnabled('instance-failover'):
305     RunTest(qa_instance.TestInstanceFailover, instance)
306
307   if qa_config.TestEnabled("instance-migrate"):
308     RunTest(qa_instance.TestInstanceMigrate, instance)
309
310     if qa_rapi.Enabled():
311       RunTest(qa_rapi.TestRapiInstanceMigrate, instance)
312
313   if qa_config.TestEnabled('instance-replace-disks'):
314     othernode = qa_config.AcquireNode(exclude=[pnode, snode])
315     try:
316       RunTest(qa_instance.TestReplaceDisks,
317               instance, pnode, snode, othernode)
318     finally:
319       qa_config.ReleaseNode(othernode)
320
321   if qa_config.TestEnabled('node-evacuate'):
322     RunTest(qa_node.TestNodeEvacuate, pnode, snode)
323
324   if qa_config.TestEnabled('node-failover'):
325     RunTest(qa_node.TestNodeFailover, pnode, snode)
326
327   if qa_config.TestEnabled('instance-disk-failure'):
328     RunTest(qa_instance.TestInstanceMasterDiskFailure,
329             instance, pnode, snode)
330     RunTest(qa_instance.TestInstanceSecondaryDiskFailure,
331             instance, pnode, snode)
332
333
334 @rapi.client.UsesRapiClient
335 def main():
336   """Main program.
337
338   """
339   parser = optparse.OptionParser(usage="%prog [options] <config-file>")
340   parser.add_option('--yes-do-it', dest='yes_do_it',
341       action="store_true",
342       help="Really execute the tests")
343   (qa_config.options, args) = parser.parse_args()
344
345   if len(args) == 1:
346     (config_file, ) = args
347   else:
348     parser.error("Wrong number of arguments.")
349
350   if not qa_config.options.yes_do_it:
351     print ("Executing this script irreversibly destroys any Ganeti\n"
352            "configuration on all nodes involved. If you really want\n"
353            "to start testing, supply the --yes-do-it option.")
354     sys.exit(1)
355
356   qa_config.Load(config_file)
357
358   rapi_user = "ganeti-qa"
359   rapi_secret = utils.GenerateSecret()
360
361   RunEnvTests()
362   SetupCluster(rapi_user, rapi_secret)
363
364   # Load RAPI certificate
365   qa_rapi.Setup(rapi_user, rapi_secret)
366
367   RunClusterTests()
368   RunOsTests()
369
370   if qa_config.TestEnabled('tags'):
371     RunTest(qa_tags.TestClusterTags)
372
373   RunCommonNodeTests()
374
375   pnode = qa_config.AcquireNode(exclude=qa_config.GetMasterNode())
376   try:
377     if qa_config.TestEnabled('node-readd'):
378       RunTest(qa_node.TestNodeReadd, pnode)
379
380     if qa_config.TestEnabled("node-modify"):
381       RunTest(qa_node.TestNodeModify, pnode)
382   finally:
383     qa_config.ReleaseNode(pnode)
384
385   pnode = qa_config.AcquireNode()
386   try:
387     if qa_config.TestEnabled('tags'):
388       RunTest(qa_tags.TestNodeTags, pnode)
389
390     if qa_rapi.Enabled():
391       RunTest(qa_rapi.TestNode, pnode)
392
393       if qa_config.TestEnabled("instance-add-plain-disk"):
394         for use_client in [True, False]:
395           rapi_instance = RunTest(qa_rapi.TestRapiInstanceAdd, pnode,
396                                   use_client)
397           RunCommonInstanceTests(rapi_instance)
398           RunTest(qa_rapi.TestRapiInstanceRemove, rapi_instance, use_client)
399           del rapi_instance
400
401     if qa_config.TestEnabled('instance-add-plain-disk'):
402       instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
403       RunCommonInstanceTests(instance)
404       RunExportImportTests(instance, pnode, None)
405       RunDaemonTests(instance, pnode)
406       RunTest(qa_instance.TestInstanceRemove, instance)
407       del instance
408
409     multinode_tests = [
410       ('instance-add-drbd-disk',
411        qa_instance.TestInstanceAddWithDrbdDisk),
412     ]
413
414     for name, func in multinode_tests:
415       if qa_config.TestEnabled(name):
416         snode = qa_config.AcquireNode(exclude=pnode)
417         try:
418           instance = RunTest(func, pnode, snode)
419           RunCommonInstanceTests(instance)
420           if qa_config.TestEnabled('instance-convert-disk'):
421             RunTest(qa_instance.TestInstanceShutdown, instance)
422             RunTest(qa_instance.TestInstanceConvertDisk, instance, snode)
423             RunTest(qa_instance.TestInstanceStartup, instance)
424           RunExportImportTests(instance, pnode, snode)
425           RunHardwareFailureTests(instance, pnode, snode)
426           RunTest(qa_instance.TestInstanceRemove, instance)
427           del instance
428         finally:
429           qa_config.ReleaseNode(snode)
430
431     if (qa_config.TestEnabled('instance-add-plain-disk') and
432         qa_config.TestEnabled("instance-export")):
433       for shutdown in [False, True]:
434         instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
435         expnode = qa_config.AcquireNode(exclude=pnode)
436         try:
437           if shutdown:
438             # Stop instance before exporting and removing it
439             RunTest(qa_instance.TestInstanceShutdown, instance)
440           RunTest(qa_instance.TestInstanceExportWithRemove, instance, expnode)
441           RunTest(qa_instance.TestBackupList, expnode)
442         finally:
443           qa_config.ReleaseNode(expnode)
444         del expnode
445         del instance
446
447   finally:
448     qa_config.ReleaseNode(pnode)
449
450   if qa_config.TestEnabled('create-cluster'):
451     RunTest(qa_node.TestNodeRemoveAll)
452
453   if qa_config.TestEnabled('cluster-destroy'):
454     RunTest(qa_cluster.TestClusterDestroy)
455
456
457 if __name__ == '__main__':
458   main()