|
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()
|