Update default instance kernel version
[ganeti-local] / test / cfgupgrade_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2010 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 testing tools/cfgupgrade"""
23
24 import os
25 import sys
26 import unittest
27 import shutil
28 import tempfile
29 import operator
30
31 from ganeti import constants
32 from ganeti import utils
33 from ganeti import errors
34 from ganeti import serializer
35 from ganeti import netutils
36
37 import testutils
38
39
40 def _RunUpgrade(path, dry_run, no_verify, ignore_hostname=True):
41   cmd = [sys.executable, "%s/tools/cfgupgrade" % testutils.GetSourceDir(),
42          "--debug", "--force", "--path=%s" % path]
43
44   if ignore_hostname:
45     cmd.append("--ignore-hostname")
46   if dry_run:
47     cmd.append("--dry-run")
48   if no_verify:
49     cmd.append("--no-verify")
50
51   result = utils.RunCmd(cmd, cwd=os.getcwd())
52   if result.failed:
53     raise Exception("cfgupgrade failed: %s, output %r" %
54                     (result.fail_reason, result.output))
55
56
57 class TestCfgupgrade(unittest.TestCase):
58   def setUp(self):
59     self.tmpdir = tempfile.mkdtemp()
60
61     self.config_path = utils.PathJoin(self.tmpdir, "config.data")
62     self.noded_cert_path = utils.PathJoin(self.tmpdir, "server.pem")
63     self.rapi_cert_path = utils.PathJoin(self.tmpdir, "rapi.pem")
64     self.rapi_users_path = utils.PathJoin(self.tmpdir, "rapi", "users")
65     self.rapi_users_path_pre24 = utils.PathJoin(self.tmpdir, "rapi_users")
66     self.known_hosts_path = utils.PathJoin(self.tmpdir, "known_hosts")
67     self.confd_hmac_path = utils.PathJoin(self.tmpdir, "hmac.key")
68     self.cds_path = utils.PathJoin(self.tmpdir, "cluster-domain-secret")
69     self.ss_master_node_path = utils.PathJoin(self.tmpdir, "ssconf_master_node")
70
71   def tearDown(self):
72     shutil.rmtree(self.tmpdir)
73
74   def _LoadConfig(self):
75     return serializer.LoadJson(utils.ReadFile(self.config_path))
76
77   def _CreateValidConfigDir(self):
78     utils.WriteFile(self.noded_cert_path, data="")
79     utils.WriteFile(self.known_hosts_path, data="")
80     utils.WriteFile(self.ss_master_node_path,
81                     data="node.has.another.name.example.net")
82
83   def testNoConfigDir(self):
84     self.assertFalse(utils.ListVisibleFiles(self.tmpdir))
85     self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
86     self.assertRaises(Exception, _RunUpgrade, self.tmpdir, True, True)
87
88   def testWrongHostname(self):
89     self._CreateValidConfigDir()
90
91     utils.WriteFile(self.config_path, data=serializer.DumpJson({
92       "version": constants.CONFIG_VERSION,
93       "cluster": {},
94       }))
95
96     hostname = netutils.GetHostname().name
97     assert hostname != utils.ReadOneLineFile(self.ss_master_node_path)
98
99     self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True,
100                       ignore_hostname=False)
101
102   def testCorrectHostname(self):
103     self._CreateValidConfigDir()
104
105     utils.WriteFile(self.config_path, data=serializer.DumpJson({
106       "version": constants.CONFIG_VERSION,
107       "cluster": {},
108       }))
109
110     utils.WriteFile(self.ss_master_node_path,
111                     data="%s\n" % netutils.GetHostname().name)
112
113     _RunUpgrade(self.tmpdir, False, True, ignore_hostname=False)
114
115   def testInconsistentConfig(self):
116     self._CreateValidConfigDir()
117     # There should be no "config_version"
118     cfg = {
119       "version": 0,
120       "cluster": {
121         "config_version": 0,
122         },
123       }
124     utils.WriteFile(self.config_path, data=serializer.DumpJson(cfg))
125     self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
126
127   def testInvalidConfig(self):
128     self._CreateValidConfigDir()
129     # Missing version from config
130     utils.WriteFile(self.config_path, data=serializer.DumpJson({}))
131     self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
132
133   def _TestSimpleUpgrade(self, from_version, dry_run):
134     cfg = {
135       "version": from_version,
136       "cluster": {},
137       }
138     self._CreateValidConfigDir()
139     utils.WriteFile(self.config_path, data=serializer.DumpJson(cfg))
140
141     self.assertFalse(os.path.isfile(self.rapi_cert_path))
142     self.assertFalse(os.path.isfile(self.confd_hmac_path))
143     self.assertFalse(os.path.isfile(self.cds_path))
144
145     _RunUpgrade(self.tmpdir, dry_run, True)
146
147     if dry_run:
148       expversion = from_version
149       checkfn = operator.not_
150     else:
151       expversion = constants.CONFIG_VERSION
152       checkfn = operator.truth
153
154     self.assert_(checkfn(os.path.isfile(self.rapi_cert_path)))
155     self.assert_(checkfn(os.path.isfile(self.confd_hmac_path)))
156     self.assert_(checkfn(os.path.isfile(self.cds_path)))
157
158     newcfg = self._LoadConfig()
159     self.assertEqual(newcfg["version"], expversion)
160
161   def testRapiUsers(self):
162     self.assertFalse(os.path.exists(self.rapi_users_path))
163     self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
164     self.assertFalse(os.path.exists(os.path.dirname(self.rapi_users_path)))
165
166     utils.WriteFile(self.rapi_users_path_pre24, data="some user\n")
167     self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), False)
168
169     self.assertTrue(os.path.isdir(os.path.dirname(self.rapi_users_path)))
170     self.assert_(os.path.islink(self.rapi_users_path_pre24))
171     self.assert_(os.path.isfile(self.rapi_users_path))
172     self.assertEqual(os.readlink(self.rapi_users_path_pre24),
173                      self.rapi_users_path)
174     for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
175       self.assertEqual(utils.ReadFile(path), "some user\n")
176
177   def testRapiUsers24AndAbove(self):
178     self.assertFalse(os.path.exists(self.rapi_users_path))
179     self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
180
181     os.mkdir(os.path.dirname(self.rapi_users_path))
182     utils.WriteFile(self.rapi_users_path, data="other user\n")
183     self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), False)
184
185     self.assert_(os.path.islink(self.rapi_users_path_pre24))
186     self.assert_(os.path.isfile(self.rapi_users_path))
187     self.assertEqual(os.readlink(self.rapi_users_path_pre24),
188                      self.rapi_users_path)
189     for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
190       self.assertEqual(utils.ReadFile(path), "other user\n")
191
192   def testRapiUsersExistingSymlink(self):
193     self.assertFalse(os.path.exists(self.rapi_users_path))
194     self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
195
196     os.mkdir(os.path.dirname(self.rapi_users_path))
197     os.symlink(self.rapi_users_path, self.rapi_users_path_pre24)
198     utils.WriteFile(self.rapi_users_path, data="hello world\n")
199
200     self._TestSimpleUpgrade(constants.BuildVersion(2, 2, 0), False)
201
202     self.assert_(os.path.isfile(self.rapi_users_path) and
203                  not os.path.islink(self.rapi_users_path))
204     self.assert_(os.path.islink(self.rapi_users_path_pre24))
205     self.assertEqual(os.readlink(self.rapi_users_path_pre24),
206                      self.rapi_users_path)
207     for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
208       self.assertEqual(utils.ReadFile(path), "hello world\n")
209
210   def testRapiUsersExistingTarget(self):
211     self.assertFalse(os.path.exists(self.rapi_users_path))
212     self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
213
214     os.mkdir(os.path.dirname(self.rapi_users_path))
215     utils.WriteFile(self.rapi_users_path, data="other user\n")
216     utils.WriteFile(self.rapi_users_path_pre24, data="hello world\n")
217
218     self.assertRaises(Exception, self._TestSimpleUpgrade,
219                       constants.BuildVersion(2, 2, 0), False)
220
221     for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
222       self.assert_(os.path.isfile(path) and not os.path.islink(path))
223     self.assertEqual(utils.ReadFile(self.rapi_users_path), "other user\n")
224     self.assertEqual(utils.ReadFile(self.rapi_users_path_pre24),
225                      "hello world\n")
226
227   def testRapiUsersDryRun(self):
228     self.assertFalse(os.path.exists(self.rapi_users_path))
229     self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
230
231     utils.WriteFile(self.rapi_users_path_pre24, data="some user\n")
232     self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), True)
233
234     self.assertFalse(os.path.isdir(os.path.dirname(self.rapi_users_path)))
235     self.assertTrue(os.path.isfile(self.rapi_users_path_pre24) and
236                     not os.path.islink(self.rapi_users_path_pre24))
237     self.assertFalse(os.path.exists(self.rapi_users_path))
238
239   def testRapiUsers24AndAboveDryRun(self):
240     self.assertFalse(os.path.exists(self.rapi_users_path))
241     self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
242
243     os.mkdir(os.path.dirname(self.rapi_users_path))
244     utils.WriteFile(self.rapi_users_path, data="other user\n")
245     self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), True)
246
247     self.assertTrue(os.path.isfile(self.rapi_users_path) and
248                     not os.path.islink(self.rapi_users_path))
249     self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
250     self.assertEqual(utils.ReadFile(self.rapi_users_path), "other user\n")
251
252   def testRapiUsersExistingSymlinkDryRun(self):
253     self.assertFalse(os.path.exists(self.rapi_users_path))
254     self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
255
256     os.mkdir(os.path.dirname(self.rapi_users_path))
257     os.symlink(self.rapi_users_path, self.rapi_users_path_pre24)
258     utils.WriteFile(self.rapi_users_path, data="hello world\n")
259
260     self._TestSimpleUpgrade(constants.BuildVersion(2, 2, 0), True)
261
262     self.assertTrue(os.path.islink(self.rapi_users_path_pre24))
263     self.assertTrue(os.path.isfile(self.rapi_users_path) and
264                     not os.path.islink(self.rapi_users_path))
265     self.assertEqual(os.readlink(self.rapi_users_path_pre24),
266                      self.rapi_users_path)
267     for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
268       self.assertEqual(utils.ReadFile(path), "hello world\n")
269
270   def testUpgradeFrom_2_0(self):
271     self._TestSimpleUpgrade(constants.BuildVersion(2, 0, 0), False)
272
273   def testUpgradeFrom_2_1(self):
274     self._TestSimpleUpgrade(constants.BuildVersion(2, 1, 0), False)
275
276   def testUpgradeFrom_2_2(self):
277     self._TestSimpleUpgrade(constants.BuildVersion(2, 2, 0), False)
278
279   def testUpgradeFrom_2_3(self):
280     self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), False)
281
282   def testUpgradeFrom_2_4(self):
283     self._TestSimpleUpgrade(constants.BuildVersion(2, 4, 0), False)
284
285   def testUpgradeFrom_2_5(self):
286     self._TestSimpleUpgrade(constants.BuildVersion(2, 5, 0), False)
287
288   def testUpgradeCurrent(self):
289     self._TestSimpleUpgrade(constants.CONFIG_VERSION, False)
290
291   def testUpgradeDryRunFrom_2_0(self):
292     self._TestSimpleUpgrade(constants.BuildVersion(2, 0, 0), True)
293
294   def testUpgradeDryRunFrom_2_1(self):
295     self._TestSimpleUpgrade(constants.BuildVersion(2, 1, 0), True)
296
297   def testUpgradeDryRunFrom_2_2(self):
298     self._TestSimpleUpgrade(constants.BuildVersion(2, 2, 0), True)
299
300   def testUpgradeDryRunFrom_2_3(self):
301     self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), True)
302
303   def testUpgradeDryRunFrom_2_4(self):
304     self._TestSimpleUpgrade(constants.BuildVersion(2, 4, 0), True)
305
306   def testUpgradeDryRunFrom_2_5(self):
307     self._TestSimpleUpgrade(constants.BuildVersion(2, 5, 0), True)
308
309   def testUpgradeCurrentDryRun(self):
310     self._TestSimpleUpgrade(constants.CONFIG_VERSION, True)
311
312
313 if __name__ == "__main__":
314   testutils.GanetiTestProgram()