4 # Copyright (C) 2012, 2013 Google Inc.
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.
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.
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
22 """Script for unittesting the outils module"""
27 from ganeti import outils
32 class SlotsAutoSlot(outils.AutoSlots):
34 def _GetSlots(mcs, attr):
38 class AutoSlotted(object):
39 __metaclass__ = SlotsAutoSlot
41 SLOTS = ["foo", "bar", "baz"]
44 class TestAutoSlot(unittest.TestCase):
46 slotted = AutoSlotted()
47 self.assertEqual(slotted.__slots__, AutoSlotted.SLOTS)
50 class TestContainerToDicts(unittest.TestCase):
51 def testUnknownType(self):
52 for value in [None, 19410, "xyz"]:
54 outils.ContainerToDicts(value)
55 except TypeError, err:
56 self.assertTrue(str(err).startswith("Unknown container type"))
58 self.fail("Exception was not raised")
60 def testEmptyDict(self):
62 self.assertFalse(type(value) in outils._SEQUENCE_TYPES)
63 self.assertEqual(outils.ContainerToDicts(value), {})
65 def testEmptySequences(self):
66 for cls in [list, tuple, set, frozenset]:
67 self.assertEqual(outils.ContainerToDicts(cls()), [])
70 class _FakeWithFromDict:
71 def FromDict(self, _):
75 class TestContainerFromDicts(unittest.TestCase):
76 def testUnknownType(self):
77 for cls in [str, int, bool]:
79 outils.ContainerFromDicts(None, cls, NotImplemented)
80 except TypeError, err:
81 self.assertTrue(str(err).startswith("Unknown container type"))
83 self.fail("Exception was not raised")
86 outils.ContainerFromDicts(None, cls(), NotImplemented)
87 except TypeError, err:
88 self.assertTrue(str(err).endswith("is not a type"))
90 self.fail("Exception was not raised")
92 def testEmptyDict(self):
94 self.assertFalse(type(value) in outils._SEQUENCE_TYPES)
95 self.assertEqual(outils.ContainerFromDicts(value, dict,
99 def testEmptySequences(self):
100 for cls in [list, tuple, set, frozenset]:
101 self.assertEqual(outils.ContainerFromDicts([], cls,
106 if __name__ == "__main__":
107 testutils.GanetiTestProgram()