Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.config_unittest.py @ 529d13a4

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

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

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

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

    
58
  def _init_cluster(self, cfg):
59
    """Initializes the cfg object"""
60
    me = utils.HostInfo()
61
    ip = constants.LOCALHOST_IP_ADDRESS
62

    
63
    cluster_config = objects.Cluster(
64
      serial_no=1,
65
      rsahostkeypub="",
66
      highest_used_port=(constants.FIRST_DRBD_PORT - 1),
67
      mac_prefix="aa:00:00",
68
      volume_group_name="xenvg",
69
      default_bridge=constants.DEFAULT_BRIDGE,
70
      tcpudp_port_pool=set(),
71
      default_hypervisor=constants.HT_FAKE,
72
      enabled_hypervisors=[constants.HT_FAKE],
73
      master_node=me.name,
74
      master_ip="127.0.0.1",
75
      master_netdev=constants.DEFAULT_BRIDGE,
76
      cluster_name="cluster.local",
77
      file_storage_dir="/tmp",
78
      )
79

    
80
    master_node_config = objects.Node(name=me.name,
81
                                      primary_ip=me.ip,
82
                                      secondary_ip=ip,
83
                                      serial_no=1,
84
                                      master_candidate=True)
85

    
86
    bootstrap.InitConfig(constants.CONFIG_VERSION,
87
                         cluster_config, master_node_config, self.cfg_file)
88

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

    
96
  def testEmpty(self):
97
    """Test instantiate config object"""
98
    self._get_object()
99

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

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

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

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

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

    
138
  def testUpdateInstance(self):
139
    """Test updates on one instance object"""
140
    cfg = self._get_object()
141
    # construct a fake instance
142
    inst = self._create_instance()
143
    fake_instance = objects.Instance()
144
    # fail if we didn't read the config
145
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance)
146

    
147
    cfg.AddInstance(inst)
148
    instance = cfg.GetInstanceInfo(cfg.GetInstanceList()[0])
149
    # first pass, must not fail
150
    cfg.Update(instance)
151
    # second pass, also must not fail (after the config has been written)
152
    cfg.Update(instance)
153
    # but the fake_instance update should still fail
154
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance)
155

    
156

    
157
if __name__ == '__main__':
158
  unittest.main()