Statistics
| Branch: | Tag: | Revision:

root / qa / qa_cluster.py @ 82ce55fa

History | View | Annotate | Download (44.6 kB)

1 c68d1f43 Michael Hanselmann
#
2 c68d1f43 Michael Hanselmann
#
3 c68d1f43 Michael Hanselmann
4 587f8ff6 Bernardo Dal Seno
# Copyright (C) 2007, 2010, 2011, 2012, 2013 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
"""Cluster related QA tests.
23 cec9845c Michael Hanselmann

24 cec9845c Michael Hanselmann
"""
25 cec9845c Michael Hanselmann
26 587f8ff6 Bernardo Dal Seno
import re
27 cec9845c Michael Hanselmann
import tempfile
28 49ceab21 Michael Hanselmann
import os.path
29 cec9845c Michael Hanselmann
30 b8669a69 Jose A. Lopes
from ganeti import _constants
31 6d4a1656 Michael Hanselmann
from ganeti import constants
32 66d1f035 René Nussbaumer
from ganeti import compat
33 cec9845c Michael Hanselmann
from ganeti import utils
34 304d9f02 Michael Hanselmann
from ganeti import pathutils
35 cec9845c Michael Hanselmann
36 cec9845c Michael Hanselmann
import qa_config
37 e4889779 Thomas Thrainer
import qa_daemon
38 cec9845c Michael Hanselmann
import qa_utils
39 cec9845c Michael Hanselmann
import qa_error
40 462f0faa Helga Velroyen
import qa_instance
41 cec9845c Michael Hanselmann
42 66d1f035 René Nussbaumer
from qa_utils import AssertEqual, AssertCommand, GetCommandOutput
43 cec9845c Michael Hanselmann
44 cec9845c Michael Hanselmann
45 23610ff8 Bernardo Dal Seno
# Prefix for LVM volumes created by QA code during tests
46 23610ff8 Bernardo Dal Seno
_QA_LV_PREFIX = "qa-"
47 23610ff8 Bernardo Dal Seno
48 c2a0947d Iustin Pop
#: cluster verify command
49 c2a0947d Iustin Pop
_CLUSTER_VERIFY = ["gnt-cluster", "verify"]
50 c2a0947d Iustin Pop
51 21bf2e2e Andrea Spadaccini
52 830da270 Michael Hanselmann
def _RemoveFileFromAllNodes(filename):
53 830da270 Michael Hanselmann
  """Removes a file from all nodes.
54 830da270 Michael Hanselmann

55 830da270 Michael Hanselmann
  """
56 2f4b4f78 Iustin Pop
  for node in qa_config.get("nodes"):
57 2f4b4f78 Iustin Pop
    AssertCommand(["rm", "-f", filename], node=node)
58 830da270 Michael Hanselmann
59 830da270 Michael Hanselmann
60 830da270 Michael Hanselmann
def _CheckFileOnAllNodes(filename, content):
61 830da270 Michael Hanselmann
  """Verifies the content of the given file on all nodes.
62 830da270 Michael Hanselmann

63 830da270 Michael Hanselmann
  """
64 830da270 Michael Hanselmann
  cmd = utils.ShellQuoteArgs(["cat", filename])
65 2f4b4f78 Iustin Pop
  for node in qa_config.get("nodes"):
66 aecba21e Michael Hanselmann
    AssertEqual(qa_utils.GetCommandOutput(node.primary, cmd), content)
67 830da270 Michael Hanselmann
68 830da270 Michael Hanselmann
69 0e79564a Bernardo Dal Seno
def _GetClusterField(field_path):
70 0e79564a Bernardo Dal Seno
  """Get the value of a cluster field.
71 17cfeee9 Bernardo Dal Seno

72 0e79564a Bernardo Dal Seno
  @type field_path: list of strings
73 0e79564a Bernardo Dal Seno
  @param field_path: Names of the groups/fields to navigate to get the desired
74 0e79564a Bernardo Dal Seno
      value, e.g. C{["Default node parameters", "oob_program"]}
75 0e79564a Bernardo Dal Seno
  @return: The effective value of the field (the actual type depends on the
76 0e79564a Bernardo Dal Seno
      chosen field)
77 17cfeee9 Bernardo Dal Seno

78 17cfeee9 Bernardo Dal Seno
  """
79 0e79564a Bernardo Dal Seno
  assert isinstance(field_path, list)
80 0e79564a Bernardo Dal Seno
  assert field_path
81 0e79564a Bernardo Dal Seno
  ret = qa_utils.GetObjectInfo(["gnt-cluster", "info"])
82 0e79564a Bernardo Dal Seno
  for key in field_path:
83 0e79564a Bernardo Dal Seno
    ret = ret[key]
84 0e79564a Bernardo Dal Seno
  return ret
85 17cfeee9 Bernardo Dal Seno
86 17cfeee9 Bernardo Dal Seno
87 587f8ff6 Bernardo Dal Seno
# Cluster-verify errors (date, "ERROR", then error code)
88 ab4832d1 Bernardo Dal Seno
_CVERROR_RE = re.compile(r"^[\w\s:]+\s+- (ERROR|WARNING):([A-Z0-9_-]+):")
89 587f8ff6 Bernardo Dal Seno
90 587f8ff6 Bernardo Dal Seno
91 587f8ff6 Bernardo Dal Seno
def _GetCVErrorCodes(cvout):
92 ab4832d1 Bernardo Dal Seno
  errs = set()
93 ab4832d1 Bernardo Dal Seno
  warns = set()
94 587f8ff6 Bernardo Dal Seno
  for l in cvout.splitlines():
95 587f8ff6 Bernardo Dal Seno
    m = _CVERROR_RE.match(l)
96 587f8ff6 Bernardo Dal Seno
    if m:
97 ab4832d1 Bernardo Dal Seno
      etype = m.group(1)
98 ab4832d1 Bernardo Dal Seno
      ecode = m.group(2)
99 ab4832d1 Bernardo Dal Seno
      if etype == "ERROR":
100 ab4832d1 Bernardo Dal Seno
        errs.add(ecode)
101 ab4832d1 Bernardo Dal Seno
      elif etype == "WARNING":
102 ab4832d1 Bernardo Dal Seno
        warns.add(ecode)
103 ab4832d1 Bernardo Dal Seno
  return (errs, warns)
104 587f8ff6 Bernardo Dal Seno
105 587f8ff6 Bernardo Dal Seno
106 ab4832d1 Bernardo Dal Seno
def _CheckVerifyErrors(actual, expected, etype):
107 ab4832d1 Bernardo Dal Seno
  exp_codes = compat.UniqueFrozenset(e for (_, e, _) in expected)
108 ab4832d1 Bernardo Dal Seno
  if not actual.issuperset(exp_codes):
109 ab4832d1 Bernardo Dal Seno
    missing = exp_codes.difference(actual)
110 ab4832d1 Bernardo Dal Seno
    raise qa_error.Error("Cluster-verify didn't return these expected"
111 ab4832d1 Bernardo Dal Seno
                         " %ss: %s" % (etype, utils.CommaJoin(missing)))
112 ab4832d1 Bernardo Dal Seno
113 ab4832d1 Bernardo Dal Seno
114 b0e8ed3f Santi Raffa
def _CheckVerifyNoWarnings(actual, expected):
115 b0e8ed3f Santi Raffa
  exp_codes = compat.UniqueFrozenset(e for (_, e, _) in expected)
116 b0e8ed3f Santi Raffa
  excess = actual.intersection(exp_codes)
117 b0e8ed3f Santi Raffa
  if excess:
118 b0e8ed3f Santi Raffa
    raise qa_error.Error("Cluster-verify returned these warnings:"
119 b0e8ed3f Santi Raffa
                         " %s" % (utils.CommaJoin(excess)))
120 b0e8ed3f Santi Raffa
121 b0e8ed3f Santi Raffa
122 b0e8ed3f Santi Raffa
def AssertClusterVerify(fail=False, errors=None,
123 b0e8ed3f Santi Raffa
                        warnings=None, no_warnings=None):
124 b0e8ed3f Santi Raffa
  """Run cluster-verify and check the result, ignoring warnings by default.
125 587f8ff6 Bernardo Dal Seno

126 587f8ff6 Bernardo Dal Seno
  @type fail: bool
127 b0e8ed3f Santi Raffa
  @param fail: if cluster-verify is expected to fail instead of succeeding.
128 587f8ff6 Bernardo Dal Seno
  @type errors: list of tuples
129 587f8ff6 Bernardo Dal Seno
  @param errors: List of CV_XXX errors that are expected; if specified, all the
130 587f8ff6 Bernardo Dal Seno
      errors listed must appear in cluster-verify output. A non-empty value
131 587f8ff6 Bernardo Dal Seno
      implies C{fail=True}.
132 ab4832d1 Bernardo Dal Seno
  @type warnings: list of tuples
133 b0e8ed3f Santi Raffa
  @param warnings: List of CV_XXX warnings that are expected to be raised; if
134 b0e8ed3f Santi Raffa
      specified, all the errors listed must appear in cluster-verify output.
135 b0e8ed3f Santi Raffa
  @type no_warnings: list of tuples
136 b0e8ed3f Santi Raffa
  @param no_warnings: List of CV_XXX warnings that we expect NOT to be raised.
137 587f8ff6 Bernardo Dal Seno
  """
138 587f8ff6 Bernardo Dal Seno
  cvcmd = "gnt-cluster verify"
139 587f8ff6 Bernardo Dal Seno
  mnode = qa_config.GetMasterNode()
140 b0e8ed3f Santi Raffa
  if errors or warnings or no_warnings:
141 aecba21e Michael Hanselmann
    cvout = GetCommandOutput(mnode.primary, cvcmd + " --error-codes",
142 ab4832d1 Bernardo Dal Seno
                             fail=(fail or errors))
143 b0e8ed3f Santi Raffa
    print cvout
144 ab4832d1 Bernardo Dal Seno
    (act_errs, act_warns) = _GetCVErrorCodes(cvout)
145 ab4832d1 Bernardo Dal Seno
    if errors:
146 ab4832d1 Bernardo Dal Seno
      _CheckVerifyErrors(act_errs, errors, "error")
147 ab4832d1 Bernardo Dal Seno
    if warnings:
148 ab4832d1 Bernardo Dal Seno
      _CheckVerifyErrors(act_warns, warnings, "warning")
149 b0e8ed3f Santi Raffa
    if no_warnings:
150 b0e8ed3f Santi Raffa
      _CheckVerifyNoWarnings(act_warns, no_warnings)
151 b0e8ed3f Santi Raffa
152 587f8ff6 Bernardo Dal Seno
  else:
153 587f8ff6 Bernardo Dal Seno
    AssertCommand(cvcmd, fail=fail, node=mnode)
154 587f8ff6 Bernardo Dal Seno
155 587f8ff6 Bernardo Dal Seno
156 92cb4940 Andrea Spadaccini
# data for testing failures due to bad keys/values for disk parameters
157 92cb4940 Andrea Spadaccini
_FAIL_PARAMS = ["nonexistent:resync-rate=1",
158 92cb4940 Andrea Spadaccini
                "drbd:nonexistent=1",
159 92cb4940 Andrea Spadaccini
                "drbd:resync-rate=invalid",
160 92cb4940 Andrea Spadaccini
                ]
161 92cb4940 Andrea Spadaccini
162 92cb4940 Andrea Spadaccini
163 92cb4940 Andrea Spadaccini
def TestClusterInitDisk():
164 92cb4940 Andrea Spadaccini
  """gnt-cluster init -D"""
165 92cb4940 Andrea Spadaccini
  name = qa_config.get("name")
166 92cb4940 Andrea Spadaccini
  for param in _FAIL_PARAMS:
167 92cb4940 Andrea Spadaccini
    AssertCommand(["gnt-cluster", "init", "-D", param, name], fail=True)
168 92cb4940 Andrea Spadaccini
169 92cb4940 Andrea Spadaccini
170 725ec2f1 René Nussbaumer
def TestClusterInit(rapi_user, rapi_secret):
171 cec9845c Michael Hanselmann
  """gnt-cluster init"""
172 cec9845c Michael Hanselmann
  master = qa_config.GetMasterNode()
173 cec9845c Michael Hanselmann
174 734fd6b4 Michael Hanselmann
  rapi_users_path = qa_utils.MakeNodePath(master, pathutils.RAPI_USERS_FILE)
175 734fd6b4 Michael Hanselmann
  rapi_dir = os.path.dirname(rapi_users_path)
176 49ceab21 Michael Hanselmann
177 725ec2f1 René Nussbaumer
  # First create the RAPI credentials
178 a62d1901 Michael Hanselmann
  fh = tempfile.NamedTemporaryFile()
179 a62d1901 Michael Hanselmann
  try:
180 a62d1901 Michael Hanselmann
    fh.write("%s %s write\n" % (rapi_user, rapi_secret))
181 a62d1901 Michael Hanselmann
    fh.flush()
182 a62d1901 Michael Hanselmann
183 aecba21e Michael Hanselmann
    tmpru = qa_utils.UploadFile(master.primary, fh.name)
184 a62d1901 Michael Hanselmann
    try:
185 49ceab21 Michael Hanselmann
      AssertCommand(["mkdir", "-p", rapi_dir])
186 734fd6b4 Michael Hanselmann
      AssertCommand(["mv", tmpru, rapi_users_path])
187 a62d1901 Michael Hanselmann
    finally:
188 2f4b4f78 Iustin Pop
      AssertCommand(["rm", "-f", tmpru])
189 a62d1901 Michael Hanselmann
  finally:
190 a62d1901 Michael Hanselmann
    fh.close()
