Statistics
| Branch: | Tag: | Revision:

root / testing / ganeti.config_unittest.py @ f6441c7c

History | View | Annotate | Download (4.1 kB)

1 e00fb268 Iustin Pop
#!/usr/bin/python
2 e00fb268 Iustin Pop
#
3 e00fb268 Iustin Pop
4 e00fb268 Iustin Pop
# Copyright (C) 2006, 2007 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 e00fb268 Iustin Pop
from ganeti import errors
33 e00fb268 Iustin Pop
from ganeti import constants
34 e00fb268 Iustin Pop
from ganeti import config
35 e00fb268 Iustin Pop
from ganeti import objects
36 e00fb268 Iustin Pop
37 e00fb268 Iustin Pop
38 e00fb268 Iustin Pop
class TestConfigRunner(unittest.TestCase):
39 e00fb268 Iustin Pop
  """Testing case for HooksRunner"""
40 e00fb268 Iustin Pop
  def setUp(self):
41 e00fb268 Iustin Pop
    fd, self.cfg_file = tempfile.mkstemp()
42 e00fb268 Iustin Pop
    os.close(fd)
43 e00fb268 Iustin Pop
44 e00fb268 Iustin Pop
  def tearDown(self):
45 e00fb268 Iustin Pop
    try:
46 e00fb268 Iustin Pop
      os.unlink(self.cfg_file)
47 e00fb268 Iustin Pop
    except OSError:
48 e00fb268 Iustin Pop
      pass
49 e00fb268 Iustin Pop
50 e00fb268 Iustin Pop
  def _get_object(self):
51 e00fb268 Iustin Pop
    """Returns a instance of ConfigWriter"""
52 e00fb268 Iustin Pop
    cfg = config.ConfigWriter(cfg_file=self.cfg_file, offline=True)
53 e00fb268 Iustin Pop
    return cfg
54 e00fb268 Iustin Pop
55 e00fb268 Iustin Pop
  def _init_cluster(self, cfg):
56 e00fb268 Iustin Pop
    """Initializes the cfg object"""
57 e00fb268 Iustin Pop
    cfg.InitConfig(socket.gethostname(), '127.0.0.1', None, '', 'aa:00:00',
58 e00fb268 Iustin Pop
                   'xenvg', constants.DEFAULT_BRIDGE)
59 e00fb268 Iustin Pop
60 e00fb268 Iustin Pop
  def _create_instance(self):
61 e00fb268 Iustin Pop
    """Create and return an instance object"""
62 e00fb268 Iustin Pop
    inst = objects.Instance(name="test.example.com", disks=[],
63 e00fb268 Iustin Pop
                            disk_template=constants.DT_DISKLESS)
64 e00fb268 Iustin Pop
    return inst
65 e00fb268 Iustin Pop
66 e00fb268 Iustin Pop
  def testEmpty(self):
67 e00fb268 Iustin Pop
    """Test instantiate config object"""
68 e00fb268 Iustin Pop
    self._get_object()
69 e00fb268 Iustin Pop
70 e00fb268 Iustin Pop
  def testInit(self):
71 e00fb268 Iustin Pop
    """Test initialize the config file"""
72 e00fb268 Iustin Pop
    cfg = self._get_object()
73 e00fb268 Iustin Pop
    self._init_cluster(cfg)
74 e00fb268 Iustin Pop
    self.failUnlessEqual(1, len(cfg.GetNodeList()))
75 e00fb268 Iustin Pop
    self.failUnlessEqual(0, len(cfg.GetInstanceList()))
76 e00fb268 Iustin Pop
77 e00fb268 Iustin Pop
  def testUpdateCluster(self):
78 e00fb268 Iustin Pop
    """Test updates on the cluster object"""
79 e00fb268 Iustin Pop
    cfg = self._get_object()
80 e00fb268 Iustin Pop
    # construct a fake cluster object
81 e00fb268 Iustin Pop
    fake_cl = objects.Cluster()
82 e00fb268 Iustin Pop
    # fail if we didn't read the config
83 e00fb268 Iustin Pop
    self.failUnlessRaises(errors.ProgrammerError, cfg.Update, fake_cl)
84 e00fb268 Iustin Pop
85 e00fb268 Iustin Pop
    self._init_cluster(cfg)
86 e00fb268 Iustin Pop
    cl = cfg.GetClusterInfo()
87 e00fb268 Iustin Pop
    # first pass, must not fail
88 e00fb268 Iustin Pop
    cfg.Update(cl)
89 e00fb268 Iustin Pop
    # second pass, also must not fail (after the config has been written)
90 e00fb268 Iustin Pop
    cfg.Update(cl)
91 e00fb268 Iustin Pop
    # but the fake_cl update should still fail
92 e00fb268 Iustin Pop
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_cl)
93 e00fb268 Iustin Pop
94 e00fb268 Iustin Pop
  def testUpdateNode(self):
95 e00fb268 Iustin Pop
    """Test updates on one node object"""
96 e00fb268 Iustin Pop
    cfg = self._get_object()
97 e00fb268 Iustin Pop
    # construct a fake node
98 e00fb268 Iustin Pop
    fake_node = objects.Node()
99 e00fb268 Iustin Pop
    # fail if we didn't read the config
100 e00fb268 Iustin Pop
    self.failUnlessRaises(errors.ProgrammerError, cfg.Update, fake_node)
101 e00fb268 Iustin Pop
102 e00fb268 Iustin Pop
    self._init_cluster(cfg)
103 e00fb268 Iustin Pop
    node = cfg.GetNodeInfo(cfg.GetNodeList()[0])
104 e00fb268 Iustin Pop
    # first pass, must not fail
105 e00fb268 Iustin Pop
    cfg.Update(node)
106 e00fb268 Iustin Pop
    # second pass, also must not fail (after the config has been written)
107 e00fb268 Iustin Pop
    cfg.Update(node)
108 e00fb268 Iustin Pop
    # but the fake_node update should still fail
109 e00fb268 Iustin Pop
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_node)
110 e00fb268 Iustin Pop
111 e00fb268 Iustin Pop
  def testUpdateInstance(self):
112 e00fb268 Iustin Pop
    """Test updates on one instance object"""
113 e00fb268 Iustin Pop
    cfg = self._get_object()
114 e00fb268 Iustin Pop
    # construct a fake instance
115 e00fb268 Iustin Pop
    inst = self._create_instance()
116 e00fb268 Iustin Pop
    fake_instance = objects.Instance()
117 e00fb268 Iustin Pop
    # fail if we didn't read the config
118 e00fb268 Iustin Pop
    self.failUnlessRaises(errors.ProgrammerError, cfg.Update, fake_instance)
119 e00fb268 Iustin Pop
120 e00fb268 Iustin Pop
    self._init_cluster(cfg)
121 e00fb268 Iustin Pop
    cfg.AddInstance(inst)
122 e00fb268 Iustin Pop
    instance = cfg.GetInstanceInfo(cfg.GetInstanceList()[0])
123 e00fb268 Iustin Pop
    # first pass, must not fail
124 e00fb268 Iustin Pop
    cfg.Update(instance)
125 e00fb268 Iustin Pop
    # second pass, also must not fail (after the config has been written)
126 e00fb268 Iustin Pop
    cfg.Update(instance)
127 e00fb268 Iustin Pop
    # but the fake_instance update should still fail
128 e00fb268 Iustin Pop
    self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance)
129 e00fb268 Iustin Pop
130 e00fb268 Iustin Pop
131 e00fb268 Iustin Pop
if __name__ == '__main__':
132 e00fb268 Iustin Pop
  unittest.main()