Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.masterd.iallocator_unittest.py @ 33b4fa9f

History | View | Annotate | Download (2.5 kB)

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

    
4
# Copyright (C) 2012 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.masterd.iallocator"""
23

    
24
import unittest
25

    
26
from ganeti import compat
27
from ganeti import constants
28
from ganeti import errors
29
from ganeti import ht
30
from ganeti.masterd import iallocator
31

    
32
import testutils
33

    
34

    
35
class _StubIAllocator(object):
36
  def __init__(self, success):
37
    self.success = success
38

    
39

    
40
class TestIAReqMultiInstanceAlloc(unittest.TestCase):
41
  def testResult(self):
42
    good_results = [
43
      # First result (all instances "allocate")
44
      [
45
        [["foo", ["a", "b"]],
46
         ["bar", ["c"]],
47
         ["baz", []]],
48
        []
49
      ],
50
      # Second result (partial "allocate", partial "fail")
51
      [
52
        [["bar", ["c", "b"]],
53
         ["baz", ["a"]]],
54
        ["foo"]
55
      ],
56
      # Third result (all instances "fail")
57
      [
58
        [],
59
        ["foo", "bar", "baz"]
60
      ],
61
      ]
62
    bad_results = [
63
      "foobar",
64
      1234,
65
      [],
66
      [[]],
67
      [[], [], []],
68
      ]
69

    
70
    result_fn = iallocator.IAReqMultiInstanceAlloc.REQ_RESULT
71

    
72
    self.assertTrue(compat.all(map(result_fn, good_results)))
73
    self.assertFalse(compat.any(map(result_fn, bad_results)))
74

    
75

    
76
class TestIARequestBase(unittest.TestCase):
77
  def testValidateResult(self):
78
    class _StubReqBase(iallocator.IARequestBase):
79
      MODE = constants.IALLOCATOR_MODE_ALLOC
80
      REQ_RESULT = ht.TBool
81

    
82
    stub = _StubReqBase()
83
    stub.ValidateResult(_StubIAllocator(True), True)
84
    self.assertRaises(errors.ResultValidationError, stub.ValidateResult,
85
                      _StubIAllocator(True), "foo")
86
    stub.ValidateResult(_StubIAllocator(False), True)
87
    # We don't validate the result if the iallocation request was not successful
88
    stub.ValidateResult(_StubIAllocator(False), "foo")
89

    
90

    
91
if __name__ == "__main__":
92
  testutils.GanetiTestProgram()