4 # Copyright (C) 2010 Google Inc.
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.
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.
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
22 """Script for testing tools/cfgupgrade"""
31 from ganeti import constants
32 from ganeti import utils
33 from ganeti import errors
34 from ganeti import serializer
39 def _RunUpgrade(path, dry_run, no_verify):
40 cmd = [sys.executable, "%s/tools/cfgupgrade" % testutils.GetSourceDir(),
41 "--debug", "--force", "--path=%s" % path]
43 cmd.append("--dry-run")
45 cmd.append("--no-verify")
47 result = utils.RunCmd(cmd, cwd=os.getcwd())
49 raise Exception("cfgupgrade failed: %s, output %r" %
50 (result.fail_reason, result.output))
53 class TestCfgupgrade(unittest.TestCase):
55 self.tmpdir = tempfile.mkdtemp()
57 self.config_path = utils.PathJoin(self.tmpdir, "config.data")
58 self.noded_cert_path = utils.PathJoin(self.tmpdir, "server.pem")
59 self.rapi_cert_path = utils.PathJoin(self.tmpdir, "rapi.pem")
60 self.known_hosts_path = utils.PathJoin(self.tmpdir, "known_hosts")
61 self.confd_hmac_path = utils.PathJoin(self.tmpdir, "hmac.key")
62 self.cds_path = utils.PathJoin(self.tmpdir, "cluster-domain-secret")
65 shutil.rmtree(self.tmpdir)
67 def _LoadConfig(self):
68 return serializer.LoadJson(utils.ReadFile(self.config_path))
70 def _CreateValidConfigDir(self):
71 utils.WriteFile(self.noded_cert_path, data="")
72 utils.WriteFile(self.known_hosts_path, data="")
74 def testNoConfigDir(self):
75 self.assertFalse(utils.ListVisibleFiles(self.tmpdir))
76 self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
77 self.assertRaises(Exception, _RunUpgrade, self.tmpdir, True, True)
79 def testInconsistentConfig(self):
80 self._CreateValidConfigDir()
81 # There should be no "config_version"
88 utils.WriteFile(self.config_path, data=serializer.DumpJson(cfg))
89 self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
91 def testInvalidConfig(self):
92 self._CreateValidConfigDir()
93 # Missing version from config
94 utils.WriteFile(self.config_path, data=serializer.DumpJson({}))
95 self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
97 def _TestSimpleUpgrade(self, from_version, dry_run):
99 "version": from_version,
102 self._CreateValidConfigDir()
103 utils.WriteFile(self.config_path, data=serializer.DumpJson(cfg))
105 self.assertFalse(os.path.isfile(self.rapi_cert_path))
106 self.assertFalse(os.path.isfile(self.confd_hmac_path))
107 self.assertFalse(os.path.isfile(self.cds_path))
109 _RunUpgrade(self.tmpdir, dry_run, True)
112 expversion = from_version
113 checkfn = operator.not_
115 expversion = constants.CONFIG_VERSION
116 checkfn = operator.truth
118 self.assert_(checkfn(os.path.isfile(self.rapi_cert_path)))
119 self.assert_(checkfn(os.path.isfile(self.confd_hmac_path)))
120 self.assert_(checkfn(os.path.isfile(self.cds_path)))
122 newcfg = self._LoadConfig()
123 self.assertEqual(newcfg["version"], expversion)
125 def testUpgradeFrom_2_0(self):
126 self._TestSimpleUpgrade(constants.BuildVersion(2, 0, 0), False)
128 def testUpgradeFrom_2_1(self):
129 self._TestSimpleUpgrade(constants.BuildVersion(2, 1, 0), False)
131 def testUpgradeCurrent(self):
132 self._TestSimpleUpgrade(constants.CONFIG_VERSION, False)
134 def testUpgradeDryRunFrom_2_0(self):
135 self._TestSimpleUpgrade(constants.BuildVersion(2, 0, 0), True)
137 def testUpgradeDryRunFrom_2_1(self):
138 self._TestSimpleUpgrade(constants.BuildVersion(2, 1, 0), True)
140 def testUpgradeCurrentDryRun(self):
141 self._TestSimpleUpgrade(constants.CONFIG_VERSION, True)
144 if __name__ == "__main__":
145 testutils.GanetiTestProgram()