191 a62d1901 Michael Hanselmann
192 a62d1901 Michael Hanselmann
  # Initialize cluster
193 3039e2dc Helga Velroyen
  enabled_disk_templates = qa_config.GetEnabledDiskTemplates()
194 e7b6183b Michael Hanselmann
  cmd = [
195 e7b6183b Michael Hanselmann
    "gnt-cluster", "init",
196 e7b6183b Michael Hanselmann
    "--primary-ip-version=%d" % qa_config.get("primary_ip_version", 4),
197 59c1d41e Michael Hanselmann
    "--enabled-hypervisors=%s" % ",".join(qa_config.GetEnabledHypervisors()),
198 2dae8d64 Helga Velroyen
    "--enabled-disk-templates=%s" %
199 3039e2dc Helga Velroyen
      ",".join(enabled_disk_templates),
200 e7b6183b Michael Hanselmann
    ]
201 3039e2dc Helga Velroyen
  if constants.DT_FILE in enabled_disk_templates:
202 3039e2dc Helga Velroyen
    cmd.append(
203 3039e2dc Helga Velroyen
        "--file-storage-dir=%s" %
204 b24b52d9 Helga Velroyen
        qa_config.get("default-file-storage-dir",
205 b24b52d9 Helga Velroyen
                      pathutils.DEFAULT_FILE_STORAGE_DIR))
206 20286f7c René Nussbaumer
207 20286f7c René Nussbaumer
  for spec_type in ("mem-size", "disk-size", "disk-count", "cpu-count",
208 20286f7c René Nussbaumer
                    "nic-count"):
209 20286f7c René Nussbaumer
    for spec_val in ("min", "max", "std"):
210 20286f7c René Nussbaumer
      spec = qa_config.get("ispec_%s_%s" %
211 3601d488 Michael Hanselmann
                           (spec_type.replace("-", "_"), spec_val), None)
212 00650761 Michael Hanselmann
      if spec is not None:
213 20286f7c René Nussbaumer
        cmd.append("--specs-%s=%s=%d" % (spec_type, spec_val, spec))
214 9486f6ae Manuel Franceschini
215 aecba21e Michael Hanselmann
  if master.secondary:
216 aecba21e Michael Hanselmann
    cmd.append("--secondary-ip=%s" % master.secondary)
217 cec9845c Michael Hanselmann
218 912737ba Helga Velroyen
  if utils.IsLvmEnabled(qa_config.GetEnabledDiskTemplates()):
219 912737ba Helga Velroyen
    vgname = qa_config.get("vg-name", constants.DEFAULT_VG)
220 912737ba Helga Velroyen
    if vgname:
221 912737ba Helga Velroyen
      cmd.append("--vg-name=%s" % vgname)
222 912737ba Helga Velroyen
    else:
223 912737ba Helga Velroyen
      raise qa_error.Error("Please specify a volume group if you enable"
224 912737ba Helga Velroyen
                           " lvm-based disk templates in the QA.")
225 23610ff8 Bernardo Dal Seno
226 3601d488 Michael Hanselmann
  master_netdev = qa_config.get("master-netdev", None)
227 3601d488 Michael Hanselmann
  if master_netdev:
228 3601d488 Michael Hanselmann
    cmd.append("--master-netdev=%s" % master_netdev)
229 3601d488 Michael Hanselmann
230 3601d488 Michael Hanselmann
  nicparams = qa_config.get("default-nicparams", None)
231 3601d488 Michael Hanselmann
  if nicparams:
232 3601d488 Michael Hanselmann
    cmd.append("--nic-parameters=%s" %
233 3601d488 Michael Hanselmann
               ",".join(utils.FormatKeyValue(nicparams)))
234 cec9845c Michael Hanselmann
235 6a0f22e1 Bernardo Dal Seno
  # Cluster value of the exclusive-storage node parameter
236 6a0f22e1 Bernardo Dal Seno
  e_s = qa_config.get("exclusive-storage")
237 6a0f22e1 Bernardo Dal Seno
  if e_s is not None:
238 6a0f22e1 Bernardo Dal Seno
    cmd.extend(["--node-parameters", "exclusive_storage=%s" % e_s])
239 6a0f22e1 Bernardo Dal Seno
  else:
240 6a0f22e1 Bernardo Dal Seno
    e_s = False
241 6a0f22e1 Bernardo Dal Seno
  qa_config.SetExclusiveStorage(e_s)
242 6a0f22e1 Bernardo Dal Seno
243 becf9d5c Michael Hanselmann
  extra_args = qa_config.get("cluster-init-args")
244 2c88200b Helga Velroyen
245 becf9d5c Michael Hanselmann
  if extra_args:
246 2c88200b Helga Velroyen
    # This option was removed in 2.10, but in order to not break QA of older
247 2c88200b Helga Velroyen
    # branches we remove it from the extra_args if it is in there.
248 2c88200b Helga Velroyen
    opt_drbd_storage = "--no-drbd-storage"
249 2c88200b Helga Velroyen
    if opt_drbd_storage in extra_args:
250 2c88200b Helga Velroyen
      extra_args.remove(opt_drbd_storage)
251 becf9d5c Michael Hanselmann
    cmd.extend(extra_args)
252 becf9d5c Michael Hanselmann
253 13d2e231 Andrea Spadaccini
  cmd.append(qa_config.get("name"))
254 becf9d5c Michael Hanselmann
255 2f4b4f78 Iustin Pop
  AssertCommand(cmd)
256 cec9845c Michael Hanselmann
257 5abecc1c Iustin Pop
  cmd = ["gnt-cluster", "modify"]
258 13d2e231 Andrea Spadaccini
259 5abecc1c Iustin Pop
  # hypervisor parameter modifications
260 5abecc1c Iustin Pop
  hvp = qa_config.get("hypervisor-parameters", {})
261 5abecc1c Iustin Pop
  for k, v in hvp.items():
262 5abecc1c Iustin Pop
    cmd.extend(["-H", "%s:%s" % (k, v)])
263 5abecc1c Iustin Pop
  # backend parameter modifications
264 5abecc1c Iustin Pop
  bep = qa_config.get("backend-parameters", "")
265 5abecc1c Iustin Pop
  if bep:
266 5abecc1c Iustin Pop
    cmd.extend(["-B", bep])
267 5abecc1c Iustin Pop
268 5abecc1c Iustin Pop
  if len(cmd) > 2:
269 5abecc1c Iustin Pop
    AssertCommand(cmd)
270 5abecc1c Iustin Pop
271 5abecc1c Iustin Pop
  # OS parameters
272 5abecc1c Iustin Pop
  osp = qa_config.get("os-parameters", {})
273 5abecc1c Iustin Pop
  for k, v in osp.items():
274 5abecc1c Iustin Pop
    AssertCommand(["gnt-os", "modify", "-O", v, k])
275 5abecc1c Iustin Pop
276 5abecc1c Iustin Pop
  # OS hypervisor parameters
277 5abecc1c Iustin Pop
  os_hvp = qa_config.get("os-hvp", {})
278 5abecc1c Iustin Pop
  for os_name in os_hvp:
279 5abecc1c Iustin Pop
    for hv, hvp in os_hvp[os_name].items():
280 5abecc1c Iustin Pop
      AssertCommand(["gnt-os", "modify", "-H", "%s:%s" % (hv, hvp), os_name])
281 5abecc1c Iustin Pop
282 cec9845c Michael Hanselmann
283 caea3b32 Iustin Pop
def TestClusterRename():
284 caea3b32 Iustin Pop
  """gnt-cluster rename"""
285 d0c8c01d Iustin Pop
  cmd = ["gnt-cluster", "rename", "-f"]
286 caea3b32 Iustin Pop
287 d0c8c01d Iustin Pop
  original_name = qa_config.get("name")
288 d0c8c01d Iustin Pop
  rename_target = qa_config.get("rename", None)
289 caea3b32 Iustin Pop
  if rename_target is None:
290 caea3b32 Iustin Pop
    print qa_utils.FormatError('"rename" entry is missing')
291 caea3b32 Iustin Pop
    return
292 caea3b32 Iustin Pop
293 2f4b4f78 Iustin Pop
  for data in [
294 2f4b4f78 Iustin Pop
    cmd + [rename_target],
295 c2a0947d Iustin Pop
    _CLUSTER_VERIFY,
296 2f4b4f78 Iustin Pop
    cmd + [original_name],
297 c2a0947d Iustin Pop
    _CLUSTER_VERIFY,
298 2f4b4f78 Iustin Pop
    ]:
299 2f4b4f78 Iustin Pop
    AssertCommand(data)
300 caea3b32 Iustin Pop
301 caea3b32 Iustin Pop
302 69df9d2b Iustin Pop
def TestClusterOob():
303 69df9d2b Iustin Pop
  """out-of-band framework"""
304 f55312bd René Nussbaumer
  oob_path_exists = "/tmp/ganeti-qa-oob-does-exist-%s" % utils.NewUUID()
305 f55312bd René Nussbaumer
306 c2a0947d Iustin Pop
  AssertCommand(_CLUSTER_VERIFY)
307 f55312bd René Nussbaumer
  AssertCommand(["gnt-cluster", "modify", "--node-parameters",
308 f55312bd René Nussbaumer
                 "oob_program=/tmp/ganeti-qa-oob-does-not-exist-%s" %
309 f55312bd René Nussbaumer
                 utils.NewUUID()])
310 f55312bd René Nussbaumer
311 c2a0947d Iustin Pop
  AssertCommand(_CLUSTER_VERIFY, fail=True)
312 f55312bd René Nussbaumer
313 69df9d2b Iustin Pop
  AssertCommand(["touch", oob_path_exists])
314 69df9d2b Iustin Pop
  AssertCommand(["chmod", "0400", oob_path_exists])
315 69df9d2b Iustin Pop
  AssertCommand(["gnt-cluster", "copyfile", oob_path_exists])
316 f55312bd René Nussbaumer
317 f55312bd René Nussbaumer
  try:
318 f55312bd René Nussbaumer
    AssertCommand(["gnt-cluster", "modify", "--node-parameters",
319 f55312bd René Nussbaumer
                   "oob_program=%s" % oob_path_exists])
320 f55312bd René Nussbaumer
321 c2a0947d Iustin Pop
    AssertCommand(_CLUSTER_VERIFY, fail=True)
322 f55312bd René Nussbaumer
323 69df9d2b Iustin Pop
    AssertCommand(["chmod", "0500", oob_path_exists])
324 69df9d2b Iustin Pop
    AssertCommand(["gnt-cluster", "copyfile", oob_path_exists])
325 f55312bd René Nussbaumer
326 c2a0947d Iustin Pop
    AssertCommand(_CLUSTER_VERIFY)
327 f55312bd René Nussbaumer
  finally:
328 69df9d2b Iustin Pop
    AssertCommand(["gnt-cluster", "command", "rm", oob_path_exists])
329 f55312bd René Nussbaumer
330 f55312bd René Nussbaumer
  AssertCommand(["gnt-cluster", "modify", "--node-parameters",
331 f55312bd René Nussbaumer
                 "oob_program="])
332 69df9d2b Iustin Pop
333 69df9d2b Iustin Pop
334 66d1f035 René Nussbaumer
def TestClusterEpo():
335 66d1f035 René Nussbaumer
  """gnt-cluster epo"""
336 66d1f035 René Nussbaumer
  master = qa_config.GetMasterNode()
337 66d1f035 René Nussbaumer
338 66d1f035 René Nussbaumer
  # Assert that OOB is unavailable for all nodes
339 aecba21e Michael Hanselmann
  result_output = GetCommandOutput(master.primary,
340 58ea8d17 Michael Hanselmann
                                   "gnt-node list --verbose --no-headers -o"
341 66d1f035 René Nussbaumer
                                   " powered")
342 66d1f035 René Nussbaumer
  AssertEqual(compat.all(powered == "(unavail)"
343 66d1f035 René Nussbaumer
                         for powered in result_output.splitlines()), True)
344 66d1f035 René Nussbaumer
345 66d1f035 René Nussbaumer
  # Conflicting
346 66d1f035 René Nussbaumer
  AssertCommand(["gnt-cluster", "epo", "--groups", "--all"], fail=True)
347 66d1f035 René Nussbaumer
  # --all doesn't expect arguments
348 66d1f035 René Nussbaumer
  AssertCommand(["gnt-cluster", "epo", "--all", "some_arg"], fail=True)
349 66d1f035 René Nussbaumer
350 66d1f035 René Nussbaumer
  # Unless --all is given master is not allowed to be in the list
351 aecba21e Michael Hanselmann
  AssertCommand(["gnt-cluster", "epo", "-f", master.primary], fail=True)
352 66d1f035 René Nussbaumer
353 66d1f035 René Nussbaumer
  # This shouldn't fail
354 66d1f035 René Nussbaumer
  AssertCommand(["gnt-cluster", "epo", "-f", "--all"])
355 66d1f035 René Nussbaumer
356 66d1f035 René Nussbaumer
  # All instances should have been stopped now
357 aecba21e Michael Hanselmann
  result_output = GetCommandOutput(master.primary,
358 58ea8d17 Michael Hanselmann
                                   "gnt-instance list --no-headers -o status")
359 3e0ed18c René Nussbaumer
  # ERROR_down because the instance is stopped but not recorded as such
360 3e0ed18c René Nussbaumer
  AssertEqual(compat.all(status == "ERROR_down"
361 66d1f035 René Nussbaumer
                         for status in result_output.splitlines()), True)
362 66d1f035 René Nussbaumer
363 66d1f035 René Nussbaumer
  # Now start everything again
