Export backend.GetMasterInfo over the rpc layer
[ganeti-local] / qa / qa_cluster.py
1 #
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 """Cluster related QA tests.
23
24 """
25
26 import tempfile
27
28 from ganeti import utils
29
30 import qa_config
31 import qa_utils
32 import qa_error
33
34 from qa_utils import AssertEqual, StartSSH
35
36
37 def _RemoveFileFromAllNodes(filename):
38   """Removes a file from all nodes.
39
40   """
41   for node in qa_config.get('nodes'):
42     cmd = ['rm', '-f', filename]
43     AssertEqual(StartSSH(node['primary'],
44                          utils.ShellQuoteArgs(cmd)).wait(), 0)
45
46
47 def _CheckFileOnAllNodes(filename, content):
48   """Verifies the content of the given file on all nodes.
49
50   """
51   cmd = utils.ShellQuoteArgs(["cat", filename])
52   for node in qa_config.get('nodes'):
53     AssertEqual(qa_utils.GetCommandOutput(node['primary'], cmd),
54                 content)
55
56
57 def TestClusterInit():
58   """gnt-cluster init"""
59   master = qa_config.GetMasterNode()
60
61   cmd = ['gnt-cluster', 'init']
62
63   if master.get('secondary', None):
64     cmd.append('--secondary-ip=%s' % master['secondary'])
65
66   bridge = qa_config.get('bridge', None)
67   if bridge:
68     cmd.append('--bridge=%s' % bridge)
69     cmd.append('--master-netdev=%s' % bridge)
70
71   htype = qa_config.get('hypervisor-type', None)
72   if htype:
73     cmd.append('--hypervisor-type=%s' % htype)
74
75   cmd.append(qa_config.get('name'))
76
77   AssertEqual(StartSSH(master['primary'],
78                        utils.ShellQuoteArgs(cmd)).wait(), 0)
79
80
81 def TestClusterRename():
82   """gnt-cluster rename"""
83   master = qa_config.GetMasterNode()
84
85   cmd = ['gnt-cluster', 'rename', '-f']
86
87   original_name = qa_config.get('name')
88   rename_target = qa_config.get('rename', None)
89   if rename_target is None:
90     print qa_utils.FormatError('"rename" entry is missing')
91     return
92
93   cmd_1 = cmd + [rename_target]
94   cmd_2 = cmd + [original_name]
95
96   cmd_verify = ['gnt-cluster', 'verify']
97
98   AssertEqual(StartSSH(master['primary'],
99                        utils.ShellQuoteArgs(cmd_1)).wait(), 0)
100
101   AssertEqual(StartSSH(master['primary'],
102                        utils.ShellQuoteArgs(cmd_verify)).wait(), 0)
103
104   AssertEqual(StartSSH(master['primary'],
105                        utils.ShellQuoteArgs(cmd_2)).wait(), 0)
106
107   AssertEqual(StartSSH(master['primary'],
108                        utils.ShellQuoteArgs(cmd_verify)).wait(), 0)
109
110
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 def TestClusterInfo():
121   """gnt-cluster info"""
122   master = qa_config.GetMasterNode()
123
124   cmd = ['gnt-cluster', 'info']
125   AssertEqual(StartSSH(master['primary'],
126                        utils.ShellQuoteArgs(cmd)).wait(), 0)
127
128
129 def TestClusterGetmaster():
130   """gnt-cluster getmaster"""
131   master = qa_config.GetMasterNode()
132
133   cmd = ['gnt-cluster', 'getmaster']
134   AssertEqual(StartSSH(master['primary'],
135                        utils.ShellQuoteArgs(cmd)).wait(), 0)
136
137
138 def TestClusterVersion():
139   """gnt-cluster version"""
140   master = qa_config.GetMasterNode()
141
142   cmd = ['gnt-cluster', 'version']
143   AssertEqual(StartSSH(master['primary'],
144                        utils.ShellQuoteArgs(cmd)).wait(), 0)
145
146
147 def TestClusterBurnin():
148   """Burnin"""
149   master = qa_config.GetMasterNode()
150
151   disk_template = (qa_config.get('options', {}).
152                    get('burnin-disk-template', 'drbd'))
153
154   # Get as many instances as we need
155   instances = []
156   try:
157     try:
158       num = qa_config.get('options', {}).get('burnin-instances', 1)
159       for _ in xrange(0, num):
160         instances.append(qa_config.AcquireInstance())
161     except qa_error.OutOfInstancesError:
162       print "Not enough instances, continuing anyway."
163
164     if len(instances) < 1:
165       raise qa_error.Error("Burnin needs at least one instance")
166
167     script = qa_utils.UploadFile(master['primary'], '../tools/burnin')
168     try:
169       # Run burnin
170       cmd = [script,
171              '--os=%s' % qa_config.get('os'),
172              '--os-size=%s' % qa_config.get('os-size'),
173              '--swap-size=%s' % qa_config.get('swap-size'),
174              '--disk-template=%s' % disk_template]
175       cmd += [inst['name'] for inst in instances]
176       AssertEqual(StartSSH(master['primary'],
177                            utils.ShellQuoteArgs(cmd)).wait(), 0)
178     finally:
179       cmd = ['rm', '-f', script]
180       AssertEqual(StartSSH(master['primary'],
181                            utils.ShellQuoteArgs(cmd)).wait(), 0)
182   finally:
183     for inst in instances:
184       qa_config.ReleaseInstance(inst)
185
186
187 def TestClusterMasterFailover():
188   """gnt-cluster masterfailover"""
189   master = qa_config.GetMasterNode()
190
191   failovermaster = qa_config.AcquireNode(exclude=master)
192   try:
193     cmd = ['gnt-cluster', 'masterfailover']
194     AssertEqual(StartSSH(failovermaster['primary'],
195                          utils.ShellQuoteArgs(cmd)).wait(), 0)
196
197     cmd = ['gnt-cluster', 'masterfailover']
198     AssertEqual(StartSSH(master['primary'],
199                          utils.ShellQuoteArgs(cmd)).wait(), 0)
200   finally:
201     qa_config.ReleaseNode(failovermaster)
202
203
204 def TestClusterCopyfile():
205   """gnt-cluster copyfile"""
206   master = qa_config.GetMasterNode()
207
208   uniqueid = utils.NewUUID()
209
210   # Create temporary file
211   f = tempfile.NamedTemporaryFile()
212   f.write(uniqueid)
213   f.flush()
214   f.seek(0)
215
216   # Upload file to master node
217   testname = qa_utils.UploadFile(master['primary'], f.name)
218   try:
219     # Copy file to all nodes
220     cmd = ['gnt-cluster', 'copyfile', testname]
221     AssertEqual(StartSSH(master['primary'],
222                          utils.ShellQuoteArgs(cmd)).wait(), 0)
223     _CheckFileOnAllNodes(testname, uniqueid)
224   finally:
225     _RemoveFileFromAllNodes(testname)
226
227
228 def TestClusterCommand():
229   """gnt-cluster command"""
230   master = qa_config.GetMasterNode()
231
232   uniqueid = utils.NewUUID()
233   rfile = "/tmp/gnt%s" % utils.NewUUID()
234   rcmd = utils.ShellQuoteArgs(['echo', '-n', uniqueid])
235   cmd = utils.ShellQuoteArgs(['gnt-cluster', 'command',
236                               "%s >%s" % (rcmd, rfile)])
237
238   try:
239     AssertEqual(StartSSH(master['primary'], cmd).wait(), 0)
240     _CheckFileOnAllNodes(rfile, uniqueid)
241   finally:
242     _RemoveFileFromAllNodes(rfile)
243
244
245 def TestClusterDestroy():
246   """gnt-cluster destroy"""
247   master = qa_config.GetMasterNode()
248
249   cmd = ['gnt-cluster', 'destroy', '--yes-do-it']
250   AssertEqual(StartSSH(master['primary'],
251                        utils.ShellQuoteArgs(cmd)).wait(), 0)