Statistics
| Branch: | Tag: | Revision:

root / qa / qa_os.py @ 96a12113

History | View | Annotate | Download (6.6 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 fca11dec René Nussbaumer
  cmd = ['gnt-os', 'modify']
60 fca11dec René Nussbaumer
61 fca11dec René Nussbaumer
  for hv_name, hv_params in hvp_dict.items():
62 fca11dec René Nussbaumer
    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 fca11dec René Nussbaumer
    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 3582eef6 Iustin Pop
def _SetupTempOs(node, dirname, 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 3582eef6 Iustin Pop
  parts = [sq(["rm", "-rf", dirname]),
90 3582eef6 Iustin Pop
           sq(["mkdir", "-p", dirname]),
91 3582eef6 Iustin Pop
           sq(["cd", dirname]),
92 8947cf2b Michael Hanselmann
           sq(["ln", "-fs", "/bin/true", "export"]),
93 8947cf2b Michael Hanselmann
           sq(["ln", "-fs", "/bin/true", "import"]),
94 8947cf2b Michael Hanselmann
           sq(["ln", "-fs", "/bin/true", "rename"])]
95 8947cf2b Michael Hanselmann
96 8947cf2b Michael Hanselmann
  if valid:
97 8947cf2b Michael Hanselmann
    parts.append(sq(["ln", "-fs", "/bin/true", "create"]))
98 8947cf2b Michael Hanselmann
99 d1a7d66f Guido Trotter
  parts.append(sq(["echo", str(constants.OS_API_V10)]) +
100 8947cf2b Michael Hanselmann
               " >ganeti_api_version")
101 8947cf2b Michael Hanselmann
102 8947cf2b Michael Hanselmann
  cmd = ' && '.join(parts)
103 8947cf2b Michael Hanselmann
104 dfe11bad Michael Hanselmann
  print qa_utils.FormatInfo("Setting up %s with %s OS definition" %
105 dfe11bad Michael Hanselmann
                            (node["primary"],
106 dfe11bad Michael Hanselmann
                             ["an invalid", "a valid"][int(valid)]))
107 8947cf2b Michael Hanselmann
108 2f4b4f78 Iustin Pop
  AssertCommand(cmd, node=node)
109 8947cf2b Michael Hanselmann
110 8947cf2b Michael Hanselmann
111 3582eef6 Iustin Pop
def _RemoveTempOs(node, dirname):
112 8947cf2b Michael Hanselmann
  """Removes a temporary OS definition.
113 8947cf2b Michael Hanselmann

114 8947cf2b Michael Hanselmann
  """
115 3582eef6 Iustin Pop
  AssertCommand(["rm", "-rf", dirname], node=node)
116 8947cf2b Michael Hanselmann
117 8947cf2b Michael Hanselmann
118 2932dc44 Michael Hanselmann
def _TestOs(mode, rapi_cb):
119 8947cf2b Michael Hanselmann
  """Generic function for OS definition testing
120 8947cf2b Michael Hanselmann

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