364 66d1f035 René Nussbaumer
  AssertCommand(["gnt-cluster", "epo", "--on", "-f", "--all"])
365 66d1f035 René Nussbaumer
366 66d1f035 René Nussbaumer
  # All instances should have been started now
367 aecba21e Michael Hanselmann
  result_output = GetCommandOutput(master.primary,
368 58ea8d17 Michael Hanselmann
                                   "gnt-instance list --no-headers -o status")
369 66d1f035 René Nussbaumer
  AssertEqual(compat.all(status == "running"
370 66d1f035 René Nussbaumer
                         for status in result_output.splitlines()), True)
371 66d1f035 René Nussbaumer
372 66d1f035 René Nussbaumer
373 69df9d2b Iustin Pop
def TestClusterVerify():
374 69df9d2b Iustin Pop
  """gnt-cluster verify"""
375 c2a0947d Iustin Pop
  AssertCommand(_CLUSTER_VERIFY)
376 c6953b6e Iustin Pop
  AssertCommand(["gnt-cluster", "verify-disks"])
377 cec9845c Michael Hanselmann
378 1377433b Michael Hanselmann
379 7af293d7 Thomas Thrainer
def TestClusterVerifyDisksBrokenDRBD(instance, inst_nodes):
380 7af293d7 Thomas Thrainer
  """gnt-cluster verify-disks with broken DRBD"""
381 e4889779 Thomas Thrainer
  qa_daemon.TestPauseWatcher()
382 7af293d7 Thomas Thrainer
383 e4889779 Thomas Thrainer
  try:
384 e4889779 Thomas Thrainer
    info = qa_instance.GetInstanceInfo(instance.name)
385 e4889779 Thomas Thrainer
    snode = inst_nodes[1]
386 e4889779 Thomas Thrainer
    for idx, minor in enumerate(info["drbd-minors"][snode.primary]):
387 e4889779 Thomas Thrainer
      if idx % 2 == 0:
388 e4889779 Thomas Thrainer
        break_drbd_cmd = \
389 e4889779 Thomas Thrainer
          "(drbdsetup %d down >/dev/null 2>&1;" \
390 e4889779 Thomas Thrainer
          " drbdsetup down resource%d >/dev/null 2>&1) || /bin/true" % \
391 e4889779 Thomas Thrainer
          (minor, minor)
392 e4889779 Thomas Thrainer
      else:
393 e4889779 Thomas Thrainer
        break_drbd_cmd = \
394 e4889779 Thomas Thrainer
          "(drbdsetup %d detach >/dev/null 2>&1;" \
395 e4889779 Thomas Thrainer
          " drbdsetup detach %d >/dev/null 2>&1) || /bin/true" % \
396 e4889779 Thomas Thrainer
          (minor, minor)
397 e4889779 Thomas Thrainer
      AssertCommand(break_drbd_cmd, node=snode)
398 e4889779 Thomas Thrainer
399 e4889779 Thomas Thrainer
    verify_output = GetCommandOutput(qa_config.GetMasterNode().primary,
400 e4889779 Thomas Thrainer
                                     "gnt-cluster verify-disks")
401 e4889779 Thomas Thrainer
    activation_msg = "Activating disks for instance '%s'" % instance.name
402 e4889779 Thomas Thrainer
    if activation_msg not in verify_output:
403 e4889779 Thomas Thrainer
      raise qa_error.Error("gnt-cluster verify-disks did not activate broken"
404 e4889779 Thomas Thrainer
                           " DRBD disks:\n%s" % verify_output)
405 e4889779 Thomas Thrainer
406 e4889779 Thomas Thrainer
    verify_output = GetCommandOutput(qa_config.GetMasterNode().primary,
407 e4889779 Thomas Thrainer
                                     "gnt-cluster verify-disks")
408 e4889779 Thomas Thrainer
    if activation_msg in verify_output:
409 e4889779 Thomas Thrainer
      raise qa_error.Error("gnt-cluster verify-disks wants to activate broken"
410 e4889779 Thomas Thrainer
                           " DRBD disks on second attempt:\n%s" % verify_output)
411 e4889779 Thomas Thrainer
412 e4889779 Thomas Thrainer
    AssertCommand(_CLUSTER_VERIFY)
413 e4889779 Thomas Thrainer
  finally:
414 e4889779 Thomas Thrainer
    qa_daemon.TestResumeWatcher()
415 7af293d7 Thomas Thrainer
416 7af293d7 Thomas Thrainer
417 1377433b Michael Hanselmann
def TestJobqueue():
418 1377433b Michael Hanselmann
  """gnt-debug test-jobqueue"""
419 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-debug", "test-jobqueue"])
420 1377433b Michael Hanselmann
421 1377433b Michael Hanselmann
422 5a85b99e Michael Hanselmann
def TestDelay(node):
423 5a85b99e Michael Hanselmann
  """gnt-debug delay"""
424 5a85b99e Michael Hanselmann
  AssertCommand(["gnt-debug", "delay", "1"])
425 5a85b99e Michael Hanselmann
  AssertCommand(["gnt-debug", "delay", "--no-master", "1"])
426 5a85b99e Michael Hanselmann
  AssertCommand(["gnt-debug", "delay", "--no-master",
427 aecba21e Michael Hanselmann
                 "-n", node.primary, "1"])
428 5a85b99e Michael Hanselmann
429 5a85b99e Michael Hanselmann
430 452913ed Iustin Pop
def TestClusterReservedLvs():
431 452913ed Iustin Pop
  """gnt-cluster reserved lvs"""
432 bab4f56a Helga Velroyen
  # if no lvm-based templates are supported, skip the test
433 bab4f56a Helga Velroyen
  if not qa_config.IsStorageTypeSupported(constants.ST_LVM_VG):
434 bab4f56a Helga Velroyen
    return
435 23610ff8 Bernardo Dal Seno
  vgname = qa_config.get("vg-name", constants.DEFAULT_VG)
436 23610ff8 Bernardo Dal Seno
  lvname = _QA_LV_PREFIX + "test"
437 23610ff8 Bernardo Dal Seno
  lvfullname = "/".join([vgname, lvname])
438 b0e8ed3f Santi Raffa
439 b0e8ed3f Santi Raffa
  # Clean cluster
440 b0e8ed3f Santi Raffa
  AssertClusterVerify()
441 b0e8ed3f Santi Raffa
442 b0e8ed3f Santi Raffa
  AssertCommand(["gnt-cluster", "modify", "--reserved-lvs", ""])
443 b0e8ed3f Santi Raffa
  AssertCommand(["lvcreate", "-L1G", "-n", lvname, vgname])
444 b0e8ed3f Santi Raffa
  AssertClusterVerify(fail=False,
445 b0e8ed3f Santi Raffa
                      warnings=[constants.CV_ENODEORPHANLV])
446 b0e8ed3f Santi Raffa
447 b0e8ed3f Santi Raffa
  AssertCommand(["gnt-cluster", "modify", "--reserved-lvs",
448 b0e8ed3f Santi Raffa
                 "%s,.*/other-test" % lvfullname])
449 b0e8ed3f Santi Raffa
  AssertClusterVerify(no_warnings=[constants.CV_ENODEORPHANLV])
450 b0e8ed3f Santi Raffa
451 b0e8ed3f Santi Raffa
  AssertCommand(["gnt-cluster", "modify", "--reserved-lvs",
452 b0e8ed3f Santi Raffa
                ".*/%s.*" % _QA_LV_PREFIX])
453 b0e8ed3f Santi Raffa
  AssertClusterVerify(no_warnings=[constants.CV_ENODEORPHANLV])
454 b0e8ed3f Santi Raffa
455 b0e8ed3f Santi Raffa
  AssertCommand(["gnt-cluster", "modify", "--reserved-lvs", ""])
456 b0e8ed3f Santi Raffa
  AssertClusterVerify(fail=False,
457 b0e8ed3f Santi Raffa
                      warnings=[constants.CV_ENODEORPHANLV])
458 b0e8ed3f Santi Raffa
459 b0e8ed3f Santi Raffa
  AssertCommand(["lvremove", "-f", lvfullname])
460 b0e8ed3f Santi Raffa
  AssertClusterVerify()
461 452913ed Iustin Pop
462 cec9845c Michael Hanselmann
463 1e7acc3b Iustin Pop
def TestClusterModifyEmpty():
464 1e7acc3b Iustin Pop
  """gnt-cluster modify"""
465 1e7acc3b Iustin Pop
  AssertCommand(["gnt-cluster", "modify"], fail=True)
466 1e7acc3b Iustin Pop
467 1e7acc3b Iustin Pop
468 92cb4940 Andrea Spadaccini
def TestClusterModifyDisk():
469 92cb4940 Andrea Spadaccini
  """gnt-cluster modify -D"""
470 92cb4940 Andrea Spadaccini
  for param in _FAIL_PARAMS:
471 92cb4940 Andrea Spadaccini
    AssertCommand(["gnt-cluster", "modify", "-D", param], fail=True)
472 92cb4940 Andrea Spadaccini
473 92cb4940 Andrea Spadaccini
474 b24b52d9 Helga Velroyen
def _GetOtherEnabledDiskTemplate(undesired_disk_templates,
475 b24b52d9 Helga Velroyen
                                 enabled_disk_templates):
476 b24b52d9 Helga Velroyen
  """Returns one template that is not in the undesired set.
477 b24b52d9 Helga Velroyen

478 b24b52d9 Helga Velroyen
  @type undesired_disk_templates: list of string
479 b24b52d9 Helga Velroyen
  @param undesired_disk_templates: a list of disk templates that we want to
480 b24b52d9 Helga Velroyen
      exclude when drawing one disk template from the list of enabled
481 b24b52d9 Helga Velroyen
      disk templates
482 b24b52d9 Helga Velroyen
  @type enabled_disk_templates: list of string
483 b24b52d9 Helga Velroyen
  @param enabled_disk_templates: list of enabled disk templates (in QA)
484 b24b52d9 Helga Velroyen

485 b24b52d9 Helga Velroyen
  """
486 b24b52d9 Helga Velroyen
  desired_templates = list(set(enabled_disk_templates)
487 b24b52d9 Helga Velroyen
                                - set(undesired_disk_templates))
488 b24b52d9 Helga Velroyen
  if desired_templates:
489 b24b52d9 Helga Velroyen
    template = desired_templates[0]
490 b24b52d9 Helga Velroyen
  else:
491 b24b52d9 Helga Velroyen
    # If no desired disk template is available for QA, choose 'diskless' and
492 b24b52d9 Helga Velroyen
    # hope for the best.
493 b24b52d9 Helga Velroyen
    template = constants.ST_DISKLESS
494 b24b52d9 Helga Velroyen
495 b24b52d9 Helga Velroyen
  return template
496 b24b52d9 Helga Velroyen
497 b24b52d9 Helga Velroyen
498 b24b52d9 Helga Velroyen
def TestClusterModifyFileBasedStorageDir(
499 b24b52d9 Helga Velroyen
    file_disk_template, dir_config_key, default_dir, option_name):
500 b24b52d9 Helga Velroyen
  """Tests gnt-cluster modify wrt to file-based directory options.
501 b24b52d9 Helga Velroyen

502 b24b52d9 Helga Velroyen
  @type file_disk_template: string
503 b24b52d9 Helga Velroyen
  @param file_disk_template: file-based disk template
504 b24b52d9 Helga Velroyen
  @type dir_config_key: string
505 b24b52d9 Helga Velroyen
  @param dir_config_key: key for the QA config to retrieve the default
506 b24b52d9 Helga Velroyen
     directory value
507 b24b52d9 Helga Velroyen
  @type default_dir: string
508 b24b52d9 Helga Velroyen
  @param default_dir: default directory, if the QA config does not specify
509 b24b52d9 Helga Velroyen
     it
510 b24b52d9 Helga Velroyen
  @type option_name: string
511 b24b52d9 Helga Velroyen
  @param option_name: name of the option of 'gnt-cluster modify' to
512 b24b52d9 Helga Velroyen
     change the directory
513 b24b52d9 Helga Velroyen

514 b24b52d9 Helga Velroyen
  """
515 b24b52d9 Helga Velroyen
  enabled_disk_templates = qa_config.GetEnabledDiskTemplates()
516 a09639d1 Santi Raffa
  assert file_disk_template in constants.DTS_FILEBASED
517 b24b52d9 Helga Velroyen
  if not qa_config.IsTemplateSupported(file_disk_template):
518 b24b52d9 Helga Velroyen
    return
519 b24b52d9 Helga Velroyen
520 b24b52d9 Helga Velroyen
  # Get some non-file-based disk template to disable file storage
521 b24b52d9 Helga Velroyen
  other_disk_template = _GetOtherEnabledDiskTemplate(
522 5a904197 Santi Raffa
    utils.storage.GetDiskTemplatesOfStorageTypes(constants.ST_FILE,
523 5a904197 Santi Raffa
                                                 constants.ST_SHARED_FILE),
524 5a904197 Santi Raffa
    enabled_disk_templates
525 5a904197 Santi Raffa
  )
526 b24b52d9 Helga Velroyen
527 b24b52d9 Helga Velroyen
  file_storage_dir = qa_config.get(dir_config_key, default_dir)
528 b24b52d9 Helga Velroyen
  invalid_file_storage_dir = "/boot/"
