Statistics
| Branch: | Tag: | Revision:

root / qa / qa_instance.py @ b3e3813e

History | View | Annotate | Download (14 kB)

1 c68d1f43 Michael Hanselmann
#
2 c68d1f43 Michael Hanselmann
#
3 c68d1f43 Michael Hanselmann
4 3582eef6 Iustin Pop
# Copyright (C) 2007, 2011 Google Inc.
5 cec9845c Michael Hanselmann
#
6 cec9845c Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 cec9845c Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 cec9845c Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 cec9845c Michael Hanselmann
# (at your option) any later version.
10 cec9845c Michael Hanselmann
#
11 cec9845c Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 cec9845c Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 cec9845c Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 cec9845c Michael Hanselmann
# General Public License for more details.
15 cec9845c Michael Hanselmann
#
16 cec9845c Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 cec9845c Michael Hanselmann
# along with this program; if not, write to the Free Software
18 cec9845c Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 cec9845c Michael Hanselmann
# 02110-1301, USA.
20 cec9845c Michael Hanselmann
21 cec9845c Michael Hanselmann
22 cec9845c Michael Hanselmann
"""Instance related QA tests.
23 cec9845c Michael Hanselmann

24 cec9845c Michael Hanselmann
"""
25 cec9845c Michael Hanselmann
26 e8ae0c20 Michael Hanselmann
import re
27 e8ae0c20 Michael Hanselmann
import time
28 e8ae0c20 Michael Hanselmann
29 cec9845c Michael Hanselmann
from ganeti import utils
30 5d640672 Michael Hanselmann
from ganeti import constants
31 288d6440 Michael Hanselmann
from ganeti import query
32 cec9845c Michael Hanselmann
33 cec9845c Michael Hanselmann
import qa_config
34 5d640672 Michael Hanselmann
import qa_utils
35 e8ae0c20 Michael Hanselmann
import qa_error
36 e8ae0c20 Michael Hanselmann
37 cc27265e René Nussbaumer
from qa_utils import AssertIn, AssertCommand, AssertEqual
38 cec9845c Michael Hanselmann
39 e8ae0c20 Michael Hanselmann
40 e8ae0c20 Michael Hanselmann
def _GetDiskStatePath(disk):
41 e8ae0c20 Michael Hanselmann
  return "/sys/block/%s/device/state" % disk
42 cec9845c Michael Hanselmann
43 cec9845c Michael Hanselmann
44 5d640672 Michael Hanselmann
def _GetGenericAddParameters():
45 1d693311 Michael Hanselmann
  params = ['-B', '%s=%s' % (constants.BE_MEMORY, qa_config.get('mem'))]
46 1d693311 Michael Hanselmann
  for idx, size in enumerate(qa_config.get('disk')):
47 1d693311 Michael Hanselmann
    params.extend(["--disk", "%s:size=%s" % (idx, size)])
48 1d693311 Michael Hanselmann
  return params
49 5d640672 Michael Hanselmann
50 5d640672 Michael Hanselmann
51 113b8d89 Michael Hanselmann
def _DiskTest(node, disk_template):
52 cec9845c Michael Hanselmann
  instance = qa_config.AcquireInstance()
53 cec9845c Michael Hanselmann
  try:
54 5d640672 Michael Hanselmann
    cmd = (['gnt-instance', 'add',
55 5d640672 Michael Hanselmann
            '--os-type=%s' % qa_config.get('os'),
56 113b8d89 Michael Hanselmann
            '--disk-template=%s' % disk_template,
57 113b8d89 Michael Hanselmann
            '--node=%s' % node] +
58 5d640672 Michael Hanselmann
           _GetGenericAddParameters())
59 cec9845c Michael Hanselmann
    cmd.append(instance['name'])
60 cec9845c Michael Hanselmann
61 2f4b4f78 Iustin Pop
    AssertCommand(cmd)
62 6a343475 Michael Hanselmann
63 6a343475 Michael Hanselmann
    _CheckSsconfInstanceList(instance["name"])
64 6a343475 Michael Hanselmann
65 cec9845c Michael Hanselmann
    return instance
66 cec9845c Michael Hanselmann
  except:
67 cec9845c Michael Hanselmann
    qa_config.ReleaseInstance(instance)
68 cec9845c Michael Hanselmann
    raise
