Statistics
| Branch: | Tag: | Revision:

root / qa / qa_os.py @ 5fa0375e

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

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

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

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