root / test / py / ganeti.ssconf_unittest.py @ 1f350e0f
History | View | Annotate | Download (8.2 kB)
1 | 911dfc49 | Michael Hanselmann | #!/usr/bin/python
|
---|---|---|---|
2 | 911dfc49 | Michael Hanselmann | #
|
3 | 911dfc49 | Michael Hanselmann | |
4 | 911dfc49 | Michael Hanselmann | # Copyright (C) 2012 Google Inc.
|
5 | 911dfc49 | Michael Hanselmann | #
|
6 | 911dfc49 | Michael Hanselmann | # This program is free software; you can redistribute it and/or modify
|
7 | 911dfc49 | Michael Hanselmann | # it under the terms of the GNU General Public License as published by
|
8 | 911dfc49 | Michael Hanselmann | # the Free Software Foundation; either version 2 of the License, or
|
9 | 911dfc49 | Michael Hanselmann | # (at your option) any later version.
|
10 | 911dfc49 | Michael Hanselmann | #
|
11 | 911dfc49 | Michael Hanselmann | # This program is distributed in the hope that it will be useful, but
|
12 | 911dfc49 | Michael Hanselmann | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 911dfc49 | Michael Hanselmann | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 911dfc49 | Michael Hanselmann | # General Public License for more details.
|
15 | 911dfc49 | Michael Hanselmann | #
|
16 | 911dfc49 | Michael Hanselmann | # You should have received a copy of the GNU General Public License
|
17 | 911dfc49 | Michael Hanselmann | # along with this program; if not, write to the Free Software
|
18 | 911dfc49 | Michael Hanselmann | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 911dfc49 | Michael Hanselmann | # 02110-1301, USA.
|
20 | 911dfc49 | Michael Hanselmann | |
21 | 911dfc49 | Michael Hanselmann | |
22 | 911dfc49 | Michael Hanselmann | """Script for testing ganeti.ssconf"""
|
23 | 911dfc49 | Michael Hanselmann | |
24 | 911dfc49 | Michael Hanselmann | import os |
25 | 911dfc49 | Michael Hanselmann | import unittest |
26 | 911dfc49 | Michael Hanselmann | import tempfile |
27 | 911dfc49 | Michael Hanselmann | import shutil |
28 | 911dfc49 | Michael Hanselmann | import errno |
29 | 911dfc49 | Michael Hanselmann | |
30 | 911dfc49 | Michael Hanselmann | from ganeti import utils |
31 | 911dfc49 | Michael Hanselmann | from ganeti import constants |
32 | 911dfc49 | Michael Hanselmann | from ganeti import errors |
33 | 911dfc49 | Michael Hanselmann | from ganeti import ssconf |
34 | 911dfc49 | Michael Hanselmann | |
35 | 911dfc49 | Michael Hanselmann | import testutils |
36 | 911dfc49 | Michael Hanselmann | |
37 | 911dfc49 | Michael Hanselmann | |
38 | 911dfc49 | Michael Hanselmann | class TestReadSsconfFile(unittest.TestCase): |
39 | 911dfc49 | Michael Hanselmann | def setUp(self): |
40 | 911dfc49 | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp()
|
41 | 911dfc49 | Michael Hanselmann | |
42 | 911dfc49 | Michael Hanselmann | def tearDown(self): |
43 | 911dfc49 | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
44 | 911dfc49 | Michael Hanselmann | |
45 | 911dfc49 | Michael Hanselmann | def testReadDirectory(self): |
46 | 911dfc49 | Michael Hanselmann | self.assertRaises(EnvironmentError, ssconf.ReadSsconfFile, self.tmpdir) |
47 | 911dfc49 | Michael Hanselmann | |
48 | 911dfc49 | Michael Hanselmann | def testNonExistantFile(self): |
49 | 911dfc49 | Michael Hanselmann | testfile = utils.PathJoin(self.tmpdir, "does.not.exist") |
50 | 911dfc49 | Michael Hanselmann | |
51 | 911dfc49 | Michael Hanselmann | self.assertFalse(os.path.exists(testfile))
|
52 | 911dfc49 | Michael Hanselmann | |
53 | 911dfc49 | Michael Hanselmann | try:
|
54 | 911dfc49 | Michael Hanselmann | ssconf.ReadSsconfFile(testfile) |
55 | 911dfc49 | Michael Hanselmann | except EnvironmentError, err: |
56 | 911dfc49 | Michael Hanselmann | self.assertEqual(err.errno, errno.ENOENT)
|
57 | 911dfc49 | Michael Hanselmann | else:
|
58 | 911dfc49 | Michael Hanselmann | self.fail("Exception was not raised") |
59 | 911dfc49 | Michael Hanselmann | |
60 | 911dfc49 | Michael Hanselmann | def testEmptyFile(self): |
61 | 911dfc49 | Michael Hanselmann | testfile = utils.PathJoin(self.tmpdir, "empty") |
62 | 911dfc49 | Michael Hanselmann | |
63 | 911dfc49 | Michael Hanselmann | utils.WriteFile(testfile, data="")
|
64 | 911dfc49 | Michael Hanselmann | |
65 | 911dfc49 | Michael Hanselmann | self.assertEqual(ssconf.ReadSsconfFile(testfile), "") |
66 | 911dfc49 | Michael Hanselmann | |
67 | 911dfc49 | Michael Hanselmann | def testSingleLine(self): |
68 | 911dfc49 | Michael Hanselmann | testfile = utils.PathJoin(self.tmpdir, "data") |
69 | 911dfc49 | Michael Hanselmann | |
70 | 911dfc49 | Michael Hanselmann | for nl in range(0, 10): |
71 | 911dfc49 | Michael Hanselmann | utils.WriteFile(testfile, data="Hello World" + ("\n" * nl)) |
72 | 911dfc49 | Michael Hanselmann | |
73 | 911dfc49 | Michael Hanselmann | self.assertEqual(ssconf.ReadSsconfFile(testfile),
|
74 | 911dfc49 | Michael Hanselmann | "Hello World")
|
75 | 911dfc49 | Michael Hanselmann | |
76 | 911dfc49 | Michael Hanselmann | def testExactlyMaxSize(self): |
77 | 911dfc49 | Michael Hanselmann | testfile = utils.PathJoin(self.tmpdir, "data") |
78 | 911dfc49 | Michael Hanselmann | |
79 | 911dfc49 | Michael Hanselmann | data = "A" * ssconf._MAX_SIZE
|
80 | 911dfc49 | Michael Hanselmann | utils.WriteFile(testfile, data=data) |
81 | 911dfc49 | Michael Hanselmann | |
82 | 911dfc49 | Michael Hanselmann | self.assertEqual(os.path.getsize(testfile), ssconf._MAX_SIZE)
|
83 | 911dfc49 | Michael Hanselmann | |
84 | 911dfc49 | Michael Hanselmann | self.assertEqual(ssconf.ReadSsconfFile(testfile),
|
85 | 911dfc49 | Michael Hanselmann | data) |
86 | 911dfc49 | Michael Hanselmann | |
87 | 911dfc49 | Michael Hanselmann | def testLargeFile(self): |
88 | 911dfc49 | Michael Hanselmann | testfile = utils.PathJoin(self.tmpdir, "data") |
89 | 911dfc49 | Michael Hanselmann | |
90 | 911dfc49 | Michael Hanselmann | for size in [ssconf._MAX_SIZE + 1, ssconf._MAX_SIZE * 2]: |
91 | 911dfc49 | Michael Hanselmann | utils.WriteFile(testfile, data="A" * size)
|
92 | 911dfc49 | Michael Hanselmann | self.assertTrue(os.path.getsize(testfile) > ssconf._MAX_SIZE)
|
93 | 911dfc49 | Michael Hanselmann | self.assertRaises(RuntimeError, ssconf.ReadSsconfFile, testfile) |
94 | 911dfc49 | Michael Hanselmann | |
95 | 911dfc49 | Michael Hanselmann | |
96 | 911dfc49 | Michael Hanselmann | class TestSimpleStore(unittest.TestCase): |
97 | 911dfc49 | Michael Hanselmann | def setUp(self): |
98 | cc7f5bfc | Michael Hanselmann | self._tmpdir = tempfile.mkdtemp()
|
99 | cc7f5bfc | Michael Hanselmann | self.ssdir = utils.PathJoin(self._tmpdir, "files") |
100 | cc7f5bfc | Michael Hanselmann | lockfile = utils.PathJoin(self._tmpdir, "lock") |
101 | cc7f5bfc | Michael Hanselmann | |
102 | cc7f5bfc | Michael Hanselmann | os.mkdir(self.ssdir)
|
103 | cc7f5bfc | Michael Hanselmann | |
104 | cc7f5bfc | Michael Hanselmann | self.sstore = ssconf.SimpleStore(cfg_location=self.ssdir, |
105 | cc7f5bfc | Michael Hanselmann | _lockfile=lockfile) |
106 | 911dfc49 | Michael Hanselmann | |
107 | 911dfc49 | Michael Hanselmann | def tearDown(self): |
108 | cc7f5bfc | Michael Hanselmann | shutil.rmtree(self._tmpdir)
|
109 | cc7f5bfc | Michael Hanselmann | |
110 | cc7f5bfc | Michael Hanselmann | def _ReadSsFile(self, filename): |
111 | cc7f5bfc | Michael Hanselmann | return utils.ReadFile(utils.PathJoin(self.ssdir, "ssconf_%s" % filename)) |
112 | 911dfc49 | Michael Hanselmann | |
113 | 911dfc49 | Michael Hanselmann | def testInvalidKey(self): |
114 | 911dfc49 | Michael Hanselmann | self.assertRaises(errors.ProgrammerError, self.sstore.KeyToFilename, |
115 | 911dfc49 | Michael Hanselmann | "not a valid key")
|
116 | 911dfc49 | Michael Hanselmann | self.assertRaises(errors.ProgrammerError, self.sstore._ReadFile, |
117 | 911dfc49 | Michael Hanselmann | "not a valid key")
|
118 | 911dfc49 | Michael Hanselmann | |
119 | 911dfc49 | Michael Hanselmann | def testKeyToFilename(self): |
120 | 911dfc49 | Michael Hanselmann | for key in ssconf._VALID_KEYS: |
121 | 911dfc49 | Michael Hanselmann | result = self.sstore.KeyToFilename(key)
|
122 | cc7f5bfc | Michael Hanselmann | self.assertTrue(utils.IsBelowDir(self.ssdir, result)) |
123 | 911dfc49 | Michael Hanselmann | self.assertTrue(os.path.basename(result).startswith("ssconf_")) |
124 | 911dfc49 | Michael Hanselmann | |
125 | 911dfc49 | Michael Hanselmann | def testReadFileNonExistingFile(self): |
126 | 911dfc49 | Michael Hanselmann | filename = self.sstore.KeyToFilename(constants.SS_CLUSTER_NAME)
|
127 | 911dfc49 | Michael Hanselmann | |
128 | 911dfc49 | Michael Hanselmann | self.assertFalse(os.path.exists(filename))
|
129 | 911dfc49 | Michael Hanselmann | try:
|
130 | 911dfc49 | Michael Hanselmann | self.sstore._ReadFile(constants.SS_CLUSTER_NAME)
|
131 | 911dfc49 | Michael Hanselmann | except errors.ConfigurationError, err:
|
132 | 911dfc49 | Michael Hanselmann | self.assertTrue(str(err).startswith("Can't read ssconf file")) |
133 | 911dfc49 | Michael Hanselmann | else:
|
134 | 911dfc49 | Michael Hanselmann | self.fail("Exception was not raised") |
135 | 911dfc49 | Michael Hanselmann | |
136 | 911dfc49 | Michael Hanselmann | for default in ["", "Hello World", 0, 100]: |
137 | 911dfc49 | Michael Hanselmann | self.assertFalse(os.path.exists(filename))
|
138 | 911dfc49 | Michael Hanselmann | result = self.sstore._ReadFile(constants.SS_CLUSTER_NAME, default=default)
|
139 | 911dfc49 | Michael Hanselmann | self.assertEqual(result, default)
|
140 | 911dfc49 | Michael Hanselmann | |
141 | 911dfc49 | Michael Hanselmann | def testReadFile(self): |
142 | 911dfc49 | Michael Hanselmann | utils.WriteFile(self.sstore.KeyToFilename(constants.SS_CLUSTER_NAME),
|
143 | 911dfc49 | Michael Hanselmann | data="cluster.example.com")
|
144 | 911dfc49 | Michael Hanselmann | |
145 | 911dfc49 | Michael Hanselmann | self.assertEqual(self.sstore._ReadFile(constants.SS_CLUSTER_NAME), |
146 | 911dfc49 | Michael Hanselmann | "cluster.example.com")
|
147 | 911dfc49 | Michael Hanselmann | |
148 | 911dfc49 | Michael Hanselmann | self.assertEqual(self.sstore._ReadFile(constants.SS_CLUSTER_NAME, |
149 | 911dfc49 | Michael Hanselmann | default="something.example.com"),
|
150 | 911dfc49 | Michael Hanselmann | "cluster.example.com")
|
151 | 911dfc49 | Michael Hanselmann | |
152 | 9b4329e9 | Michael Hanselmann | def testReadAllNoFiles(self): |
153 | 9b4329e9 | Michael Hanselmann | self.assertEqual(self.sstore.ReadAll(), {}) |
154 | 9b4329e9 | Michael Hanselmann | |
155 | 9b4329e9 | Michael Hanselmann | def testReadAllSingleFile(self): |
156 | 9b4329e9 | Michael Hanselmann | utils.WriteFile(self.sstore.KeyToFilename(constants.SS_CLUSTER_NAME),
|
157 | 9b4329e9 | Michael Hanselmann | data="cluster.example.com")
|
158 | 9b4329e9 | Michael Hanselmann | self.assertEqual(self.sstore.ReadAll(), { |
159 | 9b4329e9 | Michael Hanselmann | constants.SS_CLUSTER_NAME: "cluster.example.com",
|
160 | 9b4329e9 | Michael Hanselmann | }) |
161 | 9b4329e9 | Michael Hanselmann | |
162 | cc7f5bfc | Michael Hanselmann | def testWriteFiles(self): |
163 | cc7f5bfc | Michael Hanselmann | values = { |
164 | cc7f5bfc | Michael Hanselmann | constants.SS_CLUSTER_NAME: "cluster.example.com",
|
165 | cc7f5bfc | Michael Hanselmann | constants.SS_CLUSTER_TAGS: "value\nwith\nnewlines\n",
|
166 | cc7f5bfc | Michael Hanselmann | constants.SS_INSTANCE_LIST: "",
|
167 | cc7f5bfc | Michael Hanselmann | } |
168 | cc7f5bfc | Michael Hanselmann | |
169 | cc7f5bfc | Michael Hanselmann | self.sstore.WriteFiles(values)
|
170 | cc7f5bfc | Michael Hanselmann | |
171 | cc7f5bfc | Michael Hanselmann | self.assertEqual(sorted(os.listdir(self.ssdir)), sorted([ |
172 | cc7f5bfc | Michael Hanselmann | "ssconf_cluster_name",
|
173 | cc7f5bfc | Michael Hanselmann | "ssconf_cluster_tags",
|
174 | cc7f5bfc | Michael Hanselmann | "ssconf_instance_list",
|
175 | cc7f5bfc | Michael Hanselmann | ])) |
176 | cc7f5bfc | Michael Hanselmann | |
177 | cc7f5bfc | Michael Hanselmann | self.assertEqual(self._ReadSsFile(constants.SS_CLUSTER_NAME), |
178 | cc7f5bfc | Michael Hanselmann | "cluster.example.com\n")
|
179 | cc7f5bfc | Michael Hanselmann | self.assertEqual(self._ReadSsFile(constants.SS_CLUSTER_TAGS), |
180 | cc7f5bfc | Michael Hanselmann | "value\nwith\nnewlines\n")
|
181 | cc7f5bfc | Michael Hanselmann | self.assertEqual(self._ReadSsFile(constants.SS_INSTANCE_LIST), "") |
182 | cc7f5bfc | Michael Hanselmann | |
183 | cc7f5bfc | Michael Hanselmann | def testWriteFilesUnknownKey(self): |
184 | cc7f5bfc | Michael Hanselmann | values = { |
185 | cc7f5bfc | Michael Hanselmann | "unknown key": "value", |
186 | cc7f5bfc | Michael Hanselmann | } |
187 | cc7f5bfc | Michael Hanselmann | |
188 | cc7f5bfc | Michael Hanselmann | self.assertRaises(errors.ProgrammerError, self.sstore.WriteFiles, |
189 | cc7f5bfc | Michael Hanselmann | values, dry_run=True)
|
190 | cc7f5bfc | Michael Hanselmann | |
191 | cc7f5bfc | Michael Hanselmann | self.assertEqual(os.listdir(self.ssdir), []) |
192 | cc7f5bfc | Michael Hanselmann | |
193 | cc7f5bfc | Michael Hanselmann | def testWriteFilesDryRun(self): |
194 | cc7f5bfc | Michael Hanselmann | values = { |
195 | cc7f5bfc | Michael Hanselmann | constants.SS_CLUSTER_NAME: "cluster.example.com",
|
196 | cc7f5bfc | Michael Hanselmann | } |
197 | cc7f5bfc | Michael Hanselmann | |
198 | cc7f5bfc | Michael Hanselmann | self.sstore.WriteFiles(values, dry_run=True) |
199 | cc7f5bfc | Michael Hanselmann | |
200 | cc7f5bfc | Michael Hanselmann | self.assertEqual(os.listdir(self.ssdir), []) |
201 | cc7f5bfc | Michael Hanselmann | |
202 | cc7f5bfc | Michael Hanselmann | def testWriteFilesNoValues(self): |
203 | cc7f5bfc | Michael Hanselmann | for dry_run in [False, True]: |
204 | cc7f5bfc | Michael Hanselmann | self.sstore.WriteFiles({}, dry_run=dry_run)
|
205 | cc7f5bfc | Michael Hanselmann | |
206 | cc7f5bfc | Michael Hanselmann | self.assertEqual(os.listdir(self.ssdir), []) |
207 | cc7f5bfc | Michael Hanselmann | |
208 | cc7f5bfc | Michael Hanselmann | def testWriteFilesTooLong(self): |
209 | cc7f5bfc | Michael Hanselmann | values = { |
210 | cc7f5bfc | Michael Hanselmann | constants.SS_INSTANCE_LIST: "A" * ssconf._MAX_SIZE,
|
211 | cc7f5bfc | Michael Hanselmann | } |
212 | cc7f5bfc | Michael Hanselmann | |
213 | cc7f5bfc | Michael Hanselmann | for dry_run in [False, True]: |
214 | cc7f5bfc | Michael Hanselmann | try:
|
215 | cc7f5bfc | Michael Hanselmann | self.sstore.WriteFiles(values, dry_run=dry_run)
|
216 | cc7f5bfc | Michael Hanselmann | except errors.ConfigurationError, err:
|
217 | cc7f5bfc | Michael Hanselmann | self.assertTrue(str(err).startswith("Value 'instance_list' has")) |
218 | cc7f5bfc | Michael Hanselmann | else:
|
219 | cc7f5bfc | Michael Hanselmann | self.fail("Exception was not raised") |
220 | cc7f5bfc | Michael Hanselmann | |
221 | cc7f5bfc | Michael Hanselmann | self.assertEqual(os.listdir(self.ssdir), []) |
222 | cc7f5bfc | Michael Hanselmann | |
223 | 911dfc49 | Michael Hanselmann | |
224 | dffa96d6 | Michael Hanselmann | class TestVerifyClusterName(unittest.TestCase): |
225 | dffa96d6 | Michael Hanselmann | def setUp(self): |
226 | dffa96d6 | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp()
|
227 | dffa96d6 | Michael Hanselmann | |
228 | dffa96d6 | Michael Hanselmann | def tearDown(self): |
229 | dffa96d6 | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
230 | dffa96d6 | Michael Hanselmann | |
231 | dffa96d6 | Michael Hanselmann | def testMissingFile(self): |
232 | dffa96d6 | Michael Hanselmann | tmploc = utils.PathJoin(self.tmpdir, "does-not-exist") |
233 | dffa96d6 | Michael Hanselmann | ssconf.VerifyClusterName(NotImplemented, _cfg_location=tmploc)
|
234 | dffa96d6 | Michael Hanselmann | |
235 | dffa96d6 | Michael Hanselmann | def testMatchingName(self): |
236 | dffa96d6 | Michael Hanselmann | tmpfile = utils.PathJoin(self.tmpdir, "ssconf_cluster_name") |
237 | dffa96d6 | Michael Hanselmann | |
238 | dffa96d6 | Michael Hanselmann | for content in ["cluster.example.com", "cluster.example.com\n\n"]: |
239 | dffa96d6 | Michael Hanselmann | utils.WriteFile(tmpfile, data=content) |
240 | dffa96d6 | Michael Hanselmann | ssconf.VerifyClusterName("cluster.example.com",
|
241 | dffa96d6 | Michael Hanselmann | _cfg_location=self.tmpdir)
|
242 | dffa96d6 | Michael Hanselmann | |
243 | dffa96d6 | Michael Hanselmann | def testNameMismatch(self): |
244 | dffa96d6 | Michael Hanselmann | tmpfile = utils.PathJoin(self.tmpdir, "ssconf_cluster_name") |
245 | dffa96d6 | Michael Hanselmann | |
246 | dffa96d6 | Michael Hanselmann | for content in ["something.example.com", "foobar\n\ncluster.example.com"]: |
247 | dffa96d6 | Michael Hanselmann | utils.WriteFile(tmpfile, data=content) |
248 | dffa96d6 | Michael Hanselmann | self.assertRaises(errors.GenericError, ssconf.VerifyClusterName,
|
249 | dffa96d6 | Michael Hanselmann | "cluster.example.com", _cfg_location=self.tmpdir) |
250 | dffa96d6 | Michael Hanselmann | |
251 | dffa96d6 | Michael Hanselmann | |
252 | 29a32ce5 | Michael Hanselmann | class TestVerifyKeys(unittest.TestCase): |
253 | 29a32ce5 | Michael Hanselmann | def testNoKeys(self): |
254 | 29a32ce5 | Michael Hanselmann | ssconf.VerifyKeys({}) |
255 | 29a32ce5 | Michael Hanselmann | |
256 | 29a32ce5 | Michael Hanselmann | def testValidKeys(self): |
257 | 29a32ce5 | Michael Hanselmann | ssconf.VerifyKeys(ssconf._VALID_KEYS) |
258 | 29a32ce5 | Michael Hanselmann | |
259 | 29a32ce5 | Michael Hanselmann | for key in ssconf._VALID_KEYS: |
260 | 29a32ce5 | Michael Hanselmann | ssconf.VerifyKeys([key]) |
261 | 29a32ce5 | Michael Hanselmann | |
262 | 29a32ce5 | Michael Hanselmann | def testInvalidKeys(self): |
263 | 29a32ce5 | Michael Hanselmann | for key in ["", ".", " ", "foo", "bar", "HelloWorld"]: |
264 | 29a32ce5 | Michael Hanselmann | self.assertRaises(errors.GenericError, ssconf.VerifyKeys, [key])
|
265 | 29a32ce5 | Michael Hanselmann | |
266 | 29a32ce5 | Michael Hanselmann | |
267 | 911dfc49 | Michael Hanselmann | if __name__ == "__main__": |
268 | 911dfc49 | Michael Hanselmann | testutils.GanetiTestProgram() |