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