Change backend._OSSearch return values
[ganeti-local] / qa / qa_cluster.py
1 # Copyright (C) 2007 Google Inc.
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 # 02110-1301, USA.
17
18
19 """Cluster related QA tests.
20
21 """
22
23 import tempfile
24
25 from ganeti import utils
26
27 import qa_config
28 import qa_utils
29 import qa_error
30
31 from qa_utils import AssertEqual, StartSSH
32
33
34 def _RemoveFileFromAllNodes(filename):
35   """Removes a file from all nodes.
36
37   """
38   for node in qa_config.get('nodes'):
39     cmd = ['rm', '-f', filename]
40     AssertEqual(StartSSH(node['primary'],
41                          utils.ShellQuoteArgs(cmd)).wait(), 0)
42
43
44 def _CheckFileOnAllNodes(filename, content):
45   """Verifies the content of the given file on all nodes.
46
47   """
48   cmd = utils.ShellQuoteArgs(["cat", filename])
49   for node in qa_config.get('nodes'):
50     AssertEqual(qa_utils.GetCommandOutput(node['primary'], cmd),
51                 content)
52
53
54 @qa_utils.DefineHook('cluster-init')
55 def TestClusterInit():
56   """gnt-cluster init"""
57   master = qa_config.GetMasterNode()
58
59   cmd = ['gnt-cluster', 'init']
60
61   if master.get('secondary', None):
62     cmd.append('--secondary-ip=%s' % master['secondary'])
63
64   bridge = qa_config.get('bridge', None)
65   if bridge:
66     cmd.append('--bridge=%s' % bridge)
67     cmd.append('--master-netdev=%s' % bridge)
68
69   htype = qa_config.get('hypervisor-type', None)
70   if htype:
71     cmd.append('--hypervisor-type=%s' % htype)
72
73   cmd.append(qa_config.get('name'))
74
75   AssertEqual(StartSSH(master['primary'],
76                        utils.ShellQuoteArgs(cmd)).wait(), 0)
77
78
79 @qa_utils.DefineHook('cluster-rename')
80 def TestClusterRename():
81   """gnt-cluster rename"""
82   master = qa_config.GetMasterNode()
83
84   cmd = ['gnt-cluster', 'rename', '-f']
85
86   original_name = qa_config.get('name')
87   rename_target = qa_config.get('rename', None)
88   if rename_target is None:
89     print qa_utils.FormatError('"rename" entry is missing')
90     return
91
92   cmd_1 = cmd + [rename_target]
93   cmd_2 = cmd + [original_name]
94
95   cmd_verify = ['gnt-cluster', 'verify']
96
97   AssertEqual(StartSSH(master['primary'],
98                        utils.ShellQuoteArgs(cmd_1)).wait(), 0)
99
100   AssertEqual(StartSSH(master['primary'],
101                        utils.ShellQuoteArgs(cmd_verify)).wait(), 0)
102
103   AssertEqual(StartSSH(master['primary'],
104                        utils.ShellQuoteArgs(cmd_2)).wait(), 0)
105
106   AssertEqual(StartSSH(master['primary'],
107                        utils.ShellQuoteArgs(cmd_verify)).wait(), 0)
108
109
110 @qa_utils.DefineHook('cluster-verify')
111 def TestClusterVerify():
112   """gnt-cluster verify"""
113   master = qa_config.GetMasterNode()
114
115   cmd = ['gnt-cluster', 'verify']
116   AssertEqual(StartSSH(master['primary'],
117                        utils.ShellQuoteArgs(cmd)).wait(), 0)
118
119
120 @qa_utils.DefineHook('cluster-info')
121 def TestClusterInfo():
122   """gnt-cluster info"""
123   master = qa_config.GetMasterNode()
124
125   cmd = ['gnt-cluster', 'info']
126   AssertEqual(StartSSH(master['primary'],
127                        utils.ShellQuoteArgs(cmd)).wait(), 0)
128
129
130 @qa_utils.DefineHook('cluster-getmaster')
131 def TestClusterGetmaster():
132   """gnt-cluster getmaster"""
133   master = qa_config.GetMasterNode()
134
135   cmd = ['gnt-cluster', 'getmaster']
136   AssertEqual(StartSSH(master['primary'],
137                        utils.ShellQuoteArgs(cmd)).wait(), 0)
138
139
140 @qa_utils.DefineHook('cluster-version')
141 def TestClusterVersion():
142   """gnt-cluster version"""
143   master = qa_config.GetMasterNode()
144
145   cmd = ['gnt-cluster', 'version']
146   AssertEqual(StartSSH(master['primary'],
147                        utils.ShellQuoteArgs(cmd)).wait(), 0)
148
149
150 @qa_utils.DefineHook('cluster-burnin')
151 def TestClusterBurnin():
152   """Burnin"""
153   master = qa_config.GetMasterNode()
154
155   disk_template = (qa_config.get('options', {}).
156                    get('burnin-disk-template', 'remote_raid1'))
157
158   # Get as many instances as we need
159   instances = []
160   try:
161     try:
162       num = qa_config.get('options', {}).get('burnin-instances', 1)
163       for _ in xrange(0, num):
164         instances.append(qa_config.AcquireInstance())
165     except qa_error.OutOfInstancesError:
166       print "Not enough instances, continuing anyway."
167
168     if len(instances) < 1:
169       raise qa_error.Error("Burnin needs at least one instance")
170
171     script = qa_utils.UploadFile(master['primary'], '../tools/burnin')
172     try:
173       # Run burnin
174       cmd = [script,
175              '--os=%s' % qa_config.get('os'),
176              '--os-size=%s' % qa_config.get('os-size'),
177              '--swap-size=%s' % qa_config.get('swap-size'),
178              '--disk-template=%s' % disk_template]
179       cmd += [inst['name'] for inst in instances]
180       AssertEqual(StartSSH(master['primary'],
181                            utils.ShellQuoteArgs(cmd)).wait(), 0)
182     finally:
183       cmd = ['rm', '-f', script]
184       AssertEqual(StartSSH(master['primary'],
185                            utils.ShellQuoteArgs(cmd)).wait(), 0)
186   finally:
187     for inst in instances:
188       qa_config.ReleaseInstance(inst)
189
190
191 @qa_utils.DefineHook('cluster-master-failover')
192 def TestClusterMasterFailover():
193   """gnt-cluster masterfailover"""
194   master = qa_config.GetMasterNode()
195
196   failovermaster = qa_config.AcquireNode(exclude=master)
197   try:
198     cmd = ['gnt-cluster', 'masterfailover']
199     AssertEqual(StartSSH(failovermaster['primary'],
200                          utils.ShellQuoteArgs(cmd)).wait(), 0)
201
202     cmd = ['gnt-cluster', 'masterfailover']
203     AssertEqual(StartSSH(master['primary'],
204                          utils.ShellQuoteArgs(cmd)).wait(), 0)
205   finally:
206     qa_config.ReleaseNode(failovermaster)
207
208
209 @qa_utils.DefineHook('cluster-copyfile')
210 def TestClusterCopyfile():
211   """gnt-cluster copyfile"""
212   master = qa_config.GetMasterNode()
213
214   uniqueid = utils.NewUUID()
215
216   # Create temporary file
217   f = tempfile.NamedTemporaryFile()
218   f.write(uniqueid)
219   f.flush()
220   f.seek(0)
221
222   # Upload file to master node
223   testname = qa_utils.UploadFile(master['primary'], f.name)
224   try:
225     # Copy file to all nodes
226     cmd = ['gnt-cluster', 'copyfile', testname]
227     AssertEqual(StartSSH(master['primary'],
228                          utils.ShellQuoteArgs(cmd)).wait(), 0)
229     _CheckFileOnAllNodes(testname, uniqueid)
230   finally:
231     _RemoveFileFromAllNodes(testname)
232
233
234 @qa_utils.DefineHook('cluster-command')
235 def TestClusterCommand():
236   """gnt-cluster command"""
237   master = qa_config.GetMasterNode()
238
239   uniqueid = utils.NewUUID()
240   rfile = "/tmp/gnt%s" % utils.NewUUID()
241   rcmd = utils.ShellQuoteArgs(['echo', '-n', uniqueid])
242   cmd = utils.ShellQuoteArgs(['gnt-cluster', 'command',
243                               "%s >%s" % (rcmd, rfile)])
244
245   try:
246     AssertEqual(StartSSH(master['primary'], cmd).wait(), 0)
247     _CheckFileOnAllNodes(rfile, uniqueid)
248   finally:
249     _RemoveFileFromAllNodes(rfile)
250
251
252 @qa_utils.DefineHook('cluster-destroy')
253 def TestClusterDestroy():
254   """gnt-cluster destroy"""
255   master = qa_config.GetMasterNode()
256
257   cmd = ['gnt-cluster', 'destroy', '--yes-do-it']
258   AssertEqual(StartSSH(master['primary'],
259                        utils.ShellQuoteArgs(cmd)).wait(), 0)