Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.config_unittest.py @ 26d3fd2f

History | View | Annotate | Download (6.2 kB)

1 e00fb268 Iustin Pop
#!/usr/bin/python
2 e00fb268 Iustin Pop
#
3 e00fb268 Iustin Pop
4 b705c7a6 Manuel Franceschini
# Copyright (C) 2006, 2007, 2010 Google Inc.
5 e00fb268 Iustin Pop
#
6 e00fb268 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 e00fb268 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 e00fb268 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 e00fb268 Iustin Pop
# (at your option) any later version.
10 e00fb268 Iustin Pop
#
11 e00fb268 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 e00fb268 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 e00fb268 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 e00fb268 Iustin Pop
# General Public License for more details.
15 e00fb268 Iustin Pop
#
16 e00fb268 Iustin Pop
# You should have received a copy of the GNU General Public License
17 e00fb268 Iustin Pop
# along with this program; if not, write to the Free Software
18 e00fb268 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 e00fb268 Iustin Pop
# 02110-1301, USA.
20 e00fb268 Iustin Pop
21 e00fb268 Iustin Pop
22 e00fb268 Iustin Pop
"""Script for unittesting the config module"""
23 e00fb268 Iustin Pop
24 e00fb268 Iustin Pop
25 e00fb268 Iustin Pop
import unittest
26 e00fb268 Iustin Pop
import os
27 e00fb268 Iustin Pop
import time
28 e00fb268 Iustin Pop
import tempfile
29 e00fb268 Iustin Pop
import os.path
30 e00fb268 Iustin Pop
import socket
31 e00fb268 Iustin Pop
32 c666722f Oleksiy Mishchenko
from ganeti import bootstrap
33 e00fb268 Iustin Pop
from ganeti import config
34 c666722f Oleksiy Mishchenko
from ganeti import constants
35 c666722f Oleksiy Mishchenko
from ganeti import errors
36 e00fb268 Iustin Pop
from ganeti import objects
37 6f1bebf9 Michael Hanselmann
from ganeti import utils
38 a744b676 Manuel Franceschini
from ganeti import netutils
39 e00fb268 Iustin Pop
40 25231ec5 Michael Hanselmann
import testutils
41 e60c73a1 René Nussbaumer
import mocks
42 e60c73a1 René Nussbaumer
43 e60c73a1 René Nussbaumer
44 e60c73a1 René Nussbaumer
def _StubGetEntResolver():
45 e60c73a1 René Nussbaumer
  return mocks.FakeGetentResolver()
46 25231ec5 Michael Hanselmann
47 e00fb268 Iustin Pop
48 e00fb268 Iustin Pop
class TestConfigRunner(unittest.TestCase):
49 e00fb268 Iustin Pop
  """Testing case for HooksRunner"""
50 e00fb268 Iustin Pop
  def setUp(self):
51 e00fb268 Iustin Pop
    fd, self.cfg_file = tempfile.mkstemp()
52 e00fb268 Iustin Pop
    os.close(fd)
53 c666722f Oleksiy Mishchenko
    self._init_cluster(self.cfg_file)
54 e00fb268 Iustin Pop
55 e00fb268 Iustin Pop
  def tearDown(self):
56 e00fb268 Iustin Pop
    try:
57 e00fb268 Iustin Pop
      os.unlink(self.cfg_file)
58 e00fb268 Iustin Pop
    except OSError:
59 e00fb268 Iustin Pop
      pass
60 e00fb268 Iustin Pop
61 e00fb268 Iustin Pop
  def _get_object(self):
62 e00fb268 Iustin Pop
    """Returns a instance of ConfigWriter"""
63 e60c73a1 René Nussbaumer
    cfg = config.ConfigWriter(cfg_file=self.cfg_file, offline=True,
64 e60c73a1 René Nussbaumer
                              _getents=_StubGetEntResolver)
65 e00fb268 Iustin Pop
    return cfg
66 e00fb268 Iustin Pop
67 e00fb268 Iustin Pop
  def _init_cluster(self, cfg):
68 e00fb268 Iustin Pop
    """Initializes the cfg object"""
69 b705c7a6 Manuel Franceschini
    me = netutils.Hostname()
70 9769bb78 Manuel Franceschini
    ip = constants.IP4_ADDRESS_LOCALHOST
