root / test / ganeti.ssconf_unittest.py @ dffa96d6
History | View | Annotate | Download (5.4 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 | 911dfc49 | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp()
|
99 | 911dfc49 | Michael Hanselmann | self.sstore = ssconf.SimpleStore(cfg_location=self.tmpdir) |
100 | 911dfc49 | Michael Hanselmann | |
101 | 911dfc49 | Michael Hanselmann | def tearDown(self): |
102 | 911dfc49 | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
103 | 911dfc49 | Michael Hanselmann | |
104 | 911dfc49 | Michael Hanselmann | def testInvalidKey(self): |
105 | 911dfc49 | Michael Hanselmann | self.assertRaises(errors.ProgrammerError, self.sstore.KeyToFilename, |
106 | 911dfc49 | Michael Hanselmann | "not a valid key")
|
107 | 911dfc49 | Michael Hanselmann | self.assertRaises(errors.ProgrammerError, self.sstore._ReadFile, |
108 | 911dfc49 | Michael Hanselmann | "not a valid key")
|
109 | 911dfc49 | Michael Hanselmann | |
110 | 911dfc49 | Michael Hanselmann | def testKeyToFilename(self): |
111 | 911dfc49 | Michael Hanselmann | for key in ssconf._VALID_KEYS: |
112 | 911dfc49 | Michael Hanselmann | result = self.sstore.KeyToFilename(key)
|
113 | 911dfc49 | Michael Hanselmann | self.assertTrue(utils.IsBelowDir(self.tmpdir, result)) |
114 | 911dfc49 | Michael Hanselmann | self.assertTrue(os.path.basename(result).startswith("ssconf_")) |
115 | 911dfc49 | Michael Hanselmann | |
116 | 911dfc49 | Michael Hanselmann | def testReadFileNonExistingFile(self): |
117 | 911dfc49 | Michael Hanselmann | filename = self.sstore.KeyToFilename(constants.SS_CLUSTER_NAME)
|
118 | 911dfc49 | Michael Hanselmann | |
119 | 911dfc49 | Michael Hanselmann | self.assertFalse(os.path.exists(filename))
|
120 | 911dfc49 | Michael Hanselmann | try:
|
121 | 911dfc49 | Michael Hanselmann | self.sstore._ReadFile(constants.SS_CLUSTER_NAME)
|
122 | 911dfc49 | Michael Hanselmann | except errors.ConfigurationError, err:
|
123 | 911dfc49 | Michael Hanselmann | self.assertTrue(str(err).startswith("Can't read ssconf file")) |
124 | 911dfc49 | Michael Hanselmann | else:
|
125 | 911dfc49 | Michael Hanselmann | self.fail("Exception was not raised") |
126 | 911dfc49 | Michael Hanselmann | |
127 | 911dfc49 | Michael Hanselmann | for default in ["", "Hello World", 0, 100]: |
128 | 911dfc49 | Michael Hanselmann | self.assertFalse(os.path.exists(filename))
|
129 | 911dfc49 | Michael Hanselmann | result = self.sstore._ReadFile(constants.SS_CLUSTER_NAME, default=default)
|
130 | 911dfc49 | Michael Hanselmann | self.assertEqual(result, default)
|
131 | 911dfc49 | Michael Hanselmann | |
132 | 911dfc49 | Michael Hanselmann | def testReadFile(self): |
133 | 911dfc49 | Michael Hanselmann | utils.WriteFile(self.sstore.KeyToFilename(constants.SS_CLUSTER_NAME),
|
134 | 911dfc49 | Michael Hanselmann | data="cluster.example.com")
|
135 | 911dfc49 | Michael Hanselmann | |
136 | 911dfc49 | Michael Hanselmann | self.assertEqual(self.sstore._ReadFile(constants.SS_CLUSTER_NAME), |
137 | 911dfc49 | Michael Hanselmann | "cluster.example.com")
|
138 | 911dfc49 | Michael Hanselmann | |
139 | 911dfc49 | Michael Hanselmann | self.assertEqual(self.sstore._ReadFile(constants.SS_CLUSTER_NAME, |
140 | 911dfc49 | Michael Hanselmann | default="something.example.com"),
|
141 | 911dfc49 | Michael Hanselmann | "cluster.example.com")
|
142 | 911dfc49 | Michael Hanselmann | |
143 | 911dfc49 | Michael Hanselmann | |
144 | dffa96d6 | Michael Hanselmann | class TestVerifyClusterName(unittest.TestCase): |
145 | dffa96d6 | Michael Hanselmann | def setUp(self): |
146 | dffa96d6 | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp()
|
147 | dffa96d6 | Michael Hanselmann | |
148 | dffa96d6 | Michael Hanselmann | def tearDown(self): |
149 | dffa96d6 | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
150 | dffa96d6 | Michael Hanselmann | |
151 | dffa96d6 | Michael Hanselmann | def testMissingFile(self): |
152 | dffa96d6 | Michael Hanselmann | tmploc = utils.PathJoin(self.tmpdir, "does-not-exist") |
153 | dffa96d6 | Michael Hanselmann | ssconf.VerifyClusterName(NotImplemented, _cfg_location=tmploc)
|
154 | dffa96d6 | Michael Hanselmann | |
155 | dffa96d6 | Michael Hanselmann | def testMatchingName(self): |
156 | dffa96d6 | Michael Hanselmann | tmpfile = utils.PathJoin(self.tmpdir, "ssconf_cluster_name") |
157 | dffa96d6 | Michael Hanselmann | |
158 | dffa96d6 | Michael Hanselmann | for content in ["cluster.example.com", "cluster.example.com\n\n"]: |
159 | dffa96d6 | Michael Hanselmann | utils.WriteFile(tmpfile, data=content) |
160 | dffa96d6 | Michael Hanselmann | ssconf.VerifyClusterName("cluster.example.com",
|
161 | dffa96d6 | Michael Hanselmann | _cfg_location=self.tmpdir)
|
162 | dffa96d6 | Michael Hanselmann | |
163 | dffa96d6 | Michael Hanselmann | def testNameMismatch(self): |
164 | dffa96d6 | Michael Hanselmann | tmpfile = utils.PathJoin(self.tmpdir, "ssconf_cluster_name") |
165 | dffa96d6 | Michael Hanselmann | |
166 | dffa96d6 | Michael Hanselmann | for content in ["something.example.com", "foobar\n\ncluster.example.com"]: |
167 | dffa96d6 | Michael Hanselmann | utils.WriteFile(tmpfile, data=content) |
168 | dffa96d6 | Michael Hanselmann | self.assertRaises(errors.GenericError, ssconf.VerifyClusterName,
|
169 | dffa96d6 | Michael Hanselmann | "cluster.example.com", _cfg_location=self.tmpdir) |
170 | dffa96d6 | Michael Hanselmann | |
171 | dffa96d6 | Michael Hanselmann | |
172 | 911dfc49 | Michael Hanselmann | if __name__ == "__main__": |
173 | 911dfc49 | Michael Hanselmann | testutils.GanetiTestProgram() |