qa_os.py 3.55 KB
Newer Older
1
2
3
#
#

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Copyright (C) 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


"""OS related QA tests.

"""

import os
import os.path

from ganeti import utils
from ganeti import constants

import qa_config
import qa_utils

from qa_utils import AssertEqual, StartSSH


_TEMP_OS_NAME = "TEMP-Ganeti-QA-OS"
_TEMP_OS_PATH = os.path.join(constants.OS_SEARCH_PATH[0], _TEMP_OS_NAME)


42
@qa_utils.DefineHook('os-list')
43
44
45
46
47
48
49
50
51
def TestOsList():
  """gnt-os list"""
  master = qa_config.GetMasterNode()

  cmd = ['gnt-os', 'list']
  AssertEqual(StartSSH(master['primary'],
                       utils.ShellQuoteArgs(cmd)).wait(), 0)


52
@qa_utils.DefineHook('os-diagnose')
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def TestOsDiagnose():
  """gnt-os diagnose"""
  master = qa_config.GetMasterNode()

  cmd = ['gnt-os', 'diagnose']
  AssertEqual(StartSSH(master['primary'],
                       utils.ShellQuoteArgs(cmd)).wait(), 0)


def _SetupTempOs(node, dir, valid):
  """Creates a temporary OS definition on the given node.

  """
  sq = utils.ShellQuoteArgs
  parts = [sq(["rm", "-rf", dir]),
           sq(["mkdir", "-p", dir]),
           sq(["cd", dir]),
           sq(["ln", "-fs", "/bin/true", "export"]),
           sq(["ln", "-fs", "/bin/true", "import"]),
           sq(["ln", "-fs", "/bin/true", "rename"])]

  if valid:
    parts.append(sq(["ln", "-fs", "/bin/true", "create"]))

  parts.append(sq(["echo", str(constants.OS_API_VERSION)]) +
               " >ganeti_api_version")

  cmd = ' && '.join(parts)

82
83
84
  print qa_utils.FormatInfo("Setting up %s with %s OS definition" %
                            (node["primary"],
                             ["an invalid", "a valid"][int(valid)]))
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

  AssertEqual(StartSSH(node['primary'], cmd).wait(), 0)


def _RemoveTempOs(node, dir):
  """Removes a temporary OS definition.

  """
  cmd = ['rm', '-rf', dir]
  AssertEqual(StartSSH(node['primary'],
                       utils.ShellQuoteArgs(cmd)).wait(), 0)


def _TestOs(mode):
  """Generic function for OS definition testing

  """
  master = qa_config.GetMasterNode()
  dir = _TEMP_OS_PATH

  nodes = []
  try:
    i = 0
    for node in qa_config.get('nodes'):
      nodes.append(node)
      if mode == 0:
        valid = False
      elif mode == 1:
        valid = True
      else:
        valid = bool(i % 2)
      _SetupTempOs(node, dir, valid)
      i += 1

    cmd = ['gnt-os', 'diagnose']
    result = StartSSH(master['primary'],
                      utils.ShellQuoteArgs(cmd)).wait()
    if mode == 1:
      AssertEqual(result, 0)
    else:
      AssertEqual(result, 1)
  finally:
    for node in nodes:
      _RemoveTempOs(node, dir)


131
@qa_utils.DefineHook('os-valid')
132
133
134
135
136
def TestOsValid():
  """Testing valid OS definition"""
  return _TestOs(1)


137
@qa_utils.DefineHook('os-invalid')
138
139
140
141
142
def TestOsInvalid():
  """Testing invalid OS definition"""
  return _TestOs(0)


143
@qa_utils.DefineHook('os-partially-valid')
144
145
146
def TestOsPartiallyValid():
  """Testing partially valid OS definition"""
  return _TestOs(2)