71 f6bd6e98 Michael Hanselmann
72 b9eeeb02 Michael Hanselmann
    cluster_config = objects.Cluster(
73 b9eeeb02 Michael Hanselmann
      serial_no=1,
74 b9eeeb02 Michael Hanselmann
      rsahostkeypub="",
75 b9eeeb02 Michael Hanselmann
      highest_used_port=(constants.FIRST_DRBD_PORT - 1),
76 b9eeeb02 Michael Hanselmann
      mac_prefix="aa:00:00",
77 b9eeeb02 Michael Hanselmann
      volume_group_name="xenvg",
78 9e33896b Luca Bigliardi
      drbd_usermode_helper="/bin/true",
79 c66f21a4 Guido Trotter
      nicparams={constants.PP_DEFAULT: constants.NICC_DEFAULTS},
80 b9eeeb02 Michael Hanselmann
      tcpudp_port_pool=set(),
81 066f465d Guido Trotter
      enabled_hypervisors=[constants.HT_FAKE],
82 f6bd6e98 Michael Hanselmann
      master_node=me.name,
83 f6bd6e98 Michael Hanselmann
      master_ip="127.0.0.1",
84 f6bd6e98 Michael Hanselmann
      master_netdev=constants.DEFAULT_BRIDGE,
85 f6bd6e98 Michael Hanselmann
      cluster_name="cluster.local",
86 f6bd6e98 Michael Hanselmann
      file_storage_dir="/tmp",
87 93be53da Balazs Lecz
      uid_pool=[],
88 b9eeeb02 Michael Hanselmann
      )
89 f6bd6e98 Michael Hanselmann
90 f6bd6e98 Michael Hanselmann
    master_node_config = objects.Node(name=me.name,
91 f6bd6e98 Michael Hanselmann
                                      primary_ip=me.ip,
92 36b8c2c1 Michael Hanselmann
                                      secondary_ip=ip,
93 4a89c54a Iustin Pop
                                      serial_no=1,
94 4a89c54a Iustin Pop
                                      master_candidate=True)
95 f6bd6e98 Michael Hanselmann
96 c666722f Oleksiy Mishchenko
    bootstrap.InitConfig(constants.CONFIG_VERSION,
97 c666722f Oleksiy Mishchenko
                         cluster_config, master_node_config, self.cfg_file)
98 e00fb268 Iustin Pop
99 e00fb268 Iustin Pop
  def _create_instance(self):
100 e00fb268 Iustin Pop
    """Create and return an instance object"""
101 4a89c54a Iustin Pop
    inst = objects.Instance(name="test.example.com", disks=[], nics=[],
102 4a89c54a Iustin Pop
                            disk_template=constants.DT_DISKLESS,
103 4a89c54a Iustin Pop
                            primary_node=self._get_object().GetMasterNode())
104 e00fb268 Iustin Pop
    return inst
105 e00fb268 Iustin Pop
106 e00fb268 Iustin Pop
  def testEmpty(self):
107 e00fb268 Iustin Pop
    """Test instantiate config object"""
108 e00fb268 Iustin Pop
    self._get_object()
109 e00fb268 Iustin Pop
110 e00fb268 Iustin Pop
  def testInit(self):
111 e00fb268 Iustin Pop
    """Test initialize the config file"""
112 e00fb268 Iustin Pop
    cfg = self._get_object()
113 e00fb268 Iustin Pop
    self.failUnlessEqual(1, len(cfg.GetNodeList()))
114 e00fb268 Iustin Pop
    self.failUnlessEqual(0, len(cfg.GetInstanceList()))
115 e00fb268 Iustin Pop
116 e00fb268 Iustin Pop
  def testUpdateCluster(self):
117 e00fb268 Iustin Pop
    """Test updates on the cluster object"""
118 e00fb268 Iustin Pop
    cfg = self._get_object()
119 e00fb268 Iustin Pop
    # construct a fake cluster object
120 e00fb268 Iustin Pop
    fake_cl = objects.Cluster()
121 e00fb268 Iustin Pop
    # fail if we didn't read the config
122 a4eae71f Michael Hanselmann
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_cl, None)
123 e00fb268 Iustin Pop
124 e00fb268 Iustin Pop
    cl = cfg.GetClusterInfo()
125 e00fb268 Iustin Pop
    # first pass, must not fail
126 a4eae71f Michael Hanselmann
    cfg.Update(cl, None)
127 e00fb268 Iustin Pop
    # second pass, also must not fail (after the config has been written)
128 a4eae71f Michael Hanselmann
    cfg.Update(cl, None)
129 e00fb268 Iustin Pop
    # but the fake_cl update should still fail
130 a4eae71f Michael Hanselmann
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_cl, None)
131 e00fb268 Iustin Pop
132 e00fb268 Iustin Pop
  def testUpdateNode(self):
