Statistics
| Branch: | Tag: | Revision:

root / qa / qa_os.py @ 6d3d13ab

History | View | Annotate | Download (6.9 kB)

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

24 8947cf2b Michael Hanselmann
"""
25 8947cf2b Michael Hanselmann
26 8947cf2b Michael Hanselmann
import os
27 8947cf2b Michael Hanselmann
import os.path
28 8947cf2b Michael Hanselmann
29 8947cf2b Michael Hanselmann
from ganeti import utils
30 8947cf2b Michael Hanselmann
from ganeti import constants
31 304d9f02 Michael Hanselmann
from ganeti import pathutils
32 8947cf2b Michael Hanselmann
33 8947cf2b Michael Hanselmann
import qa_config
34 8947cf2b Michael Hanselmann
import qa_utils
35 2932dc44 Michael Hanselmann
import qa_error
36 8947cf2b Michael Hanselmann
37 2932dc44 Michael Hanselmann
from qa_utils import AssertCommand, AssertIn, AssertNotIn
38 8947cf2b Michael Hanselmann
39 8947cf2b Michael Hanselmann
40 8947cf2b Michael Hanselmann
_TEMP_OS_NAME = "TEMP-Ganeti-QA-OS"
41 304d9f02 Michael Hanselmann
_TEMP_OS_PATH = os.path.join(pathutils.OS_SEARCH_PATH[0], _TEMP_OS_NAME)
42 8947cf2b Michael Hanselmann
43 80009a71 Michael Hanselmann
(_ALL_VALID,
44 80009a71 Michael Hanselmann
 _ALL_INVALID,
45 80009a71 Michael Hanselmann
 _PARTIALLY_VALID) = range(1, 4)
46 80009a71 Michael Hanselmann
47 8947cf2b Michael Hanselmann
48 8947cf2b Michael Hanselmann
def TestOsList():
49 8947cf2b Michael Hanselmann
  """gnt-os list"""
50 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-os", "list"])
51 8947cf2b Michael Hanselmann
52 8947cf2b Michael Hanselmann
53 8947cf2b Michael Hanselmann
def TestOsDiagnose():
54 8947cf2b Michael Hanselmann
  """gnt-os diagnose"""
55 2f4b4f78 Iustin Pop
  AssertCommand(["gnt-os", "diagnose"])
56 8947cf2b Michael Hanselmann
57 8947cf2b Michael Hanselmann
58 2f4b4f78 Iustin Pop
def _TestOsModify(hvp_dict, fail=False):
59 fca11dec René Nussbaumer
  """gnt-os modify"""
60 d0c8c01d Iustin Pop
  cmd = ["gnt-os", "modify"]
61 fca11dec René Nussbaumer
62 fca11dec René Nussbaumer
  for hv_name, hv_params in hvp_dict.items():
63 d0c8c01d Iustin Pop
    cmd.append("-H")
64 fca11dec René Nussbaumer
    options = []
65 fca11dec René Nussbaumer
    for key, value in hv_params.items():
66 fca11dec René Nussbaumer
      options.append("%s=%s" % (key, value))
67 d0c8c01d Iustin Pop
    cmd.append("%s:%s" % (hv_name, ",".join(options)))
68 fca11dec René Nussbaumer
69 fca11dec René Nussbaumer
  cmd.append(_TEMP_OS_NAME)
70 2f4b4f78 Iustin Pop
  AssertCommand(cmd, fail=fail)
71 fca11dec René Nussbaumer
72 fca11dec René Nussbaumer
73 074e139f Michael Hanselmann
def _TestOsStates(os_name):
74 e1df06f2 Iustin Pop
  """gnt-os modify, more stuff"""
75 e1df06f2 Iustin Pop
  cmd = ["gnt-os", "modify"]
76 e1df06f2 Iustin Pop
77 e1df06f2 Iustin Pop
  for param in ["hidden", "blacklisted"]:
78 e1df06f2 Iustin Pop
    for val in ["yes", "no"]:
79 074e139f Michael Hanselmann
      new_cmd = cmd + ["--%s" % param, val, os_name]
80 2f4b4f78 Iustin Pop
      AssertCommand(new_cmd)
81 89e8af70 Iustin Pop
      # check that double-running the command is OK
82 2f4b4f78 Iustin Pop
      AssertCommand(new_cmd)
83 e1df06f2 Iustin Pop
84 e1df06f2 Iustin Pop
85 732052c1 Iustin Pop
def _SetupTempOs(node, dirname, variant, valid):
86 8947cf2b Michael Hanselmann
  """Creates a temporary OS definition on the given node.
