Statistics
| Branch: | Tag: | Revision:

root / test / cfgupgrade_unittest.py @ fe4f6dca

History | View | Annotate | Download (11.9 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2010, 2012 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
      "instances": {},
95
      }))
96

    
97
    hostname = netutils.GetHostname().name
98
    assert hostname != utils.ReadOneLineFile(self.ss_master_node_path)
99

    
100
    self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True,
101
                      ignore_hostname=False)
102

    
103
  def testCorrectHostname(self):
104
    self._CreateValidConfigDir()
105

    
106
    utils.WriteFile(self.config_path, data=serializer.DumpJson({
107
      "version": constants.CONFIG_VERSION,
108
      "cluster": {},
109
      "instances": {},
110
      }))
111

    
112
    utils.WriteFile(self.ss_master_node_path,
113
                    data="%s\n" % netutils.GetHostname().name)
114

    
115
    _RunUpgrade(self.tmpdir, False, True, ignore_hostname=False)
116

    
117
  def testInconsistentConfig(self):
118
    self._CreateValidConfigDir()
119
    # There should be no "config_version"
120
    cfg = {
121
      "version": 0,
122
      "cluster": {
123
        "config_version": 0,
124
        },
125
      "instances": {},
126
      }
127
    utils.WriteFile(self.config_path, data=serializer.DumpJson(cfg))
128
    self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
129

    
130
  def testInvalidConfig(self):
131
    self._CreateValidConfigDir()
132
    # Missing version from config
133
    utils.WriteFile(self.config_path, data=serializer.DumpJson({}))
134
    self.assertRaises(Exception, _RunUpgrade, self.tmpdir, False, True)
135

    
136
  def _TestSimpleUpgrade(self, from_version, dry_run):
137
    cfg = {
138
      "version": from_version,
139
      "cluster": {},
140
      "instances": {},
141
      }
142
    self._CreateValidConfigDir()
143
    utils.WriteFile(self.config_path, data=serializer.DumpJson(cfg))
144

    
145
    self.assertFalse(os.path.isfile(self.rapi_cert_path))
146
    self.assertFalse(os.path.isfile(self.confd_hmac_path))
147
    self.assertFalse(os.path.isfile(self.cds_path))
148

    
149
    _RunUpgrade(self.tmpdir, dry_run, True)
150

    
151
    if dry_run:
152
      expversion = from_version
153
      checkfn = operator.not_
154
    else:
155
      expversion = constants.CONFIG_VERSION
156
      checkfn = operator.truth
157

    
158
    self.assert_(checkfn(os.path.isfile(self.rapi_cert_path)))
159
    self.assert_(checkfn(os.path.isfile(self.confd_hmac_path)))
160
    self.assert_(checkfn(os.path.isfile(self.cds_path)))
161

    
162
    newcfg = self._LoadConfig()
163
    self.assertEqual(newcfg["version"], expversion)
164

    
165
  def testRapiUsers(self):
166
    self.assertFalse(os.path.exists(self.rapi_users_path))
167
    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
168
    self.assertFalse(os.path.exists(os.path.dirname(self.rapi_users_path)))
169

    
170
    utils.WriteFile(self.rapi_users_path_pre24, data="some user\n")
171
    self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), False)
172

    
173
    self.assertTrue(os.path.isdir(os.path.dirname(self.rapi_users_path)))
174
    self.assert_(os.path.islink(self.rapi_users_path_pre24))
175
    self.assert_(os.path.isfile(self.rapi_users_path))
176
    self.assertEqual(os.readlink(self.rapi_users_path_pre24),
177
                     self.rapi_users_path)
178
    for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
179
      self.assertEqual(utils.ReadFile(path), "some user\n")
180

    
181
  def testRapiUsers24AndAbove(self):
182
    self.assertFalse(os.path.exists(self.rapi_users_path))
183
    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
184

    
185
    os.mkdir(os.path.dirname(self.rapi_users_path))
186
    utils.WriteFile(self.rapi_users_path, data="other user\n")
187
    self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), False)
188

    
189
    self.assert_(os.path.islink(self.rapi_users_path_pre24))
190
    self.assert_(os.path.isfile(self.rapi_users_path))
191
    self.assertEqual(os.readlink(self.rapi_users_path_pre24),
192
                     self.rapi_users_path)
193
    for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
194
      self.assertEqual(utils.ReadFile(path), "other user\n")
195

    
196
  def testRapiUsersExistingSymlink(self):
197
    self.assertFalse(os.path.exists(self.rapi_users_path))
198
    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
199

    
200
    os.mkdir(os.path.dirname(self.rapi_users_path))
201
    os.symlink(self.rapi_users_path, self.rapi_users_path_pre24)
202
    utils.WriteFile(self.rapi_users_path, data="hello world\n")
203

    
204
    self._TestSimpleUpgrade(constants.BuildVersion(2, 2, 0), False)
205

    
206
    self.assert_(os.path.isfile(self.rapi_users_path) and
207
                 not os.path.islink(self.rapi_users_path))
208
    self.assert_(os.path.islink(self.rapi_users_path_pre24))
209
    self.assertEqual(os.readlink(self.rapi_users_path_pre24),
210
                     self.rapi_users_path)
211
    for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
212
      self.assertEqual(utils.ReadFile(path), "hello world\n")
213

    
214
  def testRapiUsersExistingTarget(self):
215
    self.assertFalse(os.path.exists(self.rapi_users_path))
216
    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
217

    
218
    os.mkdir(os.path.dirname(self.rapi_users_path))
