4 # Copyright (C) 2006, 2007, 2010 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
38 from ganeti import netutils
43 class TestConfigRunner(unittest.TestCase):
44 """Testing case for HooksRunner"""
46 fd, self.cfg_file = tempfile.mkstemp()
48 self._init_cluster(self.cfg_file)
52 os.unlink(self.cfg_file)
56 def _get_object(self):
57 """Returns a instance of ConfigWriter"""
58 cfg = config.ConfigWriter(cfg_file=self.cfg_file, offline=True)
61 def _init_cluster(self, cfg):
62 """Initializes the cfg object"""
63 me = netutils.Hostname()
64 ip = constants.IP4_ADDRESS_LOCALHOST
66 cluster_config = objects.Cluster(
69 highest_used_port=(constants.FIRST_DRBD_PORT - 1),
70 mac_prefix="aa:00:00",
71 volume_group_name="xenvg",
72 drbd_usermode_helper="/bin/true",
73 nicparams={constants.PP_DEFAULT: constants.NICC_DEFAULTS},
74 tcpudp_port_pool=set(),
75 enabled_hypervisors=[constants.HT_FAKE],
77 master_ip="127.0.0.1",
78 master_netdev=constants.DEFAULT_BRIDGE,
79 cluster_name="cluster.local",
80 file_storage_dir="/tmp",
84 master_node_config = objects.Node(name=me.name,
88 master_candidate=True)
90 bootstrap.InitConfig(constants.CONFIG_VERSION,
91 cluster_config, master_node_config, self.cfg_file)
93 def _create_instance(self):
94 """Create and return an instance object"""
95 inst = objects.Instance(name="test.example.com", disks=[], nics=[],
96 disk_template=constants.DT_DISKLESS,
97 primary_node=self._get_object().GetMasterNode())
101 """Test instantiate config object"""
105 """Test initialize the config file"""
106 cfg = self._get_object()
107 self.failUnlessEqual(1, len(cfg.GetNodeList()))
108 self.failUnlessEqual(0, len(cfg.GetInstanceList()))
110 def testUpdateCluster(self):
111 """Test updates on the cluster object"""
112 cfg = self._get_object()
113 # construct a fake cluster object
114 fake_cl = objects.Cluster()
115 # fail if we didn't read the config
116 self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_cl, None)
118 cl = cfg.GetClusterInfo()
119 # first pass, must not fail
121 # second pass, also must not fail (after the config has been written)
123 # but the fake_cl update should still fail
124 self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_cl, None)
126 def testUpdateNode(self):
127 """Test updates on one node object"""
128 cfg = self._get_object()
129 # construct a fake node
130 fake_node = objects.Node()
131 # fail if we didn't read the config
132 self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_node,
135 node = cfg.GetNodeInfo(cfg.GetNodeList()[0])
136 # first pass, must not fail
137 cfg.Update(node, None)
138 # second pass, also must not fail (after the config has been written)
139 cfg.Update(node, None)
140 # but the fake_node update should still fail
141 self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_node,
144 def testUpdateInstance(self):
145 """Test updates on one instance object"""
146 cfg = self._get_object()
147 # construct a fake instance
148 inst = self._create_instance()
149 fake_instance = objects.Instance()
150 # fail if we didn't read the config
151 self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance,
154 cfg.AddInstance(inst, "my-job")
155 instance = cfg.GetInstanceInfo(cfg.GetInstanceList()[0])
156 # first pass, must not fail
157 cfg.Update(instance, None)
158 # second pass, also must not fail (after the config has been written)
159 cfg.Update(instance, None)
160 # but the fake_instance update should still fail
161 self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance,
164 def testNICParameterSyntaxCheck(self):
165 """Test the NIC's CheckParameterSyntax function"""
166 mode = constants.NIC_MODE
167 link = constants.NIC_LINK
168 m_bridged = constants.NIC_MODE_BRIDGED
169 m_routed = constants.NIC_MODE_ROUTED
170 CheckSyntax = objects.NIC.CheckParameterSyntax
172 CheckSyntax(constants.NICC_DEFAULTS)
173 CheckSyntax({mode: m_bridged, link: 'br1'})
174 CheckSyntax({mode: m_routed, link: 'default'})
175 self.assertRaises(errors.ConfigurationError,
176 CheckSyntax, {mode: '000invalid', link: 'any'})
177 self.assertRaises(errors.ConfigurationError,
178 CheckSyntax, {mode: m_bridged, link: None})
179 self.assertRaises(errors.ConfigurationError,
180 CheckSyntax, {mode: m_bridged, link: ''})
183 if __name__ == '__main__':
184 testutils.GanetiTestProgram()