69 cec9845c Michael Hanselmann
70 cec9845c Michael Hanselmann
71 cec9845c Michael Hanselmann
def TestInstanceAddWithPlainDisk(node):
72 cec9845c Michael Hanselmann
  """gnt-instance add -t plain"""
73 113b8d89 Michael Hanselmann
  return _DiskTest(node['primary'], 'plain')
74 cec9845c Michael Hanselmann
75 cec9845c Michael Hanselmann
76 7d7609a3 Michael Hanselmann
def TestInstanceAddWithDrbdDisk(node, node2):
77 7d7609a3 Michael Hanselmann
  """gnt-instance add -t drbd"""
78 7d7609a3 Michael Hanselmann
  return _DiskTest("%s:%s" % (node['primary'], node2['primary']),
79 7d7609a3 Michael Hanselmann
                   'drbd')
80 7d7609a3 Michael Hanselmann
81 7d7609a3 Michael Hanselmann
82 cec9845c Michael Hanselmann
def TestInstanceRemove(instance):
83 cec9845c Michael Hanselmann
  """gnt-instance remove"""
84 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-instance", "remove", "-f", instance["name"]])
85 cec9845c Michael Hanselmann
86 cec9845c Michael Hanselmann
  qa_config.ReleaseInstance(instance)
87 cec9845c Michael Hanselmann
88 cec9845c Michael Hanselmann
89 cec9845c Michael Hanselmann
def TestInstanceStartup(instance):
90 cec9845c Michael Hanselmann
  """gnt-instance startup"""
91 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-instance", "startup", instance["name"]])
92 cec9845c Michael Hanselmann
93 cec9845c Michael Hanselmann
94 cec9845c Michael Hanselmann
def TestInstanceShutdown(instance):
95 cec9845c Michael Hanselmann
  """gnt-instance shutdown"""
96 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-instance", "shutdown", instance["name"]])
97 cec9845c Michael Hanselmann
98 cec9845c Michael Hanselmann
99 8a4e8898 Michael Hanselmann
def TestInstanceReboot(instance):
100 8a4e8898 Michael Hanselmann
  """gnt-instance reboot"""
101 1d103c02 Iustin Pop
  options = qa_config.get('options', {})
102 1d103c02 Iustin Pop
  reboot_types = options.get("reboot-types", constants.REBOOT_TYPES)
103 2f4b4f78 Iustin Pop
  name = instance["name"]
104 1d103c02 Iustin Pop
  for rtype in reboot_types:
105 2f4b4f78 Iustin Pop
    AssertCommand(["gnt-instance", "reboot", "--type=%s" % rtype, name])
106 8a4e8898 Michael Hanselmann
107 cc27265e René Nussbaumer
  AssertCommand(["gnt-instance", "shutdown", name])
108 cc27265e René Nussbaumer
  AssertCommand(["gnt-instance", "reboot", name])
109 cc27265e René Nussbaumer
110 cc27265e René Nussbaumer
  master = qa_config.GetMasterNode()
111 cc27265e René Nussbaumer
  cmd = ["gnt-instance", "list", "--no-header", "-o", "status", name]
112 cc27265e René Nussbaumer
  result_output = qa_utils.GetCommandOutput(master["primary"],
113 cc27265e René Nussbaumer
                                            utils.ShellQuoteArgs(cmd))
114 cc27265e René Nussbaumer
  AssertEqual(result_output.strip(), constants.INSTST_RUNNING)
115 cc27265e René Nussbaumer
116 8a4e8898 Michael Hanselmann
117 283f9d4c Michael Hanselmann
def TestInstanceReinstall(instance):
118 283f9d4c Michael Hanselmann
  """gnt-instance reinstall"""
119 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-instance", "reinstall", "-f", instance["name"]])
120 283f9d4c Michael Hanselmann
121 283f9d4c Michael Hanselmann
122 6a343475 Michael Hanselmann
def _ReadSsconfInstanceList():
123 6a343475 Michael Hanselmann
  """Reads ssconf_instance_list from the master node.
124 6a343475 Michael Hanselmann

125 6a343475 Michael Hanselmann
  """
126 6a343475 Michael Hanselmann
  master = qa_config.GetMasterNode()