529 b24b52d9 Helga Velroyen
530 b24b52d9 Helga Velroyen
  for fail, cmd in [
531 b24b52d9 Helga Velroyen
    (False, ["gnt-cluster", "modify",
532 e0e44476 Michele Tartara
             "--enabled-disk-templates=%s" % file_disk_template,
533 e0e44476 Michele Tartara
             "--ipolicy-disk-templates=%s" % file_disk_template]),
534 b24b52d9 Helga Velroyen
    (False, ["gnt-cluster", "modify",
535 e0e44476 Michele Tartara
             "--%s=%s" % (option_name, file_storage_dir)]),
536 b24b52d9 Helga Velroyen
    (False, ["gnt-cluster", "modify",
537 e0e44476 Michele Tartara
             "--%s=%s" % (option_name, invalid_file_storage_dir)]),
538 b24b52d9 Helga Velroyen
    # file storage dir is set to an inacceptable path, thus verify
539 b24b52d9 Helga Velroyen
    # should fail
540 b24b52d9 Helga Velroyen
    (True, ["gnt-cluster", "verify"]),
541 b24b52d9 Helga Velroyen
    # unsetting the storage dir while file storage is enabled
542 b24b52d9 Helga Velroyen
    # should fail
543 b24b52d9 Helga Velroyen
    (True, ["gnt-cluster", "modify",
544 b24b52d9 Helga Velroyen
            "--%s=" % option_name]),
545 b24b52d9 Helga Velroyen
    (False, ["gnt-cluster", "modify",
546 e0e44476 Michele Tartara
             "--%s=%s" % (option_name, file_storage_dir)]),
547 b24b52d9 Helga Velroyen
    (False, ["gnt-cluster", "modify",
548 e0e44476 Michele Tartara
             "--enabled-disk-templates=%s" % other_disk_template,
549 e0e44476 Michele Tartara
             "--ipolicy-disk-templates=%s" % other_disk_template]),
550 b24b52d9 Helga Velroyen
    (False, ["gnt-cluster", "modify",
551 e0e44476 Michele Tartara
             "--%s=%s" % (option_name, invalid_file_storage_dir)]),
552 b24b52d9 Helga Velroyen
    # file storage is set to an inacceptable path, but file storage
553 b24b52d9 Helga Velroyen
    # is disabled, thus verify should not fail
554 b24b52d9 Helga Velroyen
    (False, ["gnt-cluster", "verify"]),
555 b24b52d9 Helga Velroyen
    # unsetting the file storage dir while file storage is not enabled
556 b24b52d9 Helga Velroyen
    # should be fine
557 b24b52d9 Helga Velroyen
    (False, ["gnt-cluster", "modify",
558 e0e44476 Michele Tartara
             "--%s=" % option_name]),
559 b24b52d9 Helga Velroyen
    # resetting everything to sane values
560 b24b52d9 Helga Velroyen
    (False, ["gnt-cluster", "modify",
561 e0e44476 Michele Tartara
             "--%s=%s" % (option_name, file_storage_dir),
562 e0e44476 Michele Tartara
             "--enabled-disk-templates=%s" % ",".join(enabled_disk_templates),
563 e0e44476 Michele Tartara
             "--ipolicy-disk-templates=%s" % ",".join(enabled_disk_templates)])
564 b24b52d9 Helga Velroyen
    ]:
565 b24b52d9 Helga Velroyen
    AssertCommand(cmd, fail=fail)
566 b24b52d9 Helga Velroyen
567 b24b52d9 Helga Velroyen
568 b24b52d9 Helga Velroyen
def TestClusterModifyFileStorageDir():
569 b24b52d9 Helga Velroyen
  """gnt-cluster modify --file-storage-dir=..."""
570 b24b52d9 Helga Velroyen
  TestClusterModifyFileBasedStorageDir(
571 b24b52d9 Helga Velroyen
      constants.DT_FILE, "default-file-storage-dir",
572 b24b52d9 Helga Velroyen
      pathutils.DEFAULT_FILE_STORAGE_DIR,
573 b24b52d9 Helga Velroyen
      "file-storage-dir")
574 b24b52d9 Helga Velroyen
575 b24b52d9 Helga Velroyen
576 b24b52d9 Helga Velroyen
def TestClusterModifySharedFileStorageDir():
577 b24b52d9 Helga Velroyen
  """gnt-cluster modify --shared-file-storage-dir=..."""
578 b24b52d9 Helga Velroyen
  TestClusterModifyFileBasedStorageDir(
579 b24b52d9 Helga Velroyen
      constants.DT_SHARED_FILE, "default-shared-file-storage-dir",
580 b24b52d9 Helga Velroyen
      pathutils.DEFAULT_SHARED_FILE_STORAGE_DIR,
581 b24b52d9 Helga Velroyen
      "shared-file-storage-dir")
582 b24b52d9 Helga Velroyen
583 b24b52d9 Helga Velroyen
584 2dae8d64 Helga Velroyen
def TestClusterModifyDiskTemplates():
585 2dae8d64 Helga Velroyen
  """gnt-cluster modify --enabled-disk-templates=..."""
586 462f0faa Helga Velroyen
  enabled_disk_templates = qa_config.GetEnabledDiskTemplates()
587 2dae8d64 Helga Velroyen
  default_disk_template = qa_config.GetDefaultDiskTemplate()
588 462f0faa Helga Velroyen
589 a25f44a4 Helga Velroyen
  _TestClusterModifyDiskTemplatesArguments(default_disk_template)
590 a25f44a4 Helga Velroyen
  _TestClusterModifyDiskTemplatesDrbdHelper(enabled_disk_templates)
591 912737ba Helga Velroyen
  _TestClusterModifyDiskTemplatesVgName(enabled_disk_templates)
592 462f0faa Helga Velroyen
593 462f0faa Helga Velroyen
  _RestoreEnabledDiskTemplates()
594 462f0faa Helga Velroyen
  nodes = qa_config.AcquireManyNodes(2)
595 462f0faa Helga Velroyen
596 462f0faa Helga Velroyen
  instance_template = enabled_disk_templates[0]
597 462f0faa Helga Velroyen
  instance = qa_instance.CreateInstanceByDiskTemplate(nodes, instance_template)
598 462f0faa Helga Velroyen
599 462f0faa Helga Velroyen
  _TestClusterModifyUsedDiskTemplate(instance_template,
600 462f0faa Helga Velroyen
                                     enabled_disk_templates)
601 462f0faa Helga Velroyen
602 462f0faa Helga Velroyen
  qa_instance.TestInstanceRemove(instance)
603 462f0faa Helga Velroyen
  _RestoreEnabledDiskTemplates()
604 462f0faa Helga Velroyen
605 462f0faa Helga Velroyen
606 462f0faa Helga Velroyen
def _RestoreEnabledDiskTemplates():
607 462f0faa Helga Velroyen
  """Sets the list of enabled disk templates back to the list of enabled disk
608 462f0faa Helga Velroyen
     templates from the QA configuration. This can be used to make sure that
609 462f0faa Helga Velroyen
     the tests that modify the list of disk templates do not interfere with
610 462f0faa Helga Velroyen
     other tests.
611 462f0faa Helga Velroyen

612 462f0faa Helga Velroyen
  """
613 eb161df2 Helga Velroyen
  enabled_disk_templates = qa_config.GetEnabledDiskTemplates()
614 eb161df2 Helga Velroyen
  cmd = ["gnt-cluster", "modify",
615 eb161df2 Helga Velroyen
         "--enabled-disk-templates=%s" % ",".join(enabled_disk_templates),
616 eb161df2 Helga Velroyen
         "--ipolicy-disk-templates=%s" % ",".join(enabled_disk_templates),
617 eb161df2 Helga Velroyen
         ]
618 d101b7be Thomas Thrainer
619 d101b7be Thomas Thrainer
  if utils.IsLvmEnabled(qa_config.GetEnabledDiskTemplates()):
620 d101b7be Thomas Thrainer
    vgname = qa_config.get("vg-name", constants.DEFAULT_VG)
621 d101b7be Thomas Thrainer
    cmd.append("--vg-name=%s" % vgname)
622 d101b7be Thomas Thrainer
623 d101b7be Thomas Thrainer
  AssertCommand(cmd, fail=False)
624 462f0faa Helga Velroyen
625 462f0faa Helga Velroyen
626 a25f44a4 Helga Velroyen
def _TestClusterModifyDiskTemplatesDrbdHelper(enabled_disk_templates):
627 a25f44a4 Helga Velroyen
  """Tests argument handling of 'gnt-cluster modify' with respect to
628 a25f44a4 Helga Velroyen
     the parameter '--drbd-usermode-helper'. This test is independent
629 a25f44a4 Helga Velroyen
     of instances.
630 a25f44a4 Helga Velroyen

631 a25f44a4 Helga Velroyen
  """
632 a25f44a4 Helga Velroyen
  _RestoreEnabledDiskTemplates()
633 a25f44a4 Helga Velroyen
634 a25f44a4 Helga Velroyen
  if constants.DT_DRBD8 not in enabled_disk_templates:
635 a25f44a4 Helga Velroyen
    return
636 a25f44a4 Helga Velroyen
  if constants.DT_PLAIN not in enabled_disk_templates:
637 a25f44a4 Helga Velroyen
    return
638 a25f44a4 Helga Velroyen
639 a25f44a4 Helga Velroyen
  drbd_usermode_helper = qa_config.get("drbd-usermode-helper", "/bin/true")
640 a25f44a4 Helga Velroyen
  bogus_usermode_helper = "/tmp/pinkbunny"
641 317a3fdb Michele Tartara
  for command, fail in [
642 317a3fdb Michele Tartara
    (["gnt-cluster", "modify",
643 317a3fdb Michele Tartara
      "--enabled-disk-templates=%s" % constants.DT_DRBD8,
644 317a3fdb Michele Tartara
      "--ipolicy-disk-templates=%s" % constants.DT_DRBD8], False),
645 317a3fdb Michele Tartara
    (["gnt-cluster", "modify",
646 317a3fdb Michele Tartara
      "--drbd-usermode-helper=%s" % drbd_usermode_helper], False),
647 317a3fdb Michele Tartara
    (["gnt-cluster", "modify",
648 317a3fdb Michele Tartara
      "--drbd-usermode-helper=%s" % bogus_usermode_helper], True),
649 317a3fdb Michele Tartara
    # unsetting helper when DRBD is enabled should not work
650 317a3fdb Michele Tartara
    (["gnt-cluster", "modify",
651 317a3fdb Michele Tartara
      "--drbd-usermode-helper="], True),
652 317a3fdb Michele Tartara
    (["gnt-cluster", "modify",
653 317a3fdb Michele Tartara
      "--enabled-disk-templates=%s" % constants.DT_PLAIN,
654 317a3fdb Michele Tartara
      "--ipolicy-disk-templates=%s" % constants.DT_PLAIN], False),
655 317a3fdb Michele Tartara
    (["gnt-cluster", "modify",
656 317a3fdb Michele Tartara
      "--drbd-usermode-helper="], False),
657 317a3fdb Michele Tartara
    (["gnt-cluster", "modify",
658 317a3fdb Michele Tartara
      "--drbd-usermode-helper=%s" % drbd_usermode_helper], False),
659 317a3fdb Michele Tartara
    (["gnt-cluster", "modify",
660 317a3fdb Michele Tartara
      "--drbd-usermode-helper=%s" % drbd_usermode_helper,
661 317a3fdb Michele Tartara
      "--enabled-disk-templates=%s" % constants.DT_DRBD8,
662 317a3fdb Michele Tartara
      "--ipolicy-disk-templates=%s" % constants.DT_DRBD8], False),
663 317a3fdb Michele Tartara
    (["gnt-cluster", "modify",
664 317a3fdb Michele Tartara
      "--drbd-usermode-helper=",
665 317a3fdb Michele Tartara
      "--enabled-disk-templates=%s" % constants.DT_PLAIN,
666 317a3fdb Michele Tartara
      "--ipolicy-disk-templates=%s" % constants.DT_PLAIN], False),
667 317a3fdb Michele Tartara
    (["gnt-cluster", "modify",
668 317a3fdb Michele Tartara
      "--drbd-usermode-helper=%s" % drbd_usermode_helper,
669 317a3fdb Michele Tartara
      "--enabled-disk-templates=%s" % constants.DT_DRBD8,
670 317a3fdb Michele Tartara
      "--ipolicy-disk-templates=%s" % constants.DT_DRBD8], False),
671 317a3fdb Michele Tartara
    ]:
672 a25f44a4 Helga Velroyen
    AssertCommand(command, fail=fail)
673 a25f44a4 Helga Velroyen
  _RestoreEnabledDiskTemplates()
674 a25f44a4 Helga Velroyen
675 a25f44a4 Helga Velroyen
676 a25f44a4 Helga Velroyen
def _TestClusterModifyDiskTemplatesArguments(default_disk_template):
677 462f0faa Helga Velroyen
  """Tests argument handling of 'gnt-cluster modify' with respect to
678 462f0faa Helga Velroyen
     the parameter '--enabled-disk-templates'. This test is independent
679 462f0faa Helga Velroyen
     of instances.
680 462f0faa Helga Velroyen

681 462f0faa Helga Velroyen
  """
682 912737ba Helga Velroyen
  _RestoreEnabledDiskTemplates()
683 462f0faa Helga Velroyen
684 2dae8d64 Helga Velroyen
  # bogus templates
685 dacd8ba4 Helga Velroyen
  AssertCommand(["gnt-cluster", "modify",
686 2dae8d64 Helga Velroyen
                 "--enabled-disk-templates=pinkbunny"],
687 dacd8ba4 Helga Velroyen
                fail=True)
688 462f0faa Helga Velroyen
689 dacd8ba4 Helga Velroyen
  # duplicate entries do no harm
