Statistics
| Branch: | Tag: | Revision:

root / test / py / ganeti.hypervisor.hv_xen_unittest.py @ b255379d

History | View | Annotate | Download (4.5 kB)

1
#!/usr/bin/python
2
#
3

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

    
21

    
22
"""Script for testing ganeti.hypervisor.hv_lxc"""
23

    
24
import unittest
25

    
26
from ganeti import constants
27
from ganeti import objects
28
from ganeti import hypervisor
29
from ganeti import utils
30
from ganeti import errors
31
from ganeti import compat
32

    
33
from ganeti.hypervisor import hv_xen
34

    
35
import testutils
36

    
37

    
38
class TestConsole(unittest.TestCase):
39
  def test(self):
40
    for cls in [hv_xen.XenPvmHypervisor, hv_xen.XenHvmHypervisor]:
41
      instance = objects.Instance(name="xen.example.com",
42
                                  primary_node="node24828")
43
      cons = cls.GetInstanceConsole(instance, {}, {})
44
      self.assertTrue(cons.Validate())
45
      self.assertEqual(cons.kind, constants.CONS_SSH)
46
      self.assertEqual(cons.host, instance.primary_node)
47
      self.assertEqual(cons.command[-1], instance.name)
48

    
49

    
50
class TestCreateConfigCpus(unittest.TestCase):
51
  def testEmpty(self):
52
    for cpu_mask in [None, ""]:
53
      self.assertEqual(hv_xen._CreateConfigCpus(cpu_mask),
54
                       "cpus = [  ]")
55

    
56
  def testAll(self):
57
    self.assertEqual(hv_xen._CreateConfigCpus(constants.CPU_PINNING_ALL),
58
                     None)
59

    
60
  def testOne(self):
61
    self.assertEqual(hv_xen._CreateConfigCpus("9"), "cpu = \"9\"")
62

    
63
  def testMultiple(self):
64
    self.assertEqual(hv_xen._CreateConfigCpus("0-2,4,5-5:3:all"),
65
                     ("cpus = [ \"0,1,2,4,5\", \"3\", \"%s\" ]" %
66
                      constants.CPU_PINNING_ALL_XEN))
67

    
68

    
69
class TestParseXmList(testutils.GanetiTestCase):
70
  def test(self):
71
    data = testutils.ReadTestData("xen-xm-list-4.0.1-dom0-only.txt")
72

    
73
    # Exclude node
74
    self.assertEqual(hv_xen._ParseXmList(data.splitlines(), False), [])
75

    
76
    # Include node
77
    result = hv_xen._ParseXmList(data.splitlines(), True)
78
    self.assertEqual(len(result), 1)
79
    self.assertEqual(len(result[0]), 6)
80

    
81
    # Name
82
    self.assertEqual(result[0][0], hv_xen._DOM0_NAME)
83

    
84
    # ID
85
    self.assertEqual(result[0][1], 0)
86

    
87
    # Memory
88
    self.assertEqual(result[0][2], 1023)
89

    
90
    # VCPUs
91
    self.assertEqual(result[0][3], 1)
92

    
93
    # State
94
    self.assertEqual(result[0][4], "r-----")
95

    
96
    # Time
97
    self.assertAlmostEqual(result[0][5], 121152.6)
98

    
99
  def testWrongLineFormat(self):
100
    tests = [
101
      ["three fields only"],
102
      ["name InvalidID 128 1 r----- 12345"],
103
      ]
104

    
105
    for lines in tests:
106
      try:
107
        hv_xen._ParseXmList(["Header would be here"] + lines, False)
108
      except errors.HypervisorError, err:
109
        self.assertTrue("Can't parse output of xm list" in str(err))
110
      else:
111
        self.fail("Exception was not raised")
112

    
113

    
114
class TestGetXmList(testutils.GanetiTestCase):
115
  def _Fail(self):
116
    return utils.RunResult(constants.EXIT_FAILURE, None,
117
                           "stdout", "stderr", None,
118
                           NotImplemented, NotImplemented)
119

    
120
  def testTimeout(self):
121
    fn = testutils.CallCounter(self._Fail)
122
    try:
123
      hv_xen._GetXmList(fn, False, _timeout=0.1)
124
    except errors.HypervisorError, err:
125
      self.assertTrue("timeout exceeded" in str(err))
126
    else:
127
      self.fail("Exception was not raised")
128

    
129
    self.assertTrue(fn.Count() < 10,
130
                    msg="'xm list' was called too many times")
131

    
132
  def _Success(self, stdout):
133
    return utils.RunResult(constants.EXIT_SUCCESS, None, stdout, "", None,
134
                           NotImplemented, NotImplemented)
135

    
136
  def testSuccess(self):
137
    data = testutils.ReadTestData("xen-xm-list-4.0.1-four-instances.txt")
138

    
139
    fn = testutils.CallCounter(compat.partial(self._Success, data))
140

    
141
    result = hv_xen._GetXmList(fn, True, _timeout=0.1)
142

    
143
    self.assertEqual(len(result), 4)
144

    
145
    self.assertEqual(map(compat.fst, result), [
146
      "Domain-0",
147
      "server01.example.com",
148
      "web3106215069.example.com",
149
      "testinstance.example.com",
150
      ])
151

    
152
    self.assertEqual(fn.Count(), 1)
153

    
154

    
155
if __name__ == "__main__":
156
  testutils.GanetiTestProgram()