Statistics
| Branch: | Tag: | Revision:

root / test / cfgupgrade_unittest.py @ 819ca990

History | View | Annotate | Download (6.8 kB)

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

    
36
import testutils
37

    
38

    
39
def _RunUpgrade(path, dry_run, no_verify):
40
  cmd = [sys.executable, "%s/tools/cfgupgrade" % testutils.GetSourceDir(),
41
         "--debug", "--force", "--path=%s" % path]
42
  if dry_run:
43
    cmd.append("--dry-run")
44
  if no_verify:
45
    cmd.append("--no-verify")
46

    
47
  result = utils.RunCmd(cmd, cwd=os.getcwd())
48
  if result.failed:
49
    raise Exception("cfgupgrade failed: %s, output %r" %
50
                    (result.fail_reason, result.output))
51

    
52

    
53
class TestCfgupgrade(unittest.TestCase):
54
  def setUp(self):
55
    self.tmpdir = tempfile.mkdtemp()
56

    
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.rapi_users_path = utils.PathJoin(self.tmpdir, "rapi", "users")
61
    self.rapi_users_path_pre24 = utils.PathJoin(self.tmpdir, "rapi_users")
62
    self.known_hosts_path = utils.PathJoin(self.tmpdir, "known_hosts")
63
    self.confd_hmac_path = utils.PathJoin(self.tmpdir, "hmac.key")
64
    self.cds_path = utils.PathJoin(self.tmpdir, "cluster-domain-secret")
65

    
66
  def tearDown(self):
67
    shutil.rmtree(self.tmpdir)
68

    
69
  def _LoadConfig(self):
70
    return serializer.LoadJson(utils.ReadFile(self.config_path))
71

    
72
  def _CreateValidConfigDir(self):
73
    utils.WriteFile(self.noded_cert_path, data="")
74
    utils.WriteFile(self.known_hosts_path, data="")
75

    
76
  def testNoConfigDir(self):
77
    self.assertFalse(utils.ListVisibleFiles(self.tmpdir))
78
    self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
79
    self.assertRaises(Exception, _RunUpgrade, self.tmpdir, True, True)
80

    
81
  def testInconsistentConfig(self):
82
    self._CreateValidConfigDir()
83
    # There should be no "config_version"
84
    cfg = {
85
      "version": 0,
86
      "cluster": {
87
        "config_version": 0,
88
        },
89
      }
90
    utils.WriteFile(self.config_path, data=serializer.DumpJson(cfg))
91
    self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
92

    
93
  def testInvalidConfig(self):
94
    self._CreateValidConfigDir()
95
    # Missing version from config
96
    utils.WriteFile(self.config_path, data=serializer.DumpJson({}))
97
    self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
98

    
99
  def _TestSimpleUpgrade(self, from_version, dry_run):
100
    cfg = {
101
      "version": from_version,
102
      "cluster": {},
103
      }
104
    self._CreateValidConfigDir()
105
    utils.WriteFile(self.config_path, data=serializer.DumpJson(cfg))
106

    
107
    self.assertFalse(os.path.isfile(self.rapi_cert_path))
108
    self.assertFalse(os.path.isfile(self.confd_hmac_path))
109
    self.assertFalse(os.path.isfile(self.cds_path))
110

    
111
    _RunUpgrade(self.tmpdir, dry_run, True)
112

    
113
    if dry_run:
114
      expversion = from_version
115
      checkfn = operator.not_
116
    else:
117
      expversion = constants.CONFIG_VERSION
118
      checkfn = operator.truth
119

    
120
    self.assert_(checkfn(os.path.isfile(self.rapi_cert_path)))
121
    self.assert_(checkfn(os.path.isfile(self.confd_hmac_path)))
122
    self.assert_(checkfn(os.path.isfile(self.cds_path)))
123

    
124
    newcfg = self._LoadConfig()
125
    self.assertEqual(newcfg["version"], expversion)
126

    
127
  def testRapiUsers(self):
128
    self.assertFalse(os.path.exists(self.rapi_users_path))
129
    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
130

    
131
    utils.WriteFile(self.rapi_users_path_pre24, data="some user\n")
132
    self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), False)
133

    
134
    self.assert_(os.path.islink(self.rapi_users_path_pre24))
135
    self.assert_(os.path.isfile(self.rapi_users_path))
136
    for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
137
      self.assertEqual(utils.ReadFile(path), "some user\n")
138

    
139
  def testRapiUsers24AndAbove(self):
140
    self.assertFalse(os.path.exists(self.rapi_users_path))
141
    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
142

    
143
    os.mkdir(os.path.dirname(self.rapi_users_path))
144
    utils.WriteFile(self.rapi_users_path, data="other user\n")
145
    self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), False)
146

    
147
    self.assert_(os.path.islink(self.rapi_users_path_pre24))
148
    self.assert_(os.path.isfile(self.rapi_users_path))
149
    for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
150
      self.assertEqual(utils.ReadFile(path), "other user\n")
151

    
152
  def testRapiUsersExistingSymlink(self):
153
    self.assertFalse(os.path.exists(self.rapi_users_path))
154
    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
155

    
156
    os.symlink(self.rapi_users_path, self.rapi_users_path_pre24)
157
    utils.WriteFile(self.rapi_users_path_pre24, data="hello world\n")
158

    
159
    self._TestSimpleUpgrade(constants.BuildVersion(2, 2, 0), False)
160

    
161
    self.assert_(os.path.isfile(self.rapi_users_path))
162
    self.assert_(os.path.islink(self.rapi_users_path_pre24))
163
    for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
164
      self.assertEqual(utils.ReadFile(path), "hello world\n")
165

    
166
  def testUpgradeFrom_2_0(self):
167
    self._TestSimpleUpgrade(constants.BuildVersion(2, 0, 0), False)
168

    
169
  def testUpgradeFrom_2_1(self):
170
    self._TestSimpleUpgrade(constants.BuildVersion(2, 1, 0), False)
171

    
172
  def testUpgradeFrom_2_2(self):
173
    self._TestSimpleUpgrade(constants.BuildVersion(2, 2, 0), False)
174

    
175
  def testUpgradeFrom_2_3(self):
176
    self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), False)
177

    
178
  def testUpgradeCurrent(self):
179
    self._TestSimpleUpgrade(constants.CONFIG_VERSION, False)
180

    
181
  def testUpgradeDryRunFrom_2_0(self):
182
    self._TestSimpleUpgrade(constants.BuildVersion(2, 0, 0), True)
183

    
184
  def testUpgradeDryRunFrom_2_1(self):
185
    self._TestSimpleUpgrade(constants.BuildVersion(2, 1, 0), True)
186

    
187
  def testUpgradeDryRunFrom_2_2(self):
188
    self._TestSimpleUpgrade(constants.BuildVersion(2, 2, 0), True)
189

    
190
  def testUpgradeDryRunFrom_2_3(self):
191
    self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), True)
192

    
193
  def testUpgradeCurrentDryRun(self):
194
    self._TestSimpleUpgrade(constants.CONFIG_VERSION, True)
195

    
196

    
197
if __name__ == "__main__":
198
  testutils.GanetiTestProgram()