690 dacd8ba4 Helga Velroyen
  AssertCommand(
691 dacd8ba4 Helga Velroyen
    ["gnt-cluster", "modify",
692 2dae8d64 Helga Velroyen
     "--enabled-disk-templates=%s,%s" %
693 eb161df2 Helga Velroyen
      (default_disk_template, default_disk_template),
694 eb161df2 Helga Velroyen
     "--ipolicy-disk-templates=%s" % default_disk_template],
695 dacd8ba4 Helga Velroyen
    fail=False)
696 462f0faa Helga Velroyen
697 912737ba Helga Velroyen
698 912737ba Helga Velroyen
def _TestClusterModifyDiskTemplatesVgName(enabled_disk_templates):
699 912737ba Helga Velroyen
  """Tests argument handling of 'gnt-cluster modify' with respect to
700 912737ba Helga Velroyen
     the parameter '--enabled-disk-templates' and '--vg-name'. This test is
701 912737ba Helga Velroyen
     independent of instances.
702 912737ba Helga Velroyen

703 912737ba Helga Velroyen
  """
704 912737ba Helga Velroyen
  if not utils.IsLvmEnabled(enabled_disk_templates):
705 912737ba Helga Velroyen
    # These tests only make sense if lvm is enabled for QA
706 912737ba Helga Velroyen
    return
707 912737ba Helga Velroyen
708 912737ba Helga Velroyen
  # determine an LVM and a non-LVM disk template for the tests
709 d48c944b Helga Velroyen
  non_lvm_template = _GetOtherEnabledDiskTemplate(constants.DTS_LVM,
710 b24b52d9 Helga Velroyen
                                                  enabled_disk_templates)
711 d48c944b Helga Velroyen
  lvm_template = list(set(enabled_disk_templates) & constants.DTS_LVM)[0]
712 912737ba Helga Velroyen
713 912737ba Helga Velroyen
  vgname = qa_config.get("vg-name", constants.DEFAULT_VG)
714 912737ba Helga Velroyen
715 912737ba Helga Velroyen
  # Clean start: unset volume group name, disable lvm storage
716 912737ba Helga Velroyen
  AssertCommand(
717 912737ba Helga Velroyen
    ["gnt-cluster", "modify",
718 912737ba Helga Velroyen
     "--enabled-disk-templates=%s" % non_lvm_template,
719 eb161df2 Helga Velroyen
     "--ipolicy-disk-templates=%s" % non_lvm_template,
720 912737ba Helga Velroyen
     "--vg-name="],
721 912737ba Helga Velroyen
    fail=False)
722 912737ba Helga Velroyen
723 912737ba Helga Velroyen
  # Try to enable lvm, when no volume group is given
724 912737ba Helga Velroyen
  AssertCommand(
725 912737ba Helga Velroyen
    ["gnt-cluster", "modify",
726 eb161df2 Helga Velroyen
     "--enabled-disk-templates=%s" % lvm_template,
727 eb161df2 Helga Velroyen
     "--ipolicy-disk-templates=%s" % lvm_template],
728 912737ba Helga Velroyen
    fail=True)
729 912737ba Helga Velroyen
730 912737ba Helga Velroyen
  # Set volume group, with lvm still disabled: just a warning
731 912737ba Helga Velroyen
  AssertCommand(["gnt-cluster", "modify", "--vg-name=%s" % vgname], fail=False)
732 912737ba Helga Velroyen
733 912737ba Helga Velroyen
  # Try unsetting vg name and enabling lvm at the same time
734 912737ba Helga Velroyen
  AssertCommand(
735 912737ba Helga Velroyen
    ["gnt-cluster", "modify",
736 912737ba Helga Velroyen
     "--enabled-disk-templates=%s" % lvm_template,
737 eb161df2 Helga Velroyen
     "--ipolicy-disk-templates=%s" % lvm_template,
738 912737ba Helga Velroyen
     "--vg-name="],
739 912737ba Helga Velroyen
    fail=True)
740 912737ba Helga Velroyen
741 912737ba Helga Velroyen
  # Enable lvm with vg name present
742 912737ba Helga Velroyen
  AssertCommand(
743 912737ba Helga Velroyen
    ["gnt-cluster", "modify",
744 eb161df2 Helga Velroyen
     "--enabled-disk-templates=%s" % lvm_template,
745 eb161df2 Helga Velroyen
     "--ipolicy-disk-templates=%s" % lvm_template],
746 912737ba Helga Velroyen
    fail=False)
747 912737ba Helga Velroyen
748 912737ba Helga Velroyen
  # Try unsetting vg name with lvm still enabled
749 912737ba Helga Velroyen
  AssertCommand(["gnt-cluster", "modify", "--vg-name="], fail=True)
750 912737ba Helga Velroyen
751 912737ba Helga Velroyen
  # Disable lvm with vg name still set
752 912737ba Helga Velroyen
  AssertCommand(
753 eb161df2 Helga Velroyen
    ["gnt-cluster", "modify",
754 eb161df2 Helga Velroyen
     "--enabled-disk-templates=%s" % non_lvm_template,
755 eb161df2 Helga Velroyen
     "--ipolicy-disk-templates=%s" % non_lvm_template,
756 eb161df2 Helga Velroyen
     ],
757 912737ba Helga Velroyen
    fail=False)
758 912737ba Helga Velroyen
759 912737ba Helga Velroyen
  # Try unsetting vg name with lvm disabled
760 912737ba Helga Velroyen
  AssertCommand(["gnt-cluster", "modify", "--vg-name="], fail=False)
761 912737ba Helga Velroyen
762 912737ba Helga Velroyen
  # Set vg name and enable lvm at the same time
763 912737ba Helga Velroyen
  AssertCommand(
764 912737ba Helga Velroyen
    ["gnt-cluster", "modify",
765 912737ba Helga Velroyen
     "--enabled-disk-templates=%s" % lvm_template,
766 eb161df2 Helga Velroyen
     "--ipolicy-disk-templates=%s" % lvm_template,
767 912737ba Helga Velroyen
     "--vg-name=%s" % vgname],
768 912737ba Helga Velroyen
    fail=False)
769 912737ba Helga Velroyen
770 912737ba Helga Velroyen
  # Unset vg name and disable lvm at the same time
771 912737ba Helga Velroyen
  AssertCommand(
772 912737ba Helga Velroyen
    ["gnt-cluster", "modify",
773 912737ba Helga Velroyen
     "--enabled-disk-templates=%s" % non_lvm_template,
774 eb161df2 Helga Velroyen
     "--ipolicy-disk-templates=%s" % non_lvm_template,
775 912737ba Helga Velroyen
     "--vg-name="],
776 912737ba Helga Velroyen
    fail=False)
777 912737ba Helga Velroyen
778 912737ba Helga Velroyen
  _RestoreEnabledDiskTemplates()
779 912737ba Helga Velroyen
780 462f0faa Helga Velroyen
781 462f0faa Helga Velroyen
def _TestClusterModifyUsedDiskTemplate(instance_template,
782 462f0faa Helga Velroyen
                                       enabled_disk_templates):
783 462f0faa Helga Velroyen
  """Tests that disk templates that are currently in use by instances cannot
784 462f0faa Helga Velroyen
     be disabled on the cluster.
785 462f0faa Helga Velroyen

786 462f0faa Helga Velroyen
  """
787 462f0faa Helga Velroyen
  # If the list of enabled disk templates contains only one template
788 462f0faa Helga Velroyen
  # we need to add some other templates, because the list of enabled disk
789 462f0faa Helga Velroyen
  # templates can only be set to a non-empty list.
790 462f0faa Helga Velroyen
  new_disk_templates = list(set(enabled_disk_templates)
791 462f0faa Helga Velroyen
                              - set([instance_template]))
792 462f0faa Helga Velroyen
  if not new_disk_templates:
793 d101b7be Thomas Thrainer
    new_disk_templates = list(set([constants.DT_DISKLESS, constants.DT_BLOCK])
794 462f0faa Helga Velroyen
                                - set([instance_template]))
795 462f0faa Helga Velroyen
  AssertCommand(
796 462f0faa Helga Velroyen
    ["gnt-cluster", "modify",
797 eb161df2 Helga Velroyen
     "--enabled-disk-templates=%s" % ",".join(new_disk_templates),
798 eb161df2 Helga Velroyen
     "--ipolicy-disk-templates=%s" % ",".join(new_disk_templates)],
799 462f0faa Helga Velroyen
    fail=True)
800 462f0faa Helga Velroyen
801 462f0faa Helga Velroyen
802 9738ca94 Iustin Pop
def TestClusterModifyBe():
803 9738ca94 Iustin Pop
  """gnt-cluster modify -B"""
804 2f4b4f78 Iustin Pop
  for fail, cmd in [
805 8ccbbe4b Guido Trotter
    # max/min mem
806 8ccbbe4b Guido Trotter
    (False, ["gnt-cluster", "modify", "-B", "maxmem=256"]),
807 8ccbbe4b Guido Trotter
    (False, ["sh", "-c", "gnt-cluster info|grep '^ *maxmem: 256$'"]),
808 8ccbbe4b Guido Trotter
    (False, ["gnt-cluster", "modify", "-B", "minmem=256"]),
809 8ccbbe4b Guido Trotter
    (False, ["sh", "-c", "gnt-cluster info|grep '^ *minmem: 256$'"]),
810 8ccbbe4b Guido Trotter
    (True, ["gnt-cluster", "modify", "-B", "maxmem=a"]),
811 8ccbbe4b Guido Trotter
    (False, ["sh", "-c", "gnt-cluster info|grep '^ *maxmem: 256$'"]),
812 8ccbbe4b Guido Trotter
    (True, ["gnt-cluster", "modify", "-B", "minmem=a"]),
813 8ccbbe4b Guido Trotter
    (False, ["sh", "-c", "gnt-cluster info|grep '^ *minmem: 256$'"]),
814 8ccbbe4b Guido Trotter
    (False, ["gnt-cluster", "modify", "-B", "maxmem=128,minmem=128"]),
815 8ccbbe4b Guido Trotter
    (False, ["sh", "-c", "gnt-cluster info|grep '^ *maxmem: 128$'"]),
816 8ccbbe4b Guido Trotter
    (False, ["sh", "-c", "gnt-cluster info|grep '^ *minmem: 128$'"]),
817 9738ca94 Iustin Pop
    # vcpus
818 2f4b4f78 Iustin Pop
    (False, ["gnt-cluster", "modify", "-B", "vcpus=4"]),
819 2f4b4f78 Iustin Pop
    (False, ["sh", "-c", "gnt-cluster info|grep '^ *vcpus: 4$'"]),
820 21bf2e2e Andrea Spadaccini
    (True, ["gnt-cluster", "modify", "-B", "vcpus=a"]),
821 2f4b4f78 Iustin Pop
    (False, ["gnt-cluster", "modify", "-B", "vcpus=1"]),
822 2f4b4f78 Iustin Pop
    (False, ["sh", "-c", "gnt-cluster info|grep '^ *vcpus: 1$'"]),
823 9738ca94 Iustin Pop
    # auto_balance
824 2f4b4f78 Iustin Pop
    (False, ["gnt-cluster", "modify", "-B", "auto_balance=False"]),
825 2f4b4f78 Iustin Pop
    (False, ["sh", "-c", "gnt-cluster info|grep '^ *auto_balance: False$'"]),
826 21bf2e2e Andrea Spadaccini
    (True, ["gnt-cluster", "modify", "-B", "auto_balance=1"]),
827 2f4b4f78 Iustin Pop
    (False, ["gnt-cluster", "modify", "-B", "auto_balance=True"]),
828 2f4b4f78 Iustin Pop
    (False, ["sh", "-c", "gnt-cluster info|grep '^ *auto_balance: True$'"]),
829 9738ca94 Iustin Pop
    ]:
830 2f4b4f78 Iustin Pop
    AssertCommand(cmd, fail=fail)
831 9738ca94 Iustin Pop
832 5abecc1c Iustin Pop
  # redo the original-requested BE parameters, if any
833 5abecc1c Iustin Pop
  bep = qa_config.get("backend-parameters", "")
834 5abecc1c Iustin Pop
  if bep:
835 5abecc1c Iustin Pop
    AssertCommand(["gnt-cluster", "modify", "-B", bep])
836 9738ca94 Iustin Pop
837 21bf2e2e Andrea Spadaccini
838 b3f3aa3d Bernardo Dal Seno
def _GetClusterIPolicy():
839 b3f3aa3d Bernardo Dal Seno
  """Return the run-time values of the cluster-level instance policy.
840 b3f3aa3d Bernardo Dal Seno

841 b3f3aa3d Bernardo Dal Seno
  @rtype: tuple
842 b3f3aa3d Bernardo Dal Seno
  @return: (policy, specs), where:
843 b3f3aa3d Bernardo Dal Seno
      - policy is a dictionary of the policy values, instance specs excluded
844 7c8ae421 Bernardo Dal Seno
      - specs is a dictionary containing only the specs, using the internal
845 7c8ae421 Bernardo Dal Seno
        format (see L{constants.IPOLICY_DEFAULTS} for an example)
846 b3f3aa3d Bernardo Dal Seno

847 b3f3aa3d Bernardo Dal Seno
  """
848 0e79564a Bernardo Dal Seno
  info = qa_utils.GetObjectInfo(["gnt-cluster", "info"])
849 0e79564a Bernardo Dal Seno
  policy = info["Instance policy - limits for instances"]
850 63e08b25 Bernardo Dal Seno
  (ret_policy, ret_specs) = qa_utils.ParseIPolicy(policy)
851 0e79564a Bernardo Dal Seno
852 b3f3aa3d Bernardo Dal Seno
  # Sanity checks
