Revision e00fb268

b/lib/config.py
297 297
    if not isinstance(instance, objects.Instance):
298 298
      raise errors.ProgrammerError("Invalid type passed to AddInstance")
299 299

  
300
    all_lvs = instance.MapLVsByNode()
301
    logger.Info("Instance '%s' DISK_LAYOUT: %s" % (instance.name, all_lvs))
300
    if instance.disk_template != constants.DT_DISKLESS:
301
      all_lvs = instance.MapLVsByNode()
302
      logger.Info("Instance '%s' DISK_LAYOUT: %s" % (instance.name, all_lvs))
302 303

  
303 304
    self._OpenConfig()
304 305
    self._config_data.instances[instance.name] = instance
......
624 625
    self._ReleaseLock()
625 626

  
626 627
    return self._config_data.cluster
628

  
629
  def Update(self, target):
630
    """Notify function to be called after updates.
631

  
632
    This function must be called when an object (as returned by
633
    GetInstanceInfo, GetNodeInfo, GetCluster) has been updated and the
634
    caller wants the modifications saved to the backing store. Note
635
    that all modified objects will be saved, but the target argument
636
    is the one the caller wants to ensure that it's saved.
637

  
638
    """
639
    if self._config_data is None:
640
      raise errors.ProgrammerError, ("Configuration file not read,"
641
                                     " cannot save.")
642
    if isinstance(target, objects.Cluster):
643
      test = target == self._config_data.cluster
644
    elif isinstance(target, objects.Node):
645
      test = target in self._config_data.nodes.values()
646
    elif isinstance(target, objects.Instance):
647
      test = target in self._config_data.instances.values()
648
    else:
649
      raise errors.ProgrammerError, ("Invalid object type (%s) passed to"
650
                                     " ConfigWriter.Update" % type(target))
651
    if not test:
652
      raise errors.ConfigurationError, ("Configuration updated since object"
653
                                        " has been read or unknown object")
654
    self._WriteConfig()
b/testing/ganeti.config_unittest.py
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 errors
33
from ganeti import constants
34
from ganeti import config
35
from ganeti import objects
36

  
37

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

  
44
  def tearDown(self):
45
    try:
46
      os.unlink(self.cfg_file)
47
    except OSError:
48
      pass
49

  
50
  def _get_object(self):
51
    """Returns a instance of ConfigWriter"""
52
    cfg = config.ConfigWriter(cfg_file=self.cfg_file, offline=True)
53
    return cfg
54

  
55
  def _init_cluster(self, cfg):
56
    """Initializes the cfg object"""
57
    cfg.InitConfig(socket.gethostname(), '127.0.0.1', None, '', 'aa:00:00',
58
                   'xenvg', constants.DEFAULT_BRIDGE)
59

  
60
  def _create_instance(self):
61
    """Create and return an instance object"""
62
    inst = objects.Instance(name="test.example.com", disks=[],
63
                            disk_template=constants.DT_DISKLESS)
64
    return inst
65

  
66
  def testEmpty(self):
67
    """Test instantiate config object"""
68
    self._get_object()
69

  
70
  def testInit(self):
71
    """Test initialize the config file"""
72
    cfg = self._get_object()
73
    self._init_cluster(cfg)
74
    self.failUnlessEqual(1, len(cfg.GetNodeList()))
75
    self.failUnlessEqual(0, len(cfg.GetInstanceList()))
76

  
77
  def testUpdateCluster(self):
78
    """Test updates on the cluster object"""
79
    cfg = self._get_object()
80
    # construct a fake cluster object
81
    fake_cl = objects.Cluster()
82
    # fail if we didn't read the config
83
    self.failUnlessRaises(errors.ProgrammerError, cfg.Update, fake_cl)
84

  
85
    self._init_cluster(cfg)
86
    cl = cfg.GetClusterInfo()
87
    # first pass, must not fail
88
    cfg.Update(cl)
89
    # second pass, also must not fail (after the config has been written)
90
    cfg.Update(cl)
91
    # but the fake_cl update should still fail
92
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_cl)
93

  
94
  def testUpdateNode(self):
95
    """Test updates on one node object"""
96
    cfg = self._get_object()
97
    # construct a fake node
98
    fake_node = objects.Node()
99
    # fail if we didn't read the config
100
    self.failUnlessRaises(errors.ProgrammerError, cfg.Update, fake_node)
101

  
102
    self._init_cluster(cfg)
103
    node = cfg.GetNodeInfo(cfg.GetNodeList()[0])
104
    # first pass, must not fail
105
    cfg.Update(node)
106
    # second pass, also must not fail (after the config has been written)
107
    cfg.Update(node)
108
    # but the fake_node update should still fail
109
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_node)
110

  
111
  def testUpdateInstance(self):
112
    """Test updates on one instance object"""
113
    cfg = self._get_object()
114
    # construct a fake instance
115
    inst = self._create_instance()
116
    fake_instance = objects.Instance()
117
    # fail if we didn't read the config
118
    self.failUnlessRaises(errors.ProgrammerError, cfg.Update, fake_instance)
119

  
120
    self._init_cluster(cfg)
121
    cfg.AddInstance(inst)
122
    instance = cfg.GetInstanceInfo(cfg.GetInstanceList()[0])
123
    # first pass, must not fail
124
    cfg.Update(instance)
125
    # second pass, also must not fail (after the config has been written)
126
    cfg.Update(instance)
127
    # but the fake_instance update should still fail
128
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance)
129

  
130

  
131
if __name__ == '__main__':
132
  unittest.main()

Also available in: Unified diff