cfgupgrade: Check master name, clarify question
[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.known_hosts_path = utils.PathJoin(self.tmpdir, "known_hosts")
65     self.confd_hmac_path = utils.PathJoin(self.tmpdir, "hmac.key")
66     self.cds_path = utils.PathJoin(self.tmpdir, "cluster-domain-secret")
67     self.ss_master_node_path = utils.PathJoin(self.tmpdir, "ssconf_master_node")
68
69   def tearDown(self):
70     shutil.rmtree(self.tmpdir)
71
72   def _LoadConfig(self):
73     return serializer.LoadJson(utils.ReadFile(self.config_path))
74
75   def _CreateValidConfigDir(self):
76     utils.WriteFile(self.noded_cert_path, data="")
77     utils.WriteFile(self.known_hosts_path, data="")
78     utils.WriteFile(self.ss_master_node_path,
79                     data="node.has.another.name.example.net")
80
81   def testNoConfigDir(self):
82     self.assertFalse(utils.ListVisibleFiles(self.tmpdir))
83     self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
84     self.assertRaises(Exception, _RunUpgrade, self.tmpdir, True, True)
85
86   def testWrongHostname(self):
87     self._CreateValidConfigDir()
88
89     utils.WriteFile(self.config_path, data=serializer.DumpJson({
90       "version": constants.CONFIG_VERSION,
91       "cluster": {},
92       }))
93
94     hostname = netutils.GetHostname().name
95     assert hostname != utils.ReadOneLineFile(self.ss_master_node_path)
96
97     self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True,
98                       ignore_hostname=False)
99
100   def testCorrectHostname(self):
101     self._CreateValidConfigDir()
102
103     utils.WriteFile(self.config_path, data=serializer.DumpJson({
104       "version": constants.CONFIG_VERSION,
105       "cluster": {},
106       }))
107
108     utils.WriteFile(self.ss_master_node_path,
109                     data="%s\n" % netutils.GetHostname().name)
110
111     _RunUpgrade(self.tmpdir, False, True, ignore_hostname=False)
112
113   def testInconsistentConfig(self):
114     self._CreateValidConfigDir()
115     # There should be no "config_version"
116     cfg = {
117       "version": 0,
118       "cluster": {
119         "config_version": 0,
120         },
121       }
122     utils.WriteFile(self.config_path, data=serializer.DumpJson(cfg))
123     self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
124
125   def testInvalidConfig(self):
126     self._CreateValidConfigDir()
127     # Missing version from config
128     utils.WriteFile(self.config_path, data=serializer.DumpJson({}))
129     self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
130
131   def _TestSimpleUpgrade(self, from_version, dry_run):
132     cfg = {
133       "version": from_version,
134       "cluster": {},
135       }
136     self._CreateValidConfigDir()
137     utils.WriteFile(self.config_path, data=serializer.DumpJson(cfg))
138
139     self.assertFalse(os.path.isfile(self.rapi_cert_path))
140     self.assertFalse(os.path.isfile(self.confd_hmac_path))
141     self.assertFalse(os.path.isfile(self.cds_path))
142
143     _RunUpgrade(self.tmpdir, dry_run, True)
144
145     if dry_run:
146       expversion = from_version
147       checkfn = operator.not_
148     else:
149       expversion = constants.CONFIG_VERSION
150       checkfn = operator.truth
151
152     self.assert_(checkfn(os.path.isfile(self.rapi_cert_path)))
153     self.assert_(checkfn(os.path.isfile(self.confd_hmac_path)))
154     self.assert_(checkfn(os.path.isfile(self.cds_path)))
155
156     newcfg = self._LoadConfig()
157     self.assertEqual(newcfg["version"], expversion)
158
159   def testUpgradeFrom_2_0(self):
160     self._TestSimpleUpgrade(constants.BuildVersion(2, 0, 0), False)
161
162   def testUpgradeFrom_2_1(self):
163     self._TestSimpleUpgrade(constants.BuildVersion(2, 1, 0), False)
164
165   def testUpgradeCurrent(self):
166     self._TestSimpleUpgrade(constants.CONFIG_VERSION, False)
167
168   def testUpgradeDryRunFrom_2_0(self):
169     self._TestSimpleUpgrade(constants.BuildVersion(2, 0, 0), True)
170
171   def testUpgradeDryRunFrom_2_1(self):
172     self._TestSimpleUpgrade(constants.BuildVersion(2, 1, 0), True)
173
174   def testUpgradeCurrentDryRun(self):
175     self._TestSimpleUpgrade(constants.CONFIG_VERSION, True)
176
177
178 if __name__ == "__main__":
179   testutils.GanetiTestProgram()