87 8947cf2b Michael Hanselmann

88 8947cf2b Michael Hanselmann
  """
89 8947cf2b Michael Hanselmann
  sq = utils.ShellQuoteArgs
90 e3f9ae2b Iustin Pop
  parts = [
91 e3f9ae2b Iustin Pop
    sq(["rm", "-rf", dirname]),
92 e3f9ae2b Iustin Pop
    sq(["mkdir", "-p", dirname]),
93 e3f9ae2b Iustin Pop
    sq(["cd", dirname]),
94 e3f9ae2b Iustin Pop
    sq(["ln", "-fs", "/bin/true", "export"]),
95 e3f9ae2b Iustin Pop
    sq(["ln", "-fs", "/bin/true", "import"]),
96 e3f9ae2b Iustin Pop
    sq(["ln", "-fs", "/bin/true", "rename"]),
97 e3f9ae2b Iustin Pop
    sq(["ln", "-fs", "/bin/true", "verify"]),
98 e3f9ae2b Iustin Pop
    ]
99 8947cf2b Michael Hanselmann
100 8947cf2b Michael Hanselmann
  if valid:
101 8947cf2b Michael Hanselmann
    parts.append(sq(["ln", "-fs", "/bin/true", "create"]))
102 8947cf2b Michael Hanselmann
103 e3f9ae2b Iustin Pop
  parts.append(sq(["echo", str(constants.OS_API_V20)]) +
104 8947cf2b Michael Hanselmann
               " >ganeti_api_version")
105 8947cf2b Michael Hanselmann
106 732052c1 Iustin Pop
  parts.append(sq(["echo", variant]) + " >variants.list")
107 e3f9ae2b Iustin Pop
  parts.append(sq(["echo", "funny this is funny"]) + " >parameters.list")
108 e3f9ae2b Iustin Pop
109 d0c8c01d Iustin Pop
  cmd = " && ".join(parts)
110 8947cf2b Michael Hanselmann
111 dfe11bad Michael Hanselmann
  print qa_utils.FormatInfo("Setting up %s with %s OS definition" %
112 dfe11bad Michael Hanselmann
                            (node["primary"],
113 dfe11bad Michael Hanselmann
                             ["an invalid", "a valid"][int(valid)]))
114 8947cf2b Michael Hanselmann
115 2f4b4f78 Iustin Pop
  AssertCommand(cmd, node=node)
116 8947cf2b Michael Hanselmann
117 8947cf2b Michael Hanselmann
118 3582eef6 Iustin Pop
def _RemoveTempOs(node, dirname):
119 8947cf2b Michael Hanselmann
  """Removes a temporary OS definition.
120 8947cf2b Michael Hanselmann

121 8947cf2b Michael Hanselmann
  """
122 3582eef6 Iustin Pop
  AssertCommand(["rm", "-rf", dirname], node=node)
123 8947cf2b Michael Hanselmann
124 8947cf2b Michael Hanselmann
125 2932dc44 Michael Hanselmann
def _TestOs(mode, rapi_cb):
126 8947cf2b Michael Hanselmann
  """Generic function for OS definition testing
127 8947cf2b Michael Hanselmann

