Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.objects_unittest.py @ d63479b5

History | View | Annotate | Download (4.5 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 testGetHVDefaults(self):
83
    cl = self.fake_cl
84
    self.failUnlessEqual(cl.GetHVDefaults(constants.HT_FAKE),
85
                         cl.hvparams[constants.HT_FAKE])
86
    self.failUnlessEqual(cl.GetHVDefaults(None), {})
87
    self.failUnlessEqual(cl.GetHVDefaults(constants.HT_XEN_PVM,
88
                                          os_name="lenny-image"),
89
                         cl.os_hvp["lenny-image"][constants.HT_XEN_PVM])
90

    
91

    
92
  def testFillHvFullMerge(self):
93
    inst_hvparams = {
94
      "blah": "blubb",
95
      }
96

    
97
    fake_dict = {
98
      "foo": "baz",
99
      "bar": "foo",
100
      "foobar": "foobar",
101
      "blah": "blubb",
102
      "blubb": "blah",
103
      }
104
    fake_inst = objects.Instance(name="foobar",
105
                                 os="lenny-image",
106
                                 hypervisor=constants.HT_FAKE,
107
                                 hvparams=inst_hvparams)
108
    self.assertEqual(fake_dict, self.fake_cl.FillHV(fake_inst))
109

    
110
  def testFillHvGlobalParams(self):
111
    fake_inst = objects.Instance(name="foobar",
112
                                 os="ubuntu-hardy",
113
                                 hypervisor=constants.HT_FAKE,
114
                                 hvparams={})
115
    self.assertEqual(self.fake_cl.hvparams[constants.HT_FAKE],
116
                     self.fake_cl.FillHV(fake_inst))
117

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

    
128
  def testFillHvEmptyParams(self):
129
    fake_inst = objects.Instance(name="foobar",
130
                                 os="ubuntu-hardy",
131
                                 hypervisor=constants.HT_XEN_PVM,
132
                                 hvparams={})
133
    self.assertEqual({}, self.fake_cl.FillHV(fake_inst))
134

    
135
  def testFillHvPartialParams(self):
136
    os = "lenny-image"
137
    fake_inst = objects.Instance(name="foobar",
138
                                 os=os,
139
                                 hypervisor=constants.HT_XEN_PVM,
140
                                 hvparams={})
141
    self.assertEqual(self.fake_cl.os_hvp[os][constants.HT_XEN_PVM],
142
                     self.fake_cl.FillHV(fake_inst))
143

    
144

    
145
if __name__ == '__main__':
146
  testutils.GanetiTestProgram()