Test “gnt-cluster command”.
[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 def TestClusterInit():
55   """gnt-cluster init"""
56   master = qa_config.GetMasterNode()
57
58   cmd = ['gnt-cluster', 'init']
59
60   if master.get('secondary', None):
61     cmd.append('--secondary-ip=%s' % master['secondary'])
62
63   bridge = qa_config.get('bridge', None)
64   if bridge:
65     cmd.append('--bridge=%s' % bridge)
66     cmd.append('--master-netdev=%s' % bridge)
67
68   cmd.append(qa_config.get('name'))
69
70   AssertEqual(StartSSH(master['primary'],
71                        utils.ShellQuoteArgs(cmd)).wait(), 0)
72
73
74 def TestClusterVerify():
75   """gnt-cluster verify"""
76   master = qa_config.GetMasterNode()
77
78   cmd = ['gnt-cluster', 'verify']
79   AssertEqual(StartSSH(master['primary'],
80                        utils.ShellQuoteArgs(cmd)).wait(), 0)
81
82
83 def TestClusterInfo():
84   """gnt-cluster info"""
85   master = qa_config.GetMasterNode()
86
87   cmd = ['gnt-cluster', 'info']
88   AssertEqual(StartSSH(master['primary'],
89                        utils.ShellQuoteArgs(cmd)).wait(), 0)
90
91
92 def TestClusterGetmaster():
93   """gnt-cluster getmaster"""
94   master = qa_config.GetMasterNode()
95
96   cmd = ['gnt-cluster', 'getmaster']
97   AssertEqual(StartSSH(master['primary'],
98                        utils.ShellQuoteArgs(cmd)).wait(), 0)
99
100
101 def TestClusterVersion():
102   """gnt-cluster version"""
103   master = qa_config.GetMasterNode()
104
105   cmd = ['gnt-cluster', 'version']
106   AssertEqual(StartSSH(master['primary'],
107                        utils.ShellQuoteArgs(cmd)).wait(), 0)
108
109
110 def TestClusterBurnin():
111   """Burnin"""
112   master = qa_config.GetMasterNode()
113
114   # Get as many instances as we need
115   instances = []
116   try:
117     num = qa_config.get('options', {}).get('burnin-instances', 1)
118     for _ in xrange(0, num):
119       instances.append(qa_config.AcquireInstance())
120   except qa_error.OutOfInstancesError:
121     print "Not enough instances, continuing anyway."
122
123   if len(instances) < 1:
124     raise qa_error.Error("Burnin needs at least one instance")
125
126   # Run burnin
127   try:
128     script = qa_utils.UploadFile(master['primary'], '../tools/burnin')
129     try:
130       cmd = [script,
131              '--os=%s' % qa_config.get('os'),
132              '--os-size=%s' % qa_config.get('os-size'),
133              '--swap-size=%s' % qa_config.get('swap-size')]
134       cmd += [inst['name'] for inst in instances]
135       AssertEqual(StartSSH(master['primary'],
136                            utils.ShellQuoteArgs(cmd)).wait(), 0)
137     finally:
138       cmd = ['rm', '-f', script]
139       AssertEqual(StartSSH(master['primary'],
140                            utils.ShellQuoteArgs(cmd)).wait(), 0)
141   finally:
142     for inst in instances:
143       qa_config.ReleaseInstance(inst)
144
145
146 def TestClusterMasterFailover():
147   """gnt-cluster masterfailover"""
148   master = qa_config.GetMasterNode()
149
150   failovermaster = qa_config.AcquireNode(exclude=master)
151   try:
152     cmd = ['gnt-cluster', 'masterfailover']
153     AssertEqual(StartSSH(failovermaster['primary'],
154                          utils.ShellQuoteArgs(cmd)).wait(), 0)
155
156     cmd = ['gnt-cluster', 'masterfailover']
157     AssertEqual(StartSSH(master['primary'],
158                          utils.ShellQuoteArgs(cmd)).wait(), 0)
159   finally:
160     qa_config.ReleaseNode(failovermaster)
161
162
163 def TestClusterCopyfile():
164   """gnt-cluster copyfile"""
165   master = qa_config.GetMasterNode()
166
167   uniqueid = utils.GetUUID()
168
169   # Create temporary file
170   f = tempfile.NamedTemporaryFile()
171   f.write(uniqueid)
172   f.flush()
173   f.seek(0)
174
175   # Upload file to master node
176   testname = qa_utils.UploadFile(master['primary'], f.name)
177   try:
178     # Copy file to all nodes
179     cmd = ['gnt-cluster', 'copyfile', testname]
180     AssertEqual(StartSSH(master['primary'],
181                          utils.ShellQuoteArgs(cmd)).wait(), 0)
182     _CheckFileOnAllNodes(testname, uniqueid)
183   finally:
184     _RemoveFileFromAllNodes(testname)
185
186
187 def TestClusterCommand():
188   """gnt-cluster command"""
189   master = qa_config.GetMasterNode()
190
191   uniqueid = utils.GetUUID()
192   rfile = "/tmp/gnt%s" % utils.GetUUID()
193   rcmd = utils.ShellQuoteArgs(['echo', '-n', uniqueid])
194   cmd = utils.ShellQuoteArgs(['gnt-cluster', 'command',
195                               "%s >%s" % (rcmd, rfile)])
196
197   try:
198     AssertEqual(StartSSH(master['primary'], cmd).wait(), 0)
199     _CheckFileOnAllNodes(rfile, uniqueid)
200   finally:
201     _RemoveFileFromAllNodes(rfile)
202
203
204 def TestClusterDestroy():
205   """gnt-cluster destroy"""
206   master = qa_config.GetMasterNode()
207
208   cmd = ['gnt-cluster', 'destroy', '--yes-do-it']
209   AssertEqual(StartSSH(master['primary'],
210                        utils.ShellQuoteArgs(cmd)).wait(), 0)