127 6a343475 Michael Hanselmann
128 6a343475 Michael Hanselmann
  cmd = ["cat", utils.PathJoin(constants.DATA_DIR,
129 6a343475 Michael Hanselmann
                               "ssconf_%s" % constants.SS_INSTANCE_LIST)]
130 6a343475 Michael Hanselmann
131 6a343475 Michael Hanselmann
  return qa_utils.GetCommandOutput(master["primary"],
132 6a343475 Michael Hanselmann
                                   utils.ShellQuoteArgs(cmd)).splitlines()
133 6a343475 Michael Hanselmann
134 6a343475 Michael Hanselmann
135 6a343475 Michael Hanselmann
def _CheckSsconfInstanceList(instance):
136 6a343475 Michael Hanselmann
  """Checks if a certain instance is in the ssconf instance list.
137 6a343475 Michael Hanselmann

138 6a343475 Michael Hanselmann
  @type instance: string
139 6a343475 Michael Hanselmann
  @param instance: Instance name
140 6a343475 Michael Hanselmann

141 6a343475 Michael Hanselmann
  """
142 6a343475 Michael Hanselmann
  AssertIn(qa_utils.ResolveInstanceName(instance),
143 6a343475 Michael Hanselmann
           _ReadSsconfInstanceList())
144 6a343475 Michael Hanselmann
145 6a343475 Michael Hanselmann
146 e5c2accd Guido Trotter
def TestInstanceRename(rename_source, rename_target):
147 18337ca9 Iustin Pop
  """gnt-instance rename"""
148 e5c2accd Guido Trotter
  _CheckSsconfInstanceList(rename_source)
149 e5c2accd Guido Trotter
  AssertCommand(["gnt-instance", "rename", rename_source, rename_target])
150 e5c2accd Guido Trotter
  _CheckSsconfInstanceList(rename_target)
151 31fe5102 René Nussbaumer
  AssertCommand(["gnt-instance", "rename", rename_target, rename_source])
152 31fe5102 René Nussbaumer
  _CheckSsconfInstanceList(rename_source)
153 31fe5102 René Nussbaumer
  qa_utils.AddToEtcHosts(["meeeeh-not-exists", rename_target])
154 31fe5102 René Nussbaumer
  try:
155 31fe5102 René Nussbaumer
    AssertCommand(["gnt-instance", "rename", rename_source, rename_target],
156 31fe5102 René Nussbaumer
                  fail=True)
157 31fe5102 René Nussbaumer
    _CheckSsconfInstanceList(rename_source)
158 31fe5102 René Nussbaumer
  finally:
159 31fe5102 René Nussbaumer
    qa_utils.RemoveFromEtcHosts(["meeeeh-not-exists", rename_target])
160 31fe5102 René Nussbaumer
  AssertCommand(["gnt-instance", "rename", rename_source, rename_target])
161 31fe5102 René Nussbaumer
  _CheckSsconfInstanceList(rename_target)
162 18337ca9 Iustin Pop
163 18337ca9 Iustin Pop
164 cec9845c Michael Hanselmann
def TestInstanceFailover(instance):
165 cec9845c Michael Hanselmann
  """gnt-instance failover"""
166 cec9845c Michael Hanselmann
  cmd = ['gnt-instance', 'failover', '--force', instance['name']]
167 2f4b4f78 Iustin Pop
  # failover ...
168 2f4b4f78 Iustin Pop
  AssertCommand(cmd)
169 76f59a32 Michael Hanselmann
  # ... and back
170 2f4b4f78 Iustin Pop
  AssertCommand(cmd)
171 76f59a32 Michael Hanselmann
172 cec9845c Michael Hanselmann
173 938bde86 Michael Hanselmann
def TestInstanceMigrate(instance):
174 938bde86 Michael Hanselmann
  """gnt-instance migrate"""
175 938bde86 Michael Hanselmann
  cmd = ["gnt-instance", "migrate", "--force", instance["name"]]
176 2f4b4f78 Iustin Pop
  # migrate ...
177 2f4b4f78 Iustin Pop
  AssertCommand(cmd)
178 938bde86 Michael Hanselmann
  # ... and back
179 2f4b4f78 Iustin Pop
  AssertCommand(cmd)
180 e9c487be René Nussbaumer
  AssertCommand(["gnt-instance", "shutdown", instance["name"]])
181 e9c487be René Nussbaumer
  AssertCommand(cmd, fail=True)
