Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.config_unittest.py @ 9e33896b

History | View | Annotate | Download (6 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 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

    
39
import testutils
40

    
41

    
42
class TestConfigRunner(unittest.TestCase):
43
  """Testing case for HooksRunner"""
44
  def setUp(self):
45
    fd, self.cfg_file = tempfile.mkstemp()
46
    os.close(fd)
47
    self._init_cluster(self.cfg_file)
48

    
49
  def tearDown(self):
50
    try:
51
      os.unlink(self.cfg_file)
52
    except OSError:
53
      pass
54

    
55
  def _get_object(self):
56
    """Returns a instance of ConfigWriter"""
57
    cfg = config.ConfigWriter(cfg_file=self.cfg_file, offline=True)
58
    return cfg
59

    
60
  def _init_cluster(self, cfg):
61
    """Initializes the cfg object"""
62
    me = utils.HostInfo()
63
    ip = constants.IP4_ADDRESS_LOCALHOST
64

    
65
    cluster_config = objects.Cluster(
66
      serial_no=1,
67
      rsahostkeypub="",
68
      highest_used_port=(constants.FIRST_DRBD_PORT - 1),
69
      mac_prefix="aa:00:00",
70
      volume_group_name="xenvg",
71
      drbd_usermode_helper="/bin/true",
72
      nicparams={constants.PP_DEFAULT: constants.NICC_DEFAULTS},
73
      tcpudp_port_pool=set(),
74
      enabled_hypervisors=[constants.HT_FAKE],
75
      master_node=me.name,
76
      master_ip="127.0.0.1",
77
      master_netdev=constants.DEFAULT_BRIDGE,
78
      cluster_name="cluster.local",
79
      file_storage_dir="/tmp",
80
      uid_pool=[],
81
      )
82

    
83
    master_node_config = objects.Node(name=me.name,
84
                                      primary_ip=me.ip,
85
                                      secondary_ip=ip,
86
                                      serial_no=1,
87
                                      master_candidate=True)
88

    
89
    bootstrap.InitConfig(constants.CONFIG_VERSION,
90
                         cluster_config, master_node_config, self.cfg_file)
91

    
92
  def _create_instance(self):
93
    """Create and return an instance object"""
94
    inst = objects.Instance(name="test.example.com", disks=[], nics=[],
95
                            disk_template=constants.DT_DISKLESS,
96
                            primary_node=self._get_object().GetMasterNode())
97
    return inst
98

    
99
  def testEmpty(self):
100
    """Test instantiate config object"""
101
    self._get_object()
102

    
103
  def testInit(self):
104
    """Test initialize the config file"""
105
    cfg = self._get_object()
106
    self.failUnlessEqual(1, len(cfg.GetNodeList()))
107
    self.failUnlessEqual(0, len(cfg.GetInstanceList()))
108

    
109
  def testUpdateCluster(self):
110
    """Test updates on the cluster object"""
111
    cfg = self._get_object()
112
    # construct a fake cluster object
113
    fake_cl = objects.Cluster()
114
    # fail if we didn't read the config
115
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_cl, None)
116

    
117
    cl = cfg.GetClusterInfo()
118
    # first pass, must not fail
119
    cfg.Update(cl, None)
120
    # second pass, also must not fail (after the config has been written)
121
    cfg.Update(cl, None)
122
    # but the fake_cl update should still fail
123
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_cl, None)
124

    
125
  def testUpdateNode(self):
126
    """Test updates on one node object"""
127
    cfg = self._get_object()
128
    # construct a fake node
129
    fake_node = objects.Node()
130
    # fail if we didn't read the config
131
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_node,
132
                          None)
133

    
134
    node = cfg.GetNodeInfo(cfg.GetNodeList()[0])
135
    # first pass, must not fail
136
    cfg.Update(node, None)
137
    # second pass, also must not fail (after the config has been written)
138
    cfg.Update(node, None)
139
    # but the fake_node update should still fail
140
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_node,
141
                          None)
142

    
143
  def testUpdateInstance(self):
144
    """Test updates on one instance object"""
145
    cfg = self._get_object()
146
    # construct a fake instance
147
    inst = self._create_instance()
148
    fake_instance = objects.Instance()
149
    # fail if we didn't read the config
150
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance,
151
                          None)
152

    
153
    cfg.AddInstance(inst, "my-job")
154
    instance = cfg.GetInstanceInfo(cfg.GetInstanceList()[0])
155
    # first pass, must not fail
156
    cfg.Update(instance, None)
157
    # second pass, also must not fail (after the config has been written)
158
    cfg.Update(instance, None)
159
    # but the fake_instance update should still fail
160
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance,
161
                          None)
162

    
163
  def testNICParameterSyntaxCheck(self):
164
    """Test the NIC's CheckParameterSyntax function"""
165
    mode = constants.NIC_MODE
166
    link = constants.NIC_LINK
167
    m_bridged = constants.NIC_MODE_BRIDGED
168
    m_routed = constants.NIC_MODE_ROUTED
169
    CheckSyntax = objects.NIC.CheckParameterSyntax
170

    
171
    CheckSyntax(constants.NICC_DEFAULTS)
172
    CheckSyntax({mode: m_bridged, link: 'br1'})
173
    CheckSyntax({mode: m_routed, link: 'default'})
174
    self.assertRaises(errors.ConfigurationError,
175
                      CheckSyntax, {mode: '000invalid', link: 'any'})
176
    self.assertRaises(errors.ConfigurationError,
177
                      CheckSyntax, {mode: m_bridged, link: None})
178
    self.assertRaises(errors.ConfigurationError,
179
                      CheckSyntax, {mode: m_bridged, link: ''})
180

    
181

    
182
if __name__ == '__main__':
183
  testutils.GanetiTestProgram()