219
    utils.WriteFile(self.rapi_users_path, data="other user\n")
220
    utils.WriteFile(self.rapi_users_path_pre24, data="hello world\n")
221

    
222
    self.assertRaises(Exception, self._TestSimpleUpgrade,
223
                      constants.BuildVersion(2, 2, 0), False)
224

    
225
    for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
226
      self.assert_(os.path.isfile(path) and not os.path.islink(path))
227
    self.assertEqual(utils.ReadFile(self.rapi_users_path), "other user\n")
228
    self.assertEqual(utils.ReadFile(self.rapi_users_path_pre24),
229
                     "hello world\n")
230

    
231
  def testRapiUsersDryRun(self):
232
    self.assertFalse(os.path.exists(self.rapi_users_path))
233
    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
234

    
235
    utils.WriteFile(self.rapi_users_path_pre24, data="some user\n")
236
    self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), True)
237

    
238
    self.assertFalse(os.path.isdir(os.path.dirname(self.rapi_users_path)))
239
    self.assertTrue(os.path.isfile(self.rapi_users_path_pre24) and
240
                    not os.path.islink(self.rapi_users_path_pre24))
241
    self.assertFalse(os.path.exists(self.rapi_users_path))
242

    
243
  def testRapiUsers24AndAboveDryRun(self):
244
    self.assertFalse(os.path.exists(self.rapi_users_path))
245
    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
246

    
247
    os.mkdir(os.path.dirname(self.rapi_users_path))
248
    utils.WriteFile(self.rapi_users_path, data="other user\n")
249
    self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), True)
250

    
251
    self.assertTrue(os.path.isfile(self.rapi_users_path) and
252
                    not os.path.islink(self.rapi_users_path))
253
    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
254
    self.assertEqual(utils.ReadFile(self.rapi_users_path), "other user\n")
255

    
256
  def testRapiUsersExistingSymlinkDryRun(self):
257
    self.assertFalse(os.path.exists(self.rapi_users_path))
258
    self.assertFalse(os.path.exists(self.rapi_users_path_pre24))
259

    
260
    os.mkdir(os.path.dirname(self.rapi_users_path))
261
    os.symlink(self.rapi_users_path, self.rapi_users_path_pre24)
262
    utils.WriteFile(self.rapi_users_path, data="hello world\n")
263

    
264
    self._TestSimpleUpgrade(constants.BuildVersion(2, 2, 0), True)
265

    
266
    self.assertTrue(os.path.islink(self.rapi_users_path_pre24))
267
    self.assertTrue(os.path.isfile(self.rapi_users_path) and
268
                    not os.path.islink(self.rapi_users_path))
269
    self.assertEqual(os.readlink(self.rapi_users_path_pre24),
270
                     self.rapi_users_path)
271
    for path in [self.rapi_users_path, self.rapi_users_path_pre24]:
272
      self.assertEqual(utils.ReadFile(path), "hello world\n")
273

    
274
  def testUpgradeFrom_2_0(self):
275
    self._TestSimpleUpgrade(constants.BuildVersion(2, 0, 0), False)
276

    
277
  def testUpgradeFrom_2_1(self):
278
    self._TestSimpleUpgrade(constants.BuildVersion(2, 1, 0), False)
279

    
280
  def testUpgradeFrom_2_2(self):
281
    self._TestSimpleUpgrade(constants.BuildVersion(2, 2, 0), False)
282

    
283
  def testUpgradeFrom_2_3(self):
284
    self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), False)
285

    
286
  def testUpgradeFrom_2_4(self):
287
    self._TestSimpleUpgrade(constants.BuildVersion(2, 4, 0), False)
288

    
289
  def testUpgradeFrom_2_5(self):
290
    self._TestSimpleUpgrade(constants.BuildVersion(2, 5, 0), False)
291

    
292
  def testUpgradeFrom_2_6(self):
293
    self._TestSimpleUpgrade(constants.BuildVersion(2, 6, 0), False)
294

    
295
  def testUpgradeCurrent(self):
296
    self._TestSimpleUpgrade(constants.CONFIG_VERSION, False)
297

    
298
  def testUpgradeDryRunFrom_2_0(self):
299
    self._TestSimpleUpgrade(constants.BuildVersion(2, 0, 0), True)
300

    
301
  def testUpgradeDryRunFrom_2_1(self):
302
    self._TestSimpleUpgrade(constants.BuildVersion(2, 1, 0), True)
303

    
304
  def testUpgradeDryRunFrom_2_2(self):
305
    self._TestSimpleUpgrade(constants.BuildVersion(2, 2, 0), True)
306

    
307
  def testUpgradeDryRunFrom_2_3(self):
308
    self._TestSimpleUpgrade(constants.BuildVersion(2, 3, 0), True)
309

    
310
  def testUpgradeDryRunFrom_2_4(self):
311
    self._TestSimpleUpgrade(constants.BuildVersion(2, 4, 0), True)
312

    
313
  def testUpgradeDryRunFrom_2_5(self):
314
    self._TestSimpleUpgrade(constants.BuildVersion(2, 5, 0), True)
315

    
316
  def testUpgradeDryRunFrom_2_6(self):
317
    self._TestSimpleUpgrade(constants.BuildVersion(2, 6, 0), True)
318

    
319
  def testUpgradeCurrentDryRun(self):
320
    self._TestSimpleUpgrade(constants.CONFIG_VERSION, True)
321

    
322

    
323
if __name__ == "__main__":
324
  testutils.GanetiTestProgram()