182 e9c487be René Nussbaumer
  AssertCommand(["gnt-instance", "migrate", "--force", "--allow-failover",
183 e9c487be René Nussbaumer
                 instance["name"]])
184 e9c487be René Nussbaumer
  AssertCommand(["gnt-instance", "start", instance["name"]])
185 e9c487be René Nussbaumer
  AssertCommand(cmd)
186 938bde86 Michael Hanselmann
187 938bde86 Michael Hanselmann
188 cec9845c Michael Hanselmann
def TestInstanceInfo(instance):
189 cec9845c Michael Hanselmann
  """gnt-instance info"""
190 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-instance", "info", instance["name"]])
191 5d640672 Michael Hanselmann
192 5d640672 Michael Hanselmann
193 c0f74c55 Iustin Pop
def TestInstanceModify(instance):
194 c0f74c55 Iustin Pop
  """gnt-instance modify"""
195 1d693311 Michael Hanselmann
  # Assume /sbin/init exists on all systems
196 1d693311 Michael Hanselmann
  test_kernel = "/sbin/init"
197 1d693311 Michael Hanselmann
  test_initrd = test_kernel
198 1d693311 Michael Hanselmann
199 c0f74c55 Iustin Pop
  orig_memory = qa_config.get('mem')
200 2f4b4f78 Iustin Pop
  #orig_bridge = qa_config.get("bridge", "xen-br0")
201 c0f74c55 Iustin Pop
  args = [
202 1d693311 Michael Hanselmann
    ["-B", "%s=128" % constants.BE_MEMORY],
203 1d693311 Michael Hanselmann
    ["-B", "%s=%s" % (constants.BE_MEMORY, orig_memory)],
204 1d693311 Michael Hanselmann
    ["-B", "%s=2" % constants.BE_VCPUS],
205 1d693311 Michael Hanselmann
    ["-B", "%s=1" % constants.BE_VCPUS],
206 1d693311 Michael Hanselmann
    ["-B", "%s=%s" % (constants.BE_VCPUS, constants.VALUE_DEFAULT)],
207 1d693311 Michael Hanselmann
208 1d693311 Michael Hanselmann
    ["-H", "%s=%s" % (constants.HV_KERNEL_PATH, test_kernel)],
209 1d693311 Michael Hanselmann
    ["-H", "%s=%s" % (constants.HV_KERNEL_PATH, constants.VALUE_DEFAULT)],
210 1d693311 Michael Hanselmann
    ["-H", "%s=%s" % (constants.HV_INITRD_PATH, test_initrd)],
211 5645d16b Iustin Pop
    ["-H", "no_%s" % (constants.HV_INITRD_PATH, )],
212 1d693311 Michael Hanselmann
    ["-H", "%s=%s" % (constants.HV_INITRD_PATH, constants.VALUE_DEFAULT)],
213 1d693311 Michael Hanselmann
214 1d693311 Michael Hanselmann
    # TODO: bridge tests
215 1d693311 Michael Hanselmann
    #["--bridge", "xen-br1"],
216 1d693311 Michael Hanselmann
    #["--bridge", orig_bridge],
217 1d693311 Michael Hanselmann
218 1d693311 Michael Hanselmann
    # TODO: Do these tests only with xen-hvm
219 1d693311 Michael Hanselmann
    #["-H", "%s=acn" % constants.HV_BOOT_ORDER],
220 1d693311 Michael Hanselmann
    #["-H", "%s=%s" % (constants.HV_BOOT_ORDER, constants.VALUE_DEFAULT)],
221 c0f74c55 Iustin Pop
    ]
222 c0f74c55 Iustin Pop
  for alist in args:
223 2f4b4f78 Iustin Pop
    AssertCommand(["gnt-instance", "modify"] + alist + [instance["name"]])
224 c0f74c55 Iustin Pop
225 c0f74c55 Iustin Pop
  # check no-modify
226 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-instance", "modify", instance["name"]], fail=True)
227 c0f74c55 Iustin Pop
228 c0f74c55 Iustin Pop
229 7f69aabb Iustin Pop
def TestInstanceConvertDisk(instance, snode):
230 7f69aabb Iustin Pop
  """gnt-instance modify -t"""
231 2f4b4f78 Iustin Pop
  name = instance["name"]
