4 # Copyright (C) 2006, 2007 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 config module"""
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
39 class TestConfigRunner(unittest.TestCase):
40 """Testing case for HooksRunner"""
42 fd, self.cfg_file = tempfile.mkstemp()
47 os.unlink(self.cfg_file)
51 def _get_object(self):
52 """Returns a instance of ConfigWriter"""
53 cfg = config.ConfigWriter(cfg_file=self.cfg_file, offline=True)
56 def _init_cluster(self, cfg):
57 """Initializes the cfg object"""
58 cfg.InitConfig(utils.HostInfo().name, '127.0.0.1', None, '', 'aa:00:00',
59 'xenvg', constants.DEFAULT_BRIDGE)
61 def _create_instance(self):
62 """Create and return an instance object"""
63 inst = objects.Instance(name="test.example.com", disks=[],
64 disk_template=constants.DT_DISKLESS)
68 """Test instantiate config object"""
72 """Test initialize the config file"""
73 cfg = self._get_object()
74 self._init_cluster(cfg)
75 self.failUnlessEqual(1, len(cfg.GetNodeList()))
76 self.failUnlessEqual(0, len(cfg.GetInstanceList()))
78 def testUpdateCluster(self):
79 """Test updates on the cluster object"""
80 cfg = self._get_object()
81 # construct a fake cluster object
82 fake_cl = objects.Cluster()
83 # fail if we didn't read the config
84 self.failUnlessRaises(errors.ProgrammerError, cfg.Update, fake_cl)
86 self._init_cluster(cfg)
87 cl = cfg.GetClusterInfo()
88 # first pass, must not fail
90 # second pass, also must not fail (after the config has been written)
92 # but the fake_cl update should still fail
93 self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_cl)
95 def testUpdateNode(self):
96 """Test updates on one node object"""
97 cfg = self._get_object()
98 # construct a fake node
99 fake_node = objects.Node()
100 # fail if we didn't read the config
101 self.failUnlessRaises(errors.ProgrammerError, cfg.Update, fake_node)
103 self._init_cluster(cfg)
104 node = cfg.GetNodeInfo(cfg.GetNodeList()[0])
105 # first pass, must not fail
107 # second pass, also must not fail (after the config has been written)
109 # but the fake_node update should still fail
110 self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_node)
112 def testUpdateInstance(self):
113 """Test updates on one instance object"""
114 cfg = self._get_object()
115 # construct a fake instance
116 inst = self._create_instance()
117 fake_instance = objects.Instance()
118 # fail if we didn't read the config
119 self.failUnlessRaises(errors.ProgrammerError, cfg.Update, fake_instance)
121 self._init_cluster(cfg)
122 cfg.AddInstance(inst)
123 instance = cfg.GetInstanceInfo(cfg.GetInstanceList()[0])
124 # first pass, must not fail
126 # second pass, also must not fail (after the config has been written)
128 # but the fake_instance update should still fail
129 self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance)
132 if __name__ == '__main__':