Revision b255379d test/py/ganeti.hypervisor.hv_xen_unittest.py

b/test/py/ganeti.hypervisor.hv_xen_unittest.py
1 1
#!/usr/bin/python
2 2
#
3 3

  
4
# Copyright (C) 2011 Google Inc.
4
# Copyright (C) 2011, 2013 Google Inc.
5 5
#
6 6
# This program is free software; you can redistribute it and/or modify
7 7
# it under the terms of the GNU General Public License as published by
......
26 26
from ganeti import constants
27 27
from ganeti import objects
28 28
from ganeti import hypervisor
29
from ganeti import utils
30
from ganeti import errors
31
from ganeti import compat
29 32

  
30 33
from ganeti.hypervisor import hv_xen
31 34

  
......
63 66
                      constants.CPU_PINNING_ALL_XEN))
64 67

  
65 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

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

Also available in: Unified diff