Statistics
| Branch: | Tag: | Revision:

root / test / py / ganeti.outils_unittest.py @ cd3b4ff4

History | View | Annotate | Download (3 kB)

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

    
4
# Copyright (C) 2012, 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 unittesting the outils module"""
23

    
24

    
25
import unittest
26

    
27
from ganeti import outils
28

    
29
import testutils
30

    
31

    
32
class SlotsAutoSlot(outils.AutoSlots):
33
  @classmethod
34
  def _GetSlots(mcs, attr):
35
    return attr["SLOTS"]
36

    
37

    
38
class AutoSlotted(object):
39
  __metaclass__ = SlotsAutoSlot
40

    
41
  SLOTS = ["foo", "bar", "baz"]
42

    
43

    
44
class TestAutoSlot(unittest.TestCase):
45
  def test(self):
46
    slotted = AutoSlotted()
47
    self.assertEqual(slotted.__slots__, AutoSlotted.SLOTS)
48

    
49

    
50
class TestContainerToDicts(unittest.TestCase):
51
  def testUnknownType(self):
52
    for value in [None, 19410, "xyz"]:
53
      try:
54
        outils.ContainerToDicts(value)
55
      except TypeError, err:
56
        self.assertTrue(str(err).startswith("Unknown container type"))
57
      else:
58
        self.fail("Exception was not raised")
59

    
60
  def testEmptyDict(self):
61
    value = {}
62
    self.assertFalse(type(value) in outils._SEQUENCE_TYPES)
63
    self.assertEqual(outils.ContainerToDicts(value), {})
64

    
65
  def testEmptySequences(self):
66
    for cls in [list, tuple, set, frozenset]:
67
      self.assertEqual(outils.ContainerToDicts(cls()), [])
68

    
69

    
70
class _FakeWithFromDict:
71
  def FromDict(self, _):
72
    raise NotImplemented
73

    
74

    
75
class TestContainerFromDicts(unittest.TestCase):
76
  def testUnknownType(self):
77
    for cls in [str, int, bool]:
78
      try:
79
        outils.ContainerFromDicts(None, cls, NotImplemented)
80
      except TypeError, err:
81
        self.assertTrue(str(err).startswith("Unknown container type"))
82
      else:
83
        self.fail("Exception was not raised")
84

    
85
      try:
86
        outils.ContainerFromDicts(None, cls(), NotImplemented)
87
      except TypeError, err:
88
        self.assertTrue(str(err).endswith("is not a type"))
89
      else:
90
        self.fail("Exception was not raised")
91

    
92
  def testEmptyDict(self):
93
    value = {}
94
    self.assertFalse(type(value) in outils._SEQUENCE_TYPES)
95
    self.assertEqual(outils.ContainerFromDicts(value, dict,
96
                                                    NotImplemented),
97
                     {})
98

    
99
  def testEmptySequences(self):
100
    for cls in [list, tuple, set, frozenset]:
101
      self.assertEqual(outils.ContainerFromDicts([], cls,
102
                                                      _FakeWithFromDict),
103
                       cls())
104

    
105

    
106
if __name__ == "__main__":
107
  testutils.GanetiTestProgram()