4 # Copyright (C) 2007, 2010, 2011 Google Inc.
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.
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.
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
22 """Cluster related QA tests.
29 from ganeti import constants
30 from ganeti import compat
31 from ganeti import utils
37 from qa_utils import AssertEqual, AssertCommand, GetCommandOutput
40 #: cluster verify command
41 _CLUSTER_VERIFY = ["gnt-cluster", "verify"]
44 def _RemoveFileFromAllNodes(filename):
45 """Removes a file from all nodes.
48 for node in qa_config.get("nodes"):
49 AssertCommand(["rm", "-f", filename], node=node)
52 def _CheckFileOnAllNodes(filename, content):
53 """Verifies the content of the given file on all nodes.
56 cmd = utils.ShellQuoteArgs(["cat", filename])
57 for node in qa_config.get("nodes"):
58 AssertEqual(qa_utils.GetCommandOutput(node["primary"], cmd), content)
61 def TestClusterInit(rapi_user, rapi_secret):
62 """gnt-cluster init"""
63 master = qa_config.GetMasterNode()
65 rapi_dir = os.path.dirname(constants.RAPI_USERS_FILE)
67 # First create the RAPI credentials
68 fh = tempfile.NamedTemporaryFile()
70 fh.write("%s %s write\n" % (rapi_user, rapi_secret))
73 tmpru = qa_utils.UploadFile(master["primary"], fh.name)
75 AssertCommand(["mkdir", "-p", rapi_dir])
76 AssertCommand(["mv", tmpru, constants.RAPI_USERS_FILE])
78 AssertCommand(["rm", "-f", tmpru])
83 cmd = ["gnt-cluster", "init"]
85 cmd.append("--primary-ip-version=%d" %
86 qa_config.get("primary_ip_version", 4))
88 if master.get("secondary", None):
89 cmd.append("--secondary-ip=%s" % master["secondary"])
91 bridge = qa_config.get("bridge", None)
93 cmd.append("--bridge=%s" % bridge)
94 cmd.append("--master-netdev=%s" % bridge)
96 htype = qa_config.get("enabled-hypervisors", None)
98 cmd.append("--enabled-hypervisors=%s" % htype)
100 cmd.append(qa_config.get("name"))
104 cmd = ["gnt-cluster", "modify"]
105 # hypervisor parameter modifications
106 hvp = qa_config.get("hypervisor-parameters", {})
107 for k, v in hvp.items():
108 cmd.extend(["-H", "%s:%s" % (k, v)])
109 # backend parameter modifications
110 bep = qa_config.get("backend-parameters", "")
112 cmd.extend(["-B", bep])
118 osp = qa_config.get("os-parameters", {})
119 for k, v in osp.items():
120 AssertCommand(["gnt-os", "modify", "-O", v, k])
122 # OS hypervisor parameters
123 os_hvp = qa_config.get("os-hvp", {})
124 for os_name in os_hvp:
125 for hv, hvp in os_hvp[os_name].items():
126 AssertCommand(["gnt-os", "modify", "-H", "%s:%s" % (hv, hvp), os_name])
129 def TestClusterRename():
130 """gnt-cluster rename"""
131 cmd = ["gnt-cluster", "rename", "-f"]
133 original_name = qa_config.get("name")
134 rename_target = qa_config.get("rename", None)
135 if rename_target is None:
136 print qa_utils.FormatError('"rename" entry is missing')
140 cmd + [rename_target],
142 cmd + [original_name],
148 def TestClusterOob():
149 """out-of-band framework"""
150 oob_path_exists = "/tmp/ganeti-qa-oob-does-exist-%s" % utils.NewUUID()
152 AssertCommand(_CLUSTER_VERIFY)
153 AssertCommand(["gnt-cluster", "modify", "--node-parameters",
154 "oob_program=/tmp/ganeti-qa-oob-does-not-exist-%s" %
157 AssertCommand(_CLUSTER_VERIFY, fail=True)
159 AssertCommand(["touch", oob_path_exists])
160 AssertCommand(["chmod", "0400", oob_path_exists])
161 AssertCommand(["gnt-cluster", "copyfile", oob_path_exists])
164 AssertCommand(["gnt-cluster", "modify", "--node-parameters",
165 "oob_program=%s" % oob_path_exists])
167 AssertCommand(_CLUSTER_VERIFY, fail=True)
169 AssertCommand(["chmod", "0500", oob_path_exists])
170 AssertCommand(["gnt-cluster", "copyfile", oob_path_exists])
172 AssertCommand(_CLUSTER_VERIFY)
174 AssertCommand(["gnt-cluster", "command", "rm", oob_path_exists])
176 AssertCommand(["gnt-cluster", "modify", "--node-parameters",
180 def TestClusterEpo():
181 """gnt-cluster epo"""
182 master = qa_config.GetMasterNode()
184 # Assert that OOB is unavailable for all nodes
185 result_output = GetCommandOutput(master["primary"],
186 "gnt-node list --verbose --no-header -o"
188 AssertEqual(compat.all(powered == "(unavail)"
189 for powered in result_output.splitlines()), True)
192 AssertCommand(["gnt-cluster", "epo", "--groups", "--all"], fail=True)
193 # --all doesn't expect arguments
194 AssertCommand(["gnt-cluster", "epo", "--all", "some_arg"], fail=True)
196 # Unless --all is given master is not allowed to be in the list
197 AssertCommand(["gnt-cluster", "epo", "-f", master["primary"]], fail=True)
199 # This shouldn't fail
200 AssertCommand(["gnt-cluster", "epo", "-f", "--all"])
202 # All instances should have been stopped now
203 result_output = GetCommandOutput(master["primary"],
204 "gnt-instance list --no-header -o status")
205 AssertEqual(compat.all(status == "ADMIN_down"
206 for status in result_output.splitlines()), True)
208 # Now start everything again
209 AssertCommand(["gnt-cluster", "epo", "--on", "-f", "--all"])
211 # All instances should have been started now
212 result_output = GetCommandOutput(master["primary"],
213 "gnt-instance list --no-header -o status")
214 AssertEqual(compat.all(status == "running"
215 for status in result_output.splitlines()), True)
218 def TestClusterVerify():
219 """gnt-cluster verify"""
220 AssertCommand(_CLUSTER_VERIFY)
221 AssertCommand(["gnt-cluster", "verify-disks"])
225 """gnt-debug test-jobqueue"""
226 AssertCommand(["gnt-debug", "test-jobqueue"])
229 def TestClusterReservedLvs():
230 """gnt-cluster reserved lvs"""
232 (False, _CLUSTER_VERIFY),
233 (False, ["gnt-cluster", "modify", "--reserved-lvs", ""]),
234 (False, ["lvcreate", "-L1G", "-nqa-test", "xenvg"]),
235 (True, _CLUSTER_VERIFY),
236 (False, ["gnt-cluster", "modify", "--reserved-lvs",
237 "xenvg/qa-test,.*/other-test"]),
238 (False, _CLUSTER_VERIFY),
239 (False, ["gnt-cluster", "modify", "--reserved-lvs", ".*/qa-.*"]),
240 (False, _CLUSTER_VERIFY),
241 (False, ["gnt-cluster", "modify", "--reserved-lvs", ""]),
242 (True, _CLUSTER_VERIFY),
243 (False, ["lvremove", "-f", "xenvg/qa-test"]),
244 (False, _CLUSTER_VERIFY),
246 AssertCommand(cmd, fail=fail)
249 def TestClusterModifyBe():
250 """gnt-cluster modify -B"""
253 (False, ["gnt-cluster", "modify", "-B", "memory=256"]),
254 (False, ["sh", "-c", "gnt-cluster info|grep '^ *memory: 256$'"]),
255 (True, ["gnt-cluster", "modify", "-B", "memory=a"]),
256 (False, ["gnt-cluster", "modify", "-B", "memory=128"]),
257 (False, ["sh", "-c", "gnt-cluster info|grep '^ *memory: 128$'"]),
259 (False, ["gnt-cluster", "modify", "-B", "vcpus=4"]),
260 (False, ["sh", "-c", "gnt-cluster info|grep '^ *vcpus: 4$'"]),
261 (True, ["gnt-cluster", "modify", "-B", "vcpus=a"]),
262 (False, ["gnt-cluster", "modify", "-B", "vcpus=1"]),
263 (False, ["sh", "-c", "gnt-cluster info|grep '^ *vcpus: 1$'"]),
265 (False, ["gnt-cluster", "modify", "-B", "auto_balance=False"]),
266 (False, ["sh", "-c", "gnt-cluster info|grep '^ *auto_balance: False$'"]),
267 (True, ["gnt-cluster", "modify", "-B", "auto_balance=1"]),
268 (False, ["gnt-cluster", "modify", "-B", "auto_balance=True"]),
269 (False, ["sh", "-c", "gnt-cluster info|grep '^ *auto_balance: True$'"]),
271 AssertCommand(cmd, fail=fail)
273 # redo the original-requested BE parameters, if any
274 bep = qa_config.get("backend-parameters", "")
276 AssertCommand(["gnt-cluster", "modify", "-B", bep])
279 def TestClusterInfo():
280 """gnt-cluster info"""
281 AssertCommand(["gnt-cluster", "info"])
284 def TestClusterRedistConf():
285 """gnt-cluster redist-conf"""
286 AssertCommand(["gnt-cluster", "redist-conf"])
289 def TestClusterGetmaster():
290 """gnt-cluster getmaster"""
291 AssertCommand(["gnt-cluster", "getmaster"])
294 def TestClusterVersion():
295 """gnt-cluster version"""
296 AssertCommand(["gnt-cluster", "version"])
299 def TestClusterRenewCrypto():
300 """gnt-cluster renew-crypto"""
301 master = qa_config.GetMasterNode()
303 # Conflicting options
304 cmd = ["gnt-cluster", "renew-crypto", "--force",
305 "--new-cluster-certificate", "--new-confd-hmac-key"]
307 ["--new-rapi-certificate", "--rapi-certificate=/dev/null"],
308 ["--new-cluster-domain-secret", "--cluster-domain-secret=/dev/null"],
310 for i in conflicting:
311 AssertCommand(cmd + i, fail=True)
313 # Invalid RAPI certificate
314 cmd = ["gnt-cluster", "renew-crypto", "--force",
315 "--rapi-certificate=/dev/null"]
316 AssertCommand(cmd, fail=True)
318 rapi_cert_backup = qa_utils.BackupFile(master["primary"],
319 constants.RAPI_CERT_FILE)
321 # Custom RAPI certificate
322 fh = tempfile.NamedTemporaryFile()
324 # Ensure certificate doesn't cause "gnt-cluster verify" to complain
325 validity = constants.SSL_CERT_EXPIRATION_WARN * 3
327 utils.GenerateSelfSignedSslCert(fh.name, validity=validity)
329 tmpcert = qa_utils.UploadFile(master["primary"], fh.name)
331 AssertCommand(["gnt-cluster", "renew-crypto", "--force",
332 "--rapi-certificate=%s" % tmpcert])
334 AssertCommand(["rm", "-f", tmpcert])
336 # Custom cluster domain secret
337 cds_fh = tempfile.NamedTemporaryFile()
338 cds_fh.write(utils.GenerateSecret())
342 tmpcds = qa_utils.UploadFile(master["primary"], cds_fh.name)
344 AssertCommand(["gnt-cluster", "renew-crypto", "--force",
345 "--cluster-domain-secret=%s" % tmpcds])
347 AssertCommand(["rm", "-f", tmpcds])
350 AssertCommand(["gnt-cluster", "renew-crypto", "--force",
351 "--new-cluster-certificate", "--new-confd-hmac-key",
352 "--new-rapi-certificate", "--new-cluster-domain-secret"])
354 # Restore RAPI certificate
355 AssertCommand(["gnt-cluster", "renew-crypto", "--force",
356 "--rapi-certificate=%s" % rapi_cert_backup])
358 AssertCommand(["rm", "-f", rapi_cert_backup])
361 def TestClusterBurnin():
363 master = qa_config.GetMasterNode()
365 options = qa_config.get("options", {})
366 disk_template = options.get("burnin-disk-template", "drbd")
367 parallel = options.get("burnin-in-parallel", False)
368 check_inst = options.get("burnin-check-instances", False)
369 do_rename = options.get("burnin-rename", "")
370 do_reboot = options.get("burnin-reboot", True)
371 reboot_types = options.get("reboot-types", constants.REBOOT_TYPES)
373 # Get as many instances as we need
377 num = qa_config.get("options", {}).get("burnin-instances", 1)
378 for _ in range(0, num):
379 instances.append(qa_config.AcquireInstance())
380 except qa_error.OutOfInstancesError:
381 print "Not enough instances, continuing anyway."
383 if len(instances) < 1:
384 raise qa_error.Error("Burnin needs at least one instance")
386 script = qa_utils.UploadFile(master["primary"], "../tools/burnin")
390 "--os=%s" % qa_config.get("os"),
391 "--disk-size=%s" % ",".join(qa_config.get("disk")),
392 "--disk-growth=%s" % ",".join(qa_config.get("disk-growth")),
393 "--disk-template=%s" % disk_template]
395 cmd.append("--parallel")
396 cmd.append("--early-release")
398 cmd.append("--http-check")
400 cmd.append("--rename=%s" % do_rename)
402 cmd.append("--no-reboot")
404 cmd.append("--reboot-types=%s" % ",".join(reboot_types))
405 cmd += [inst["name"] for inst in instances]
408 AssertCommand(["rm", "-f", script])
411 for inst in instances:
412 qa_config.ReleaseInstance(inst)
415 def TestClusterMasterFailover():
416 """gnt-cluster master-failover"""
417 master = qa_config.GetMasterNode()
418 failovermaster = qa_config.AcquireNode(exclude=master)
420 cmd = ["gnt-cluster", "master-failover"]
422 AssertCommand(cmd, node=failovermaster)
423 # Back to original master node
424 AssertCommand(cmd, node=master)
426 qa_config.ReleaseNode(failovermaster)
429 def TestClusterMasterFailoverWithDrainedQueue():
430 """gnt-cluster master-failover with drained queue"""
431 drain_check = ["test", "-f", constants.JOB_QUEUE_DRAIN_FILE]
433 master = qa_config.GetMasterNode()
434 failovermaster = qa_config.AcquireNode(exclude=master)
436 # Ensure queue is not drained
437 for node in [master, failovermaster]:
438 AssertCommand(drain_check, node=node, fail=True)
440 # Drain queue on failover master
441 AssertCommand(["touch", constants.JOB_QUEUE_DRAIN_FILE], node=failovermaster)
443 cmd = ["gnt-cluster", "master-failover"]
445 AssertCommand(drain_check, node=failovermaster)
446 AssertCommand(cmd, node=failovermaster)
447 AssertCommand(drain_check, fail=True)
448 AssertCommand(drain_check, node=failovermaster, fail=True)
450 # Back to original master node
451 AssertCommand(cmd, node=master)
453 qa_config.ReleaseNode(failovermaster)
455 AssertCommand(drain_check, fail=True)
456 AssertCommand(drain_check, node=failovermaster, fail=True)
459 def TestClusterCopyfile():
460 """gnt-cluster copyfile"""
461 master = qa_config.GetMasterNode()
463 uniqueid = utils.NewUUID()
465 # Create temporary file
466 f = tempfile.NamedTemporaryFile()
471 # Upload file to master node
472 testname = qa_utils.UploadFile(master["primary"], f.name)
474 # Copy file to all nodes
475 AssertCommand(["gnt-cluster", "copyfile", testname])
476 _CheckFileOnAllNodes(testname, uniqueid)
478 _RemoveFileFromAllNodes(testname)
481 def TestClusterCommand():
482 """gnt-cluster command"""
483 uniqueid = utils.NewUUID()
484 rfile = "/tmp/gnt%s" % utils.NewUUID()
485 rcmd = utils.ShellQuoteArgs(["echo", "-n", uniqueid])
486 cmd = utils.ShellQuoteArgs(["gnt-cluster", "command",
487 "%s >%s" % (rcmd, rfile)])
491 _CheckFileOnAllNodes(rfile, uniqueid)
493 _RemoveFileFromAllNodes(rfile)
496 def TestClusterDestroy():
497 """gnt-cluster destroy"""
498 AssertCommand(["gnt-cluster", "destroy", "--yes-do-it"])
501 def TestClusterRepairDiskSizes():
502 """gnt-cluster repair-disk-sizes"""
503 AssertCommand(["gnt-cluster", "repair-disk-sizes"])