853 7c8ae421 Bernardo Dal Seno
  assert "minmax" in ret_specs and "std" in ret_specs
854 7c8ae421 Bernardo Dal Seno
  assert len(ret_specs["minmax"]) > 0
855 0e79564a Bernardo Dal Seno
  assert len(ret_policy) > 0
856 0e79564a Bernardo Dal Seno
  return (ret_policy, ret_specs)
857 b3f3aa3d Bernardo Dal Seno
858 b3f3aa3d Bernardo Dal Seno
859 b3f3aa3d Bernardo Dal Seno
def TestClusterModifyIPolicy():
860 b3f3aa3d Bernardo Dal Seno
  """gnt-cluster modify --ipolicy-*"""
861 b3f3aa3d Bernardo Dal Seno
  basecmd = ["gnt-cluster", "modify"]
862 b3f3aa3d Bernardo Dal Seno
  (old_policy, old_specs) = _GetClusterIPolicy()
863 b3f3aa3d Bernardo Dal Seno
  for par in ["vcpu-ratio", "spindle-ratio"]:
864 b3f3aa3d Bernardo Dal Seno
    curr_val = float(old_policy[par])
865 b3f3aa3d Bernardo Dal Seno
    test_values = [
866 b3f3aa3d Bernardo Dal Seno
      (True, 1.0),
867 b3f3aa3d Bernardo Dal Seno
      (True, 1.5),
868 b3f3aa3d Bernardo Dal Seno
      (True, 2),
869 b3f3aa3d Bernardo Dal Seno
      (False, "a"),
870 b3f3aa3d Bernardo Dal Seno
      # Restore the old value
871 b3f3aa3d Bernardo Dal Seno
      (True, curr_val),
872 b3f3aa3d Bernardo Dal Seno
      ]
873 b3f3aa3d Bernardo Dal Seno
    for (good, val) in test_values:
874 b3f3aa3d Bernardo Dal Seno
      cmd = basecmd + ["--ipolicy-%s=%s" % (par, val)]
875 b3f3aa3d Bernardo Dal Seno
      AssertCommand(cmd, fail=not good)
876 b3f3aa3d Bernardo Dal Seno
      if good:
877 b3f3aa3d Bernardo Dal Seno
        curr_val = val
878 b3f3aa3d Bernardo Dal Seno
      # Check the affected parameter
879 b3f3aa3d Bernardo Dal Seno
      (eff_policy, eff_specs) = _GetClusterIPolicy()
880 b3f3aa3d Bernardo Dal Seno
      AssertEqual(float(eff_policy[par]), curr_val)
881 b3f3aa3d Bernardo Dal Seno
      # Check everything else
882 b3f3aa3d Bernardo Dal Seno
      AssertEqual(eff_specs, old_specs)
883 b3f3aa3d Bernardo Dal Seno
      for p in eff_policy.keys():
884 b3f3aa3d Bernardo Dal Seno
        if p == par:
885 b3f3aa3d Bernardo Dal Seno
          continue
886 b3f3aa3d Bernardo Dal Seno
        AssertEqual(eff_policy[p], old_policy[p])
887 b3f3aa3d Bernardo Dal Seno
888 ae3ab08b Helga Velroyen
  # Allowing disk templates via ipolicy requires them to be
889 ae3ab08b Helga Velroyen
  # enabled on the cluster.
890 ae3ab08b Helga Velroyen
  if not (qa_config.IsTemplateSupported(constants.DT_PLAIN)
891 ae3ab08b Helga Velroyen
          and qa_config.IsTemplateSupported(constants.DT_DRBD8)):
892 ae3ab08b Helga Velroyen
    return
893 b3f3aa3d Bernardo Dal Seno
  # Disk templates are treated slightly differently
894 b3f3aa3d Bernardo Dal Seno
  par = "disk-templates"
895 9db0b351 Bernardo Dal Seno
  disp_str = "allowed disk templates"
896 b3f3aa3d Bernardo Dal Seno
  curr_val = old_policy[disp_str]
897 b3f3aa3d Bernardo Dal Seno
  test_values = [
898 b3f3aa3d Bernardo Dal Seno
    (True, constants.DT_PLAIN),
899 b3f3aa3d Bernardo Dal Seno
    (True, "%s,%s" % (constants.DT_PLAIN, constants.DT_DRBD8)),
900 b3f3aa3d Bernardo Dal Seno
    (False, "thisisnotadisktemplate"),
901 b3f3aa3d Bernardo Dal Seno
    (False, ""),
902 b3f3aa3d Bernardo Dal Seno
    # Restore the old value
903 b3f3aa3d Bernardo Dal Seno
    (True, curr_val.replace(" ", "")),
904 b3f3aa3d Bernardo Dal Seno
    ]
905 b3f3aa3d Bernardo Dal Seno
  for (good, val) in test_values:
906 b3f3aa3d Bernardo Dal Seno
    cmd = basecmd + ["--ipolicy-%s=%s" % (par, val)]
907 b3f3aa3d Bernardo Dal Seno
    AssertCommand(cmd, fail=not good)
908 b3f3aa3d Bernardo Dal Seno
    if good:
909 b3f3aa3d Bernardo Dal Seno
      curr_val = val
910 b3f3aa3d Bernardo Dal Seno
    # Check the affected parameter
911 b3f3aa3d Bernardo Dal Seno
    (eff_policy, eff_specs) = _GetClusterIPolicy()
912 b3f3aa3d Bernardo Dal Seno
    AssertEqual(eff_policy[disp_str].replace(" ", ""), curr_val)
913 b3f3aa3d Bernardo Dal Seno
    # Check everything else
914 b3f3aa3d Bernardo Dal Seno
    AssertEqual(eff_specs, old_specs)
915 b3f3aa3d Bernardo Dal Seno
    for p in eff_policy.keys():
916 b3f3aa3d Bernardo Dal Seno
      if p == disp_str:
917 b3f3aa3d Bernardo Dal Seno
        continue
918 b3f3aa3d Bernardo Dal Seno
      AssertEqual(eff_policy[p], old_policy[p])
919 b3f3aa3d Bernardo Dal Seno
920 b3f3aa3d Bernardo Dal Seno
921 ec996117 Bernardo Dal Seno
def TestClusterSetISpecs(new_specs=None, diff_specs=None, fail=False,
922 ec996117 Bernardo Dal Seno
                         old_values=None):
923 b3f3aa3d Bernardo Dal Seno
  """Change instance specs.
924 b3f3aa3d Bernardo Dal Seno

925 ec996117 Bernardo Dal Seno
  At most one of new_specs or diff_specs can be specified.
926 ec996117 Bernardo Dal Seno

927 ec996117 Bernardo Dal Seno
  @type new_specs: dict
928 ec996117 Bernardo Dal Seno
  @param new_specs: new complete specs, in the same format returned by
929 ec996117 Bernardo Dal Seno
      L{_GetClusterIPolicy}
930 ec996117 Bernardo Dal Seno
  @type diff_specs: dict
931 7c8ae421 Bernardo Dal Seno
  @param diff_specs: partial specs, it can be an incomplete specifications, but
932 7c8ae421 Bernardo Dal Seno
      if min/max specs are specified, their number must match the number of the
933 7c8ae421 Bernardo Dal Seno
      existing specs
934 b3f3aa3d Bernardo Dal Seno
  @type fail: bool
935 b3f3aa3d Bernardo Dal Seno
  @param fail: if the change is expected to fail
936 b3f3aa3d Bernardo Dal Seno
  @type old_values: tuple
937 b3f3aa3d Bernardo Dal Seno
  @param old_values: (old_policy, old_specs), as returned by
938 ec996117 Bernardo Dal Seno
      L{_GetClusterIPolicy}
939 b3f3aa3d Bernardo Dal Seno
  @return: same as L{_GetClusterIPolicy}
940 b3f3aa3d Bernardo Dal Seno

941 b3f3aa3d Bernardo Dal Seno
  """
942 63e08b25 Bernardo Dal Seno
  build_cmd = lambda opts: ["gnt-cluster", "modify"] + opts
943 ec996117 Bernardo Dal Seno
  return qa_utils.TestSetISpecs(
944 ec996117 Bernardo Dal Seno
    new_specs=new_specs, diff_specs=diff_specs,
945 ec996117 Bernardo Dal Seno
    get_policy_fn=_GetClusterIPolicy, build_cmd_fn=build_cmd,
946 ec996117 Bernardo Dal Seno
    fail=fail, old_values=old_values)
947 b3f3aa3d Bernardo Dal Seno
948 b3f3aa3d Bernardo Dal Seno
949 b3f3aa3d Bernardo Dal Seno
def TestClusterModifyISpecs():
950 b3f3aa3d Bernardo Dal Seno
  """gnt-cluster modify --specs-*"""
951 cb178a1e Bernardo Dal Seno
  params = ["memory-size", "disk-size", "disk-count", "cpu-count", "nic-count"]
952 b3f3aa3d Bernardo Dal Seno
  (cur_policy, cur_specs) = _GetClusterIPolicy()
953 7c8ae421 Bernardo Dal Seno
  # This test assumes that there is only one min/max bound
954 7c8ae421 Bernardo Dal Seno
  assert len(cur_specs[constants.ISPECS_MINMAX]) == 1
955 b3f3aa3d Bernardo Dal Seno
  for par in params:
956 b3f3aa3d Bernardo Dal Seno
    test_values = [
957 b3f3aa3d Bernardo Dal Seno
      (True, 0, 4, 12),
958 b3f3aa3d Bernardo Dal Seno
      (True, 4, 4, 12),
959 b3f3aa3d Bernardo Dal Seno
      (True, 4, 12, 12),
960 b3f3aa3d Bernardo Dal Seno
      (True, 4, 4, 4),
961 b3f3aa3d Bernardo Dal Seno
      (False, 4, 0, 12),
962 b3f3aa3d Bernardo Dal Seno
      (False, 4, 16, 12),
963 b3f3aa3d Bernardo Dal Seno
      (False, 4, 4, 0),
964 b3f3aa3d Bernardo Dal Seno
      (False, 12, 4, 4),
965 b3f3aa3d Bernardo Dal Seno
      (False, 12, 4, 0),
966 b3f3aa3d Bernardo Dal Seno
      (False, "a", 4, 12),
967 b3f3aa3d Bernardo Dal Seno
      (False, 0, "a", 12),
968 b3f3aa3d Bernardo Dal Seno
      (False, 0, 4, "a"),
969 b3f3aa3d Bernardo Dal Seno
      # This is to restore the old values
970 b3f3aa3d Bernardo Dal Seno
      (True,
971 7c8ae421 Bernardo Dal Seno
       cur_specs[constants.ISPECS_MINMAX][0][constants.ISPECS_MIN][par],
972 7c8ae421 Bernardo Dal Seno
       cur_specs[constants.ISPECS_STD][par],
973 7c8ae421 Bernardo Dal Seno
       cur_specs[constants.ISPECS_MINMAX][0][constants.ISPECS_MAX][par])
974 b3f3aa3d Bernardo Dal Seno
      ]
975 b3f3aa3d Bernardo Dal Seno
    for (good, mn, st, mx) in test_values:
976 ec996117 Bernardo Dal Seno
      new_vals = {
977 7c8ae421 Bernardo Dal Seno
        constants.ISPECS_MINMAX: [{
978 7c8ae421 Bernardo Dal Seno
          constants.ISPECS_MIN: {par: mn},
979 7c8ae421 Bernardo Dal Seno
          constants.ISPECS_MAX: {par: mx}
980 7c8ae421 Bernardo Dal Seno
          }],
981 7c8ae421 Bernardo Dal Seno
        constants.ISPECS_STD: {par: st}
982 ec996117 Bernardo Dal Seno
        }
983 b3f3aa3d Bernardo Dal Seno
      cur_state = (cur_policy, cur_specs)
984 b3f3aa3d Bernardo Dal Seno
      # We update cur_specs, as we've copied the values to restore already
985 ec996117 Bernardo Dal Seno
      (cur_policy, cur_specs) = TestClusterSetISpecs(
986 ec996117 Bernardo Dal Seno
        diff_specs=new_vals, fail=not good, old_values=cur_state)
987 b3f3aa3d Bernardo Dal Seno
988 b5a93c73 Bernardo Dal Seno
    # Get the ipolicy command
989 b5a93c73 Bernardo Dal Seno
    mnode = qa_config.GetMasterNode()
990 b5a93c73 Bernardo Dal Seno
    initcmd = GetCommandOutput(mnode.primary, "gnt-cluster show-ispecs-cmd")
991 b5a93c73 Bernardo Dal Seno
    modcmd = ["gnt-cluster", "modify"]
992 b5a93c73 Bernardo Dal Seno
    opts = initcmd.split()
993 b5a93c73 Bernardo Dal Seno
    assert opts[0:2] == ["gnt-cluster", "init"]
994 b5a93c73 Bernardo Dal Seno
    for k in range(2, len(opts) - 1):
995 b5a93c73 Bernardo Dal Seno
      if opts[k].startswith("--ipolicy-"):
996 b5a93c73 Bernardo Dal Seno
        assert k + 2 <= len(opts)
997 b5a93c73 Bernardo Dal Seno
        modcmd.extend(opts[k:k + 2])
998 b5a93c73 Bernardo Dal Seno
    # Re-apply the ipolicy (this should be a no-op)
999 b5a93c73 Bernardo Dal Seno
    AssertCommand(modcmd)
1000 b5a93c73 Bernardo Dal Seno
    new_initcmd = GetCommandOutput(mnode.primary, "gnt-cluster show-ispecs-cmd")
1001 b5a93c73 Bernardo Dal Seno
    AssertEqual(initcmd, new_initcmd)
