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 bootstrap
33 from ganeti import config
34 from ganeti import constants
35 from ganeti import errors
36 from ganeti import objects
37 from ganeti import utils
40 class TestConfigRunner(unittest.TestCase):
41 """Testing case for HooksRunner"""
43 fd, self.cfg_file = tempfile.mkstemp()
45 self._init_cluster(self.cfg_file)
49 os.unlink(self.cfg_file)
53 def _get_object(self):
54 """Returns a instance of ConfigWriter"""
55 cfg = config.ConfigWriter(cfg_file=self.cfg_file, offline=True)
58 def _init_cluster(self, cfg):
59 """Initializes the cfg object"""
61 ip = constants.LOCALHOST_IP_ADDRESS
63 cluster_config = objects.Cluster(
66 highest_used_port=(constants.FIRST_DRBD_PORT - 1),
67 mac_prefix="aa:00:00",
68 volume_group_name="xenvg",
69 nicparams={constants.PP_DEFAULT: constants.NICC_DEFAULTS},
70 tcpudp_port_pool=set(),
71 default_hypervisor=constants.HT_FAKE,
73 master_ip="127.0.0.1",
74 master_netdev=constants.DEFAULT_BRIDGE,
75 cluster_name="cluster.local",
76 file_storage_dir="/tmp",
79 master_node_config = objects.Node(name=me.name,
83 master_candidate=True)
85 bootstrap.InitConfig(constants.CONFIG_VERSION,
86 cluster_config, master_node_config, self.cfg_file)
88 def _create_instance(self):
89 """Create and return an instance object"""
90 inst = objects.Instance(name="test.example.com", disks=[], nics=[],
91 disk_template=constants.DT_DISKLESS,
92 primary_node=self._get_object().GetMasterNode())
96 """Test instantiate config object"""
100 """Test initialize the config file"""
101 cfg = self._get_object()
102 self.failUnlessEqual(1, len(cfg.GetNodeList()))
103 self.failUnlessEqual(0, len(cfg.GetInstanceList()))
105 def testUpdateCluster(self):
106 """Test updates on the cluster object"""
107 cfg = self._get_object()
108 # construct a fake cluster object
109 fake_cl = objects.Cluster()
110 # fail if we didn't read the config
111 self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_cl)
113 cl = cfg.GetClusterInfo()
114 # first pass, must not fail
116 # second pass, also must not fail (after the config has been written)
118 # but the fake_cl update should still fail
119 self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_cl)
121 def testUpdateNode(self):
122 """Test updates on one node object"""
123 cfg = self._get_object()
124 # construct a fake node
125 fake_node = objects.Node()
126 # fail if we didn't read the config
127 self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_node)
129 node = cfg.GetNodeInfo(cfg.GetNodeList()[0])
130 # first pass, must not fail
132 # second pass, also must not fail (after the config has been written)
134 # but the fake_node update should still fail
135 self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_node)
137 def testUpdateInstance(self):
138 """Test updates on one instance object"""
139 cfg = self._get_object()
140 # construct a fake instance
141 inst = self._create_instance()
142 fake_instance = objects.Instance()
143 # fail if we didn't read the config
144 self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance)
146 cfg.AddInstance(inst)
147 instance = cfg.GetInstanceInfo(cfg.GetInstanceList()[0])
148 # first pass, must not fail
150 # second pass, also must not fail (after the config has been written)
152 # but the fake_instance update should still fail
153 self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance)
155 def testNICParameterSyntaxCheck(self):
156 """Test the NIC's CheckParameterSyntax function"""
157 mode = constants.NIC_MODE
158 link = constants.NIC_LINK
159 m_bridged = constants.NIC_MODE_BRIDGED
160 m_routed = constants.NIC_MODE_ROUTED
161 CheckSyntax = objects.NIC.CheckParameterSyntax
163 CheckSyntax(constants.NICC_DEFAULTS)
164 CheckSyntax({mode: m_bridged, link: 'br1'})
165 CheckSyntax({mode: m_routed, link: 'default'})
166 self.assertRaises(errors.ConfigurationError,
167 CheckSyntax, {mode: '000invalid', link: 'any'})
168 self.assertRaises(errors.ConfigurationError,
169 CheckSyntax, {mode: m_bridged, link: None})
170 self.assertRaises(errors.ConfigurationError,
171 CheckSyntax, {mode: m_bridged, link: ''})
174 if __name__ == '__main__':