232 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-instance", "modify", "-t", "plain", name])
233 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-instance", "modify", "-t", "drbd",
234 2f4b4f78 Iustin Pop
                 "-n", snode["primary"], name])
235 7f69aabb Iustin Pop
236 7f69aabb Iustin Pop
237 283f9d4c Michael Hanselmann
def TestInstanceList():
238 283f9d4c Michael Hanselmann
  """gnt-instance list"""
239 288d6440 Michael Hanselmann
  qa_utils.GenericQueryTest("gnt-instance", query.INSTANCE_FIELDS.keys())
240 283f9d4c Michael Hanselmann
241 283f9d4c Michael Hanselmann
242 2214cf14 Michael Hanselmann
def TestInstanceListFields():
243 2214cf14 Michael Hanselmann
  """gnt-instance list-fields"""
244 2214cf14 Michael Hanselmann
  qa_utils.GenericQueryFieldsTest("gnt-instance", query.INSTANCE_FIELDS.keys())
245 2214cf14 Michael Hanselmann
246 2214cf14 Michael Hanselmann
247 4379b1fa Michael Hanselmann
def TestInstanceConsole(instance):
248 4379b1fa Michael Hanselmann
  """gnt-instance console"""
249 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-instance", "console", "--show-cmd", instance["name"]])
250 4379b1fa Michael Hanselmann
251 4379b1fa Michael Hanselmann
252 7910e7a5 Michael Hanselmann
def TestReplaceDisks(instance, pnode, snode, othernode):
253 7910e7a5 Michael Hanselmann
  """gnt-instance replace-disks"""
254 3582eef6 Iustin Pop
  # pylint: disable-msg=W0613
255 3582eef6 Iustin Pop
  # due to unused pnode arg
256 3582eef6 Iustin Pop
  # FIXME: should be removed from the function completely
257 7910e7a5 Michael Hanselmann
  def buildcmd(args):
258 7910e7a5 Michael Hanselmann
    cmd = ['gnt-instance', 'replace-disks']
259 7910e7a5 Michael Hanselmann
    cmd.extend(args)
260 7910e7a5 Michael Hanselmann
    cmd.append(instance["name"])
261 7910e7a5 Michael Hanselmann
    return cmd
262 7910e7a5 Michael Hanselmann
263 2f4b4f78 Iustin Pop
  for data in [
264 2f4b4f78 Iustin Pop
    ["-p"],
265 2f4b4f78 Iustin Pop
    ["-s"],
266 2f4b4f78 Iustin Pop
    ["--new-secondary=%s" % othernode["primary"]],
267 2f4b4f78 Iustin Pop
    # and restore
268 2f4b4f78 Iustin Pop
    ["--new-secondary=%s" % snode["primary"]],
269 2f4b4f78 Iustin Pop
    ]:
270 2f4b4f78 Iustin Pop
    AssertCommand(buildcmd(data))
271 7910e7a5 Michael Hanselmann
272 9026e935 René Nussbaumer
  AssertCommand(buildcmd(["-a"]))
273 9026e935 René Nussbaumer
  AssertCommand(["gnt-instance", "stop", instance["name"]])
274 9026e935 René Nussbaumer
  AssertCommand(buildcmd(["-a"]), fail=True)
275 9026e935 René Nussbaumer
  AssertCommand(["gnt-instance", "activate-disks", instance["name"]])
276 9026e935 René Nussbaumer
  AssertCommand(buildcmd(["-a"]))
277 9026e935 René Nussbaumer
  AssertCommand(["gnt-instance", "start", instance["name"]])
278 9026e935 René Nussbaumer
279 7910e7a5 Michael Hanselmann
280 5d640672 Michael Hanselmann
def TestInstanceExport(instance, node):
281 bc696589 Michael Hanselmann
  """gnt-backup export -n ..."""
282 2f4b4f78 Iustin Pop
  name = instance["name"]
283 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-backup", "export", "-n", node["primary"], name])
284 2f4b4f78 Iustin Pop
  return qa_utils.ResolveInstanceName(name)
285 5d640672 Michael Hanselmann
286 5d640672 Michael Hanselmann
287 8d8d650c Michael Hanselmann
def TestInstanceExportWithRemove(instance, node):
288 8d8d650c Michael Hanselmann
  """gnt-backup export --remove-instance"""