133 e00fb268 Iustin Pop
    """Test updates on one node object"""
134 e00fb268 Iustin Pop
    cfg = self._get_object()
135 e00fb268 Iustin Pop
    # construct a fake node
136 e00fb268 Iustin Pop
    fake_node = objects.Node()
137 e00fb268 Iustin Pop
    # fail if we didn't read the config
138 a4eae71f Michael Hanselmann
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_node,
139 a4eae71f Michael Hanselmann
                          None)
140 e00fb268 Iustin Pop
141 e00fb268 Iustin Pop
    node = cfg.GetNodeInfo(cfg.GetNodeList()[0])
142 e00fb268 Iustin Pop
    # first pass, must not fail
143 a4eae71f Michael Hanselmann
    cfg.Update(node, None)
144 e00fb268 Iustin Pop
    # second pass, also must not fail (after the config has been written)
145 a4eae71f Michael Hanselmann
    cfg.Update(node, None)
146 e00fb268 Iustin Pop
    # but the fake_node update should still fail
147 a4eae71f Michael Hanselmann
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_node,
148 a4eae71f Michael Hanselmann
                          None)
149 e00fb268 Iustin Pop
150 e00fb268 Iustin Pop
  def testUpdateInstance(self):
151 e00fb268 Iustin Pop
    """Test updates on one instance object"""
152 e00fb268 Iustin Pop
    cfg = self._get_object()
153 e00fb268 Iustin Pop
    # construct a fake instance
154 e00fb268 Iustin Pop
    inst = self._create_instance()
155 e00fb268 Iustin Pop
    fake_instance = objects.Instance()
156 e00fb268 Iustin Pop
    # fail if we didn't read the config
157 a4eae71f Michael Hanselmann
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance,
158 a4eae71f Michael Hanselmann
                          None)
159 e00fb268 Iustin Pop
160 0debfb35 Guido Trotter
    cfg.AddInstance(inst, "my-job")
161 e00fb268 Iustin Pop
    instance = cfg.GetInstanceInfo(cfg.GetInstanceList()[0])
162 e00fb268 Iustin Pop
    # first pass, must not fail
163 a4eae71f Michael Hanselmann
    cfg.Update(instance, None)
164 e00fb268 Iustin Pop
    # second pass, also must not fail (after the config has been written)
165 a4eae71f Michael Hanselmann
    cfg.Update(instance, None)
166 e00fb268 Iustin Pop
    # but the fake_instance update should still fail
167 a4eae71f Michael Hanselmann
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance,
168 a4eae71f Michael Hanselmann
                          None)
169 e00fb268 Iustin Pop
170 255e19d4 Guido Trotter
  def testNICParameterSyntaxCheck(self):
171 255e19d4 Guido Trotter
    """Test the NIC's CheckParameterSyntax function"""
172 255e19d4 Guido Trotter
    mode = constants.NIC_MODE
173 255e19d4 Guido Trotter
    link = constants.NIC_LINK
174 255e19d4 Guido Trotter
    m_bridged = constants.NIC_MODE_BRIDGED
175 255e19d4 Guido Trotter
    m_routed = constants.NIC_MODE_ROUTED
176 255e19d4 Guido Trotter
    CheckSyntax = objects.NIC.CheckParameterSyntax
177 255e19d4 Guido Trotter
178 255e19d4 Guido Trotter
    CheckSyntax(constants.NICC_DEFAULTS)
179 255e19d4 Guido Trotter
    CheckSyntax({mode: m_bridged, link: 'br1'})
180 255e19d4 Guido Trotter
    CheckSyntax({mode: m_routed, link: 'default'})
181 255e19d4 Guido Trotter
    self.assertRaises(errors.ConfigurationError,
182 255e19d4 Guido Trotter
                      CheckSyntax, {mode: '000invalid', link: 'any'})
183 255e19d4 Guido Trotter
    self.assertRaises(errors.ConfigurationError,
184 255e19d4 Guido Trotter
                      CheckSyntax, {mode: m_bridged, link: None})
185 255e19d4 Guido Trotter
    self.assertRaises(errors.ConfigurationError,
186 255e19d4 Guido Trotter
                      CheckSyntax, {mode: m_bridged, link: ''})
187 255e19d4 Guido Trotter
188 e00fb268 Iustin Pop
189 e00fb268 Iustin Pop
if __name__ == '__main__':
190 25231ec5 Michael Hanselmann
  testutils.GanetiTestProgram()