Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.config_unittest.py @ d4fa5c23

History | View | Annotate | Download (4.1 kB)

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

    
4
# Copyright (C) 2006, 2007 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 config module"""
23

    
24

    
25
import unittest
26
import os
27
import time
28
import tempfile
29
import os.path
30
import socket
31

    
32
from ganeti import errors
33
from ganeti import constants
34
from ganeti import config
35
from ganeti import objects
36
from ganeti import utils
37

    
38

    
39
class TestConfigRunner(unittest.TestCase):
40
  """Testing case for HooksRunner"""
41
  def setUp(self):
42
    fd, self.cfg_file = tempfile.mkstemp()
43
    os.close(fd)
44

    
45
  def tearDown(self):
46
    try:
47
      os.unlink(self.cfg_file)
48
    except OSError:
49
      pass
50

    
51
  def _get_object(self):
52
    """Returns a instance of ConfigWriter"""
53
    cfg = config.ConfigWriter(cfg_file=self.cfg_file, offline=True)
54
    return cfg
55

    
56
  def _init_cluster(self, cfg):
57
    """Initializes the cfg object"""
58
    cfg.InitConfig(utils.HostInfo().name, constants.LOCALHOST_IP_ADDRESS,
59
                   None, '', 'aa:00:00', 'xenvg',
60
                   constants.DEFAULT_BRIDGE)
61

    
62
  def _create_instance(self):
63
    """Create and return an instance object"""
64
    inst = objects.Instance(name="test.example.com", disks=[],
65
                            disk_template=constants.DT_DISKLESS)
66
    return inst
67

    
68
  def testEmpty(self):
69
    """Test instantiate config object"""
70
    self._get_object()
71

    
72
  def testInit(self):
73
    """Test initialize the config file"""
74
    cfg = self._get_object()
75
    self._init_cluster(cfg)
76
    self.failUnlessEqual(1, len(cfg.GetNodeList()))
77
    self.failUnlessEqual(0, len(cfg.GetInstanceList()))
78

    
79
  def testUpdateCluster(self):
80
    """Test updates on the cluster object"""
81
    cfg = self._get_object()
82
    # construct a fake cluster object
83
    fake_cl = objects.Cluster()
84
    # fail if we didn't read the config
85
    self.failUnlessRaises(errors.ProgrammerError, cfg.Update, fake_cl)
86

    
87
    self._init_cluster(cfg)
88
    cl = cfg.GetClusterInfo()
89
    # first pass, must not fail
90
    cfg.Update(cl)
91
    # second pass, also must not fail (after the config has been written)
92
    cfg.Update(cl)
93
    # but the fake_cl update should still fail
94
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_cl)
95

    
96
  def testUpdateNode(self):
97
    """Test updates on one node object"""
98
    cfg = self._get_object()
99
    # construct a fake node
100
    fake_node = objects.Node()
101
    # fail if we didn't read the config
102
    self.failUnlessRaises(errors.ProgrammerError, cfg.Update, fake_node)
103

    
104
    self._init_cluster(cfg)
105
    node = cfg.GetNodeInfo(cfg.GetNodeList()[0])
106
    # first pass, must not fail
107
    cfg.Update(node)
108
    # second pass, also must not fail (after the config has been written)
109
    cfg.Update(node)
110
    # but the fake_node update should still fail
111
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_node)
112

    
113
  def testUpdateInstance(self):
114
    """Test updates on one instance object"""
115
    cfg = self._get_object()
116
    # construct a fake instance
117
    inst = self._create_instance()
118
    fake_instance = objects.Instance()
119
    # fail if we didn't read the config
120
    self.failUnlessRaises(errors.ProgrammerError, cfg.Update, fake_instance)
121

    
122
    self._init_cluster(cfg)
123
    cfg.AddInstance(inst)
124
    instance = cfg.GetInstanceInfo(cfg.GetInstanceList()[0])
125
    # first pass, must not fail
126
    cfg.Update(instance)
127
    # second pass, also must not fail (after the config has been written)
128
    cfg.Update(instance)
129
    # but the fake_instance update should still fail
130
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance)
131

    
132

    
133
if __name__ == '__main__':
134
  unittest.main()