289 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-backup", "export", "-n", node["primary"],
290 2f4b4f78 Iustin Pop
                 "--remove-instance", instance["name"]])
291 8d8d650c Michael Hanselmann
292 8d8d650c Michael Hanselmann
293 bc696589 Michael Hanselmann
def TestInstanceExportNoTarget(instance):
294 bc696589 Michael Hanselmann
  """gnt-backup export (without target node, should fail)"""
295 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-backup", "export", instance["name"]], fail=True)
296 bc696589 Michael Hanselmann
297 bc696589 Michael Hanselmann
298 5d640672 Michael Hanselmann
def TestInstanceImport(node, newinst, expnode, name):
299 5d640672 Michael Hanselmann
  """gnt-backup import"""
300 5d640672 Michael Hanselmann
  cmd = (['gnt-backup', 'import',
301 5d640672 Michael Hanselmann
          '--disk-template=plain',
302 5d640672 Michael Hanselmann
          '--no-ip-check',
303 53a8da8e Iustin Pop
          '--net', '0:mac=generate',
304 5d640672 Michael Hanselmann
          '--src-node=%s' % expnode['primary'],
305 5d640672 Michael Hanselmann
          '--src-dir=%s/%s' % (constants.EXPORT_DIR, name),
306 5d640672 Michael Hanselmann
          '--node=%s' % node['primary']] +
307 5d640672 Michael Hanselmann
         _GetGenericAddParameters())
308 5d640672 Michael Hanselmann
  cmd.append(newinst['name'])
309 2f4b4f78 Iustin Pop
  AssertCommand(cmd)
310 283f9d4c Michael Hanselmann
311 283f9d4c Michael Hanselmann
312 283f9d4c Michael Hanselmann
def TestBackupList(expnode):
313 283f9d4c Michael Hanselmann
  """gnt-backup list"""
314 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-backup", "list", "--node=%s" % expnode["primary"]])
315 e8ae0c20 Michael Hanselmann
316 e8ae0c20 Michael Hanselmann
317 e8ae0c20 Michael Hanselmann
def _TestInstanceDiskFailure(instance, node, node2, onmaster):
318 e8ae0c20 Michael Hanselmann
  """Testing disk failure."""
319 e8ae0c20 Michael Hanselmann
  master = qa_config.GetMasterNode()
320 e8ae0c20 Michael Hanselmann
  sq = utils.ShellQuoteArgs
321 e8ae0c20 Michael Hanselmann
322 46f9a948 Michael Hanselmann
  instance_full = qa_utils.ResolveInstanceName(instance["name"])
323 e8ae0c20 Michael Hanselmann
  node_full = qa_utils.ResolveNodeName(node)
324 e8ae0c20 Michael Hanselmann
  node2_full = qa_utils.ResolveNodeName(node2)
325 e8ae0c20 Michael Hanselmann
326 29df1f02 Michael Hanselmann
  print qa_utils.FormatInfo("Getting physical disk names")
327 e8ae0c20 Michael Hanselmann
  cmd = ['gnt-node', 'volumes', '--separator=|', '--no-headers',
328 e8ae0c20 Michael Hanselmann
         '--output=node,phys,instance',
329 e8ae0c20 Michael Hanselmann
         node['primary'], node2['primary']]
330 e8ae0c20 Michael Hanselmann
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
331 e8ae0c20 Michael Hanselmann
332 e8ae0c20 Michael Hanselmann
  # Get physical disk names
333 e8ae0c20 Michael Hanselmann
  re_disk = re.compile(r'^/dev/([a-z]+)\d+$')
334 e8ae0c20 Michael Hanselmann
  node2disk = {}
335 e8ae0c20 Michael Hanselmann
  for line in output.splitlines():
336 e8ae0c20 Michael Hanselmann
    (node_name, phys, inst) = line.split('|')
337 e8ae0c20 Michael Hanselmann
    if inst == instance_full:
338 e8ae0c20 Michael Hanselmann
      if node_name not in node2disk:
339 e8ae0c20 Michael Hanselmann
        node2disk[node_name] = []
340 e8ae0c20 Michael Hanselmann
341 e8ae0c20 Michael Hanselmann
      m = re_disk.match(phys)
342 e8ae0c20 Michael Hanselmann
      if not m:
343 2f4b4f78 Iustin Pop
        raise qa_error.Error("Unknown disk name format: %s" % phys)