1002 b3f3aa3d Bernardo Dal Seno
1003 b3f3aa3d Bernardo Dal Seno
1004 cec9845c Michael Hanselmann
def TestClusterInfo():
1005 cec9845c Michael Hanselmann
  """gnt-cluster info"""
1006 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-cluster", "info"])
1007 283f9d4c Michael Hanselmann
1008 283f9d4c Michael Hanselmann
1009 3e8b5a9c Iustin Pop
def TestClusterRedistConf():
1010 3e8b5a9c Iustin Pop
  """gnt-cluster redist-conf"""
1011 3e8b5a9c Iustin Pop
  AssertCommand(["gnt-cluster", "redist-conf"])
1012 3e8b5a9c Iustin Pop
1013 3e8b5a9c Iustin Pop
1014 283f9d4c Michael Hanselmann
def TestClusterGetmaster():
1015 283f9d4c Michael Hanselmann
  """gnt-cluster getmaster"""
1016 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-cluster", "getmaster"])
1017 283f9d4c Michael Hanselmann
1018 283f9d4c Michael Hanselmann
1019 283f9d4c Michael Hanselmann
def TestClusterVersion():
1020 283f9d4c Michael Hanselmann
  """gnt-cluster version"""
1021 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-cluster", "version"])
1022 cec9845c Michael Hanselmann
1023 cec9845c Michael Hanselmann
1024 6d4a1656 Michael Hanselmann
def TestClusterRenewCrypto():
1025 6d4a1656 Michael Hanselmann
  """gnt-cluster renew-crypto"""
1026 6d4a1656 Michael Hanselmann
  master = qa_config.GetMasterNode()
1027 6d4a1656 Michael Hanselmann
1028 6d4a1656 Michael Hanselmann
  # Conflicting options
1029 6d4a1656 Michael Hanselmann
  cmd = ["gnt-cluster", "renew-crypto", "--force",
1030 3db3eb2a Michael Hanselmann
         "--new-cluster-certificate", "--new-confd-hmac-key"]
1031 3db3eb2a Michael Hanselmann
  conflicting = [
1032 3db3eb2a Michael Hanselmann
    ["--new-rapi-certificate", "--rapi-certificate=/dev/null"],
1033 3db3eb2a Michael Hanselmann
    ["--new-cluster-domain-secret", "--cluster-domain-secret=/dev/null"],
1034 3db3eb2a Michael Hanselmann
    ]
1035 3db3eb2a Michael Hanselmann
  for i in conflicting:
1036 21bf2e2e Andrea Spadaccini
    AssertCommand(cmd + i, fail=True)
1037 6d4a1656 Michael Hanselmann
1038 6d4a1656 Michael Hanselmann
  # Invalid RAPI certificate
1039 6d4a1656 Michael Hanselmann
  cmd = ["gnt-cluster", "renew-crypto", "--force",
1040 6d4a1656 Michael Hanselmann
         "--rapi-certificate=/dev/null"]
1041 2f4b4f78 Iustin Pop
  AssertCommand(cmd, fail=True)
1042 6d4a1656 Michael Hanselmann
1043 aecba21e Michael Hanselmann
  rapi_cert_backup = qa_utils.BackupFile(master.primary,
1044 304d9f02 Michael Hanselmann
                                         pathutils.RAPI_CERT_FILE)
1045 502f5236 Michael Hanselmann
  try:
1046 502f5236 Michael Hanselmann
    # Custom RAPI certificate
1047 502f5236 Michael Hanselmann
    fh = tempfile.NamedTemporaryFile()
1048 6d4a1656 Michael Hanselmann
1049 502f5236 Michael Hanselmann
    # Ensure certificate doesn't cause "gnt-cluster verify" to complain
1050 502f5236 Michael Hanselmann
    validity = constants.SSL_CERT_EXPIRATION_WARN * 3
1051 6d4a1656 Michael Hanselmann
1052 5e26633b Michael Hanselmann
    utils.GenerateSelfSignedSslCert(fh.name, validity=validity)
1053 6d4a1656 Michael Hanselmann
1054 aecba21e Michael Hanselmann
    tmpcert = qa_utils.UploadFile(master.primary, fh.name)
1055 502f5236 Michael Hanselmann
    try:
1056 2f4b4f78 Iustin Pop
      AssertCommand(["gnt-cluster", "renew-crypto", "--force",
1057 2f4b4f78 Iustin Pop
                     "--rapi-certificate=%s" % tmpcert])
1058 502f5236 Michael Hanselmann
    finally:
1059 2f4b4f78 Iustin Pop
      AssertCommand(["rm", "-f", tmpcert])
1060 502f5236 Michael Hanselmann
1061 5e26633b Michael Hanselmann
    # Custom cluster domain secret
1062 5e26633b Michael Hanselmann
    cds_fh = tempfile.NamedTemporaryFile()
1063 5e26633b Michael Hanselmann
    cds_fh.write(utils.GenerateSecret())
1064 5e26633b Michael Hanselmann
    cds_fh.write("\n")
1065 5e26633b Michael Hanselmann
    cds_fh.flush()
1066 5e26633b Michael Hanselmann
1067 aecba21e Michael Hanselmann
    tmpcds = qa_utils.UploadFile(master.primary, cds_fh.name)
1068 5e26633b Michael Hanselmann
    try:
1069 2f4b4f78 Iustin Pop
      AssertCommand(["gnt-cluster", "renew-crypto", "--force",
1070 2f4b4f78 Iustin Pop
                     "--cluster-domain-secret=%s" % tmpcds])
1071 5e26633b Michael Hanselmann
    finally:
1072 2f4b4f78 Iustin Pop
      AssertCommand(["rm", "-f", tmpcds])
1073 5e26633b Michael Hanselmann
1074 502f5236 Michael Hanselmann
    # Normal case
1075 2f4b4f78 Iustin Pop
    AssertCommand(["gnt-cluster", "renew-crypto", "--force",
1076 2f4b4f78 Iustin Pop
                   "--new-cluster-certificate", "--new-confd-hmac-key",
1077 2f4b4f78 Iustin Pop
                   "--new-rapi-certificate", "--new-cluster-domain-secret"])
1078 3db3eb2a Michael Hanselmann
1079 502f5236 Michael Hanselmann
    # Restore RAPI certificate
1080 2f4b4f78 Iustin Pop
    AssertCommand(["gnt-cluster", "renew-crypto", "--force",
1081 2f4b4f78 Iustin Pop
                   "--rapi-certificate=%s" % rapi_cert_backup])
1082 3db3eb2a Michael Hanselmann
  finally:
1083 2f4b4f78 Iustin Pop
    AssertCommand(["rm", "-f", rapi_cert_backup])
1084 3db3eb2a Michael Hanselmann
1085 6d4a1656 Michael Hanselmann
1086 cec9845c Michael Hanselmann
def TestClusterBurnin():
1087 cec9845c Michael Hanselmann
  """Burnin"""
1088 cec9845c Michael Hanselmann
  master = qa_config.GetMasterNode()
1089 cec9845c Michael Hanselmann
1090 d0c8c01d Iustin Pop
  options = qa_config.get("options", {})
1091 68c8c3df Michael Hanselmann
  disk_template = options.get("burnin-disk-template", constants.DT_DRBD8)
1092 d0c8c01d Iustin Pop
  parallel = options.get("burnin-in-parallel", False)
1093 d0c8c01d Iustin Pop
  check_inst = options.get("burnin-check-instances", False)
1094 d0c8c01d Iustin Pop
  do_rename = options.get("burnin-rename", "")
1095 d0c8c01d Iustin Pop
  do_reboot = options.get("burnin-reboot", True)
1096 1d103c02 Iustin Pop
  reboot_types = options.get("reboot-types", constants.REBOOT_TYPES)
1097 23103544 Michael Hanselmann
1098 cec9845c Michael Hanselmann
  # Get as many instances as we need
1099 cec9845c Michael Hanselmann
  instances = []
1100 cec9845c Michael Hanselmann
  try:
1101 23103544 Michael Hanselmann
    try:
1102 d0c8c01d Iustin Pop
      num = qa_config.get("options", {}).get("burnin-instances", 1)
1103 f1501b3f Michael Hanselmann
      for _ in range(0, num):
1104 23103544 Michael Hanselmann
        instances.append(qa_config.AcquireInstance())
1105 23103544 Michael Hanselmann
    except qa_error.OutOfInstancesError:
1106 23103544 Michael Hanselmann
      print "Not enough instances, continuing anyway."
1107 cec9845c Michael Hanselmann
1108 23103544 Michael Hanselmann
    if len(instances) < 1:
1109 23103544 Michael Hanselmann
      raise qa_error.Error("Burnin needs at least one instance")
1110 cec9845c Michael Hanselmann
1111 aecba21e Michael Hanselmann
    script = qa_utils.UploadFile(master.primary, "../tools/burnin")
1112 cec9845c Michael Hanselmann
    try:
1113 090128b6 Christos Stavrakakis
      disks = qa_config.GetDiskOptions()
1114 23103544 Michael Hanselmann
      # Run burnin
1115 5d9d1aff Klaus Aehlig
      cmd = ["env",
1116 b8669a69 Jose A. Lopes
             "PYTHONPATH=%s" % _constants.VERSIONEDSHAREDIR,
1117 5d9d1aff Klaus Aehlig
             script,
1118 d0c8c01d Iustin Pop
             "--os=%s" % qa_config.get("os"),
1119 f356202a Guido Trotter
             "--minmem-size=%s" % qa_config.get(constants.BE_MINMEM),
1120 f356202a Guido Trotter
             "--maxmem-size=%s" % qa_config.get(constants.BE_MAXMEM),
1121 090128b6 Christos Stavrakakis
             "--disk-size=%s" % ",".join([d.get("size") for d in disks]),
1122 090128b6 Christos Stavrakakis
             "--disk-growth=%s" % ",".join([d.get("growth") for d in disks]),
1123 d0c8c01d Iustin Pop
             "--disk-template=%s" % disk_template]
1124 0b0a150a Iustin Pop
      if parallel:
1125 d0c8c01d Iustin Pop
        cmd.append("--parallel")
1126 d0c8c01d Iustin Pop
        cmd.append("--early-release")
1127 0b0a150a Iustin Pop
      if check_inst:
1128 d0c8c01d Iustin Pop
        cmd.append("--http-check")
1129 4dc76b24 Iustin Pop
      if do_rename:
1130 d0c8c01d Iustin Pop
        cmd.append("--rename=%s" % do_rename)
1131 58598264 Iustin Pop
      if not do_reboot:
1132 d0c8c01d Iustin Pop
        cmd.append("--no-reboot")
1133 1d103c02 Iustin Pop
      else:
1134 d0c8c01d Iustin Pop
        cmd.append("--reboot-types=%s" % ",".join(reboot_types))
1135 b5f33afa Michael Hanselmann
      cmd += [inst.name for inst in instances]
1136 2f4b4f78 Iustin Pop
      AssertCommand(cmd)
1137 cec9845c Michael Hanselmann
    finally:
1138 2f4b4f78 Iustin Pop
      AssertCommand(["rm", "-f", script])
1139 2f4b4f78 Iustin Pop
1140 cec9845c Michael Hanselmann
  finally:
1141 cec9845c Michael Hanselmann
    for inst in instances:
1142 6f88e076 Michael Hanselmann
      inst.Release()
1143 cec9845c Michael Hanselmann
1144 cec9845c Michael Hanselmann
1145 cec9845c Michael Hanselmann
def TestClusterMasterFailover():
1146 c28502b1 Iustin Pop
  """gnt-cluster master-failover"""
1147 cec9845c Michael Hanselmann
  master = qa_config.GetMasterNode()
1148 cec9845c Michael Hanselmann
  failovermaster = qa_config.AcquireNode(exclude=master)
1149 cec9845c Michael Hanselmann
1150 2f4b4f78 Iustin Pop
  cmd = ["gnt-cluster", "master-failover"]
1151 c9e52e4c Helga Velroyen
  node_list_cmd = ["gnt-node", "list"]
1152 2f4b4f78 Iustin Pop
  try:
1153 2f4b4f78 Iustin Pop
    AssertCommand(cmd, node=failovermaster)
1154 c9e52e4c Helga Velroyen
    AssertCommand(node_list_cmd, node=failovermaster)
1155 ff699aa9 Michael Hanselmann
    # Back to original master node
1156 2f4b4f78 Iustin Pop
    AssertCommand(cmd, node=master)
1157 c9e52e4c Helga Velroyen
    AssertCommand(node_list_cmd, node=master)
1158 cec9845c Michael Hanselmann
  finally:
1159 565cb4bf Michael Hanselmann
    failovermaster.Release()
1160 cec9845c Michael Hanselmann
1161 cec9845c Michael Hanselmann
1162 aa104b5e Klaus Aehlig
def TestUpgrade():
1163 aa104b5e Klaus Aehlig
  """Test gnt-cluster upgrade.
1164 aa104b5e Klaus Aehlig

1165 aa104b5e Klaus Aehlig
  This tests the 'gnt-cluster upgrade' command by flipping
1166 aa104b5e Klaus Aehlig
  between the current and a different version of Ganeti.
1167 aa104b5e Klaus Aehlig
  To also recover subtile points in the configuration up/down
1168 aa104b5e Klaus Aehlig
  grades, instances are left over both upgrades.
1169 aa104b5e Klaus Aehlig

1170 aa104b5e Klaus Aehlig
  """
1171 aa104b5e Klaus Aehlig
  this_version = qa_config.get("dir-version")
