Implement replacing cluster certs and keys via “gnt-cluster renew-crypto”
[ganeti-local] / qa / ganeti-qa.py
1 #!/usr/bin/python
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 """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
42 def RunTest(fn, *args):
43   """Runs a test after printing a header.
44
45   """
46   if fn.__doc__:
47     desc = fn.__doc__.splitlines()[0].strip()
48   else:
49     desc = '%r' % fn
50
51   now = str(datetime.datetime.now())
52
53   print
54   print '---', now, ('-' * (55 - len(now)))
55   print desc
56   print '-' * 60
57
58   return fn(*args)
59
60
61 def RunEnvTests():
62   """Run several environment tests.
63
64   """
65   if not qa_config.TestEnabled('env'):
66     return
67
68   RunTest(qa_env.TestSshConnection)
69   RunTest(qa_env.TestIcmpPing)
70   RunTest(qa_env.TestGanetiCommands)
71
72
73 def SetupCluster():
74   """Initializes the cluster.
75
76   """
77   if qa_config.TestEnabled('create-cluster'):
78     RunTest(qa_cluster.TestClusterInit)
79     RunTest(qa_node.TestNodeAddAll)
80   else:
81     # consider the nodes are already there
82     qa_node.MarkNodeAddedAll()
83   if qa_config.TestEnabled('node-info'):
84     RunTest(qa_node.TestNodeInfo)
85
86
87 def RunClusterTests():
88   """Runs tests related to gnt-cluster.
89
90   """
91   if qa_config.TestEnabled("cluster-renew-crypto"):
92     RunTest(qa_cluster.TestClusterRenewCrypto)
93
94   if qa_config.TestEnabled('cluster-verify'):
95     RunTest(qa_cluster.TestClusterVerify)
96
97   if qa_config.TestEnabled('cluster-rename'):
98     RunTest(qa_cluster.TestClusterRename)
99
100   if qa_config.TestEnabled('cluster-info'):
101     RunTest(qa_cluster.TestClusterVersion)
102     RunTest(qa_cluster.TestClusterInfo)
103     RunTest(qa_cluster.TestClusterGetmaster)
104
105   if qa_config.TestEnabled('cluster-copyfile'):
106     RunTest(qa_cluster.TestClusterCopyfile)
107
108   if qa_config.TestEnabled('cluster-command'):
109     RunTest(qa_cluster.TestClusterCommand)
110
111   if qa_config.TestEnabled('cluster-burnin'):
112     RunTest(qa_cluster.TestClusterBurnin)
113
114   if qa_config.TestEnabled('cluster-master-failover'):
115     RunTest(qa_cluster.TestClusterMasterFailover)
116
117   if qa_rapi.Enabled():
118     RunTest(qa_rapi.TestVersion)
119     RunTest(qa_rapi.TestEmptyCluster)
120
121
122 def RunOsTests():
123   """Runs all tests related to gnt-os.
124
125   """
126   if not qa_config.TestEnabled('os'):
127     return
128
129   RunTest(qa_os.TestOsList)
130   RunTest(qa_os.TestOsDiagnose)
131   RunTest(qa_os.TestOsValid)
132   RunTest(qa_os.TestOsInvalid)
133   RunTest(qa_os.TestOsPartiallyValid)
134   RunTest(qa_os.TestOsModifyValid)
135   RunTest(qa_os.TestOsModifyInvalid)
136
137
138 def RunCommonInstanceTests(instance):
139   """Runs a few tests that are common to all disk types.
140
141   """
142   if qa_config.TestEnabled('instance-shutdown'):
143     RunTest(qa_instance.TestInstanceShutdown, instance)
144     RunTest(qa_instance.TestInstanceStartup, instance)
145
146   if qa_config.TestEnabled('instance-list'):
147     RunTest(qa_instance.TestInstanceList)
148
149   if qa_config.TestEnabled('instance-info'):
150     RunTest(qa_instance.TestInstanceInfo, instance)
151
152   if qa_config.TestEnabled('instance-modify'):
153     RunTest(qa_instance.TestInstanceModify, instance)
154
155   if qa_config.TestEnabled('instance-console'):
156     RunTest(qa_instance.TestInstanceConsole, instance)
157
158   if qa_config.TestEnabled('instance-reinstall'):
159     RunTest(qa_instance.TestInstanceShutdown, instance)
160     RunTest(qa_instance.TestInstanceReinstall, instance)
161     RunTest(qa_instance.TestInstanceStartup, instance)
162
163   if qa_config.TestEnabled('instance-reboot'):
164     RunTest(qa_instance.TestInstanceReboot, instance)
165
166   if qa_config.TestEnabled('instance-rename'):
167     RunTest(qa_instance.TestInstanceShutdown, instance)
168     RunTest(qa_instance.TestInstanceRename, instance)
169     RunTest(qa_instance.TestInstanceStartup, instance)
170
171   if qa_config.TestEnabled('tags'):
172     RunTest(qa_tags.TestInstanceTags, instance)
173
174   if qa_config.TestEnabled('node-volumes'):
175     RunTest(qa_node.TestNodeVolumes)
176
177   if qa_config.TestEnabled("node-storage"):
178     RunTest(qa_node.TestNodeStorage)
179
180   if qa_rapi.Enabled():
181     RunTest(qa_rapi.TestInstance, instance)
182
183
184 def RunExportImportTests(instance, pnode):
185   """Tries to export and import the instance.
186
187   """
188   if qa_config.TestEnabled('instance-export'):
189     expnode = qa_config.AcquireNode(exclude=pnode)
190     try:
191       name = RunTest(qa_instance.TestInstanceExport, instance, expnode)
192
193       RunTest(qa_instance.TestBackupList, expnode)
194
195       if qa_config.TestEnabled('instance-import'):
196         newinst = qa_config.AcquireInstance()
197         try:
198           RunTest(qa_instance.TestInstanceImport, pnode, newinst,
199                   expnode, name)
200           RunTest(qa_instance.TestInstanceRemove, newinst)
201         finally:
202           qa_config.ReleaseInstance(newinst)
203     finally:
204       qa_config.ReleaseNode(expnode)
205
206
207 def RunDaemonTests(instance, pnode):
208   """Test the ganeti-watcher script.
209
210   """
211   automatic_restart = \
212     qa_config.TestEnabled('instance-automatic-restart')
213   consecutive_failures = \
214     qa_config.TestEnabled('instance-consecutive-failures')
215
216   if automatic_restart or consecutive_failures:
217     qa_daemon.PrintCronWarning()
218
219     if automatic_restart:
220       RunTest(qa_daemon.TestInstanceAutomaticRestart, pnode, instance)
221
222     if consecutive_failures:
223       RunTest(qa_daemon.TestInstanceConsecutiveFailures, pnode, instance)
224
225
226 def RunHardwareFailureTests(instance, pnode, snode):
227   """Test cluster internal hardware failure recovery.
228
229   """
230   if qa_config.TestEnabled('instance-failover'):
231     RunTest(qa_instance.TestInstanceFailover, instance)
232
233   if qa_config.TestEnabled('instance-replace-disks'):
234     othernode = qa_config.AcquireNode(exclude=[pnode, snode])
235     try:
236       RunTest(qa_instance.TestReplaceDisks,
237               instance, pnode, snode, othernode)
238     finally:
239       qa_config.ReleaseNode(othernode)
240
241   if qa_config.TestEnabled('node-evacuate'):
242     RunTest(qa_node.TestNodeEvacuate, pnode, snode)
243
244   if qa_config.TestEnabled('node-failover'):
245     RunTest(qa_node.TestNodeFailover, pnode, snode)
246
247   if qa_config.TestEnabled('instance-disk-failure'):
248     RunTest(qa_instance.TestInstanceMasterDiskFailure,
249             instance, pnode, snode)
250     RunTest(qa_instance.TestInstanceSecondaryDiskFailure,
251             instance, pnode, snode)
252
253
254 def main():
255   """Main program.
256
257   """
258   parser = optparse.OptionParser(usage="%prog [options] <config-file>")
259   parser.add_option('--yes-do-it', dest='yes_do_it',
260       action="store_true",
261       help="Really execute the tests")
262   (qa_config.options, args) = parser.parse_args()
263
264   if len(args) == 1:
265     (config_file, ) = args
266   else:
267     parser.error("Wrong number of arguments.")
268
269   if not qa_config.options.yes_do_it:
270     print ("Executing this script irreversibly destroys any Ganeti\n"
271            "configuration on all nodes involved. If you really want\n"
272            "to start testing, supply the --yes-do-it option.")
273     sys.exit(1)
274
275   qa_config.Load(config_file)
276
277   RunEnvTests()
278   SetupCluster()
279   RunClusterTests()
280   RunOsTests()
281
282   if qa_config.TestEnabled('tags'):
283     RunTest(qa_tags.TestClusterTags)
284
285   if qa_config.TestEnabled('node-readd'):
286     master = qa_config.GetMasterNode()
287     pnode = qa_config.AcquireNode(exclude=master)
288     try:
289       RunTest(qa_node.TestNodeReadd, pnode)
290     finally:
291       qa_config.ReleaseNode(pnode)
292
293   pnode = qa_config.AcquireNode()
294   try:
295     if qa_config.TestEnabled('tags'):
296       RunTest(qa_tags.TestNodeTags, pnode)
297
298     if qa_rapi.Enabled():
299       RunTest(qa_rapi.TestNode, pnode)
300
301     if qa_config.TestEnabled('instance-add-plain-disk'):
302       instance = RunTest(qa_instance.TestInstanceAddWithPlainDisk, pnode)
303       RunCommonInstanceTests(instance)
304       RunExportImportTests(instance, pnode)
305       RunDaemonTests(instance, pnode)
306       RunTest(qa_instance.TestInstanceRemove, instance)
307       del instance
308
309     multinode_tests = [
310       ('instance-add-drbd-disk',
311        qa_instance.TestInstanceAddWithDrbdDisk),
312     ]
313
314     for name, func in multinode_tests:
315       if qa_config.TestEnabled(name):
316         snode = qa_config.AcquireNode(exclude=pnode)
317         try:
318           instance = RunTest(func, pnode, snode)
319           RunCommonInstanceTests(instance)
320           RunExportImportTests(instance, pnode)
321           RunHardwareFailureTests(instance, pnode, snode)
322           RunTest(qa_instance.TestInstanceRemove, instance)
323           del instance
324         finally:
325           qa_config.ReleaseNode(snode)
326
327   finally:
328     qa_config.ReleaseNode(pnode)
329
330   if qa_config.TestEnabled('create-cluster'):
331     RunTest(qa_node.TestNodeRemoveAll)
332
333   if qa_config.TestEnabled('cluster-destroy'):
334     RunTest(qa_cluster.TestClusterDestroy)
335
336
337 if __name__ == '__main__':
338   main()