Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.objects_unittest.py @ 7b64b9ea

History | View | Annotate | Download (4.1 kB)

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

    
4
# Copyright (C) 2006, 2007, 2008 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
# 0.0510-1301, USA.
20

    
21

    
22
"""Script for unittesting the objects module"""
23

    
24

    
25
import unittest
26

    
27
from ganeti import constants
28
from ganeti import objects
29

    
30
import testutils
31

    
32

    
33
class SimpleObject(objects.ConfigObject):
34
  __slots__ = ['a', 'b']
35

    
36

    
37
class TestDictState(unittest.TestCase):
38
  """Simple dict tansformation tests"""
39

    
40
  def testSimpleObjectToDict(self):
41
    o1 = SimpleObject(a='1')
42
    self.assertEquals(o1.ToDict(), {'a': '1'})
43
    self.assertEquals(o1.__getstate__(), {'a': '1'})
44
    self.assertEquals(o1.__getstate__(), o1.ToDict())
45
    o1.a = 2
46
    o1.b = 5
47
    self.assertEquals(o1.ToDict(), {'a': 2, 'b': 5})
48
    o2 = SimpleObject.FromDict(o1.ToDict())
49
    self.assertEquals(o1.ToDict(), {'a': 2, 'b': 5})
50

    
51

    
52
class TestClusterObject(unittest.TestCase):
53
  """Tests done on a L{objects.Cluster}"""
54

    
55
  def setUp(self):
56
    hvparams = {
57
      constants.HT_FAKE: {
58
        "foo": "bar",
59
        "bar": "foo",
60
        "foobar": "barfoo",
61
        },
62
      }
63
    os_hvp = {
64
      "lenny-image": {
65
        constants.HT_FAKE: {
66
          "foo": "baz",
67
          "foobar": "foobar",
68
          "blah": "blibb",
69
          "blubb": "blah",
70
          },
71
        constants.HT_XEN_PVM: {
72
          "root_path": "/dev/sda5",
73
          "foo": "foobar",
74
          },
75
        },
76
      "ubuntu-hardy": {
77
        },
78
      }
79
    self.fake_cl = objects.Cluster(hvparams=hvparams, os_hvp=os_hvp)
80
    self.fake_cl.UpgradeConfig()
81

    
82
  def testFillHvFullMerge(self):
83
    inst_hvparams = {
84
      "blah": "blubb",
85
      }
86

    
87
    fake_dict = {
88
      "foo": "baz",
89
      "bar": "foo",
90
      "foobar": "foobar",
91
      "blah": "blubb",
92
      "blubb": "blah",
93
      }
94
    fake_inst = objects.Instance(name="foobar",
95
                                 os="lenny-image",
96
                                 hypervisor=constants.HT_FAKE,
97
                                 hvparams=inst_hvparams)
98
    self.assertEqual(fake_dict, self.fake_cl.FillHV(fake_inst))
99

    
100
  def testFillHvGlobalParams(self):
101
    fake_inst = objects.Instance(name="foobar",
102
                                 os="ubuntu-hardy",
103
                                 hypervisor=constants.HT_FAKE,
104
                                 hvparams={})
105
    self.assertEqual(self.fake_cl.hvparams[constants.HT_FAKE],
106
                     self.fake_cl.FillHV(fake_inst))
107

    
108
  def testFillHvInstParams(self):
109
    inst_hvparams = {
110
      "blah": "blubb",
111
      }
112
    fake_inst = objects.Instance(name="foobar",
113
                                 os="ubuntu-hardy",
114
                                 hypervisor=constants.HT_XEN_PVM,
115
                                 hvparams=inst_hvparams)
116
    self.assertEqual(inst_hvparams, self.fake_cl.FillHV(fake_inst))
117

    
118
  def testFillHvEmptyParams(self):
119
    fake_inst = objects.Instance(name="foobar",
120
                                 os="ubuntu-hardy",
121
                                 hypervisor=constants.HT_XEN_PVM,
122
                                 hvparams={})
123
    self.assertEqual({}, self.fake_cl.FillHV(fake_inst))
124

    
125
  def testFillHvPartialParams(self):
126
    os = "lenny-image"
127
    fake_inst = objects.Instance(name="foobar",
128
                                 os=os,
129
                                 hypervisor=constants.HT_XEN_PVM,
130
                                 hvparams={})
131
    self.assertEqual(self.fake_cl.os_hvp[os][constants.HT_XEN_PVM],
132
                     self.fake_cl.FillHV(fake_inst))
133

    
134

    
135
if __name__ == '__main__':
136
  testutils.GanetiTestProgram()