128 8947cf2b Michael Hanselmann
  """
129 2932dc44 Michael Hanselmann
  master = qa_config.GetMasterNode()
130 2932dc44 Michael Hanselmann
131 2932dc44 Michael Hanselmann
  name = _TEMP_OS_NAME
132 732052c1 Iustin Pop
  variant = "default"
133 732052c1 Iustin Pop
  fullname = "%s+%s" % (name, variant)
134 3582eef6 Iustin Pop
  dirname = _TEMP_OS_PATH
135 8947cf2b Michael Hanselmann
136 2932dc44 Michael Hanselmann
  # Ensure OS is usable
137 2932dc44 Michael Hanselmann
  cmd = ["gnt-os", "modify", "--hidden=no", "--blacklisted=no", name]
138 2932dc44 Michael Hanselmann
  AssertCommand(cmd)
139 2932dc44 Michael Hanselmann
140 8947cf2b Michael Hanselmann
  nodes = []
141 8947cf2b Michael Hanselmann
  try:
142 2f4b4f78 Iustin Pop
    for i, node in enumerate(qa_config.get("nodes")):
143 8947cf2b Michael Hanselmann
      nodes.append(node)
144 80009a71 Michael Hanselmann
      if mode == _ALL_INVALID:
145 8947cf2b Michael Hanselmann
        valid = False
146 80009a71 Michael Hanselmann
      elif mode == _ALL_VALID:
147 8947cf2b Michael Hanselmann
        valid = True
148 80009a71 Michael Hanselmann
      elif mode == _PARTIALLY_VALID:
149 8947cf2b Michael Hanselmann
        valid = bool(i % 2)
150 80009a71 Michael Hanselmann
      else:
151 80009a71 Michael Hanselmann
        raise AssertionError("Unknown mode %s" % mode)
152 732052c1 Iustin Pop
      _SetupTempOs(node, dirname, variant, valid)
153 2f4b4f78 Iustin Pop
154 2932dc44 Michael Hanselmann
    # TODO: Use Python 2.6's itertools.permutations
155 2932dc44 Michael Hanselmann
    for (hidden, blacklisted) in [(False, False), (True, False),
156 2932dc44 Michael Hanselmann
                                  (False, True), (True, True)]:
157 2932dc44 Michael Hanselmann
      # Change OS' visibility
158 2932dc44 Michael Hanselmann
      cmd = ["gnt-os", "modify", "--hidden", ["no", "yes"][int(hidden)],
159 2932dc44 Michael Hanselmann
             "--blacklisted", ["no", "yes"][int(blacklisted)], name]
160 2932dc44 Michael Hanselmann
      AssertCommand(cmd)
161 2932dc44 Michael Hanselmann
162 2932dc44 Michael Hanselmann
      # Diagnose, checking exit status
163 2932dc44 Michael Hanselmann
      AssertCommand(["gnt-os", "diagnose"], fail=(mode != _ALL_VALID))
164 2932dc44 Michael Hanselmann
165 2932dc44 Michael Hanselmann
      # Diagnose again, ignoring exit status
166 2932dc44 Michael Hanselmann
      output = qa_utils.GetCommandOutput(master["primary"],
167 2932dc44 Michael Hanselmann
                                         "gnt-os diagnose || :")
168 2932dc44 Michael Hanselmann
      for line in output.splitlines():
169 2932dc44 Michael Hanselmann
        if line.startswith("OS: %s [global status:" % name):
170 2932dc44 Michael Hanselmann
          break
171 2932dc44 Michael Hanselmann
      else:
172 2932dc44 Michael Hanselmann
        raise qa_error.Error("Didn't find OS '%s' in 'gnt-os diagnose'" % name)
173 2932dc44 Michael Hanselmann
174 2932dc44 Michael Hanselmann
      # Check info for all
175 2932dc44 Michael Hanselmann
      cmd = ["gnt-os", "info"]
176 2932dc44 Michael Hanselmann
      output = qa_utils.GetCommandOutput(master["primary"],
177 2932dc44 Michael Hanselmann
                                         utils.ShellQuoteArgs(cmd))
178 2932dc44 Michael Hanselmann
      AssertIn("%s:" % name, output.splitlines())
179 2932dc44 Michael Hanselmann
180 2932dc44 Michael Hanselmann
      # Check info for OS
181 2932dc44 Michael Hanselmann
      cmd = ["gnt-os", "info", name]
182 2932dc44 Michael Hanselmann
      output = qa_utils.GetCommandOutput(master["primary"],
183 2932dc44 Michael Hanselmann
                                         utils.ShellQuoteArgs(cmd)).splitlines()
184 2932dc44 Michael Hanselmann
      AssertIn("%s:" % name, output)
185 2932dc44 Michael Hanselmann
      for (field, value) in [("valid", mode == _ALL_VALID),
186 2932dc44 Michael Hanselmann
                             ("hidden", hidden),
187 2932dc44 Michael Hanselmann
                             ("blacklisted", blacklisted)]:
188 2932dc44 Michael Hanselmann
        AssertIn("  - %s: %s" % (field, value), output)
189 2932dc44 Michael Hanselmann
190 2932dc44 Michael Hanselmann
      # Only valid OSes should be listed
191 2932dc44 Michael Hanselmann
      cmd = ["gnt-os", "list", "--no-headers"]
192 2932dc44 Michael Hanselmann
      output = qa_utils.GetCommandOutput(master["primary"],
193 2932dc44 Michael Hanselmann
                                         utils.ShellQuoteArgs(cmd))
194 2932dc44 Michael Hanselmann
      if mode == _ALL_VALID and not (hidden or blacklisted):
195 2932dc44 Michael Hanselmann
        assert_fn = AssertIn
196 2932dc44 Michael Hanselmann
      else:
197 2932dc44 Michael Hanselmann
        assert_fn = AssertNotIn
198 732052c1 Iustin Pop
      assert_fn(fullname, output.splitlines())
199 2932dc44 Michael Hanselmann
200 2932dc44 Michael Hanselmann
      # Check via RAPI
201 2932dc44 Michael Hanselmann
      if rapi_cb:
202 732052c1 Iustin Pop
        assert_fn(fullname, rapi_cb())
203 8947cf2b Michael Hanselmann
  finally:
204 8947cf2b Michael Hanselmann
    for node in nodes:
205 3582eef6 Iustin Pop
      _RemoveTempOs(node, dirname)
206 8947cf2b Michael Hanselmann
207 8947cf2b Michael Hanselmann
208 2932dc44 Michael Hanselmann
def TestOsValid(rapi_cb):
209 8947cf2b Michael Hanselmann
  """Testing valid OS definition"""
210 2932dc44 Michael Hanselmann
  return _TestOs(_ALL_VALID, rapi_cb)
211 8947cf2b Michael Hanselmann
212 8947cf2b Michael Hanselmann
213 2932dc44 Michael Hanselmann
def TestOsInvalid(rapi_cb):
214 8947cf2b Michael Hanselmann
  """Testing invalid OS definition"""
215 2932dc44 Michael Hanselmann
  return _TestOs(_ALL_INVALID, rapi_cb)
216 8947cf2b Michael Hanselmann
217 8947cf2b Michael Hanselmann
218 2932dc44 Michael Hanselmann
def TestOsPartiallyValid(rapi_cb):
219 8947cf2b Michael Hanselmann
  """Testing partially valid OS definition"""
220 2932dc44 Michael Hanselmann
  return _TestOs(_PARTIALLY_VALID, rapi_cb)
221 fca11dec René Nussbaumer
222 fca11dec René Nussbaumer
223 fca11dec René Nussbaumer
def TestOsModifyValid():
224 fca11dec René Nussbaumer
  """Testing a valid os modify invocation"""
225 fca11dec René Nussbaumer
  hv_dict = {
226 fca11dec René Nussbaumer
    constants.HT_XEN_PVM: {
227 fca11dec René Nussbaumer
      constants.HV_ROOT_PATH: "/dev/sda5",
228 fca11dec René Nussbaumer
      },
229 fca11dec René Nussbaumer
    constants.HT_XEN_HVM: {
230 fca11dec René Nussbaumer
      constants.HV_ACPI: False,
231 fca11dec René Nussbaumer
      constants.HV_PAE: True,
232 fca11dec René Nussbaumer
      },
233 fca11dec René Nussbaumer
    }
234 fca11dec René Nussbaumer
235 fca11dec René Nussbaumer
  return _TestOsModify(hv_dict)
236 fca11dec René Nussbaumer
237 fca11dec René Nussbaumer
238 fca11dec René Nussbaumer
def TestOsModifyInvalid():
239 fca11dec René Nussbaumer
  """Testing an invalid os modify invocation"""
240 fca11dec René Nussbaumer
  hv_dict = {
241 fca11dec René Nussbaumer
    "blahblahblubb": {"bar": ""},
242 fca11dec René Nussbaumer
    }
243 fca11dec René Nussbaumer
244 2f4b4f78 Iustin Pop
  return _TestOsModify(hv_dict, fail=True)
245 e1df06f2 Iustin Pop
246 e1df06f2 Iustin Pop
247 074e139f Michael Hanselmann
def TestOsStatesNonExisting():
248 074e139f Michael Hanselmann
  """Testing OS states with non-existing OS"""
249 074e139f Michael Hanselmann
  AssertCommand(["test", "-e", _TEMP_OS_PATH], fail=True)
250 074e139f Michael Hanselmann
  return _TestOsStates(_TEMP_OS_NAME)