344 e8ae0c20 Michael Hanselmann
345 e8ae0c20 Michael Hanselmann
      name = m.group(1)
346 e8ae0c20 Michael Hanselmann
      if name not in node2disk[node_name]:
347 e8ae0c20 Michael Hanselmann
        node2disk[node_name].append(name)
348 e8ae0c20 Michael Hanselmann
349 e8ae0c20 Michael Hanselmann
  if [node2_full, node_full][int(onmaster)] not in node2disk:
350 f4bc1f2c Michael Hanselmann
    raise qa_error.Error("Couldn't find physical disks used on"
351 f4bc1f2c Michael Hanselmann
                         " %s node" % ["secondary", "master"][int(onmaster)])
352 e8ae0c20 Michael Hanselmann
353 29df1f02 Michael Hanselmann
  print qa_utils.FormatInfo("Checking whether nodes have ability to stop"
354 29df1f02 Michael Hanselmann
                            " disks")
355 e8ae0c20 Michael Hanselmann
  for node_name, disks in node2disk.iteritems():
356 e8ae0c20 Michael Hanselmann
    cmds = []
357 e8ae0c20 Michael Hanselmann
    for disk in disks:
358 e8ae0c20 Michael Hanselmann
      cmds.append(sq(["test", "-f", _GetDiskStatePath(disk)]))
359 2f4b4f78 Iustin Pop
    AssertCommand(" && ".join(cmds), node=node_name)
360 e8ae0c20 Michael Hanselmann
361 29df1f02 Michael Hanselmann
  print qa_utils.FormatInfo("Getting device paths")
362 e8ae0c20 Michael Hanselmann
  cmd = ['gnt-instance', 'activate-disks', instance['name']]
363 e8ae0c20 Michael Hanselmann
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
364 e8ae0c20 Michael Hanselmann
  devpath = []
365 e8ae0c20 Michael Hanselmann
  for line in output.splitlines():
366 e8ae0c20 Michael Hanselmann
    (_, _, tmpdevpath) = line.split(':')
367 e8ae0c20 Michael Hanselmann
    devpath.append(tmpdevpath)
368 29df1f02 Michael Hanselmann
  print devpath
369 e8ae0c20 Michael Hanselmann
370 29df1f02 Michael Hanselmann
  print qa_utils.FormatInfo("Getting drbd device paths")
371 e8ae0c20 Michael Hanselmann
  cmd = ['gnt-instance', 'info', instance['name']]
372 e8ae0c20 Michael Hanselmann
  output = qa_utils.GetCommandOutput(master['primary'], sq(cmd))
373 29df1f02 Michael Hanselmann
  pattern = (r'\s+-\s+sd[a-z]+,\s+type:\s+drbd8?,\s+.*$'
374 e8ae0c20 Michael Hanselmann
             r'\s+primary:\s+(/dev/drbd\d+)\s+')
375 e8ae0c20 Michael Hanselmann
  drbddevs = re.findall(pattern, output, re.M)
376 29df1f02 Michael Hanselmann
  print drbddevs
377 e8ae0c20 Michael Hanselmann
378 e8ae0c20 Michael Hanselmann
  halted_disks = []
379 e8ae0c20 Michael Hanselmann
  try:
380 29df1f02 Michael Hanselmann
    print qa_utils.FormatInfo("Deactivating disks")
381 b1ffe1eb Michael Hanselmann
    cmds = []
382 b1ffe1eb Michael Hanselmann
    for name in node2disk[[node2_full, node_full][int(onmaster)]]:
383 b1ffe1eb Michael Hanselmann
      halted_disks.append(name)
384 b1ffe1eb Michael Hanselmann
      cmds.append(sq(["echo", "offline"]) + " >%s" % _GetDiskStatePath(name))
385 2f4b4f78 Iustin Pop
    AssertCommand(" && ".join(cmds), node=[node2, node][int(onmaster)])
386 b1ffe1eb Michael Hanselmann
387 29df1f02 Michael Hanselmann
    print qa_utils.FormatInfo("Write to disks and give some time to notice"
388 29df1f02 Michael Hanselmann
                              " to notice the problem")
389 e8ae0c20 Michael Hanselmann
    cmds = []
390 e8ae0c20 Michael Hanselmann
    for disk in devpath:
