4 # Copyright (C) 2007 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.
28 from ganeti import constants
29 from ganeti import utils
35 from qa_utils import AssertEqual, AssertNotEqual, StartSSH
38 def _RemoveFileFromAllNodes(filename):
39 """Removes a file from all nodes.
42 for node in qa_config.get('nodes'):
43 cmd = ['rm', '-f', filename]
44 AssertEqual(StartSSH(node['primary'],
45 utils.ShellQuoteArgs(cmd)).wait(), 0)
48 def _CheckFileOnAllNodes(filename, content):
49 """Verifies the content of the given file on all nodes.
52 cmd = utils.ShellQuoteArgs(["cat", filename])
53 for node in qa_config.get('nodes'):
54 AssertEqual(qa_utils.GetCommandOutput(node['primary'], cmd),
58 def TestClusterInit(rapi_user, rapi_secret):
59 """gnt-cluster init"""
60 master = qa_config.GetMasterNode()
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 cmd = ["mv", tmpru, constants.RAPI_USERS_FILE]
71 AssertEqual(StartSSH(master["primary"],
72 utils.ShellQuoteArgs(cmd)).wait(), 0)
74 cmd = ["rm", "-f", tmpru]
75 AssertEqual(StartSSH(master["primary"],
76 utils.ShellQuoteArgs(cmd)).wait(), 0)
81 cmd = ['gnt-cluster', 'init']
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'))
97 AssertEqual(StartSSH(master['primary'],
98 utils.ShellQuoteArgs(cmd)).wait(), 0)
101 def TestClusterRename():
102 """gnt-cluster rename"""
103 master = qa_config.GetMasterNode()
105 cmd = ['gnt-cluster', 'rename', '-f']
107 original_name = qa_config.get('name')
108 rename_target = qa_config.get('rename', None)
109 if rename_target is None:
110 print qa_utils.FormatError('"rename" entry is missing')
113 cmd_1 = cmd + [rename_target]
114 cmd_2 = cmd + [original_name]
116 cmd_verify = ['gnt-cluster', 'verify']
118 AssertEqual(StartSSH(master['primary'],
119 utils.ShellQuoteArgs(cmd_1)).wait(), 0)
121 AssertEqual(StartSSH(master['primary'],
122 utils.ShellQuoteArgs(cmd_verify)).wait(), 0)
124 AssertEqual(StartSSH(master['primary'],
125 utils.ShellQuoteArgs(cmd_2)).wait(), 0)
127 AssertEqual(StartSSH(master['primary'],
128 utils.ShellQuoteArgs(cmd_verify)).wait(), 0)
131 def TestClusterVerify():
132 """gnt-cluster verify"""
133 master = qa_config.GetMasterNode()
135 cmd = ['gnt-cluster', 'verify']
136 AssertEqual(StartSSH(master['primary'],
137 utils.ShellQuoteArgs(cmd)).wait(), 0)
140 def TestClusterInfo():
141 """gnt-cluster info"""
142 master = qa_config.GetMasterNode()
144 cmd = ['gnt-cluster', 'info']
145 AssertEqual(StartSSH(master['primary'],
146 utils.ShellQuoteArgs(cmd)).wait(), 0)
149 def TestClusterGetmaster():
150 """gnt-cluster getmaster"""
151 master = qa_config.GetMasterNode()
153 cmd = ['gnt-cluster', 'getmaster']
154 AssertEqual(StartSSH(master['primary'],
155 utils.ShellQuoteArgs(cmd)).wait(), 0)
158 def TestClusterVersion():
159 """gnt-cluster version"""
160 master = qa_config.GetMasterNode()
162 cmd = ['gnt-cluster', 'version']
163 AssertEqual(StartSSH(master['primary'],
164 utils.ShellQuoteArgs(cmd)).wait(), 0)
167 def TestClusterRenewCrypto():
168 """gnt-cluster renew-crypto"""
169 master = qa_config.GetMasterNode()
171 # Conflicting options
172 cmd = ["gnt-cluster", "renew-crypto", "--force",
173 "--new-cluster-certificate", "--new-confd-hmac-key"]
175 ["--new-rapi-certificate", "--rapi-certificate=/dev/null"],
176 ["--new-cluster-domain-secret", "--cluster-domain-secret=/dev/null"],
178 for i in conflicting:
179 AssertNotEqual(StartSSH(master["primary"],
180 utils.ShellQuoteArgs(cmd + i)).wait(), 0)
182 # Invalid RAPI certificate
183 cmd = ["gnt-cluster", "renew-crypto", "--force",
184 "--rapi-certificate=/dev/null"]
185 AssertNotEqual(StartSSH(master["primary"],
186 utils.ShellQuoteArgs(cmd)).wait(), 0)
188 # Custom RAPI certificate
189 fh = tempfile.NamedTemporaryFile()
191 # Ensure certificate doesn't cause "gnt-cluster verify" to complain
192 validity = constants.SSL_CERT_EXPIRATION_WARN * 3
194 utils.GenerateSelfSignedSslCert(fh.name, validity=validity)
196 tmpcert = qa_utils.UploadFile(master["primary"], fh.name)
198 cmd = ["gnt-cluster", "renew-crypto", "--force",
199 "--rapi-certificate=%s" % tmpcert]
200 AssertEqual(StartSSH(master["primary"],
201 utils.ShellQuoteArgs(cmd)).wait(), 0)
203 cmd = ["rm", "-f", tmpcert]
204 AssertEqual(StartSSH(master["primary"],
205 utils.ShellQuoteArgs(cmd)).wait(), 0)
207 # Custom cluster domain secret
208 cds_fh = tempfile.NamedTemporaryFile()
209 cds_fh.write(utils.GenerateSecret())
213 tmpcds = qa_utils.UploadFile(master["primary"], cds_fh.name)
215 cmd = ["gnt-cluster", "renew-crypto", "--force",
216 "--cluster-domain-secret=%s" % tmpcds]
217 AssertEqual(StartSSH(master["primary"],
218 utils.ShellQuoteArgs(cmd)).wait(), 0)
220 cmd = ["rm", "-f", tmpcds]
221 AssertEqual(StartSSH(master["primary"],
222 utils.ShellQuoteArgs(cmd)).wait(), 0)
225 cmd = ["gnt-cluster", "renew-crypto", "--force",
226 "--new-cluster-certificate", "--new-confd-hmac-key",
227 "--new-rapi-certificate", "--new-cluster-domain-secret"]
228 AssertEqual(StartSSH(master["primary"],
229 utils.ShellQuoteArgs(cmd)).wait(), 0)
232 def TestClusterBurnin():
234 master = qa_config.GetMasterNode()
236 options = qa_config.get('options', {})
237 disk_template = options.get('burnin-disk-template', 'drbd')
238 parallel = options.get('burnin-in-parallel', False)
239 check_inst = options.get('burnin-check-instances', False)
240 do_rename = options.get('burnin-rename', '')
242 # Get as many instances as we need
246 num = qa_config.get('options', {}).get('burnin-instances', 1)
247 for _ in range(0, num):
248 instances.append(qa_config.AcquireInstance())
249 except qa_error.OutOfInstancesError:
250 print "Not enough instances, continuing anyway."
252 if len(instances) < 1:
253 raise qa_error.Error("Burnin needs at least one instance")
255 script = qa_utils.UploadFile(master['primary'], '../tools/burnin')
259 '--os=%s' % qa_config.get('os'),
260 '--disk-size=%s' % ",".join(qa_config.get('disk')),
261 '--disk-growth=%s' % ",".join(qa_config.get('disk-growth')),
262 '--disk-template=%s' % disk_template]
264 cmd.append('--parallel')
265 cmd.append('--early-release')
267 cmd.append('--http-check')
269 cmd.append('--rename=%s' % do_rename)
270 cmd += [inst['name'] for inst in instances]
271 AssertEqual(StartSSH(master['primary'],
272 utils.ShellQuoteArgs(cmd)).wait(), 0)
274 cmd = ['rm', '-f', script]
275 AssertEqual(StartSSH(master['primary'],
276 utils.ShellQuoteArgs(cmd)).wait(), 0)
278 for inst in instances:
279 qa_config.ReleaseInstance(inst)
282 def TestClusterMasterFailover():
283 """gnt-cluster masterfailover"""
284 master = qa_config.GetMasterNode()
286 failovermaster = qa_config.AcquireNode(exclude=master)
288 cmd = ['gnt-cluster', 'masterfailover']
289 AssertEqual(StartSSH(failovermaster['primary'],
290 utils.ShellQuoteArgs(cmd)).wait(), 0)
292 cmd = ['gnt-cluster', 'masterfailover']
293 AssertEqual(StartSSH(master['primary'],
294 utils.ShellQuoteArgs(cmd)).wait(), 0)
296 qa_config.ReleaseNode(failovermaster)
299 def TestClusterCopyfile():
300 """gnt-cluster copyfile"""
301 master = qa_config.GetMasterNode()
303 uniqueid = utils.NewUUID()
305 # Create temporary file
306 f = tempfile.NamedTemporaryFile()
311 # Upload file to master node
312 testname = qa_utils.UploadFile(master['primary'], f.name)
314 # Copy file to all nodes
315 cmd = ['gnt-cluster', 'copyfile', testname]
316 AssertEqual(StartSSH(master['primary'],
317 utils.ShellQuoteArgs(cmd)).wait(), 0)
318 _CheckFileOnAllNodes(testname, uniqueid)
320 _RemoveFileFromAllNodes(testname)
323 def TestClusterCommand():
324 """gnt-cluster command"""
325 master = qa_config.GetMasterNode()
327 uniqueid = utils.NewUUID()
328 rfile = "/tmp/gnt%s" % utils.NewUUID()
329 rcmd = utils.ShellQuoteArgs(['echo', '-n', uniqueid])
330 cmd = utils.ShellQuoteArgs(['gnt-cluster', 'command',
331 "%s >%s" % (rcmd, rfile)])
334 AssertEqual(StartSSH(master['primary'], cmd).wait(), 0)
335 _CheckFileOnAllNodes(rfile, uniqueid)
337 _RemoveFileFromAllNodes(rfile)
340 def TestClusterDestroy():
341 """gnt-cluster destroy"""
342 master = qa_config.GetMasterNode()
344 cmd = ['gnt-cluster', 'destroy', '--yes-do-it']
345 AssertEqual(StartSSH(master['primary'],
346 utils.ShellQuoteArgs(cmd)).wait(), 0)