1172 aa104b5e Klaus Aehlig
  other_version = qa_config.get("other-dir-version")
1173 aa104b5e Klaus Aehlig
  if this_version is None or other_version is None:
1174 aa104b5e Klaus Aehlig
    print qa_utils.FormatInfo("Test not run, as versions not specified")
1175 aa104b5e Klaus Aehlig
    return
1176 aa104b5e Klaus Aehlig
1177 aa104b5e Klaus Aehlig
  inst_creates = []
1178 aa104b5e Klaus Aehlig
  upgrade_instances = qa_config.get("upgrade-instances", [])
1179 aa104b5e Klaus Aehlig
  live_instances = []
1180 aa104b5e Klaus Aehlig
  for (test_name, templ, cf, n) in qa_instance.available_instance_tests:
1181 aa104b5e Klaus Aehlig
    if (qa_config.TestEnabled(test_name) and
1182 aa104b5e Klaus Aehlig
        qa_config.IsTemplateSupported(templ) and
1183 aa104b5e Klaus Aehlig
        templ in upgrade_instances):
1184 aa104b5e Klaus Aehlig
      inst_creates.append((cf, n))
1185 9c3dcbbf Klaus Aehlig
1186 aa104b5e Klaus Aehlig
  for (cf, n) in inst_creates:
1187 aa104b5e Klaus Aehlig
    nodes = qa_config.AcquireManyNodes(n)
1188 aa104b5e Klaus Aehlig
    live_instances.append(cf(nodes))
1189 aa104b5e Klaus Aehlig
1190 aa104b5e Klaus Aehlig
  AssertCommand(["gnt-cluster", "upgrade", "--to", other_version])
1191 aa104b5e Klaus Aehlig
  AssertCommand(["gnt-cluster", "verify"])
1192 aa104b5e Klaus Aehlig
1193 aa104b5e Klaus Aehlig
  for instance in live_instances:
1194 aa104b5e Klaus Aehlig
    qa_instance.TestInstanceRemove(instance)
1195 aa104b5e Klaus Aehlig
    instance.Release()
1196 aa104b5e Klaus Aehlig
  live_instances = []
1197 aa104b5e Klaus Aehlig
  for (cf, n) in inst_creates:
1198 aa104b5e Klaus Aehlig
    nodes = qa_config.AcquireManyNodes(n)
1199 aa104b5e Klaus Aehlig
    live_instances.append(cf(nodes))
1200 aa104b5e Klaus Aehlig
1201 aa104b5e Klaus Aehlig
  AssertCommand(["gnt-cluster", "upgrade", "--to", this_version])
1202 aa104b5e Klaus Aehlig
  AssertCommand(["gnt-cluster", "verify"])
1203 aa104b5e Klaus Aehlig
1204 aa104b5e Klaus Aehlig
  for instance in live_instances:
1205 aa104b5e Klaus Aehlig
    qa_instance.TestInstanceRemove(instance)
1206 aa104b5e Klaus Aehlig
    instance.Release()
1207 aa104b5e Klaus Aehlig
1208 d80e2abe Santi Raffa
1209 2df92990 Michael Hanselmann
def _NodeQueueDrainFile(node):
1210 2df92990 Michael Hanselmann
  """Returns path to queue drain file for a node.
1211 2df92990 Michael Hanselmann

1212 2df92990 Michael Hanselmann
  """
1213 2df92990 Michael Hanselmann
  return qa_utils.MakeNodePath(node, pathutils.JOB_QUEUE_DRAIN_FILE)
1214 2df92990 Michael Hanselmann
1215 2df92990 Michael Hanselmann
1216 2df92990 Michael Hanselmann
def _AssertDrainFile(node, **kwargs):
1217 2df92990 Michael Hanselmann
  """Checks for the queue drain file.
1218 2df92990 Michael Hanselmann

1219 2df92990 Michael Hanselmann
  """
1220 2df92990 Michael Hanselmann
  AssertCommand(["test", "-f", _NodeQueueDrainFile(node)], node=node, **kwargs)
1221 2df92990 Michael Hanselmann
1222 2df92990 Michael Hanselmann
1223 ff699aa9 Michael Hanselmann
def TestClusterMasterFailoverWithDrainedQueue():
1224 ff699aa9 Michael Hanselmann
  """gnt-cluster master-failover with drained queue"""
1225 ff699aa9 Michael Hanselmann
  master = qa_config.GetMasterNode()
1226 ff699aa9 Michael Hanselmann
  failovermaster = qa_config.AcquireNode(exclude=master)
1227 ff699aa9 Michael Hanselmann
1228 ff699aa9 Michael Hanselmann
  # Ensure queue is not drained
1229 ff699aa9 Michael Hanselmann
  for node in [master, failovermaster]:
1230 2df92990 Michael Hanselmann
    _AssertDrainFile(node, fail=True)
1231 ff699aa9 Michael Hanselmann
1232 ff699aa9 Michael Hanselmann
  # Drain queue on failover master
1233 2df92990 Michael Hanselmann
  AssertCommand(["touch", _NodeQueueDrainFile(failovermaster)],
1234 2df92990 Michael Hanselmann
                node=failovermaster)
1235 ff699aa9 Michael Hanselmann
1236 ff699aa9 Michael Hanselmann
  cmd = ["gnt-cluster", "master-failover"]
1237 ff699aa9 Michael Hanselmann
  try:
1238 2df92990 Michael Hanselmann
    _AssertDrainFile(failovermaster)
1239 ff699aa9 Michael Hanselmann
    AssertCommand(cmd, node=failovermaster)
1240 2df92990 Michael Hanselmann
    _AssertDrainFile(master, fail=True)
1241 2df92990 Michael Hanselmann
    _AssertDrainFile(failovermaster, fail=True)
1242 ff699aa9 Michael Hanselmann
1243 ff699aa9 Michael Hanselmann
    # Back to original master node
1244 ff699aa9 Michael Hanselmann
    AssertCommand(cmd, node=master)
1245 ff699aa9 Michael Hanselmann
  finally:
1246 565cb4bf Michael Hanselmann
    failovermaster.Release()
1247 ff699aa9 Michael Hanselmann
1248 2df92990 Michael Hanselmann
  # Ensure queue is not drained
1249 2df92990 Michael Hanselmann
  for node in [master, failovermaster]:
1250 2df92990 Michael Hanselmann
    _AssertDrainFile(node, fail=True)
1251 ff699aa9 Michael Hanselmann
1252 ff699aa9 Michael Hanselmann
1253 cec9845c Michael Hanselmann
def TestClusterCopyfile():
1254 cec9845c Michael Hanselmann
  """gnt-cluster copyfile"""
1255 cec9845c Michael Hanselmann
  master = qa_config.GetMasterNode()
1256 cec9845c Michael Hanselmann
1257 24818e8f Michael Hanselmann
  uniqueid = utils.NewUUID()
1258 830da270 Michael Hanselmann
1259 cec9845c Michael Hanselmann
  # Create temporary file
1260 cec9845c Michael Hanselmann
  f = tempfile.NamedTemporaryFile()
1261 830da270 Michael Hanselmann
  f.write(uniqueid)
1262 cec9845c Michael Hanselmann
  f.flush()
1263 cec9845c Michael Hanselmann
  f.seek(0)
1264 cec9845c Michael Hanselmann
1265 cec9845c Michael Hanselmann
  # Upload file to master node
1266 aecba21e Michael Hanselmann
  testname = qa_utils.UploadFile(master.primary, f.name)
1267 cec9845c Michael Hanselmann
  try:
1268 cec9845c Michael Hanselmann
    # Copy file to all nodes
1269 2f4b4f78 Iustin Pop
    AssertCommand(["gnt-cluster", "copyfile", testname])
1270 830da270 Michael Hanselmann
    _CheckFileOnAllNodes(testname, uniqueid)
1271 cec9845c Michael Hanselmann
  finally:
1272 830da270 Michael Hanselmann
    _RemoveFileFromAllNodes(testname)
1273 830da270 Michael Hanselmann
1274 830da270 Michael Hanselmann
1275 830da270 Michael Hanselmann
def TestClusterCommand():
1276 830da270 Michael Hanselmann
  """gnt-cluster command"""
1277 24818e8f Michael Hanselmann
  uniqueid = utils.NewUUID()
1278 24818e8f Michael Hanselmann
  rfile = "/tmp/gnt%s" % utils.NewUUID()
1279 d0c8c01d Iustin Pop
  rcmd = utils.ShellQuoteArgs(["echo", "-n", uniqueid])
1280 d0c8c01d Iustin Pop
  cmd = utils.ShellQuoteArgs(["gnt-cluster", "command",
1281 830da270 Michael Hanselmann
                              "%s >%s" % (rcmd, rfile)])
1282 830da270 Michael Hanselmann
1283 830da270 Michael Hanselmann
  try:
1284 2f4b4f78 Iustin Pop
    AssertCommand(cmd)
1285 830da270 Michael Hanselmann
    _CheckFileOnAllNodes(rfile, uniqueid)
1286 830da270 Michael Hanselmann
  finally:
1287 830da270 Michael Hanselmann
    _RemoveFileFromAllNodes(rfile)
1288 cec9845c Michael Hanselmann
1289 cec9845c Michael Hanselmann
1290 cec9845c Michael Hanselmann
def TestClusterDestroy():
1291 cec9845c Michael Hanselmann
  """gnt-cluster destroy"""
1292 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-cluster", "destroy", "--yes-do-it"])
1293 65a884ef Iustin Pop
1294 65a884ef Iustin Pop
1295 65a884ef Iustin Pop
def TestClusterRepairDiskSizes():
1296 65a884ef Iustin Pop
  """gnt-cluster repair-disk-sizes"""
1297 65a884ef Iustin Pop
  AssertCommand(["gnt-cluster", "repair-disk-sizes"])
1298 50ef6a41 Bernardo Dal Seno
1299 50ef6a41 Bernardo Dal Seno
1300 50ef6a41 Bernardo Dal Seno
def TestSetExclStorCluster(newvalue):
1301 50ef6a41 Bernardo Dal Seno
  """Set the exclusive_storage node parameter at the cluster level.
1302 50ef6a41 Bernardo Dal Seno

1303 50ef6a41 Bernardo Dal Seno
  @type newvalue: bool
1304 50ef6a41 Bernardo Dal Seno
  @param newvalue: New value of exclusive_storage
1305 50ef6a41 Bernardo Dal Seno
  @rtype: bool
1306 50ef6a41 Bernardo Dal Seno
  @return: The old value of exclusive_storage
1307 50ef6a41 Bernardo Dal Seno

1308 50ef6a41 Bernardo Dal Seno
  """
1309 0e79564a Bernardo Dal Seno
  es_path = ["Default node parameters", "exclusive_storage"]
1310 0e79564a Bernardo Dal Seno
  oldvalue = _GetClusterField(es_path)
1311 50ef6a41 Bernardo Dal Seno
  AssertCommand(["gnt-cluster", "modify", "--node-parameters",
1312 50ef6a41 Bernardo Dal Seno
                 "exclusive_storage=%s" % newvalue])
1313 0e79564a Bernardo Dal Seno
  effvalue = _GetClusterField(es_path)
1314 50ef6a41 Bernardo Dal Seno
  if effvalue != newvalue:
1315 50ef6a41 Bernardo Dal Seno
    raise qa_error.Error("exclusive_storage has the wrong value: %s instead"
1316 50ef6a41 Bernardo Dal Seno
                         " of %s" % (effvalue, newvalue))
1317 6a0f22e1 Bernardo Dal Seno
  qa_config.SetExclusiveStorage(newvalue)
1318 50ef6a41 Bernardo Dal Seno
  return oldvalue
1319 e8b919a1 Bernardo Dal Seno
1320 e8b919a1 Bernardo Dal Seno
1321 21e2734f Bernardo Dal Seno
def TestExclStorSharedPv(node):
1322 21e2734f Bernardo Dal Seno
  """cluster-verify reports LVs that share the same PV with exclusive_storage.
1323 21e2734f Bernardo Dal Seno

1324 21e2734f Bernardo Dal Seno
  """
1325 21e2734f Bernardo Dal Seno
  vgname = qa_config.get("vg-name", constants.DEFAULT_VG)
1326 21e2734f Bernardo Dal Seno
  lvname1 = _QA_LV_PREFIX + "vol1"
1327 21e2734f Bernardo Dal Seno
  lvname2 = _QA_LV_PREFIX + "vol2"
1328 aecba21e Michael Hanselmann
  node_name = node.primary
1329 21e2734f Bernardo Dal Seno
  AssertCommand(["lvcreate", "-L1G", "-n", lvname1, vgname], node=node_name)
1330 b0e8ed3f Santi Raffa
  AssertClusterVerify(fail=False,
1331 b0e8ed3f Santi Raffa
                      warnings=[constants.CV_ENODEORPHANLV])
1332 21e2734f Bernardo Dal Seno
  AssertCommand(["lvcreate", "-L1G", "-n", lvname2, vgname], node=node_name)
1333 b0e8ed3f Santi Raffa
  AssertClusterVerify(fail=True,
1334 b0e8ed3f Santi Raffa
                      errors=[constants.CV_ENODELVM],
1335 b0e8ed3f Santi Raffa
                      warnings=[constants.CV_ENODEORPHANLV])
1336 21e2734f Bernardo Dal Seno
  AssertCommand(["lvremove", "-f", "/".join([vgname, lvname1])], node=node_name)
1337 21e2734f Bernardo Dal Seno
  AssertCommand(["lvremove", "-f", "/".join([vgname, lvname2])], node=node_name)
1338 21e2734f Bernardo Dal Seno
  AssertClusterVerify()