391 e8ae0c20 Michael Hanselmann
      cmds.append(sq(["dd", "count=1", "bs=512", "conv=notrunc",
392 e8ae0c20 Michael Hanselmann
                      "if=%s" % disk, "of=%s" % disk]))
393 e8ae0c20 Michael Hanselmann
    for _ in (0, 1, 2):
394 2f4b4f78 Iustin Pop
      AssertCommand(" && ".join(cmds), node=node)
395 e8ae0c20 Michael Hanselmann
      time.sleep(3)
396 e8ae0c20 Michael Hanselmann
397 29df1f02 Michael Hanselmann
    print qa_utils.FormatInfo("Debugging info")
398 b1ffe1eb Michael Hanselmann
    for name in drbddevs:
399 2f4b4f78 Iustin Pop
      AssertCommand(["drbdsetup", name, "show"], node=node)
400 b1ffe1eb Michael Hanselmann
401 2f4b4f78 Iustin Pop
    AssertCommand(["gnt-instance", "info", instance["name"]])
402 e8ae0c20 Michael Hanselmann
403 e8ae0c20 Michael Hanselmann
  finally:
404 29df1f02 Michael Hanselmann
    print qa_utils.FormatInfo("Activating disks again")
405 e8ae0c20 Michael Hanselmann
    cmds = []
406 e8ae0c20 Michael Hanselmann
    for name in halted_disks:
407 e8ae0c20 Michael Hanselmann
      cmds.append(sq(["echo", "running"]) + " >%s" % _GetDiskStatePath(name))
408 2f4b4f78 Iustin Pop
    AssertCommand("; ".join(cmds), node=[node2, node][int(onmaster)])
409 e8ae0c20 Michael Hanselmann
410 b1ffe1eb Michael Hanselmann
  if onmaster:
411 b1ffe1eb Michael Hanselmann
    for name in drbddevs:
412 2f4b4f78 Iustin Pop
      AssertCommand(["drbdsetup", name, "detach"], node=node)
413 b1ffe1eb Michael Hanselmann
  else:
414 b1ffe1eb Michael Hanselmann
    for name in drbddevs:
415 2f4b4f78 Iustin Pop
      AssertCommand(["drbdsetup", name, "disconnect"], node=node2)
416 b1ffe1eb Michael Hanselmann
417 29df1f02 Michael Hanselmann
  # TODO
418 2f4b4f78 Iustin Pop
  #AssertCommand(["vgs"], [node2, node][int(onmaster)])
419 b1ffe1eb Michael Hanselmann
420 29df1f02 Michael Hanselmann
  print qa_utils.FormatInfo("Making sure disks are up again")
421 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-instance", "replace-disks", instance["name"]])
422 29df1f02 Michael Hanselmann
423 29df1f02 Michael Hanselmann
  print qa_utils.FormatInfo("Restarting instance")
424 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-instance", "shutdown", instance["name"]])
425 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-instance", "startup", instance["name"]])
426 e8ae0c20 Michael Hanselmann
427 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-cluster", "verify"])
428 e8ae0c20 Michael Hanselmann
429 e8ae0c20 Michael Hanselmann
430 e8ae0c20 Michael Hanselmann
def TestInstanceMasterDiskFailure(instance, node, node2):
431 e8ae0c20 Michael Hanselmann
  """Testing disk failure on master node."""
432 3582eef6 Iustin Pop
  # pylint: disable-msg=W0613
433 3582eef6 Iustin Pop
  # due to unused args
434 f4bc1f2c Michael Hanselmann
  print qa_utils.FormatError("Disk failure on primary node cannot be"
435 f4bc1f2c Michael Hanselmann
                             " tested due to potential crashes.")
436 e8ae0c20 Michael Hanselmann
  # The following can cause crashes, thus it's disabled until fixed
437 dfe11bad Michael Hanselmann
  #return _TestInstanceDiskFailure(instance, node, node2, True)
438 e8ae0c20 Michael Hanselmann
439 e8ae0c20 Michael Hanselmann
440 e8ae0c20 Michael Hanselmann
def TestInstanceSecondaryDiskFailure(instance, node, node2):
441 e8ae0c20 Michael Hanselmann
  """Testing disk failure on secondary node."""
442 e8ae0c20 Michael Hanselmann
  return _TestInstanceDiskFailure(instance, node, node2, False)