4 # Copyright (C) 2007, 2010 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 utils
36 from qa_utils import AssertEqual, AssertCommand
39 def _RemoveFileFromAllNodes(filename):
40 """Removes a file from all nodes.
43 for node in qa_config.get("nodes"):
44 AssertCommand(["rm", "-f", filename], node=node)
47 def _CheckFileOnAllNodes(filename, content):
48 """Verifies the content of the given file on all nodes.
51 cmd = utils.ShellQuoteArgs(["cat", filename])
52 for node in qa_config.get("nodes"):
53 AssertEqual(qa_utils.GetCommandOutput(node["primary"], cmd), content)
56 def TestClusterInit(rapi_user, rapi_secret):
57 """gnt-cluster init"""
58 master = qa_config.GetMasterNode()
60 rapi_dir = os.path.dirname(constants.RAPI_USERS_FILE)
62 # First create the RAPI credentials
63 fh = tempfile.NamedTemporaryFile()
65 fh.write("%s %s write\n" % (rapi_user, rapi_secret))
68 tmpru = qa_utils.UploadFile(master["primary"], fh.name)
70 AssertCommand(["mkdir", "-p", rapi_dir])
71 AssertCommand(["mv", tmpru, constants.RAPI_USERS_FILE])
73 AssertCommand(["rm", "-f", tmpru])
78 cmd = ['gnt-cluster', 'init']
80 cmd.append("--primary-ip-version=%d" %
81 qa_config.get("primary_ip_version", 4))
83 if master.get('secondary', None):
84 cmd.append('--secondary-ip=%s' % master['secondary'])
86 bridge = qa_config.get('bridge', None)
88 cmd.append('--bridge=%s' % bridge)
89 cmd.append('--master-netdev=%s' % bridge)
91 htype = qa_config.get('enabled-hypervisors', None)
93 cmd.append('--enabled-hypervisors=%s' % htype)
95 cmd.append(qa_config.get('name'))
100 def TestClusterRename():
101 """gnt-cluster rename"""
102 cmd = ['gnt-cluster', 'rename', '-f']
104 original_name = qa_config.get('name')
105 rename_target = qa_config.get('rename', None)
106 if rename_target is None:
107 print qa_utils.FormatError('"rename" entry is missing')
110 cmd_verify = ['gnt-cluster', 'verify']
113 cmd + [rename_target],
115 cmd + [original_name],
121 def TestClusterVerify():
122 """gnt-cluster verify"""
123 AssertCommand(["gnt-cluster", "verify"])
127 """gnt-debug test-jobqueue"""
128 AssertCommand(["gnt-debug", "test-jobqueue"])
131 def TestClusterReservedLvs():
132 """gnt-cluster reserved lvs"""
133 CVERIFY = ["gnt-cluster", "verify"]
136 (False, ["gnt-cluster", "modify", "--reserved-lvs", ""]),
137 (False, ["lvcreate", "-L1G", "-nqa-test", "xenvg"]),
139 (False, ["gnt-cluster", "modify", "--reserved-lvs", "qa-test,other-test"]),
141 (False, ["gnt-cluster", "modify", "--reserved-lvs", "qa-.*"]),
143 (False, ["gnt-cluster", "modify", "--reserved-lvs", ""]),
145 (False, ["lvremove", "-f", "xenvg/qa-test"]),
148 AssertCommand(cmd, fail=fail)
151 def TestClusterModifyBe():
152 """gnt-cluster modify -B"""
155 (False, ["gnt-cluster", "modify", "-B", "memory=256"]),
156 (False, ["sh", "-c", "gnt-cluster info|grep '^ *memory: 256$'"]),
157 (True, ["gnt-cluster", "modify", "-B", "memory=a"]),
158 (False, ["gnt-cluster", "modify", "-B", "memory=128"]),
159 (False, ["sh", "-c", "gnt-cluster info|grep '^ *memory: 128$'"]),
161 (False, ["gnt-cluster", "modify", "-B", "vcpus=4"]),
162 (False, ["sh", "-c", "gnt-cluster info|grep '^ *vcpus: 4$'"]),
163 (True, ["gnt-cluster", "modify", "-B", "vcpus=a"]),
164 (False, ["gnt-cluster", "modify", "-B", "vcpus=1"]),
165 (False, ["sh", "-c", "gnt-cluster info|grep '^ *vcpus: 1$'"]),
167 (False, ["gnt-cluster", "modify", "-B", "auto_balance=False"]),
168 (False, ["sh", "-c", "gnt-cluster info|grep '^ *auto_balance: False$'"]),
169 (True, ["gnt-cluster", "modify", "-B", "auto_balance=1"]),
170 (False, ["gnt-cluster", "modify", "-B", "auto_balance=True"]),
171 (False, ["sh", "-c", "gnt-cluster info|grep '^ *auto_balance: True$'"]),
173 AssertCommand(cmd, fail=fail)
176 def TestClusterInfo():
177 """gnt-cluster info"""
178 AssertCommand(["gnt-cluster", "info"])
181 def TestClusterGetmaster():
182 """gnt-cluster getmaster"""
183 AssertCommand(["gnt-cluster", "getmaster"])
186 def TestClusterVersion():
187 """gnt-cluster version"""
188 AssertCommand(["gnt-cluster", "version"])
191 def TestClusterRenewCrypto():
192 """gnt-cluster renew-crypto"""
193 master = qa_config.GetMasterNode()
195 # Conflicting options
196 cmd = ["gnt-cluster", "renew-crypto", "--force",
197 "--new-cluster-certificate", "--new-confd-hmac-key"]
199 ["--new-rapi-certificate", "--rapi-certificate=/dev/null"],
200 ["--new-cluster-domain-secret", "--cluster-domain-secret=/dev/null"],
202 for i in conflicting:
203 AssertCommand(cmd+i, fail=True)
205 # Invalid RAPI certificate
206 cmd = ["gnt-cluster", "renew-crypto", "--force",
207 "--rapi-certificate=/dev/null"]
208 AssertCommand(cmd, fail=True)
210 rapi_cert_backup = qa_utils.BackupFile(master["primary"],
211 constants.RAPI_CERT_FILE)
213 # Custom RAPI certificate
214 fh = tempfile.NamedTemporaryFile()
216 # Ensure certificate doesn't cause "gnt-cluster verify" to complain
217 validity = constants.SSL_CERT_EXPIRATION_WARN * 3
219 utils.GenerateSelfSignedSslCert(fh.name, validity=validity)
221 tmpcert = qa_utils.UploadFile(master["primary"], fh.name)
223 AssertCommand(["gnt-cluster", "renew-crypto", "--force",
224 "--rapi-certificate=%s" % tmpcert])
226 AssertCommand(["rm", "-f", tmpcert])
228 # Custom cluster domain secret
229 cds_fh = tempfile.NamedTemporaryFile()
230 cds_fh.write(utils.GenerateSecret())
234 tmpcds = qa_utils.UploadFile(master["primary"], cds_fh.name)
236 AssertCommand(["gnt-cluster", "renew-crypto", "--force",
237 "--cluster-domain-secret=%s" % tmpcds])
239 AssertCommand(["rm", "-f", tmpcds])
242 AssertCommand(["gnt-cluster", "renew-crypto", "--force",
243 "--new-cluster-certificate", "--new-confd-hmac-key",
244 "--new-rapi-certificate", "--new-cluster-domain-secret"])
246 # Restore RAPI certificate
247 AssertCommand(["gnt-cluster", "renew-crypto", "--force",
248 "--rapi-certificate=%s" % rapi_cert_backup])
250 AssertCommand(["rm", "-f", rapi_cert_backup])
253 def TestClusterBurnin():
255 master = qa_config.GetMasterNode()
257 options = qa_config.get('options', {})
258 disk_template = options.get('burnin-disk-template', 'drbd')
259 parallel = options.get('burnin-in-parallel', False)
260 check_inst = options.get('burnin-check-instances', False)
261 do_rename = options.get('burnin-rename', '')
262 do_reboot = options.get('burnin-reboot', True)
263 reboot_types = options.get("reboot-types", constants.REBOOT_TYPES)
265 # Get as many instances as we need
269 num = qa_config.get('options', {}).get('burnin-instances', 1)
270 for _ in range(0, num):
271 instances.append(qa_config.AcquireInstance())
272 except qa_error.OutOfInstancesError:
273 print "Not enough instances, continuing anyway."
275 if len(instances) < 1:
276 raise qa_error.Error("Burnin needs at least one instance")
278 script = qa_utils.UploadFile(master['primary'], '../tools/burnin')
282 '--os=%s' % qa_config.get('os'),
283 '--disk-size=%s' % ",".join(qa_config.get('disk')),
284 '--disk-growth=%s' % ",".join(qa_config.get('disk-growth')),
285 '--disk-template=%s' % disk_template]
287 cmd.append('--parallel')
288 cmd.append('--early-release')
290 cmd.append('--http-check')
292 cmd.append('--rename=%s' % do_rename)
294 cmd.append('--no-reboot')
296 cmd.append('--reboot-types=%s' % ",".join(reboot_types))
297 cmd += [inst['name'] for inst in instances]
300 AssertCommand(["rm", "-f", script])
303 for inst in instances:
304 qa_config.ReleaseInstance(inst)
307 def TestClusterMasterFailover():
308 """gnt-cluster master-failover"""
309 master = qa_config.GetMasterNode()
310 failovermaster = qa_config.AcquireNode(exclude=master)
312 cmd = ["gnt-cluster", "master-failover"]
314 AssertCommand(cmd, node=failovermaster)
315 AssertCommand(cmd, node=master)
317 qa_config.ReleaseNode(failovermaster)
320 def TestClusterCopyfile():
321 """gnt-cluster copyfile"""
322 master = qa_config.GetMasterNode()
324 uniqueid = utils.NewUUID()
326 # Create temporary file
327 f = tempfile.NamedTemporaryFile()
332 # Upload file to master node
333 testname = qa_utils.UploadFile(master['primary'], f.name)
335 # Copy file to all nodes
336 AssertCommand(["gnt-cluster", "copyfile", testname])
337 _CheckFileOnAllNodes(testname, uniqueid)
339 _RemoveFileFromAllNodes(testname)
342 def TestClusterCommand():
343 """gnt-cluster command"""
344 uniqueid = utils.NewUUID()
345 rfile = "/tmp/gnt%s" % utils.NewUUID()
346 rcmd = utils.ShellQuoteArgs(['echo', '-n', uniqueid])
347 cmd = utils.ShellQuoteArgs(['gnt-cluster', 'command',
348 "%s >%s" % (rcmd, rfile)])
352 _CheckFileOnAllNodes(rfile, uniqueid)
354 _RemoveFileFromAllNodes(rfile)
357 def TestClusterDestroy():
358 """gnt-cluster destroy"""
359 AssertCommand(["gnt-cluster", "destroy", "--yes-do-it"])